$(function() {    
    
    $('.visu img').noContext();
    $('.photos_popup img').noContext();
    $('.thumbs_popup img').noContext();
    $('.photo_bien img').noContext();
    
    $('#popin').popin({width:800,	height:340, className: "bgPub2"});
    $('#popin').click();
    
    $('#search_form input:radio[name=annonces]').change(function() {    
      updateSearchForm(true);
    });
    
    $('.visu').mouseenter(function() {    
      display_more_info(this);
    });
    
    $('fieldset').mouseleave(function() {    
      hide_more_info(this);
    });
    
    $('.thumbs_popup img').mouseover(function() {
      popup_photo_switch(this);
    });
    
    $('.thumbs_popup img').mouseover(function() {
      popup_photo_switch(this);
    });
    
    $('.thumbs img').mouseover(function() {
      photo_switch(this);
    });
    
    $('.tab_outils tr').mouseover(function() {
      affiche_calc_help(this);
    });
    
    $('#menu_annonces #descriptionbien').click(function() {
      
    });
    
    updateSearchForm(false);
});

function  updateSliderValues(budget_min, budget_max)
{
  $("#prix_mini").val(budget_min);  
  $("#prix_maxi").val(budget_max);
  
  type_transaction = $('#search_form input:radio[name=annonces]:checked').val();
  
  if ( (type_transaction == 'louer' && budget_max == max_price_louer) || ( type_transaction == 'acheter' && budget_max == max_price_acheter ) )
  {
        budget_max_lib = 'Max';
  }
  else
  {
        budget_max_lib = budget_max + " €";
  }
  
  if ( lang == 'fr' )
  {
      text_slide = "de " + budget_min + " € à " + budget_max_lib;
  }
  else
  {
      text_slide = "from " + budget_min + " € to " + budget_max_lib;      
  }
  $( "#amount" ).html(text_slide);
  
  values = [budget_min, budget_max];
  
  $( "#slider-range" ).slider( "option", "values", values);
  
  
  
}

function updateSearchForm(with_slider)
{  
  type_transaction = $('#search_form input:radio[name=annonces]:checked').val();
  
  if ( with_slider )
  {  
    if ( type_transaction == 'louer' )
    {
      $( "#slider-range" ).slider( "option", "min", min_price_louer);
      $( "#slider-range" ).slider( "option", "max", max_price_louer);
      values = [ min_price_louer , max_price_louer ]    
      $( "#slider-range" ).slider( "option", "step", step_price_louer);
      
      updateSliderValues(min_price_louer, max_price_louer); 
    }
    if ( type_transaction == 'acheter' )
    {
      $( "#slider-range" ).slider( "option", "min", min_price_acheter);
      $( "#slider-range" ).slider( "option", "max", max_price_acheter);
      values = [ min_price_acheter , max_price_acheter ]    
      $( "#slider-range" ).slider( "option", "step", step_price_acheter);
      
      updateSliderValues(min_price_acheter, max_price_acheter);
    }
  }
  
  databind = $('#search_form').serialize();
  
  $.ajax({
  type: 'POST',
  processData: true,
  url: url_ville,
  data: databind,
  dataType: 'html',
  success: function(data)
        {    
          $('#villes_biens').html(data);
        }
  });  
  
  $.ajax({
  type: 'POST',
  processData: true,
  url: url_categorie,
  data: databind,
  dataType: 'html',
  success: function(data)
        {    
          $('#categorie_bien').html(data);
        }
  });  
}

function displaySlideMenu(menu_id)
{
    onglets = $('#cadre_onglets_hp .slides');

    
    onglets.stop();  
    
    onglets.hide(0, function()
                            {    
                              $('#'+menu_id).show();
                            }
                );
    $('#menu_annonces li a').removeClass('actif');
    
    
    if ( menu_id == 'exclusivites' )
    {
          $('#slide_exclusivites').show();
    }
    else
    {
          $('#slide_exclusivites').hide();
    }
    
    $('.li_'+menu_id+' a').addClass('actif');
    
		$('#'+menu_id)
		.cycle(
          {
            fx:     'scrollLeft',
            speed:   2000,
            timeout: 5000,
            next:   '#slideshow_next',
            prev:   '#slideshow_prev' ,
            pause:   1
          }
    );    
    current_menu_id = menu_id;
}

