// JavaScript Document
$(document).ready(function ()
{
     $('.curr').selectbox(  
       {  
         inputClass: 'currselectbox', //css class for the input which will replace the select tag, display the background image  
         containerClass: 'selectbox-curr', // The list container class (a div element)  
         hoverClass: 'current', // css class for the current element  
         currentClass: 'selected', // css class for the selected element  
         debug: false // debug mode on/off  
     }); 		
 	$("#country_id").bind("change",function()
	{
		UpdateCountry(this);;
	});     
 	
 	$(".buttons a").bind("click", function()
 	{
 		if($(this).hasClass("searchbutton"))
 		{
 			$(this).removeClass("searchbutton");
 			$(this).addClass("searchbutton_on");
 			elem = this;
 			$(".buttons a").each(function ()
 			{
 				if(elem!= this)
 				{
 					$(this).addClass("searchbutton");
 					$(this).removeClass("searchbutton_on");
 				}
 			});
 			
 		}else
 		{
 			$(this).removeClass("searchbutton_on");
 			$(this).addClass("searchbutton");
 		}
 		if($(this).hasClass("simple"))
 		{
 			$(".line2").hide();
 			$(".line3").hide();
 			setCookie("search=simple");
 			$("#type").val("1");
 		}else
 		{
 			setCookie("search=advanced");
 			$(".line2").show();
 			$(".line3").show();
 			$("#type").val("2");
 		}
 		return false;
 	});
 	if($("#ville_id").val()!="")
 	{
 	
 		UpdateVille($("#ville_id"));
 	}
	$("#ville_id").change(function (){	 

		UpdateVille(this);
		
	
	});
	$("#currency").validate(
	{

			submitHandler: function(form) {

				if( $("#currency #to").val() != $("#currency #from").val() )
				{
					$(".converted").html("<img src=\"/images/loading.gif\" />");
	
					jQuery(form).ajaxSubmit({
					  success: function(t) {
						$(".converted").html("= "+ t +" "+ $("#currency #to").val());
					  }
						
					});				
				}else
				{
					$(".converted").html("= " + $("#value").val()+" "+$("#currency #to").val()) ;
					
					 return false;	
				}
			}
	});
});
function UpdateVille(elem)
{
	$.ajax({
		type: "POST",
		url: "/" + $("#sf_culture").val() +"/quartiers/get/id/" + $(elem).val(),
		data: "id="+ $(elem).val(),
		success: function(msg){
		
		 $("#quartiers").html(msg);
		},
		error: function (error,txt)
		{
			alert(txt);	
		}
	});	

}
function UpdateCountry(elem)
{
	$.ajax({
		type: "POST",
		url: "/" + $("#sf_culture").val() +"/villes/get/id/" + $(elem).val(),
		data: "id="+ $(elem).val(),
		success: function(msg){
		
		 $("#search_villes").html(msg);
		 UpdateVille($("#ville_id"));
		},
		error: function (error,txt)
		{
			alert(txt);	
		}
		
	});	

}
function setCookie(cook)
{
	days = 3; // -ve for deleting it.

	var date = new Date();

	date.setTime(date.getTime ()+(days*24*60*60*1000));

	var expires = "; expires="+date.toGMTString();		
	document.cookie = cook + expires;
}