$(function(){
	
	var home_diapo_active = false;
	var home_diapo_interval = 0;
	var tps = 1000;
	$('#home_diapo_puce div').click(function(){
		
		if(!$(this).hasClass('active') && !home_diapo_active){
			clearInterval(home_diapo_interval);
			
			home_diapo_active = true;
			var nb = $(this).html();
			$('#home_diapo_puce div.active').removeClass('active');
			$(this).addClass('active');
			
			$('#home_diapo_images img.active').animate({'opacity': 0}, tps).removeClass('active');
			$($('#home_diapo_images img')[nb-1]).animate({'opacity': 1}, tps).addClass('active');
			
			$('#home_diapo_coup_coeur').animate({'left': -200}, tps/2, function(){
				$(this).html('<p>'+$($('#home_diapo_images p')[(nb*2)-1]).html()+'</p>').animate({'left': 6}, tps/2);
			});
			
			$('#home_diapo_accroche').animate({'opacity': 0}, tps/2, function(){
				$(this).html($($('#home_diapo_images p')[(nb*2)-2]).html()).animate({'opacity': 1}, tps/2);
				home_diapo_active = false;
				init_home_diapo();
			});
		}
	});
	
	function init_home_diapo(){
		home_diapo_interval = setInterval(function(){
			if($('#home_diapo_puce div.active').next('div').length > 0)
				$('#home_diapo_puce div.active').next('div').triggerHandler('click');
			else
				$('#home_diapo_puce').find('div').first().triggerHandler('click');
		}, 5000);
	}
	$('#home_diapo_images img').css('opacity', 0);
	$('#home_diapo_puce').find('div').first().triggerHandler('click');
	
	$('input[name=transaction]').change(function(){
		if($('#home_vente').attr('checked')){
			var prix_min = 100000;
			var prix_max = 700000;
			
			var prix_range_min = 100000;
			var prix_range_max = 700000;
			
			var step = 10000;
		}else{
			var prix_min = 0;
			var prix_max = 2000;
			
			var prix_range_min = 0;
			var prix_range_max = 2000;
			
			var step = 50
		}
		
		
		$( ".home_search_prix_range" ).slider('destroy').slider({
			range: true,
			step: step,
			animate: 100,
			min: prix_range_min,
			max: prix_range_max,
			values: [ prix_min, prix_max ],
			slide: function( event, ui ) {
				$( ".home_search_prix_range_min" ).html(ui.values[ 0 ] + " €");
				if(ui.values[0] == prix_range_min && prix_range_min > 0)
					$( ".home_search_prix_range_min" ).html(ui.values[ 0 ] + " € et -");
				if($( "#prix_min" ))
					$( "#prix_min" ).val(ui.values[ 0 ]);
				
				$( ".home_search_prix_range_max" ).html(ui.values[ 1 ] + " €");
				if(ui.values[1] == prix_range_max)
					$( ".home_search_prix_range_max" ).html(ui.values[ 1 ] + " € et +");
				if($( "#prix_max" ))
					$( "#prix_max" ).val(ui.values[ 1 ]);
			}
		});
		$( ".home_search_prix_range_min" ).html($( ".home_search_prix_range" ).slider( "values", 0 )+" €");
		$( ".home_search_prix_range_max" ).html($( ".home_search_prix_range" ).slider( "values", 1 )+" €");
		if($( ".home_search_prix_range" ).slider( "values", 1 ) == prix_range_max)
			$( ".home_search_prix_range_max" ).html($( ".home_search_prix_range" ).slider( "values", 1 )+" € et +");
		if($( ".home_search_prix_range" ).slider( "values", 0 ) == prix_range_min && prix_range_min > 0)
			$( ".home_search_prix_range_min" ).html($( ".home_search_prix_range" ).slider( "values", 0 )+" € et -");
	}).change();
	
	
	$('#home_diapo_back').click(function(){
		document.location.href = $('#home_diapo_accroche').find('a').attr('href');
	});
	
	$('.home_cat_img img').click(function(){
		document.location.href = $(this).parents('.home_cat').find('.home_cat_en_savoir_plus').attr('href');
	});
	
	var ids_dernier_bien = {maison: [], terrain: [], appartement: []};
	$('.home_cat_fleche').click(function(){
		
		$(this).qtip({
			content: 'Chargement...',
			position: {
				my: 'center',
				at: 'center'
			},
			style: {
				classes: 'ui-tooltip-shadow ui-tooltip-light'
			},
            show: false,
            hide: false
		});
		$(this).qtip('api').show();
		
		var fleche = $(this);
		var where;
		var ids;
		if($(this).hasClass('appartement')){
			where = 'transaction = 1 AND id_type = 2';
			if(ids_dernier_bien.appartement.length == 0)
				ids_dernier_bien.appartement.push($(this).parents('.home_cat').find('input').val())
			ids = ids_dernier_bien.appartement;
		}else if($(this).hasClass('maison')){
			where = 'transaction = 1 AND id_type = 1';
			if(ids_dernier_bien.maison.length == 0)
				ids_dernier_bien.maison.push($(this).parents('.home_cat').find('input').val())
			ids = ids_dernier_bien.maison;
		}else{
			where = 'transaction = 2';
			if(ids_dernier_bien.terrain.length == 0)
				ids_dernier_bien.terrain.push($(this).parents('.home_cat').find('input').val())
			ids = ids_dernier_bien.terrain;
		}
		
		var cat = $(this).parents('.home_cat');
		
		$.ajax({
			url: 'modules/Bien/ajax/bien_dernier_bien.ajax.php',
			type: 'post',
			data: {where: where, ids: ids},
			success: function(data){
				if(data){
					data = jQuery.parseJSON(data);
					
					if(data.id_type == 2){
						ids_dernier_bien.appartement.push(data.id_bien);
					}else if(data.id_type == 1){
						ids_dernier_bien.maison.push(data.id_bien);
					}else{
						ids_dernier_bien.terrain.push(data.id_bien);
					}
					
					if(cat.find('.home_cat_img img').attr('src') != data.img){
						cat.find('.home_cat_desc').animate({'opacity': 0}, tps/2);
						cat.find('.home_cat_img img').animate({'opacity': 0}, tps/2, function(){
							$(this).attr('src', data.img).load(function(){
								$(this).animate({'opacity': 1}, tps/2);
								cat.find('.home_cat_desc').html(data.descriptif_court).animate({'opacity': 1}, tps/2);
								fleche.qtip('hide').qtip({
									content: 'Cliquez pour voir les annonces suivantes.',
									position: {
										my: 'bottom center',
										at: 'top center'
									},
									show: {
										event: 'mouseenter'
									},
									hide: 'mouseleave',
									style: {
										classes: 'ui-tooltip-shadow ui-tooltip-light'
									}
								});
							});
						});
					}else{
						cat.find('.home_cat_desc').animate({'opacity': 0}, tps/2, function(){
							$(this).html(data.descriptif_court).animate({'opacity': 1}, tps/2);
							fleche.qtip('hide').qtip({
								content: 'Cliquez pour voir les annonces suivantes.',
								position: {
									my: 'bottom center',
									at: 'top center'
								},
								show: {
									event: 'mouseenter'
								},
								hide: 'mouseleave',
								style: {
									classes: 'ui-tooltip-shadow ui-tooltip-light'
								}
							});
						});
					}
				}else{
					fleche.fadeOut();
					fleche.qtip('hide');
				}
			}
			
		});
	});
	
	$('.home_share').hover(
		function(){
			$(this).find('.home_share_buttons').stop(true, true).fadeIn();
		},
		function(){
			$(this).find('.home_share_buttons').stop(true, true).fadeOut();
		}
	);
	
	$('.home_cat_fleche').qtip({
		content: 'Cliquez pour voir les annonces suivantes.',
		position: {
			my: 'bottom center',
			at: 'top center'
		},
		show: {
			event: 'mouseenter'
		},
		hide: 'mouseleave',
		style: {
			classes: 'ui-tooltip-shadow ui-tooltip-light'
		}
	});
	
	$('#home_lieu').multiselect({
		height: 250,
		checkAllText: 'Tout sélectionner',
		uncheckAllText : 'Tous désélectionner',
		noneSelectedText : 'Villes',
		selectedText : '# ville(s) sur #',
		classes : 'select_lieu'
	});
	
	$('.home_search_label_loc select').click(function(){
		$('.home_search_label_loc').change();
	});
	$('.home_search_label_loc').change(function(){
		
		$('#home_loc').click();
		
	});
});
