
function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
	var oSuggest = $("#CityAjax")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function lookupLocal(){
	var oSuggest = $("#CityLocal")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

$(document).ready(function() {

	// $('div[class^="retour"] a').click(function(){
		// history.go(-1);
		// return false;
	// });
	
	$("#form_etap_2").submit(function(){
	
		if($('#je_souscris').is(':checked')&&$(".push_inscription_locataires :checkbox:checked").length<3)
		{
			alert('Vous devez sélectionner 3 types de bien');
			return false;
		}
	
	});


    $("#formconditions :checkbox").click(function(){
		if($("#formconditions :checkbox:checked").length==2){
			$('#formconditions :submit').removeAttr('disabled');
		}else{
		    $('#formconditions :submit').attr('disabled', true);
		}
	});
	
    $("#accept_conditions_contrat").click(function(){
		if($(this).is(':checked')){
			$('#form_etap_3 :submit').removeAttr('disabled');
		}else{
		    $('#form_etap_3 :submit').attr('disabled', true);
		}
	});    
	
	$("#accept_conditions_generales").click(function(){
		if($(this).is(':checked')){
			$('#form_etap_2 :submit').removeAttr('disabled');
		}else{
		    $('#form_etap_2 :submit').attr('disabled', true);
		}
	});
	
    $("#form_etap_2 .box_offre :checkbox").click(function(){
		if($(".push_inscription_locataires .box_offre :checkbox:checked").length==3){
		    $('.push_inscription_locataires .box_offre :checkbox').attr('disabled', true);
			$('.push_inscription_locataires .box_offre :checkbox:checked').removeAttr('disabled');
		}else{
			$('.push_inscription_locataires .box_offre :checkbox').removeAttr('disabled');		
		}
		$('#je_souscris').attr('checked', 'checked');
	});
	
	$('#pas_inscription').click(function(){
		$('.push_inscription_locataires .box_offre :checkbox').removeAttr('disabled');		
		$('.push_inscription_locataires .box_offre :checkbox').removeAttr('checked');		
	});
	
	$('#btn_alerte_mail').click(function(){
		$('#msg').load(this.href);
		return false;
	});
	
	$('#btn_coup_de_coeur').click(function(){
		$('#msg').load(this.href);
		return false;
	});
	
	$('#btn_coup_de_coeur_').click(function(){
		$('#msg').load(this.href);
		return false;
	});
   
	$('#slider').slider({
			range: true,
			min: 200,
			max: 2000,
			values: [$("#budjet_min").val(), $("#budjet_max").val()],
			slide: function(event, ui) {
				$("#budjet_min").val(Math.floor(ui.values[0]));
				$("#budjet_max").val(Math.floor(ui.values[1]));
			},
			step: 50
	});
	
	$('#slider a:first').mousemove(function(){
		$('#tooltip .body').html($("#budjet_min").val()+"€");
	});
	$('#slider a:last').mousemove(function(){
		$('#tooltip .body').html($("#budjet_max").val()+"€");
	});
	
	$('#slider a').tooltip({
	bodyHandler: function() {
		if ($(this).is(":first-child"))
			return $("#budjet_min").val()+"€";
		else 
			return $("#budjet_max").val()+"€";
	},
    track: true, 
    delay: 0, 
	opacity: 1, 
    showURL: false, 
    showBody: " - ",
	extraClass: "popup_detail",	
    fade: 50 
	});
	
	$('#btn_sh_annonces').click(function(){
		if($(".bloc_annonces_immobilieres").is(":visible"))
			$('.bloc_annonces_immobilieres').slideUp("slow");
		else
			$('.bloc_annonces_immobilieres').slideDown("slow");
			
		return false;
	});
	
	$('#btn_sh_annonces_attente').click(function(){
		if($(".bloc_annonces_immobilieres_attente").is(":visible"))
			$('.bloc_annonces_immobilieres_attente').slideUp("slow");
		else
			$('.bloc_annonces_immobilieres_attente').slideDown("slow");
			
		return false;
	});
	
	$('#btn_sh_info').click(function(){
		if($(".bloc_donnee_perso").is(":visible"))
			$('.bloc_donnee_perso').slideUp("slow");
		else
			$('.bloc_donnee_perso').slideDown("slow");
			
		return false;
	});
	
	$('#btn_sh_cp').click(function(){
		if($(".bloc_coup_de_coeur").is(":visible"))
			$('.bloc_coup_de_coeur').slideUp("slow");
		else
			$('.bloc_coup_de_coeur').slideDown("slow");
			
		return false;
	});
	
	$('#btn_sh_mail').click(function(){
		if($(".bloc_mail").is(":visible"))
			$('.bloc_mail').slideUp("slow");
		else
			$('.bloc_mail').slideDown("slow");
			
		return false;
	});
	
	$('#btn_sh_abonnement').click(function(){
		if($(".bloc_abonnement").is(":visible"))
			$('.bloc_abonnement').slideUp("slow");
		else
			$('.bloc_abonnement').slideDown("slow");
			
		return false;
	});
	
	populateCalendar('input_calendar');
	populateCalendar('calendar');
	
	$("#input_calendar .case_disponibilite td").click(function(){
		if ( $(this).hasClass("tab_bg_orange") ){
			$(this).removeClass("tab_bg_orange");
			$(this).addClass("tab_bg_gris");
		}
		else{
			$(this).removeClass("tab_bg_gris");
			$(this).addClass("tab_bg_orange");		
		}
		getCalendarValues('input_calendar');
	});

	$('#input_calendar .case_disponibilite td').tooltip({
	bodyHandler: function() {
		return $(this).attr("abbr");
	},
    track: true, 
    delay: 0, 
    showURL: false, 
    showBody: " - ", 
	extraClass: "popup_detail",
    fade: 250 
	});

	$('#pub_link').click(function(){
		$.get('index.html', { pubid: $('#pub_link img').attr("id") });
	});
	
	$("#ville").autocomplete(
		'http://www.locaclic.com/pages/scripts/rechercheVille.php',
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			autoFill:false,
			maxItemsToShow:10,
			mustMatch:0,
			scroll: false
		}
	);
	
	/*
	$("#ville").autocompleteArray(
		["abondance","alby sur cheran","alex","alleves","allinges","allonzier la caille","amancy","ambilly","amphion","andilly","annecy","annecy le vieux","annemasse","anthy sur leman","araches la frasse","arbusigny","archamps","arcine","arenthon","argentiere","argonay","armoy","arthaz pont notre dame","aviernoz","avoriaz","ayse","ballaison","bassy","beaumont","bellevaux","bernex","bloye","bluffy","boege","bogeve","bonne","bonnevaux","bonneville","bons en chablais","bossey","boussy","brenthonne","brizon","burdignin","cercier","cernex","cervens","chainaz les frasses","challonges","chamonix mont blanc","champanges","chapeiry","charvonnex","chatel","chatillon sur cluses","chaumont","chavannaz","chavanod","chene en semine","chenex","chens sur leman","chessenaz","chevaline","chevenoz","chevrier","chilly","choisy","clarafond","clermont","cluses","collonges sous saleve","combloux","cons ste colombe","contamine sarzin","contamine sur arve","copponex","cordon","cornier","coudree","cran gevrier","cranves sales","crempigny bonneguete","cruseilles","cusy","cuvat","demi quartier","desingy","dingy en vuache","dingy st clair","domancy","doussard","douvaine","draillant","droisy","duingt","entremont","entrevernes","epagny","esery","essert romand","esserts saleve","etaux","eteaux","etercy","etrembieres","evian les bains","evires","excenevex","faucigny","faverges","feigeres","ferrieres","fessy","feternes","fillinges","flaine","franclens","frangy","gaillard","giez","groisy","gruffy","habere lullin","habere poche","hauteville sur fier","hery sur alby","hirmentaz","jonzier epagny","juvigny","la balme de sillingy","la balme de sillingy","la balme de thuy","la balme de thuy","la baume","la chapelle d abondance","la chapelle rambaud","la chapelle st maurice","la clusaz","la cote d arbroz","la cote d'hyot","la croisette","la forclaz","la frasse","la muraz","la riviere enverse","la roche sur foron","la tour","la vernaz","larringes","lathuile","le biot","le bouchet mont charvin","le chinaillon","le fayet","le grand bornand","le lyaud","le petit bornand les glieres","le praz de lys","le reposoir","le sappey","le semnoz","les bossons ","les carroz","les clefs","les contamines montjoie","les gets","les houches","les ollieres","les praz de chamonix","les villards sur thones","leschaux","loisin","lornay","lovagny","lucinges","lugrin","lullin","lully","lyaud","machilly","magland","manigod","marcellaz","marcellaz albanais","margencel","marignier","marigny st marcel","marin","marlens","marlioz","marnaz","massingy","massongy","maxilly sur leman","megeve","megevette","meillerie","menthon st bernard","menthonnex en bornes","menthonnex sous clermont","mesigny","messery","metz tessy","meythet","mieussy","minzier","monnetier mornex","mont saxonnex","montagny les lanches","montmin","montriond","morillon","morzine","moye","mures","musieges","nancy sur cluses","nangy","naves parmelan","nernier","neuvecelle","neydens","nonglard","novel","onnion","orange","orcier","pas de l'echelle","passeirier","passy","peillonnex","perrignier","pers jussy","plateau d'assy","poisy","praz sur arly","presilly","pringy","publier","quintal","reignier","reyvroz","rumilly","sales","sallanches","sallenoves","samoens","savigny","saxel","scientrier","sciez","scionzier","serraval","servoz","sevrier","seynod","seyssel","seythenex","seytroux","sillingy","sixt fer a cheval","sommand","st andre de boege","st blaise","st cergues","st didier","st eusebe","st eustache","st felix","st ferreol","st germain sur rhone","st gervais les bains","st gingolph","st jean d aulps","st jean de sixt","st jean de tholome","st jeoire en faucigny","st jorioz","st julien en genevois","st laurent","st martin bellevue","st martin sur arve ","st maurice de rumilly","st nicolas de veroce","st paul en chablais","st pierre en faucigny","st sigismond","st sixt","st sylvestre","talloires","taninges","thairy","thollon les memises","thones","thonon les bains","thorens glieres","thusy","thyez","usinens","vacheresse","vailly","val de fier","valleiry","vallieres","vallorcine","vanzy","vaulx","veigy foncenex","verchaix","vercland","vers","versonnex","vetraz monthoux","veyrier du lac","vieugy","villard","villaz","ville en sallaz","ville la grand","villy le bouveret","villy le pelloux","vinzier","viry","viuz en sallaz","viuz la chiesaz","vougy","vovray en bornes","vulbens","yvoire"],
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			autoFill:false,
			maxItemsToShow:10
			,mustMatch:0
		}
	);
	*/
});

function getCalendarValues(id){
	$('#'+id+' > tbody > tr').each(function(){
		var day = new Array();
		$(this).find('.tab_bg_orange').each(function(){
			day.push($(this).attr('abbr'));
		});
		
		$('#input_'+$(this).attr('id')).val(day.join('|'));
	});


}
function populateCalendar(id){

	$('#'+id+' > tbody > tr').each(function(){
		$(this).find('[abbr]').each(function(){
			$(this).removeClass("tab_bg_orange");
			$(this).addClass("tab_bg_gris");		
		});

		if($('#input_'+$(this).attr('id')).val()){
			var day = $('#input_'+$(this).attr('id')).val().split('|');
			
			for(i=0; i< day.length ; i++){
				$(this).find('[abbr="'+day[i]+'"]').each(function(){
					$(this).removeClass("tab_bg_gris");
					$(this).addClass("tab_bg_orange");		
				});
			}
		}
	});
}