var init = false;

function displayOngletBien(onglet)
{
    if ( onglet == 'descriptif' )
    {
      $('#localisation').hide();
      $('#localisation_link').removeClass('actif');
      $('#descriptionbien').show();
      $('#descriptif_link').addClass('actif');
    }
    if ( onglet == 'localisation' )
    {
      $('#descriptionbien').hide();
      $('#descriptif_link').removeClass('actif');
      $('#localisation').show();
      $('#localisation_link').addClass('actif');
      
      if ( !init )
      {
          $('#google_map').html('<iframe id="googlemap_frame" width="420" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+google_map_link+'"></iframe>');
          init = true;
      }
      
    }
}

function display_more_info(elem)
{
  popup = $(elem).prev('.popup');  
  popup.show();
}

function hide_more_info(elem)
{
  popup = $(elem).find('.popup');  
  popup.hide();
}

function popup_photo_switch(img_thumb)
{
  src = $(img_thumb).attr('src');
  new_src = src.replace("/th_", "/");
  
  main_img = $(img_thumb).closest('.thumbs_popup').prev('a').find('.img_popup');  
  
  main_img.attr('src',new_src);
  
}

function photo_switch(img_thumb)
{
  src = $(img_thumb).attr('src');
  new_src = src.replace("/th_", "/");
  
  main_img = $('.photo_bien .view .photo_bien');  
  
  main_img.attr('src',new_src);
}


function affiche_calc_help(tr_line)
{
    title = $(tr_line).attr('title');    
    $('#help_outils').html(title);
}


function changeTypeHabitat()
{
	if ($('#alerte_type_bien').val()==1)
	{
		for ( $i=1; $i<=5; $i++ )
		{
			$ajout = '';
			if ( $i == 5 ) { $ajout = ' ou +' } 			
			 
			$('#type_' + $i).html(' T' + $i + $ajout);			
		}
    $('#td_type_habitat :checkbox').removeAttr("disabled");    
    
    
		$('#td_type_habitat').css('background-color', '#FFF');
		$('#td_surface_terrain').css('background-color', '#CCC');
		$('#alerte_surface_terrain').attr("disabled", true);
    
	}
	else if ($('#alerte_type_bien').val()==2)
	{
		for ( $i=1; $i<=5; $i++ )
		{
			$ajout = '';			
			if ( $i == 5 ) { $ajout = ' ou +' } 
			if ( $i == 1 ) { $titre = ' pièce' } else { $titre = ' pièces' } 			
			 
			$('#type_' + $i).html(' ' + $i + $titre + $ajout);			
		}
    $('#td_type_habitat :checkbox').removeAttr("disabled");    
    
		$('#td_type_habitat').css('background-color', '#FFF');
		$('#td_surface_terrain').css('background-color', '#FFF');
		$('#alerte_surface_terrain').removeAttr("disabled");
	}
	else if ($('#alerte_type_bien').val()==3)
	{
		
    $('#td_type_habitat :checkbox').prop("checked", false);    
    $('#td_type_habitat :checkbox').attr("disabled", true);	
    
		$('#td_type_habitat').css('background-color', '#CCC');
    
		$('#td_surface_terrain').css('background-color', '#FFF');
		$('#alerte_surface_terrain').removeAttr("disabled"); 
	}
	else
	{
    $('#td_type_habitat :checkbox').attr("disabled", true);	
    $('#td_type_habitat :checkbox').prop("checked", false);
    
		$('#td_type_habitat').css('background-color', '#FFF');
		$('#td_surface_terrain').css('background-color', '#CCC')
		$('#surface_terrain').attr("disabled", true);
	}	
	
}





