//this function preloads the specific images, that are used in the "Search Results" section, 
//and that are not present in the document, when the page is loaded
function preload_sr_images_ny(){
	 var sr_images_paths = new Array(
		"/images/search_results/layer_w800_bg_top.jpg",
		"/images/search_results/layer_w800_bg_bottom.jpg",
		"/images/search_results/layer_w800_bg_gred.jpg",
		"/images/search_results/close_layer_x.png",
		"/images/search_results/manhattan_tab_on.jpg",
		"/images/search_results/x_clear.gif",
		"/images/search_results/submit_btn.jpg"
	 );
	 preload_images(sr_images_paths);
}

/*Verifications and actions that must be perform on "f" form submission: start */
function f_ny_form_submit_actions(){
	cnl_create_data_str(); //create data string for neighborhoods
	ctybl_create_data_str(); //create data string for styles and year built
}

//DOCUMENT READY: all the functions that have to be called and the tasks that have to be performed, 
//when the "Search Results" section is loaded
/* DOCUMENT READY: start */
$(document).ready(function(){
	
	/* Update location description */
	updLocationDescriptionSpan();
	
	/* preload images */
	preload_sr_images_ny();
	
	/* present "Property Details Layer" if global variable ml_num has a value */
	if((typeof(ml_num_sel)!='undefined') && (ml_num_sel != '0') && (ml_num_sel != '')){
	  show_property_details_layer(ml_num_sel, data_source, '');
	}
	
	//Go to LI pages when the "ny_sel_property_type_li" select changes
	$("#ny_sel_property_type_li").bind("change", function() {
	  var selected_val = $(this).val();
	  switch (selected_val) {
		  case "lirs":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Residential+Sales&f_location_ht=Hamptons&ny_f_location_br=br&f_submit_btn.x=61&f_submit_btn.y=14&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "lirr":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Residential+Rentals&f_location_ht=Hamptons&ny_f_location_br=br&f_submit_btn.x=81&f_submit_btn.y=8&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "lics":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Commercial+Sales&f_location_ht=Hamptons&ny_f_location_br=br&f_submit_btn.x=64&f_submit_btn.y=3&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "licr":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Commercial+Rentals&f_location_ht=Hamptons&ny_f_location_br=br&f_submit_btn.x=53&f_submit_btn.y=5&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "lil":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Land&f_location_ht=Hamptons&ny_f_location_br=br&f_submit_btn.x=57&f_submit_btn.y=13&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
      }
	});
	
	//Go to HT pages when the "ny_sel_property_type_li" select changes
	$("#ny_sel_property_type_ht").bind("change", function() {
	  var selected_val = $(this).val();
	  switch (selected_val) {
		  case "htrs":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Long+Island&f_location_ht=Residential+Sales&ny_f_location_br=br&f_submit_btn.x=52&f_submit_btn.y=3&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "htrr":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Long+Island&f_location_ht=Residential+Rentals&ny_f_location_br=br&f_submit_btn.x=74&f_submit_btn.y=3&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "htwp":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Long+Island&f_location_ht=Waterfront+Properties&ny_f_location_br=br&f_submit_btn.x=40&f_submit_btn.y=7&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "htsr":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Long+Island&f_location_ht=Seasonal+Rentals&ny_f_location_br=br&f_submit_btn.x=77&f_submit_btn.y=4&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
		  case "htl":
			window.location = '/templates/finder/results_li.xpnd?f_location_ny=nyc&f_location_li=Long+Island&f_location_ht=Land&ny_f_location_br=br&f_submit_btn.x=46&f_submit_btn.y=7&f_submit_btn=SUBMIT&f_submit_check=1&f_sort_by=price_dsc&f_sort_order=dsc&f_towns=&f_school_districts=&f_style=&f_yr_built=&f_min_price=No+Min.&f_max_price=No+Max.&f_max_annual_taxes=No+Limit&f_tot_bedrooms=Any&f_tot_bathrooms=Any&f_min_lot_size=Any';
			break;
      }
	});
	
	//Go to NY pages when the "sel_property_type_li" select changes
	$("#ny_sel_property_type_ny").bind("change", function() {
	  var selected_val = $(this).val();
	  switch (selected_val) {
		 case "New York City":
			window.location = '/templates/finder/results_mht.xpnd?ny_f_location_ny=Residential+Sales';
			break;
		 case "Residential Sales":
			window.location = '/templates/finder/results_mht.xpnd?ny_f_location_ny=Residential+Sales';
			break;
		 case "Residential Rentals":
			window.location = '/templates/finder/results_mht.xpnd?ny_f_location_ny=Residential+Rentals';
			break;
	  } 
	});
	//Go to NY pages when the "ny_sel_property_type_br" select changes
	$("#ny_sel_property_type_br").bind("change", function() {
	  var selected_val = $(this).val();
	  switch (selected_val) {
		 case "brrs":
			window.location = '/templates/finder/results_brk.xpnd?ny_f_location_ny=nyc&ny_f_location_li=li&ny_f_location_ht=ht&ny_f_location_br=Residential+Sales&f_submit_btn_ny.x=71&f_submit_btn_ny.y=15&f_submit_btn_ny=SUBMIT&f_submit_check_ny=1&f_sort_by_ny=price_dsc&f_sort_order_ny=dsc&f_min_price_ny=No+Min.&f_max_price_ny=No+Max.&f_maint_common_charges_ny=No+Limit&f_real_estate_taxes_ny=No+Limit&f_tot_bedrooms_ny=Any&f_tot_bathrooms_ny=Any&f_tot_rooms_ny=Any&f_aprox_sqft_ny=No+Limit';
			break;
	  } 
	});
	
	//"f_ny" form submit button
	$("#f_submit_btn_ny").bind("click", function() {
		f_ny_form_submit_actions();
		submit_form('f_ny');
	});
	  
	//Choose Neighborhoods layer
	
	$("#choose_neighborhoods_btn_ny").bind("click", function() {
		show_hide_layer('choose_neighborhoods_layer');
	});
	$("#cnl_close").bind("click", function() {
		show_hide_layer('choose_neighborhoods_layer');
	});
	$("#cnl_btn_clear").bind("click", function() {
		cnl_clear_checkboxes();
	});
	$("#cnl_btn_submit").bind("click", function() {
		f_ny_form_submit_actions();
		submit_form("f_ny");
	});
	
	//Choose Type & Year Built layer
	$("#choose_type_yrbuilt_btn_ny").bind("click", function() {
		show_hide_layer('choose_type_yrbuilt_layer');
	});
	$("#ctybl_close").bind("click", function() {
		show_hide_layer('choose_type_yrbuilt_layer');
	});
	$("#ctybl_btn_clear").bind("click", function() {
		ctybl_clear_checkboxes();
	});
	$("#ctybl_btn_submit").bind("click", function() {
		f_ny_form_submit_actions();
		submit_form("f_ny");
	});
});
/* DOCUMENT READY: end */

