/*global*/

	//var BASE_URL = 'http://localhost/sencities/';
	//var BASE_URL = 'http://dev.sencities.com/';
	var BASE_URL = 'http://www.sencities.com/';
	
	var i=false; 


/*Ajax*/
	function searchByForm(page,container,tab,params){	

		if(tab){
			if (isArray(tab)){
				if (tab.length>0){
					var tpParams = Form.serialize($(tab[0]));
					for (var i=1;i<tab.length;i++){
						tpParams = tpParams+'&'+Form.serialize($(tab[i])); 
					}
					
					var params = tpParams+'&'+params;
				}
			}else{
					var params = Form.serialize($(tab))+'&'+params;
			}
		}else{
			var params = params;
		}
			
			new Ajax.Request(page,{ 
							evalJS:true,
							asynchronous:true,
							parameters:params,
							method:'post',
							onCreate: function(xhr) {
									$(container).innerHTML= '<div id="loading"><div class="img_loader"><img src="' + BASE_URL +'styles/default/imgs/ajax-loader.gif"/></div><div class="text_loader">chargement</div></div>';
									
			},
							onSuccess: function(xhr) {
							
									if (xhr.status == 200){
									$(container).innerHTML = xhr.responseText;
									$(container).update(xhr.responseText.stripScripts());
									xhr.responseText.extractScripts().each(function(script){
			 							if (window.execScript) { window.execScript(script);}
			 							else {window.eval(script);}
									})
								}else{
									$(container).innerHTML = 'Erreur';
								}
							},
							onComplete: function(xhr) {
								//new Effect.ScrollTo('search_result',{duration:2});
							},
							onFailure: function(xhr) {
								$(container).innerHTML = 'Erreur';
							}
														
				});
	}

	function autoComplete(page,champ,div){
		new Ajax.Autocompleter (
		  champ,      // ID of the source field
		  div,  // ID of the DOM element to update
		  page, // Remote script URI
		  {method: 'post', minChars: 3,paramName:champ,afterUpdateElement : getSelectedIdCity}
		);
	}

	function getSelectedIdCity(text,li) {
		$('id_city').value=li.id;
	}


	function deleteInputValue(elt){
		elt.value = '';	
	}
	
	function setInputValue(elt,name){
		elt.value =name;
	}
	
	function typing(id,text)
	{
		var Obj = $(id);
		if(Obj.value == text)    {
			Obj.value = "";
		}
	}

	function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
	   else
	      return true;
	}
	
	function visibility(id)
	{
		if ($(id).style.display == "none")
		{
			$(id).style.display = "block";
		} 
		else 
		{
			$(id).style.display = "none";
		}
	}

	
	function switchButton(element,buttonDevelop,buttonSummarize){
		var d = buttonDevelop.length;
		var s = buttonSummarize.length;
		
		var image = element.firstChild;
		var name = image.getAttribute("src");
		var iname = name.substr(name.length - d, d);
	
		if(iname == buttonDevelop)
		{		
			image.setAttribute("src", buttonSummarize);
			return;				
		}
		iname = name.substr(name.length - s, s);
		if(iname == buttonSummarize)
		{
			image.setAttribute("src", buttonDevelop);
			return;				
		}
	}

	function change_image(name,target) {
		document.images[name].src = target;
	}
	
	function hiddenDiv(nom,btn)
		{	
			
			var btnAll = btn.substr(0,btn.length - 1);
			var nomAll = nom.substr(0,nom.length - 1);

			
			var NumDiv = 1;
			var nom = $(nom);
			var btn = $(btn);
			
			
			nom.className = nomAll;
			btn.className = 'on';
			
			while ($( nomAll + NumDiv) )
					{
					SetDiv = $(nomAll + NumDiv );
					SetBtn = $(btnAll + NumDiv );
						if ( SetDiv && SetDiv.className != 'cachediv' )
							{
								if(SetDiv != nom){
										SetDiv.className = ( SetDiv.className == 'cachediv' ) ? '' : 'cachediv';
										SetBtn. className = '';
								}		
							}
					NumDiv++;
				}
		}
	
	
	function changeColor(id) 
	{ 	
		var tab = id.split('_');
		//alert(tab[1]);
		for (i=1; i<6; i++){
	   		if(i != tab[1]){
	   			var temp_elt = $(tab[0] + '_' + i);
				if (temp_elt != null){
	   				temp_elt.style.color='black';
				}
	   		}
	    } 
	    var elt = $(tab[0] + '_' + tab[1]);
		elt.style.color='#d14512'; 
	}
	
		
	function feedInput(Id)
	{
		var tab = Id.split('_');
		var elt = $(tab[0]);
		elt.value=tab[1];
	}
	
	function tagSelect(Id)
	{
		var tag_elt = $('tags');
		var tabId = Id.split('_');
		var elt = $(Id);
		if(tag_elt.value!='')
		{
			var tabTag = tag_elt.value.split(',');		
			if(elt.style.color == 'black')//ADD
			{
				tabTag[tabTag.length] = tabId[1];
				tag_elt.value = tabTag.join(',');
				elt.style.color = 'red';
			}
			else if(elt.style.color == 'red')//REMOVE
			{
				var cpt = 0;
				var new_tab = new Array();
				for(i=0; i<tabTag.length; i++)
				{
					if(tabTag[i]==tabId[1])
					{
						var delId = i;
					}
					else
					{
						new_tab[cpt] = tabTag[i];
						cpt++;
					}
				}
				tag_elt.value = new_tab.join(',');
				elt.style.color = 'black';
			}
		}
		else
		{
			tag_elt.value=tabId[1];
			elt.style.color = 'red';
		}
		
	}

	function initializeCurseur(){
		document.onmousemove=move;
	}
	
	function move(e) { 
		if(i) { 
			if (navigator.appName!="Microsoft Internet Explorer") { 
				$("curseur").style.left=e.pageX+5+"px"; 
				$("curseur").style.top=e.pageY+10+"px"; 
			}else{
				if(document.documentElement.clientWidth>0) { 
					$("curseur").style.left=20+event.x+document.documentElement.scrollLeft+"px"; 
					$("curseur").style.top=5+event.y+document.documentElement.scrollTop+"px"; 
				} else { 
					$("curseur").style.left=20+event.x+document.body.scrollLeft+"px"; 
					$("curseur").style.top=5+event.y+document.body.scrollTop+"px"; 
				} 
			} 
		} 
	} 
	
	function montre(text) { 
		if(i==false) { 
			$("curseur").style.visibility="visible"; 
			$("curseur").innerHTML = text; i=true; 
		} 
	} 
	
	function cache() { 
		if(i==true) { 
			$("curseur").style.visibility="hidden"; i=false; 
		} 
	} 

		
	