function error_display(message, title, icon, callback) { // display an error popup, with onliy an OK button. This is different from teh confirmation_display function which can go either way. 
	$('#error_console').html('<span class="ui-icon ui-icon-'+icon+' picon"></span><p>'+message+'</p>').attr('title',title);
	// add and remove the classes depending on type of warning. 
	// set title. 
	$("#error_console").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					eval(callback);
					$(this).dialog('destroy');
				}
			}
		});
	$("#error_console").dialog('open');
}

function confirmation_display(message, title, icon, functionyes, functionno) { // confirm yes or no on certain actions. run functionyes or functionno depending on the client's response on the pop-up. 
	var yesorno = '';
	$('#error_console').html('<span class="ui-icon ui-icon-'+icon+' picon"></span><p>'+message+'</p>').attr('title',title);;
	// add and remove the classes depending on type of warning. 
	$("#error_console").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Yes: function() {
					$(this).dialog('close');
					eval(functionyes);
					$(this).dialog('destroy');
				},
				No: function() {
					$(this).dialog('close');
					eval(functionno);
					$(this).dialog('destroy');
				}
			}
		});
	
	$("#error_console").dialog('open');
	return yesorno;
}

function activatefield(thisfield, activecolour, inactivecolour) {
	
	if(activecolour=='' || activecolour==undefined || activecolour==false) {
		var activecolour = '#333';
	}
	if(inactivecolour=='' || inactivecolour==undefined || inactivecolour==false) {
		var inactivecolour = '#999';
	}
	
	thisfield.css('color',inactivecolour);
	thisfield.attr('rel',thisfield.val());
	
	thisfield.focus(function(){
		// action for focus!
		if(thisfield.attr('rel')!=thisfield.val() && thisfield.val()!='') {
			// field has already been modified. set the colour to active, do NOT modify content
			thisfield.css('color',activecolour);
		} else if(thisfield.attr('rel')==thisfield.val()) {
			// field still has not been touched. remove content, set to active colour
			thisfield.val('').css('color',activecolour);
		}
	
		return true;
	});
	
	thisfield.blur(function(){
		// action for blur!
		if(thisfield.attr('rel')!=thisfield.val() && thisfield.val()!='') {
			// field has already been modified. set the colour to active, do NOT modify content
			thisfield.css('color',activecolour);
		} else if(thisfield.attr('rel')==thisfield.val() || thisfield.val()=='') {
			// the user did NOT change anything on the field, or left a blank - reset to original.
			thisfield.val(thisfield.attr('rel')).css('color',inactivecolour);
		}
	
		return true;
	});
}


function ajax_sendemail(formid) {
	$("#"+formid+" input[type='button']").val('processing...').attr('disabled',true);;
	var target = 'http://'+document.domain+'/ajax_sendemail.php';
	$.post(target, $("#"+formid).serialize(),
		function(data){
			if(data.code==1) {
				// success!
				error_display('Your message has been sent successfully! Thank you.','Success!','check','empty_captcha_form("'+formid+'");');
			} else {
				// fail!
				error_display(data.message, 'oops!','alert','Recaptcha.reload();$("#recaptcha_response_field").val("");');
			}
			$("#"+formid+" input[type='button']").val('Send Message').attr('disabled',false);;
			
		}, 'json'
	);
}

function listing_search_load_areas() {
	var selectedoption = $('#listing_search_city').find('option:selected');
		var areaid = selectedoption.attr('rel');
		if(areaid!='' && areaid!=undefined && areaid!=false && selectedoption.val()!='') {
			$.post('http://'+document.domain+'/ajax_subareas.php', {areaid: areaid},
				function(data){
					if(data.code==1) {
						var total = 0;
						for ( x in data.subareas ) total++;
						
						var columns = 3;
						var firstcolumn = Math.ceil(total/columns);
						var secondcolumn = Math.ceil((total-firstcolumn)/2);
						var columncount = 0;
						var count = 0;
						var subareashtml = '';
						$.each(data.subareas, function(key, value){
							if(count==0 && columncount==0) {
								subareashtml = subareashtml+'<div class="grid_3 alpha">';
							} else if(count==firstcolumn && columncount==0) {
								// hit the max # for first column, close div and open another. 
								subareashtml = subareashtml+'</div><div class="grid_3">';
								count = 0;
								columncount++;
							} else if(count==secondcolumn && columncount==1) {
								subareashtml = subareashtml+ '</div><div class="grid_3 omega">';
								count = 0;
							}
							
							subareashtml = subareashtml+'<div class="grid_3 alpha omega"><div class="listing_search_checkbox"><input type="checkbox" name="listing_search_subarea[]" id="listing_search_subarea_'+value.id+'" value="'+value.name+'" rel="'+value.id+'"></div><div class="listing_search_label"><label for="listing_search_subarea_'+value.id+'">'+value.name+'</label></div></div>';
							count++;
						});
						subareashtml = subareashtml+'</div>';
						$('#listing_search_subareas').html(subareashtml);
					} else {
						// fail!
						error_display(data.message, 'oops!','alert',data.message);
					}
				}, 'json'
			);
		} else {
			// area id is blank, meaning there's no valid selection. wipe out the DIV
			$('#listing_search_subareas').html('');
		}
}

function listing_max_price_flag() {
	if($('#listing_search_max_price').val()=='0') {
		// unlimited selected - fad eout the min field
		$('#listing_search_min_price').fadeTo(300,0.5);
	} else {
		// not unlimited price - fade in the min field again.
		$('#listing_search_min_price').fadeTo(300,1.0);
	}
}

function listing_max_sqft_flag() {
	if($('#listing_search_max_sqft').val()=='0') {
		// unlimited selected - fad eout the min field
		$('#listing_search_min_sqft').fadeTo(300,0.5);
	} else {
		// not unlimited price - fade in the min field again.
		$('#listing_search_min_sqft').fadeTo(300,1.0);
	}
}

function empty_captcha_form(formid) {
	var thisform = $('#'+formid);
	thisform.find('input[type!="button"], textarea').each(function(){
		$(this).val('');
		$(this).blur();
	});
	Recaptcha.reload();
	return false;
}


function activate_slideouts() {
	$('.slideout').each(function(){
		var drawer = $(this).find('.slidedout_drawer');
		var handle = $(this).find('.slideout_handle');
		handle.animate({'height':40},600);
		var title = handle.find('h5').html();
		handle.html('<a href="#" title="'+title+'"><img src="/images/blank.gif" /></a>');
		handle.find('a').click(function(){
			drawer.slideToggle(300);
			return false;
		});
		
		
		
	});
}


$(document).ready(function(){
	$('body').prepend('<div id="error_console" class="ui-state-error ui-corner-all ui-helper-hidden">Error me here.</div>');
	
	$('#captcha ul li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
	$('#listing_search_submit').button();
	
	$('#listing_search_form').submit(function(){
		if($('#listing_search_city').val()!='') {
		$.post('http://'+document.domain+'/admin/ajax_listingsurl.php',$(this).serialize(),
			function(data){
				// using result, create new URL. 
				if(data.code==1) {
				window.location = data.listingurl;
				} else {
					error_display(data.message, "Oops!", 'alert','return false;');
				}
			},
			'json'
		);
		} else {
			error_display('You must select a city first.','Need City','alert','');
		}
		return false;
	});
	
	
	$('#listing_search_city').change(function(){
		listing_search_load_areas();
	});
	
	activate_slideouts();

});