/* Sort by functionality: start */
function set_sort_by_value(sort_by){
  $('#f_sort_by_ny').val(sort_by);
}

function set_sort_order_value(sort_order){
  $('#f_sort_order_ny').val(sort_order);
}

function set_sort_values(sort_by, sort_order){
	if(sort_order == ""){
	  var regex_sort_order = /(_asc|_dsc)/;
	  if(sort_by.search(regex_sort_order) != -1){
		sort_order = (sort_by.search(/_asc/) != -1) ? "asc" : "dsc";
	  }
	  else{
		sort_order = "asc";
	  }
	}
	else{
	  if(sort_by == "zip")
		sort_order = "asc"
	  else
	    sort_by = sort_by.replace(/(_asc|_dsc)/, ("_" + sort_order) );	
	}
	set_sort_by_value(sort_by);
	set_sort_order_value(sort_order);
	submit_form("f_ny");
}
/* Sort by functionality: end */


/* functions to change description on location panel: start */
function changeLocationDescription(loc_type, val)
{
  var ny_sel_ny = $('#ny_sel_property_type_ny');
  
  var span_ny = $('#location_description_ny');
  
  var str_base = "You are now searching";
  var str_end = "<br>Click above to change location or property type."
  var default_msg = "Please choose a location and property type above."; 
  var location = ''; 
  
  location = "New York City";
  ny_sel_ny.val(val);
  
  switch (val) {
	  case "New York City":
		span_ny.html(default_msg);
		break;
	  default:
		span_ny.html(str_base + " " + location + " " + val + "." + str_end);
		break;
  } 
}

