var _reg = new RegExp("[_]", "g");
var _timer = null;
var _curSize = 'normal';

function round2(val){
	return Math.round(val * 100) / 100;
}

function messageGlobal(style, msg, dest){
	var id = dest || 'ajaxMessageGlobal';
	if (style != "") {
		$(id).className = style + 'box';
		new $(id).update(msg);
		$(id).show();
	} else {
		$(id).update('');
		$(id).className = '';
		$(id).hide();
	}
}

function emptySelect(name, Empty){
	$(name).childElements().each(function(elt){
		elt.remove();
	});
	if (!Empty) {
		$(name).options[0] = new Option(' - ', 0);
	}
}
/**
 * formulaire de recherche générale
 * ================================
 */
function search_acteurTypeObserve(searchName, input, select){
	$(select).observe('change', function(e){
		var elt = e.element();
		searchName.url = _root + "search.php?mode=name&type=" + elt.value;
		$(input).value = '';
		$(input).focus();
	});
}

function goActeur(text, li){
	var infos = li.id.split(_reg);
	window.location.href = _root + "user.php?u=" + infos[1];
}

function search_regionObserve(select){
	$(select).observe('change', function(e){
		var elt = e.element();
		window.location.href = _searchResultUrl + "?r=" + elt.value;
		return;
	});
}

function search_productObserve(select){
	$(select).observe('change', function(e){
		var elt = e.element();
		window.location.href = _searchResultUrl + "?p=" + elt.value;
		return;
	});
}

function search_resultObserve(){
	$('selectRegion').observe('change', search_updDpt);
	//$('selectDpt').observe('change', search_makeUrl);
	//$('selectActeurType').observe('change', search_makeUrl);
	//$('selectProduct').observe('change', search_makeUrl);
	//$('nowantPagination').observe('change', search_makeUrl);
}

function search_updDpt(e) {
	var elt = e.element();
	
	new Ajax.Request(_root + 'search_result.php?upd=dpt&r='+elt.value, {
		postBody: $H({
			'action': 'upd',
			'type': 'dpt',
			'r': elt.value
		}).toQueryString(),
		onComplete: function(xhr, json) {
			$('selectDpt').update(xhr.responseText);
		}
	});
}

function search_makeUrl(e){
	var elt = e.element();
	
	var args = $('form_result').serialize(true);
	// Si changement de région, on supprime le département
	if (elt.id == 'selectRegion') delete (args.d);
	
	window.location.href = _searchResultUrl + "?" + Object.toQueryString(args);
	return;
}

/*
 * ==================== GESTION DES ONGLETS  ======================
 */
