var tabs = ['sales','lettings','studentlets'];

function show_search(div_to_show){
	
	//alert('s ' + div_to_show);
	
	$.each(tabs, function( intIndex, objValue ){
		if(objValue!=div_to_show){
			$('#home_' + objValue + ':visible').hide();
		}
		else{
			if($('#home_' + objValue ).is(':hidden')){
				$('#home_preview').hide();
				do_ajax(div_to_show, true);
				$('#home_' + objValue).show('slide','','', show_preview);
			}
		}
	});
	
}



function do_ajax(form_id, dontshow){
	//alert("form: " + form_id);
	//console.log($('form#'+form_id).serialize());
	//alert('s' + $('form#' + form_id + ' input[name=search_type]').val());
	$.post('/property/' + form_id + '/search/ajax',
	
		{ 
			search_type: $('form#' + form_id + ' input[name=search_type]').val(),
			location: $('form#' + form_id + ' select[name=location]').val(),
			bedrooms: $('form#' + form_id + ' select[name=bedrooms]').val(),
			min_price: $('form#' + form_id + ' select[name=min_price]').val(),
			max_price: $('form#' + form_id + ' select[name=max_price]').val(),
			min_price_lettings: $('form#' + form_id + ' select[name=min_price_lettings]').val(),
			max_price_lettings: $('form#' + form_id + ' select[name=max_price_lettings]').val(),
			min_price_studentlets: $('form#' + form_id + ' select[name=min_price_studentlets]').val(),
			max_price_studentlets: $('form#' + form_id + ' select[name=max_price_studentlets]').val(),
			keywords: $('form#' + form_id + ' input[name=keywords]').val()
		},
		
		function(data){
			$('#home_preview span').text( data );
			
			if(dontshow!=true) {
				$('#home_preview').hide();
				$('#home_preview').fadeIn('normal');
			}
			//console.log(data);
			//alert(data);
		}
		
	);
}

function initiate_ajax(){
	
	// Bind an event handler on the appropriate form elements
	$('select').change(function(){
		do_ajax($(this).parents('form').attr('id'));							
	});
	
	$('input[name="keywords"]').keyup(function() {
		if ($(this).val() != $(this).lastValue && $(this).val().length>2) {
		  if ($(this).timer) clearTimeout($(this).timer);
		  $(this).timer = setTimeout('do_ajax("' + $(this).parents('form').attr('id') + '")', 350);
		  $(this).lastValue = $(this).val();
		}
	});
	
	return;
	
}


function auto_fill_home(id, v){
	
	if( ($(id).val()) && ($(id).val()!=v) ) return;
	
	$(id).css({ color: '#CB1003'}).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val('').css({ color: '#333333' });
		}
	}).blur(function(){
		if($(this).val()==''){
			$(this).css({ color: '#CB1003' }).val(v);
		}
	});
	
	return;
	
}



function show_preview(section){ 
	
	$('#home_preview').show('slide');
	return;

}



function check_lets(){

	if( ! $('input[name="lettings_type"]:checked').val() ){

		$('span#error').fadeIn();
		return false;
	}
	
	return;
}


function slide_switch() {
   
	var $active = $('#slideshow img.active');

	if ( $active.length == 0 ) $active = $('#slideshow img:last');
	
	var $next =  $active.next().length ? $active.next()
		: $('#slideshow img:first');
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1500, function() {
			$active.removeClass('active last-active');
		});
	
	return;
}



$(document).ready(function(){

	// Kick off slideshow
	setInterval( "slide_switch()", 5000 );
	
	// Bind "click to reveal search"
	$.each(tabs, function( intIndex, objValue ){
		//alert(objValue);
		$('#show_' + objValue).click(function(c){
			c.preventDefault();
			show_search(objValue);
    	});
	
	});
	
	
	// Search keywords
	auto_fill_home($('input[name="keywords"]'), 'Keyword e.g. town / feature');

	// Form listeners for preview
	initiate_ajax();
	
	return;
});
