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

function auto_fill_livesearch(id, v){
	
	if( ($(id).val()) && ($(id).val()!=v) ) return;
	
	$(id).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val('').addClass('focus');
		}
	}).blur(function(){
		if($(this).val()==''){
			$(this).removeClass('focus').val(v);
		}
	});

}


function submit_freetext(){

	var term = $('input[name="livesearch"]').val();
	if(term && term.length>2 && term != 'e.g. postcode, road, semi, pool'){
		window.location = '/ajaxsearch/all/' + urlencode(term) + '/desc/1';
	}
}

function urlencode(str){
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}


function toggle_prices(show_div){

	switch(show_div){
		
		case 'sales':
			$('#all_searchform .sales_info').show();
			$('#all_searchform .lettings_info').hide();
			$('#all_searchform .newhomes_info').hide();
			$('#all_searchform').attr('action','/property-for-sale/search/results');
		break;
		
		case 'lettings':
			$('#all_searchform .sales_info').hide();
			$('#all_searchform .lettings_info').show();
			$('#all_searchform .newhomes_info').hide();
			$('#all_searchform').attr('action','/property-to-let/search/results');
		break;
		
		case 'newhomes':
			$('#all_searchform .sales_info').hide();
			$('#all_searchform .lettings_info').hide();
			$('#all_searchform .newhomes_info').show();
			$('#all_searchform').attr('action','/new-homes/search/results');
		break;
		
	}
	
}

function check_all_form(){
	
	// Check a radio has been selected
	if(!$('input[name=search_type]:checked').val()){
		$('p#all_error').text('Please choose a search type').fadeIn();
		return false;
	}	
	
	// Check min is less than max
	if(!$('input[name=search_type]:checked').val()){
		
	}
}


$(document).ready(function(){
 
	//Jquery use for setting up rel="external" links
	$('a[rel=external]').each(function(){
		this.target = '_blank';
	});	
	
	// Livesearch stuff
	auto_fill_livesearch($('#search'), 'Know the area or property? Type any keywords here...');
	
	$('#search').autocomplete('/ajaxsearch/search/', {
	 
	  minChars: 3,
	  mustMatch: false,
	  selectFirst: false,
	  autoFill: false,
	  dataType: 'json',
	  matchSubset: false,
	  scroll: false,
	  width: 360,
	  
	  parse: function(jdata) {
		var parsed_data = []; 
		
		if (!jdata || jdata.length == 0) {
             // handle no results case specially
			parsed_data[0] = {data: {
								id:-1,
								url:'#'
							  }};
        }
		else{
			
			for (var i=0; i<jdata.length; i++){
				obj = jdata[i];
				parsed_data[i] = { 
					data: obj, 
					id: obj.id
				};
			}
			
		}
		return parsed_data;
	  },
	 
	  formatItem: function(property, i, n){
		//alert(property +','+ i +','+ n);
		
		if(property.id==-1){	
			return '<div class="ajax">Sorry, no results<\/div>';
		}
	
		var html = '<div class="ajax">';
		
		if(property.directory){
			html += '<img src="\/images\/properties\/' + property.directory + '\/thumb.jpg" class="left" \/>';
		}
		else{
			html += '<img src="\/images\/awaiting_thumb.jpg" class="left" \/>';
		}
		
		html += '<p class="address"><span class="bold">' + property.address + '<\/span><br \/>';
		
		if(property.bedrooms>1) html += property.bedrooms + ' Bed ';
		if(property.name) html +=  property.name;
		
		html += '<\/p>\
				<p class="price_str">' + property.price_str + '<\/p>\
				<\/div>';
		
		return html;
	  
	  }
	  
	
	}).result(function(event, item){
	  	location.href = item.url;
	});

		
	auto_fill($('#keywords'), 'Keyword (town/feature/etc)');
	
	$('.property-holder').click(function(){

    	window.location=$(this).find("a").attr("href"); return false;
	
	});
	
	
	$('.property-holder').hover(
		function(){
			$(this).css({ borderColor: '#000000' });
		}, 
		function(){
			$(this).css({ borderColor: '' });
		}
    );
	
	$('.gallery_showcase').hover(
		function(){
			$(this).css({ borderColor: '#000000' });
		}, 
		function(){
			$(this).css({ borderColor: '' });
		}
    );
	
	$('#all_searchform').submit(check_all_form);
	
	// Switch submit img on hover
	$('input.submit_img').hover(
		function(){
			$(this).attr('src', $(this).attr('src').replace('view','view_over'));
		}, 
		function(){
			$(this).attr('src', $(this).attr('src').replace('view_over','view'));
		}
	);
	
	$('a.swap').hover(
		function(){
			$(this).children().attr('src', $(this).children().attr('src').replace('.png','_over.png'));
		}, 
		function(){
			$(this).children().attr('src', $(this).children().attr('src').replace('_over.png','.png'));
		}
	);
	
	$('input[type="radio"]').css('border','none');
	
	/*
	// Affiliates Slider
	// <jon@kaweb.co.uk>
	$('#afflink').toggle(function(){

		$('#affhold').animate({ height: '40px' }, 500, function(){
 
			$('#afflink span').html("&darr;") 
		});
		
		return false;
		
	}, function(){
	
		$('#affhold').animate({ height: '0px' }, 250, function(){

			$('#afflink span').html("&uarr;");
		});
		
		return false;
	});
	*/

});