function observeTabs(tab, first){
	var currentLocation = document.location.href;
	var currentTab = '';
	if (currentLocation.match(/.+\#(.+)$/)) {
		currentTab = currentLocation.replace(/.+\#([-_a-zA-Z0-9]+)/, '$1');
	}
	$(tab).descendants().each(function(elt){
		if (!elt.hasClassName('tab')) return;
		first = currentTab != '' ? currentTab : first || elt.id;
		// observe la tab pour le clique
		elt.observe('click', dispTab);
		// Initialise les onglets
		if ($(elt.id + '_content').visible() && elt.id != tab + '_' + first) $(elt.id + '_content').hide();
		else {
			elt.addClassName('tab_active');
			elt.up('a').addClassName('tab_active')
			$(elt.id + '_content').show();
		}
	});
}

function dispTab(e){
	var elt = typeof(e) == 'object' ? e.element() : $(e);
	var infos = elt.id.split(_reg);
	$$('.tab-content').each(function(tmp){
		if (!tmp.id.include(infos[0])) return;
		if (tmp.id == elt.id + '_content') {
			tmp.show();
			elt.addClassName('tab_active');
			elt.up('a').addClassName('tab_active')
		} else {
			tmp.hide();
			elt.up('ul').descendants().each(function(tab){
				if (!tab.hasClassName('tab')) return;
				if (tab.id != elt.id) {
					tab.removeClassName('tab_active');
					tab.up('a').removeClassName('tab_active')
				}
			});
		}
	});
}

/*
 * ==================== GESTION DES USERS  ======================
 */
function user_checkWWWTimer(e){
	// Réinit le timer
	if (typeof(e) == 'object') {
		if (_timer) clearTimeout(_timer);
		var check = 'user_checkWWW("' + e.element().id + '")';
		_timer = setTimeout(check, 400);
	}
}

function user_checkWWW(e){
	var elt = typeof(e) == 'object' ? e.element() : $(e);
	var input = elt.value || elt.innerHTML;
	var forbChar = /[^a-z-]/gi;
	if (forbChar.test(input)) {
		elt.value = input.replace(forbChar, '');
		input = elt.value;
	}
	
	if (input.length <= 2) return false;
	new Ajax.Request(_root + 'user.php', {
		postBody: $H({
			'm': 'profile',
			'a': 'check_www',
			'www': input
		}).toQueryString(),
		//onCreate: function() {elt.disable();},
		onComplete: function(xhr, json){
			$('img_' + elt.id).alt = json.title;
			$('img_' + elt.id).title = json.title;
			$('img_' + elt.id).src = json.img;
			//elt.enable();
		}
	});
}

function scrollBox(box, e){
	var elt = e;
	var parent = null;
	
	if (elt.id == box) parent = elt;
	else if (elt.up('div.bloc-top') && elt.up('div.bloc-top').id == box) parent = elt.up('div.bloc-top');
	else {
		if ($(box).getStyle('bottom') == '0px') {
			new Effect.Morph(box, {
				style: {
					bottom: '97px'
				}, // CSS Properties
				duration: 0.4 // Core Effect properties
			});
		}
		return false;
	}
	
	if (parent.getStyle('bottom') == '97px') {
		new Effect.Morph(parent, {
			style: {
				bottom: '0px'
			}, // CSS Properties
			duration: 0.8 // Core Effect properties
		});
	}
}

/**
 * Modification de la taille des polices
 * Piqué à phpBB3 :o)
 */
function fontSizeUp(){
	active = getActiveStyleSheet();
	
	
	switch (active) {
	
		case 'A':
			setActiveStyleSheet('A+');
			break;
			
		case 'A+':
			setActiveStyleSheet('A++');
			break;
			
		case 'A++':
			setActiveStyleSheet('A');
			break;
			
		default:
			setActiveStyleSheet('A');
			break;
	}
}

function fontSizeDown(){
	active = getActiveStyleSheet();
	
	switch (active) {
		case 'A++':
			setActiveStyleSheet('A+');
			break;
			
		case 'A+':
			setActiveStyleSheet('A');
			break;
			
		case 'A':
			setActiveStyleSheet('A++');
			break;
			
		default:
			setActiveStyleSheet('A');
			break;
	}
}

function setActiveStyleSheet(title){
	var i, a, main;
	
	for (i = 0; (a = document.getElementsByTagName('link')[i]); i++) {
		if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title')) {
			a.disabled = true;
			if (a.getAttribute('title') == title) {
				a.disabled = false;
			}
		}
	}
}

function getActiveStyleSheet(){
	var i, a;
	
	for (i = 0; (a = document.getElementsByTagName('link')[i]); i++) {
		if (a.getAttribute('rel').indexOf('style') != -1 && a.getAttribute('title') && !a.disabled) {
			return a.getAttribute('title');
		}
	}
	
	return null;
}


/**
 * Recherche de ville dans une base de donnée
 * Si inexistant, interroge google pour obtenir la liste des villes et coordonnées
 * les insères en base pour éviter des recherches gmap
 */
function gmap_seachCity(e){
	var elt = typeof(e) == 'object' ? e.element() : $(e);
	var search = elt.value;
	if (search.length > 2) {
		gLocalSearch.execute(search); // execute la recherche et lance la fonction de callBack
	}
}

function gmap_seachCityCallBack(){
	if (gLocalSearch.results.length > 0) {
		//map.setCenter(new GLatLng(gLocalSearch.results[0].lat, gLocalSearch.results[0].lng), 14);
		alert(gLocalSearch.results.toJSON());
		return true;
	} else {
		//window.alert(_lang['GMAP_NOT_FOUND']);
		return false;
	}
}