function updLocationDescriptionSpan()
{
  if((typeof(location_ny)!='undefined') && (location_ny!='') && (location_ny!='New York City'))
    changeLocationDescription('ny', location_ny);
}
/* functions to change description on location panel: end */


/* Choose Neighborhoods layer: start */

//unchecks the checkboxes contained in one of the layers (Style or Year Built) of the "Choose Style & Year Built" layer
function cnl_clear_checkboxes()
{
  var c1 = '#cnl_manhattan_panel';
  var c1_checked_items = $(c1 + " :checked");
	
  if($(c1).is(':visible')){
    c1_checked_items.each(function() {
	  $(this).attr('checked', false);
    });
  }
}

//creates the data string that contains all the selected neighborhoods
function cnl_create_data_str()
{
  //ID Example: cnlchbx_1(item id)
  
  var c1 = '#cnl_manhattan_panel';
  var c1_checked_items = $(c1 + " input:checked");
  
  var data_str = '';
  var i = 0;
  var cnlchbx_id = '';
  var main_id_part = '';

  //Styles
  c1_checked_items.each(function() {
	cnlchbx_id = $(this).attr('id');
	main_id_part = cnlchbx_id.replace(/^cnlchbx_/,'');
	data_str +=  (i == 0) ? main_id_part : ( "-" + main_id_part);
	i++;
  });
  
  //style data_str example: 1-2-9-11
  $("#f_neighborhoods").val(data_str);
    
}
/* Choose Neighborhoods layer: end */

/* Choose Type & Year Built layer: start */
//unchecks the checkboxes contained in the "Choose Type & Year Built" layer
function ctybl_clear_checkboxes()
{
  var c1 = '#ctybl_listing_type_panel';
  var c2 = '#ctybl_year_built_panel';

  var c1_checked_items = $(c1 + " :checked");
  var c2_checked_items = $(c2 + " :checked");
	
  if($(c1).is(':visible')){
    c1_checked_items.each(function() {
	  $(this).attr('checked', false);
    });
  }
  if($(c2).is(':visible')){
    c2_checked_items.each(function() {
	  $(this).attr('checked', false);
    });
  }
}

//creates the data string that contains all the selected school districts for each county
function ctybl_create_data_str()
{
  //ID Example (Styles): ctyblchbx_1(styles)_1(sales|rentals)_1(item id)
  //ID Example (Year Built): ctyblchbx_2(year built)_7(item id)
  
  var c1 = '#ctybl_listing_type_panel';
  var c2 = '#ctybl_year_built_panel';
  var c1_checked_items = $(c1 + " input:checked");
  var c2_checked_items = $(c2 + " input:checked");
  
  var data_str = '';
  var i = 0;
  var ctyblchbx_id = '';
  var ctyblchbx_id_parts = '';
  var main_id_part = '';

  //Styles
  c1_checked_items.each(function() {
	ctyblchbx_id = $(this).attr('id');
	main_id_part = ctyblchbx_id.replace(/^ctyblchbx_/,'');
	data_str +=  (i == 0) ? main_id_part : ( "-" + main_id_part);
	i++;
  });
  
  //style data_str example: 1_1_1-1_1_2-1_1_9-1_1_10
  $("#f_styles_mht").val(data_str);
  
  //Year Built
  i = 0;
  data_str = '';
  
  c2_checked_items.each(function() {
	ctyblchbx_id = $(this).attr('id');
	main_id_part = ctyblchbx_id.replace(/^ctyblchbx_/,'');
	data_str +=  (i == 0) ? main_id_part : ( "-" + main_id_part);
	i++;
  });
  
  //year built data_str example: 2_1-2_2-2_9-2_12
  $("#f_yr_built_mht").val(data_str);
  
}
/* Choose Style, Year Built layer: end */
