// "Global" vars
var selected_areas = [];
var running_total = 0;
var last_keywords = '';
var current_keywords = '';
var timer;

function toggle_search_area(area, find_it){
	
	var area_name = !find_it ? area.id : area;
	//alert(area_name);
	//alert('a:' + in_array(area_name, selected_areas));
	
	if(!selected_areas.in_array(area_name)){	// Area has been selected, so add in
		
		selected_areas.push(area_name);
		$(area_name).setStyle({  backgroundImage: 'url("/images/tick.png")' });
		//$('searchmap').insert({after: '<img src="/images/search/tick_circle.png" id="'+area_name+'_tick" class="tick pngfix" title="Area is selected, click to de-select" onclick="toggle_search_area(\''+area_name+'\', true);">'});
		
		//$(area_name).src = '/images/search/' + area_name + new_file;
	}
	else{								// Area has been de-selected, so remove
		selected_areas.splice(selected_areas.indexOf(area_name), 1);
		$(area_name).setStyle({ backgroundImage: 'none' });

		//$(area_name+'_tick').remove();
		//$(area_name).src = '/images/search/' + area_name + new_file;
	}
	
	//alert(selected_areas);
	if(selected_areas.size()>0){
		appear_if_not_visible("next");
		refresh_div("info");
		$("info").update('When you have finished, click the arrow');
		
		setTimeout('get_results_preview_1()', 1);
	}
	else{
		refresh_div("info");
		$("info").update('Choose the branch(es) you are interested in');
		
		toggle_div("total_1", false);
		toggle_div("next", false);
	}
	
	return true;	
}



function submit_form(){
	$("areas").value = selected_areas.toJSON();
	//alert(selected_areas.toJSON());
	$("search_form").submit(); 
}

// Makes sure the max > min and warns if not
function check_price_range(no_ajax){
	
	var do_ajax = no_ajax || true;
	
	if($('min_price')){
		minimum = $("min_price").options[$("min_price").selectedIndex].value;
		maximum = $("max_price").options[$("max_price").selectedIndex].value;
	}
	else{
		minimum = $("min_price_rentals").options[$("min_price_rentals").selectedIndex].value;
		maximum = $("max_price_rentals").options[$("max_price_rentals").selectedIndex].value;
	}
	
	//alert(minimum + ' ' + maximum);
	if(minimum>0 && maximum>0 && (parseInt(minimum)>parseInt(maximum))){
		$("errors").update("The minimum amount cannot be more than the maximum amount");
		toggle_div("errors", true);
		toggle_div("search", false);
	}
	else{
		toggle_div("errors", false);
		toggle_div("search", true);
		if(do_ajax) get_results_preview();
	}
	return true;
}


function get_results_preview_1(){
		
	new Ajax.Request('/search/ajaxpreview', {
		
		method:'post',
		
		parameters: {
			areas: selected_areas.toJSON(),
			search_type: parseInt($("search_type").value),
			reduced: parseInt($("reduced").value),
			flexible: parseInt($("flexible").value)
		},
		
		onLoading: function(){
			$("total_1").update('<img src="/images/search/loading.gif" alt="" />');
		},

		onSuccess: function(transport){
			$("total_1").update((transport.responseText ? transport.responseText : "0") + " <span>properties found</span>");
			$("total_2").update((transport.responseText ? transport.responseText : "0") + " <span>properties found</span>");
			refresh_div('total_1');
		},
		
		onFailure: function(transport){ handle_error(request, "/"); }
	
	});

}




function get_results_preview(){
	//alert(selected_areas);
	if($('min_price') || $('max_price')){
		var min_price_val = $("min_price").options[$("min_price").selectedIndex].value;
		var max_price_val = $("max_price").options[$("max_price").selectedIndex].value;
		var min_price_rentals_val = 0;
		var max_price_rentals_val = 0;
	}
	else {
		var min_price_val = 0;
		var max_price_val = 0;	
		var min_price_rentals_val = $("min_price_rentals").options[$("min_price_rentals").selectedIndex].value;
		var max_price_rentals_val = $("max_price_rentals").options[$("max_price_rentals").selectedIndex].value;
	}
	
	
	new Ajax.Request('/search/ajaxpreview', {
		
		method:'post',
		
		parameters: {
			areas: selected_areas.toJSON(), 
			bedrooms: $("bedrooms").options[$("bedrooms").selectedIndex].value,
			min_price: min_price_val,
			max_price: max_price_val,
			min_price_rentals: min_price_rentals_val,
			max_price_rentals: max_price_rentals_val,
			property_type_id: $("property_type_id").options[$("property_type_id").selectedIndex].value,
			search_type: parseInt($("search_type").value),
			reduced: parseInt($("reduced").value),
			flexible: parseInt($("flexible").value),
			keywords: current_keywords // $F() cannot be used in IE for some reason */
		},
		
		onLoading: function(){
			$("total_2").update('<img src="/images/search/loading.gif" alt="" />');
		},

		onSuccess: function(transport){
			$("total_2").update((transport.responseText ? transport.responseText : "0") + " <span>properties found</span>");
			refresh_div('total_2');
		},
		
		onFailure: function(transport){ handle_error(request, "/"); }
	
	});

}


/*	This bit has been written to trigger a get_results_preview() following an update to the 
	keywords field. There is a ready-made class (Form.Element.DelayedObserver) in controls.js but it
	only worked in FF, so I had to write this workaround. Also, $().value, $().getValue() or indeed $F() doesn't seem to work in IE for text fields
	so I had to set a global variable (current_keywords) to access the value (which is sent via onkeyup=monitor_keywords(this.value)).
	Grr indeed.
*/
function check_keywords(){
	if(last_keywords == current_keywords) return false;	
	// Keywords must have changed...
	if((current_keywords.length)<=2) return false; // 2 letters is not enough :(
	last_keywords = current_keywords;
	get_results_preview(); 
	return true;
}

function monitor_keywords(keywords){
	current_keywords = keywords;
	if(timer) clearTimeout(timer);
	timer = setTimeout(check_keywords, 750);
	return true;
}
