function searchWhereSuggestion( data, input, fctCloseMenu, departements )
{
 var dataReceiver = $(input).attr('hidden_field');
 
 if( data.status == "true" )
 {
  var iCities = 0;
  var iDepartements = 0;
  
  var div = document.createElement( "div" );
  var listCities = document.createElement( "ul" );
  var listDepartements = document.createElement( "ul" );
  
  $.each( data.result.cities,
          function( i, n )
          {
           iCities++;
           element = document.createElement( "li" );
           $(element).html( n.city_formated + " (" + n.zip_code_formated + ")" );
           
           // Event
           $(element).click( function()
                             {
                              $(input).val( n.city + " (" + n.zip_code + ")" );
                              var convertedVal = ( n.city_search.replace( /\s|\'/g, '-' ) + "-" + n.zip_code.replace( /\s|\'/g, '-' ) ).toLowerCase();
                              if( $("input#" + dataReceiver).length == 1 )
                               $("input#" + dataReceiver).val( convertedVal );
                              else
                              {
                               $("input#search_where_txtSearch").val( convertedVal );
                              }
                              fctCloseMenu();
                             } );
           
           $(listCities).append( element );
          } );
  
  if( !( typeof departements != "undefined" && departements == false ) )
   {
   $.each( data.result.departements,
           function( i, n )
           {
            iDepartements++;
            element = document.createElement( "li" );
            $(element).html( n.departement_name_formated + " (" + n.departement_code_formated + ")" );
            
            // Event
            $(element).click( function()
                              {
                               $(input).val( n.departement_name + " (" + n.departement_code + ")" );
                               if( $("input#" + dataReceiver).length == 1 )
                                $("input#" + dataReceiver).val( ( n.departement_name_search.replace( /\s|\'/g, '-' ) + "-" + n.departement_code.replace( /\s|\'/g, '-' ) ).toLowerCase() );
                               fctCloseMenu();
                              } );
            
            $(listDepartements).append( element );
           } );
  }
  
  if( iCities > 0 || iDepartements > 0 )
  {
   if( iDepartements > 0 )
   {
    element = document.createElement( "li" );
    $(element).html( "Départements" );
    $(element).addClass( "title" );
    
    $(listDepartements).prepend( element );
    $(div).append( listDepartements );
   }
   
   if( iCities > 0 )
   {
    element = document.createElement( "li" );
    $(element).html( "Villes" );
    $(element).addClass( "title" );
    
    $(listCities).prepend( element );
    $(div).append( listCities );
   }
   
   return div;
  }
  else
  {
   element = document.createElement( "li" );
   $(element).html( "<em>Aucun r&eacute;sultat.</em>" );
   $(listCities).append( element );
   $(div).append( listCities );
   
   return div;
  }
 }
 else
 {
  return false;
 }
}


function validSearch(form,type)
{
 var bReturn = false;
 
 if( ( bReturn = validSearchRef(form,type) ) === false )
 {
  var advancedSearch = false;
  if( $("div#advanced_search").length == 1 && $("div#advanced_search").css("display") == "block" )
   advancedSearch = true;
  else
   advancedSearch = false;
  
  
  // Inputs
 
  
  var inputWhere = $(form).find("input[name='where']:checked");
  if( inputWhere.length == 0 )
   inputWhere = $(form).find("input[name='where']:first");
  
  var inputWhere_1 = $(form).find("input#search_where_txtSearch_1");
  var inputWhere_2 = $(form).find("input#search_where_txtSearch_2");
  var inputWhere_3 = $(form).find("input#search_where_txtSearch_3");
  var inputWhere_4 = $(form).find("input#search_where_txtSearch_4");
  //var inputWhere_1 = $(form).find("input[name='where_1']");
  /*var inputWhere_2 = $(form).find("input[name='where_2']");
  var inputWhere_3 = $(form).find("input[name='where_3']");
  var inputWhere_4 = $(form).find("input[name='where_4']");
  */
  
  
  var inputProperty = $(form).find("input[name='property']");
  if( inputProperty.length == 0 )
   inputProperty = $(form).find("select[name='property']");
  
  
  var inputBudget = $(form).find("input[name='budget']");
  if( inputBudget.length == 0 )
   inputBudget = $(form).find("select[name='budget']");
  
  var inputBudget_catched = $(form).find("input[name='budget']");
   
  var inputBudget_min = $(form).find("input[name='budget_min']");
  var inputBudget_max = $(form).find("input[name='budget_max']");
  
  
  var inputSurface_min = $(form).find("input[name='surface_min']");
  var inputSurface_max = $(form).find("input[name='surface_max']");

   
  
  var inputNumberOfBedrooms = $(form).find("input[name='number_of_bedrooms']");
  if( inputNumberOfBedrooms.length == 0 )
   inputNumberOfBedrooms = $(form).find("select[name='number_of_bedrooms']");
  
  
  
  var inputSquareMeterPrice = $(form).find("input[name='square_meter_price']");
  if( inputSquareMeterPrice.length == 0 )
   inputSquareMeterPrice = $(form).find("select[name='square_meter_price']");
  
  
  var inputHabitableSurface = $(form).find("input[name='habitable_surface']");
  if( inputHabitableSurface.length == 0 )
   inputHabitableSurface = $(form).find("select[name='habitable_surface']");
  
  if( advancedSearch == true )
  {
   var inputAgency = $(form).find("input[name='agency']");
   if( inputAgency.length == 0 )
    inputAgency = $(form).find("select[name='agency']");
   
   var inputQualification = $(form).find("input[name='qualification']");
   if( inputQualification.length == 0 )
    inputQualification = $(form).find("select[name='qualification']");
   
   var inputKmAround = $(form).find("input[name='km_around']");
   if( inputKmAround.length == 0 )
    inputKmAround = $(form).find("select[name='km_around']");
   
   var inputOrderBy = $(form).find("input[name='order_by']");
   if( inputOrderBy.length == 0 )
    inputOrderBy = $(form).find("select[name='order_by']");
   
   var inputOrder = $(form).find("input[name='order']");
   if( inputOrder.length == 0 )
    inputOrder = $(form).find("select[name='order']");
  }
  
  
  // Init action
  var action = "";
  
  
  if( $(form).find("input#"+inputWhere.attr('id')+"_txtSearch").val() != "" && typeof($(form).find("input#"+inputWhere.attr('id')+"_txtSearch").val()) != "undefined")
   action += $(form).find("input#"+inputWhere.attr('id')+"_txtSearch").val() + "/";
  else if( $(inputWhere_1).length > 0 && $(inputWhere_1).val().length > 0 ) 
   action += $(inputWhere_1).val() + "/"; 
  else action += "-/";
  
  
   
   //action += 'search/';
   if( advancedSearch == true )
   {
    if( $(inputKmAround).val() != "" )
     action += $(inputKmAround).val() + "kms/";
   }
   
   
  
   
   

   
   switch( $(inputProperty).val() )
   {
    case 'house':     action += "maison/"; break;
    case 'apartment': action += "appartement/"; break;
    case 'homestead': action += "propriete/"; break;
    case 'terrain':   action += "terrain/"; break;
    case 'new_all':   action += "neuf/"; break;
    case 'new_house':   action += "maison-neuve/"; break;
    case 'new_apartment':   action += "appartement-neuf/"; break;
   }
   
   
    
   //if( $(inputWhere_1).length > 0 ) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "ville_1=" + $(inputWhere_1).val();
   if( $(inputWhere_2).length > 0 && $(inputWhere_2).val().length > 0) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "ville_2=" + $(inputWhere_2).val();
   if( $(inputWhere_3).length > 0 && $(inputWhere_3).val().length > 0) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "ville_3=" + $(inputWhere_3).val();
   if( $(inputWhere_4).length > 0 && $(inputWhere_4).val().length > 0) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "ville_4=" + $(inputWhere_4).val();
   
 
   if( $(inputBudget).length > 0 && $(inputBudget).val() != "0-1000000000" )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "budget=" + $(inputBudget).val();
   if( $(inputBudget_catched).length > 0 && parseFloat($(inputBudget_catched).val().replace( / /g, '' )) > 0 )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "budget=" + $(inputBudget_catched).val();
   if( $(inputBudget_min).length > 0 && parseFloat($(inputBudget_min).val().replace( / /g, '' )) > 0 )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "budget_min=" + $(inputBudget_min).val().replace( / /g, '' );
   if( $(inputBudget_max).length > 0 && parseFloat($(inputBudget_max).val().replace( / /g, '' )) > 0 )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "budget_max=" + $(inputBudget_max).val().replace( / /g, '' );
   if( $(inputSurface_min).length > 0 && parseFloat($(inputSurface_min).val().replace( / /g, '' )) > 0 )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "surface_min=" + $(inputSurface_min).val().replace( / /g, '' );
   if( $(inputSurface_max).length > 0 && parseFloat($(inputSurface_max).val().replace( / /g, '' )) > 0 )                     action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "surface_max=" + $(inputSurface_max).val().replace( / /g, '' );
   if( $(inputNumberOfBedrooms).length > 0 && $(inputNumberOfBedrooms).val() != "0" )            action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "number_of_bedrooms=" + $(inputNumberOfBedrooms).val();
   if( $(inputHabitableSurface).length > 0 && $(inputHabitableSurface).val() != "0-1000000000" ) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "habitable_surface=" + $(inputHabitableSurface).val();
   if( $(inputSquareMeterPrice).length > 0 && $(inputSquareMeterPrice).val() != "0-1000000000" ) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "square_meter_price=" + $(inputSquareMeterPrice).val();
   

   
   
   if( advancedSearch == true )
   {
    if( $(inputAgency).length > 0 && $(inputAgency).val() != "" )               action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "agency=" + $(inputAgency).val();
    if( $(inputQualification).length > 0 && $(inputQualification).val() != "" ) action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "qualification=" + $(inputQualification).val();
    if( $(inputOrderBy).length > 0 && $(inputOrderBy).val() != "" )             action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "order_by=" + $(inputOrderBy).val();
    if( $(inputOrder).length > 0 && $(inputOrder).val() != "" )                 action += ( action.substr( action.length-1, 1 ) == "/" ? "?" : ";" ) + "order=" + $(inputOrder).val();
   }
  //}
  
  
  if( typeof type != "undefined" || type == "vente" )
  {
   if( action == "" )
    action = "/vente/search/";
   else
    action = "/vente/" + action;
  }
  else
  {
   if( action == "" )
    action = "/achat/search/";
   else
    action = "/achat/" + action;
  }

  // Change action of form
  $(form)[0].action = action;
  bReturn = true;
 }
 
 return bReturn;
}


function validSearchRef(form,type)
{
 var bReturn = false;
 
 if( $(form).find("input[name='reference']").length == 1 && $(form).find("input[name='reference']").val() != '' )
 {
  // Init action
  var action = "";
  if( typeof type != "undefined" || type == "vente" )
   action = "/vente";
  else
   action = "/achat";
  action += "/search/" + $(form).find("input[name='reference']").val().replace( /\s/g, '-' ).toLowerCase() + "/";
  
  // Reset form
  $(form).find(":input:not(:submit,:button,input[name='reference'])").val('');
  
  // Change action of form
  $(form)[0].action = action;
  bReturn = true;
 }
 
 return bReturn;
}


function developAdvancedSearch()
{
 if( $("div#advanced_search").css("display") == "block" )
 {
  $("div#advanced_search").slideUp( function(){ $("h3#advanced_search_title").removeClass("develop"); } );
  $("div#advanced_search :input:not( :button, :submit )").val('');
 }
 else
  $("div#advanced_search").slideDown( function(){ $("h3#advanced_search_title").addClass("develop"); } );
}


function height_img_fc() {
 var maxX = 200;
 var maxY = 150;

  $("#preview img").css( 'height', maxY );
  $("#preview img").css( 'width', maxX );


// $("#preview")
//  .css("top",(e.pageY - xOffset) + "px")
// 	.css("left",(e.pageX + yOffset) + "px")
}


this.imagePreview = function(){
	/* CONFIG */

		xOffset = 10;
		yOffset = 30;
  maxX = 400;
  maxY = 300;

		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result

	/* END CONFIG */
	$("img.preview").hover(function(e)
  {
   this.t = this.title;
   this.title = "";
   var c = (this.t != "") ? "<br/>" + this.t : "";
   $("body").append("<p id='preview'><img src='"+ $(this).attr('href') +"' alt='Image preview' />"+ c +"</p>");

   $("#preview").delay(500)
    .fadeIn("fast");

   setTimeout('height_img_fc()', 500);
   var position = $(this).offset();
   $("#preview")
    .css("top", (position.top - 190 ) + "px")
    .css("left", (position.left - 1) + "px");
  },
	 function(){
		 this.title = this.t;
		 $("#preview").remove();
  }
 );
};

// starting the script on page load
$(document).ready(function(){
	imagePreview();
 thousandSeparator();
});

function thousandSeparator()
{
 $.each($('.thousandSeparator'), function(index, elem) 
 {
  $(elem).keyup(function()
  {
   var newNumber = "";
   var oldNumber = $(this).val().replace( /\s/g, '' );
   
   for( var i = 0 ; i < oldNumber.length ; i++ )
    newNumber = ( i % 3 == 2 && i < oldNumber.length - 1 ? " " : "" ) + oldNumber.charAt( oldNumber.length - 1 - i ) + newNumber;
    
   $(this).val( newNumber );
  });
  
 });
}



