// JavaScript Document
function updateMapSearch(page, num_results, area)
{
	//var area = "All";
	var params = "json=map_search&page="+page+"&num_results="+num_results+"&property_category=&area="+area+"&price_low=&price_high=&bedrooms=&bathrooms=&sq_foot=";
	jQuery.ajax({
		type: "POST",
		url: '/mls/mls.js.php',
		data: params,
		dataType: 'json',
		success: function(json){
			//var htmlStr = '<p style="font-size: 11px;">Results <strong>'+(json.min_result==0?(json.count==0?0:1):json.min_result)+' - '+json.max_result+'</strong> of <strong>'+json.count+'</strong> &nbsp;&nbsp;&nbsp; '+(page>1?'<a href="#" onclick="updateMapSearch('+eval(page - 1)+', 10, \''+area+'\'); return false;" class="prevPage">':'<span class="prevPage">')+'Previous'+(page>1?'</a>':'</span>')+' | '+(page<json.num_pages?'<a href="#" onclick="updateMapSearch('+eval(page + 1)+', 10,\''+area+'\'); return false;" class="nextPage">':'<span class="nextPage">')+'Next'+(page<json.num_pages?'</a>':'</span>');
			var htmlStr = '<p style="font-size: 11px;">Results <strong>'+(json.min_result==0?(json.count==0?0:1):json.min_result)+' - '+json.max_result+'</strong> of <strong>'+json.count+'</strong> &nbsp;&nbsp;&nbsp; '+(page>1?'<a href="#" onclick="updateMapSearch('+eval(page - 1)+', 10, \''+area+'\'); return false;" class="prevPage"><img src="images/map_previous.gif" border=0 alt="Previous" /></a>':'<img src="images/map_previous_inactive.gif" border=0 alt="Previous" />')+' &nbsp; '+(page<json.num_pages?'<a href="#" onclick="updateMapSearch('+eval(page + 1)+', 10,\''+area+'\'); return false;" class="nextPage"><img src="images/map_next.gif" border=0 alt="Next" /></a>':'<img src="images/map_next_inactive.gif" border=0 alt="Next" />');
			htmlStr += '<table width="95%" border="0" cellpadding="0" cellspacing="0" style="margin-right: 10px;">';
			for(var i = 0; i < num_results; i++)
			{
				if (json.MLNumber[i] != null)
				{
					htmlStr += '<tr id="mapListing_'+json.MLNumber[i]+'"><td onmouseover="jQuery(this).css(\'background-color\',\'#dfdfdf\');" onmouseout=jQuery(this).css(\'background-color\',\'#efefef\');"" onclick="';
					if (json.latitude[i]>0)
					{
						htmlStr += 'var point = new GLatLng('+json.latitude[i]+', '+json.longitude[i]+'); panToLocation(point); marker['+eval(i+1)+'].openInfoWindowHtml(info_arr['+eval(i+1)+']);';
					}
					else
						htmlStr += 'panGeoFromAddress(\''+json.address[i]+' '+json.city[i]+', '+json.state[i]+' '+json.zip[i]+'\');';
					htmlStr += '">';
					htmlStr += '<div class="mapListing"><table width="100%" border="0"><tr><td width="25" valign="top"><img src="http://search.lbar.com/mls/images/marker'+eval(i+1)+'.gif" border="0" alt="'+eval(i+1)+'" align="left" style="margin-right: 5px;" /></td>';
					listing_image = json.listing_image[i] != 'http://search.lbar.com/' ? json.listing_image[i] : 'http://search.lbar.com/mls/images/no_image_sm.gif';
					htmlStr += '<td align="left" width="100" valign="top"><img src="'+listing_image+'" border="0" width="100" style="border:1px solid #b1b1b1;" /></td>';
					htmlStr += '<td valign="top" align="left">';
					htmlStr += '	<table width="100%"><tr><td align="left"><a href="/mls/details//'+json.MLNumber[i]+'.html">'+json.address[i]+' '+json.city[i]+', '+json.state[i]+' '+json.zip[i]+'</a></td><td align="right"><strong>'+json.price[i]+'</strong></td></tr></table>';
					htmlStr += '	<strong>MLS#: '+json.MLNumber[i]+'</strong><br />';
					htmlStr += 		json.marketing[i] != '' ? json.marketing[i]+'...<br />' : '';
					htmlStr += 		json.distance_from_zip[i] != '' ? '<strong>'+json.distance_from_zip[i]+'</strong> Miles from zip code<br />' : '';
					htmlStr += '	<a href="/mls/details//'+json.MLNumber[i]+'.html">view details</a>';
					htmlStr += '</td></tr></table></div></td></tr>';
					//htmlStr += '<td valign="top" align="left"><strong style="font-size: 11px;">'+json.price[i]+'</strong><br /><strong>MLS#: '+json.MLNumber[i]+'</strong><br />'+json.address[i]+'<br />'+json.city[i]+', '+json.state[i]+' '+json.zip[i]+'<br /><a href="mls_details.php?id='+json.MLNumber[i]+'&property_type='+jQuery('.propertyCategory').attr("value")+'">view details</a></td></tr></table></div></td></tr>';
				}
			}
			htmlStr += '</table>';
			jQuery('#mapResults').html(htmlStr);
			//jQuery('#test').html(htmlStr);
			loadMap(page);
		}
	});
}

jQuery('.mapListing').mouseout(function() {
	jQuery(this).css('background-color','#efefef');
});
	
//returns a string that you can use to post/get values with ajax	
function serializeForm()
{
	var r = '';
	if (jQuery('#area').attr('value'))
		r += "area="+jQuery('#area').attr('value');
	else if (jQuery('#subdivisionArea').attr('value'))
		r += "area="+jQuery('#subdivisionArea').attr('value');
	r += "&property_category="+jQuery('.propertyCategory:radio:checked').attr("value");
	r += "&price_low="+jQuery('#price_low').attr("value");
	r += "&price_high="+jQuery('#price_high').attr("value");
	r += "&acres_low="+jQuery('#acres_low').attr("value");
	r += "&acres_high="+jQuery('#acres_high').attr("value");
	r += "&age_low="+jQuery('#age_low').attr("value");
	r += "&age_high="+jQuery('#age_high').attr("value");
	r += "&bedrooms="+jQuery('#bedrooms').attr("value");
	r += "&bathrooms="+jQuery('#bathrooms').attr("value");
	r += "&sq_foot="+jQuery('#sq_foot').attr("value");
	//r += "&display_virtual="+jQuery('#display_virtual').attr('value');
	r += "&display_openhouse="+jQuery('#display_openhouse').attr('value');
	if(jQuery('#display_openhouse').attr('value') == 'Yes')
	{
		r += "&oh_start_date="+jQuery('#ohStartDate').attr('value');
		r += "&oh_end_date="+jQuery('#ohEndDate').attr('value');
	}
	if (jQuery('#Zip') && jQuery('#Zip').attr('value'))
	{
		r += "&zip_code="+jQuery('#Zip').attr('value');
	}
	if (jQuery('#City') && jQuery('#City').attr('value'))
	{
		r += "&city="+jQuery('#City').attr('value');
	}
	if (jQuery('#Address') && jQuery('#Address').attr('value'))
	{
		r += "&address="+jQuery('#Address').attr('value');
	}
	if (jQuery('#MlsNumber') && jQuery('#MlsNumber').attr('value'))
	{
		r += "&mls_number="+jQuery('#MlsNumber').attr('value');
	}
	
	if (jQuery('#radZip') && jQuery('#radZip').attr('value'))
	{
		r += "&rad_zip="+jQuery('#radZip').attr('value');
	}
	if (jQuery('#maxDistance') && jQuery('#maxDistance').attr('value'))
	{
		r += "&max_distance="+jQuery('#maxDistance').attr('value');
	}
	
	jQuery('.propertyTypes:checkbox').each(
		function()
		{
			if(jQuery(this)[0].checked)
			{
				r += '&property_type[]='+jQuery(this).attr('value');
			}
		}
	);
	jQuery('.propertyStatus:checkbox').each(
		function()
		{
			if(jQuery(this)[0].checked)
			{
				r += '&property_status[]='+jQuery(this).attr('value');
			}
		}
	);
	jQuery('#subdivisionSelect input:checkbox').each(
		function()
		{
			if(jQuery(this)[0].checked)
			{
				r += '&subdivision[]='+jQuery(this).attr('value');
			}
		}
	);
	jQuery('#countySelect input:checkbox').each(
		function()
		{
			if(jQuery(this)[0].checked)
			{
				r += '&area[]='+jQuery(this).attr('value');
			}
		}
	);
	
	if(jQuery('#advanced_search:visible').length > 0)
	{
		jQuery('.homestyle:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&homestyle[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.construction:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&construction[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.garage:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&garage[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.basement:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&basement[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.features:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&features[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.newconstruction:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&newconstruction[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.dining:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&dining[]='+jQuery(this).attr('value');
				}
			}
		);
		jQuery('.misc:checkbox').each(
			function()
			{
				if(jQuery(this)[0].checked)
				{
					r += '&misc[]='+jQuery(this).attr('value');
				}
			}
		);
	}
	return r;
}

function updateCount()
{
	if (update_count == true)
	{
		var params = 'json=get_count&'+serializeForm();
		jQuery.ajax({
			type: "GET",
			url: '/mls/mls.js.php',
			data: params,
			dataType: 'json',
			success: function(json){
				//alert(json);
				//jQuery('#searchCountSearchBox').text(json.count);
				if (json.count>0)
				{
					jQuery('#searchCountDiv').html('<div id="searchCount"><span id="searchCountSearchBox" style="font-weight:bold;">'+json.count+'</span> listings matching criteria</div>');
				}
				else
				{
					jQuery('#searchCountDiv').html('<div id="searchCountRed"><span id="searchCountSearchBox" style="font-weight:bold;">'+json.count+'</span> listings! Please revise criteria</div>');
				}
			}
		});
	}
}

function schoolDetails(id)
{
	var params = 'json=school_details&id='+id;
	jQuery.ajax({
		type: "GET",
		url: '/mls/mls.js.php',
		data: params,
		dataType: 'html',
		success: function(response){
			jQuery('#school_details').html(response);
		}
	});
}

function updateAreas(display)
{
	var params = 'json=update_areas&display='+display;
	jQuery.ajax({
		type: "GET",
		url: '/mls/mls.js.php',
		data: params,
		dataType: 'json',
		success: function(json){
			var htmlStr = '';
			var even = false;
			if (json.count>0)
			{
				for(var i = 0; i < json.count; i++)
				{
					htmlStr += '<div'+(even?' class="labelOdd"':'')+' onmouseover="setClass(this, \'labelHover\');" onmouseout="setClass(this, \''+(even?'labelOdd':'')+'\');" style="cursor:pointer">	\
								<input id="area'+i+'" type="checkbox" name="area[]" value="'+json.areas[i]+'"'+(json.areas_checked[i]?' checked':'')+' /> '+json.areas[i]+'</div>';
					even = !even;
				}
				jQuery('#countySelect').html(htmlStr);
				if (display == 'all')
				{
					jQuery('#countyLinkSwitcher').html('<a id="displayPopularCounties" href="#">Show only Popular Counties</a>');
					jQuery('#displayPopularCounties').click( 
						function()
						{
							updateAreas('popular');
						}
					);
				}
				else
				{
					jQuery('#countyLinkSwitcher').html('<a id="displayAllCounties" href="#">Show All Counties</a>');
					jQuery('#displayAllCounties').click( 
						function()
						{
							updateAreas('all');
						}
					);
				}
			}
		}
	});
}

function updateForm()
{
	var params = 'property_category='+jQuery('.propertyCategory:radio:checked').attr("value")+'&json=true';
	if(jQuery('#subdivisionSelect input').length > 0)
	{
		//params = 'subdivision='+jQuery('#subdivision').attr("value")+'&'+params;
		jQuery('#subdivisionSelect input:checkbox').each(
			function()
			{
				if(this.checked)
				{
					params = 'subdivision[]='+this.value+'&'+params;
				}
			}
		);
	}
	if(jQuery('#area').length > 0)
	{
		params = 'area='+jQuery('#area').attr("value")+'&'+params;
	}
	jQuery.ajax({
		type: "POST",
		url: '/mls/mls.js.php',
		data: params,
		dataType: 'json',
		success: function(json){
			//output prices
			var oldLow = jQuery('#price_low').attr('value');
			var oldHigh = jQuery('#price_high').attr('value');
			var canSelectOldLow = false;
			var canSelectOldHigh = false;
			jQuery('#price_low')[0].options.length = 0;
			jQuery('#price_high')[0].options.length = 0;
			if(json.display_price_range.length > 0)
			{
				var minVal = 100000000;
				var maxVal = 0;
				for(var i = 0; i < json.display_price_range.length; i++)
				{
					if(!canSelectOldLow && json.value_price_range[i] == oldLow)
					{
						canSelectOldLow = true;
					}
					if(!canSelectOldHigh && json.value_price_range[i] == oldHigh)
					{
						canSelectOldHigh = true;
					}
					if(json.value_price_range[i] < minVal)
					{
						minVal = json.value_price_range[i];
					}
					if(json.value_price_range[i] > maxVal)
					{
						maxVal = json.value_price_range[i];
					}
					jQuery('#price_low')[0].options[i] = new Option(json.display_price_range[i], json.value_price_range[i]);
					jQuery('#price_high')[0].options[i] = new Option(json.display_price_range[i], json.value_price_range[i]);
				}
				jQuery('#price_low').attr('value', (!canSelectOldLow)?minVal:oldLow);
				jQuery('#price_high').attr('value', (!canSelectOldHigh)?maxVal:oldHigh);
			}
			else
			{
				jQuery('#price_low')[0].options[0] = new Option('$0', '0');
				jQuery('#price_high')[0].options[0] = new Option('$0', '0');
			}
			
			//parse bedrooms
			var maxBr = 1;
			jQuery('#bedrooms')[0].options.length = 0;
			if(json.bedrooms > 0)
			{
				maxBr = json.bedrooms;
			}
			for(var i = 0; i <= maxBr; i++)
			{
				jQuery('#bedrooms')[0].options[i] = new Option(((i == 0)?'No Minimum':((i == 5)?i+'+':i)), i);
			}
			
			//parse bathrooms
			maxBr = 1;
			jQuery('#bathrooms')[0].options.length = 0;
			if(json.bathrooms > 0)
			{
				maxBr = json.bathrooms;
			}
			for(var i = 0; i <= maxBr; i++)
			{
				jQuery('#bathrooms')[0].options[i] = new Option(((i == 0)?'No Minimum':((i == 5)?i+'+':i)), i);
			}
			
			// property types
			var htmlStr = '';
			for(var i = 0; i < json.property_types.length; i++)
			{
				htmlStr += '<div class="propCategoryDiv"><input type="checkbox" id="property_type'+i+'" name="property_type['+i+']" value="'+json.property_types[i]+'" /> <label for="property_type'+i+'">'+json.property_types[i]+'</label></div>';
			}
			jQuery('#property_types').html(htmlStr);
			
			
			//parse square feet
			jQuery('#sq_foot')[0].options.length = 0;
			if(json.sqft_range.length > 0)
			{
				jQuery('#sq_foot')[0].options[0] = new Option('Any', 'Any');
				for(var i = 0; i < json.sqft_range.length; i++)
				{
					jQuery('#sq_foot')[0].options[i+1] = new Option(json.sqft_range[i], json.sqft_range[i]);
				}
			}					
			
		}
	});
	//updateCount();
}


// Map Functions
function createMarker(point, index, info, extra) {
	var icon = new GIcon(baseIcon);
	if (info != '')
		icon.image = "http://search.lbar.com/mls/images/marker" + index + ".png";
	else
		icon.image = "http://search.lbar.com/mls/images/marker_home.png";
	marker[index] = new GMarker(point, icon);
	info_arr[index] = info;
	
	if (info != '')
	{
		GEvent.addListener(marker[index], "click", function() {
			marker[index].openInfoWindowHtml(info);
			//jQuery('#mapListing_'+MLNumber).mouseover(function() {
				//alert('mapresults height: '+jQuery('#mapresults').height()+' - mapresults top: '+jQuery('#mapresults').offset().top+' - '+jQuery('#mapListing_'+MLNumber).offset().top);
				//alert( jQuery('#mapListing_'+MLNumber).scrollTop() );
				//jQuery('#mapListing_'+MLNumber).css('background-color','#e5ecf3');
				//$('#mapresults').scrollTop( jQuery('#mapListing_'+MLNumber).scrollTop() );
			//});
		});
	}
	return marker[index];
}

function panToLocation(point)
{
	map.panTo(point);
}

function showAddress(address, i, info, MLNumber) {
	if (geocoder) {
		geocoder.getLocations(
			address, 
			function(response)
			{
				if (response.Placemark != null)
				{
					var place = response.Placemark[0];
					//var ret = new Array(place.Point.coordinates[1],place.Point.coordinates[0]);
					//alert(place.Point.coordinates[1]);
					var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
					map.addOverlay(createMarker(point, eval(i+1), info, MLNumber));
					panToLocation(point, 14);
				}
				else
				{
					var point = new GLatLng(38.048906, -84.499838);
					panToLocation(point);
				}
			}
		);
	}
}
function showAddressNoPan(address, i, info, MLNumber) {
	if (geocoder) {
		geocoder.getLocations(
			address, 
			function(response)
			{
				if (response.Placemark != null)
				{
					var place = response.Placemark[0];
					//var ret = new Array(place.Point.coordinates[1],place.Point.coordinates[0]);
					//alert(place.Point.coordinates[1]);
					var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
					map.addOverlay(createMarker(point, eval(i+1), info, MLNumber));
				}
			}
		);
	}
}

function panGeoFromAddress(address)
{
	if (geocoder) {
		geocoder.getLocations(
			address, 
			function(response)
			{
				var place = response.Placemark[0];
				//var ret = new Array(place.Point.coordinates[1],place.Point.coordinates[0]);
				//alert(place.Point.coordinates[1]);
				var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
				panToLocation(point);
			}
		);
	}
}

function resetSearch()
{
	update_count = false;
	
	jQuery('.propertyCategory:radio').each(
		function()
		{
			jQuery(this)[0].checked = false;
		}
	);
	jQuery('#propertyCategoryResidential').attr('checked','checked');
	jQuery("#advanced_search_div").css("display","inline");
	jQuery('#propertyTypesList').css("display","block");
	
	jQuery('.propertyTypes:checkbox').each(
		function()
		{
			jQuery(this)[0].checked = false;
		}
	);
	
	jQuery('#area').attr('value','All');
	jQuery('#subdivisionArea').attr('value','All');
	jQuery('#Address').attr('value','');
	jQuery('#City').attr('value','');
	jQuery('#Zip').attr('value','');
	jQuery('#MlsNumber').attr('value','');
	
	jQuery('#radAddress').attr('value','');
	jQuery('#radCity').attr('value','');
	jQuery('#radState').attr('value','Kentucky');
	jQuery('#radZip').attr('value','');
	
	jQuery('#price_low').attr('value','0');
	jQuery('#price_high').attr('value','0');
	
	jQuery('.propertyStatus:checkbox').each(
		function()
		{
			jQuery(this)[0].checked = false;
		}
	);
	jQuery('#property_status0')[0].checked = true;
	
	jQuery('#bedrooms').attr('value','0');
	jQuery('#bathrooms').attr('value','0');
	jQuery('#sq_foot').attr('value','Any');
	
	jQuery('#age_low').attr('value','');
	jQuery('#age_high').attr('value','');
	
	jQuery('.homestyle:checkbox, .construction:checkbox, .garage:checkbox, .basement:checkbox, .features:checkbox, .dining:checkbox, .misc:checkbox, #subdivisionSelect input:checkbox').each(
		function()
		{
			jQuery(this)[0].checked = false;
		}
	);
	
	return true;
}

function setClass(theRow, thePointerClass)
{
    if (typeof(theRow.style) == 'undefined') {
        return false;
    }else{
		theRow.className=thePointerClass;
		return true;
	}
}

var marker = Array();
var info_arr = Array();

var GB_ANIMATION = true;

var update_count = false;

jQuery(document).ready(function(){
	/* if (jQuery('.propertyCategory:radio:checked').attr('value') != 'farm')
	{
		jQuery('#farm_acreage').css("display","none");
	} */
	jQuery('#displayAllCounties').click( 
		function()
		{
			updateAreas('all');
		}
	);
	
	jQuery('#area').change( updateCount );
	//jQuery('#subdivisionSelect input:checkbox').change( updateForm );
	jQuery('#subdivisionSelect input:checkbox').change( updateCount );
	jQuery('#countySelect input:checkbox').change( updateCount );
	jQuery('.propertyCategory').click(
		function() {
			var theVal = jQuery('.propertyCategory:radio:checked').attr('value');
			if (theVal != 'residential')
			{
				jQuery("#advanced_search_div").css("display","none");
				jQuery('#advanced_search').css("display","none");
				jQuery('#propertyTypesList').css("display","none");
				
				jQuery('.propertyTypes:checkbox').each(
					function()
					{
						jQuery(this)[0].checked = false;
					}
				);
				
				/* if (theVal == 'farm')
				{
					jQuery('#farm_acreage').css("display","");
				}
				else
				{
					jQuery('#farm_acreage').css("display","none");
				} */
			}
			else
			{
				jQuery("#advanced_search_div").css("display","inline");
				jQuery('#propertyTypesList').css("display","block");
				//jQuery('#farm_acreage').css("display","none");
			}
			//updateForm();
			updateCount();
		}
	);
	
	jQuery("a.greybox").click(function(){
		var t = this.title || this.innerHTML || this.href;
		GB_show(t,this.href,470,600);
		return false;
	});
	
	// open house
	jQuery("#display_openhouse").change(function() {
		if(jQuery(this).attr("value") == 'Yes')
		{
			jQuery("#oh_dates").css("display","inline");
		}
		else
		{
			jQuery("#oh_dates").css("display","none");
		}
		//updateCount();
	});
	jQuery("#display_openhouse").change();
	
	// find a realtor
	jQuery("input[@name='realtor_type']").click(function() {
		if (jQuery(this).attr("value") == "agents")
		{
			jQuery("#sort").attr("value","LastName ASC");
			jQuery("#last_name_div").css("display","inline");
			jQuery("#first_name_div").css("display","inline");
		}
		else if (jQuery(this).attr("value") == "offices")
		{
			jQuery("#sort").attr("value","OfficeName ASC");
			jQuery("#last_name_div").css("display","none");
			jQuery("#first_name_div").css("display","none");
		}
	});
	
	jQuery('#price_low, #price_high').click(function() {
		update_count = true;
		updateCount();
	});
	
	jQuery('.propertyTypes:checkbox, .propertyStatus:checkbox, #price_low, #price_high, #acres_low, #acres_high, #age_low, #age_high, #bedrooms, #bathrooms, #sq_foot, #display_virtual, #Zip, #City, #Address, #MlsNumber, #radZip, #maxDistance').change(updateCount);
	jQuery('.homestyle:checkbox, .construction:checkbox, .garage:checkbox, .basement:checkbox, .features:checkbox, .newconstruction:checkbox, .dining:checkbox, .misc:checkbox, .CalCell, .CellAnchor').click(updateCount);
	
	// subdivision seach
	jQuery('#subdivisionArea').change(
		function()
		{
			var params = 'property_category='+jQuery('.propertyCategory:radio:checked').attr("value")+'&json=subdivisions_in_area&area='+jQuery(this).attr('value');
			var checkedBoxes = new Array();
			jQuery('#subdivisionSelect :checkbox').each(
				function()
				{
					if(jQuery(this)[0].checked)
					{
						checkedBoxes[checkedBoxes.length] = jQuery(this).attr('value');
					}
				}
			);
			
			//alert(checkdBoxes.length);
			
			jQuery.ajax({
				type: "POST",
				url: '/mls/mls.js.php',
				data: params,
				dataType: 'json',
				success: function(json)
				{
					var theHtml = '';
					if(json.subdivisions.length > 0)
					{
						var checkBoxInArray = '';
						for(var i = 0; i < json.subdivisions.length; i++)
						{
							checkBoxInArray = checkedBoxes.indexOf(json.subdivisions[i]);
							theHtml += '<div'+((i % 2 == 0)?' class="labelOdd"':'')+' onmouseover="setClass(this, \'labelHover\');" onmouseout="setClass(this, \''+((i % 2 == 0)?' labelOdd':'')+'\');">	\
											<input id="subdivision'+i+'" type="checkbox" name="subdivision[]" value="'+json.subdivisions[i]+'"'+((checkBoxInArray && checkBoxInArray != -1)?' checked':'')+' /> '+json.subdivisions[i]+'	\
										</div>';
						}
						jQuery('#subdivisionSelect').html(theHtml);
						jQuery('#subdivisionSelect').reattachSelectBox();					//reattach click events
						//jQuery('#subdivisionSelect input:checkbox').change( updateForm );	//reattach ajax action
					}
					else
					{
						jQuery('#subdivisionSelect').html('Sorry, but there were no subdivisions to select for this area.');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown)
				{
					alert('There was an error with your request:\n'+textStatus+'\n'+errorThrown);
				}
			});
			//updateCount();
		}
	);
	
	// mortgage calculator
	jQuery('a.mortgageCalculator').click(
		function()
		{
			var url = jQuery(this).attr('href');
			var outStr = 'height=' + 300 + ',width=' + 400 + ',menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no';
			window.open(url, 'mortgageCalculator', outStr);
			return false;
		}
	);
	
	jQuery('a.savedListings').click(
		function()
		{
			var url = jQuery(this).attr('href');
			var outStr = 'height=' + 400 + ',width=' + 620 + ',menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no';
			window.open(url, 'My Favorite Homes', outStr);
			return false;
		}
	);
	
	jQuery('a.inquireListing').click(
		function()
		{
			var url = jQuery(this).attr('href');
			var outStr = 'height=' + 400 + ',width=' + 620 + ',menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no';
			window.open(url, 'inquireListing', outStr);
			return false;
		}
	);
	
	jQuery('a.sendListing').click(
		function()
		{
			var url = jQuery(this).attr('href');
			var outStr = 'height=' + 400 + ',width=' + 620 + ',menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no';
			window.open(url, 'sendListing', outStr);
			return false;
		}
	);
	
	jQuery('a.searchAgain').click(
		function()
		{
			//jQuery('#search_div').slideToggle('normal');
			jQuery('#search_div').css('display','');
			jQuery('#search_again_div').css('display','none');
			jQuery('#hide_search_div').css('display','');
			jQuery('#mapresults').css('height','402px');
			jQuery('#map').css('height','400px');
			return false;
		}
	);
	
	jQuery('a.hideSearch').click(
		function()
		{
			//jQuery('#search_div').slideToggle('normal');
			//jQuery('#search_div:visible').slideUp('normal');
			jQuery('#search_div').css('display','none');
			jQuery('#hide_search_div').css('display','none');
			jQuery('#search_again_div').css('display','');
			jQuery('#mapresults').css('height','542px');
			jQuery('#map').css('height','540px');
		}
	);
	
	jQuery('a.tips').cluetip({
		splitTitle: '|'
	});
	
	jQuery(window).resize(
		function()
		{
			if (jQuery('#map'))
			{
				jQuery('#mapLeft').css('height',jQuery(window).height()-213);
				jQuery('#map').css('height',jQuery(window).height()-215);
			}
		}
	);
	
	jQuery(function(){
		jQuery('input').keydown(function(e){
			if (e.keyCode == 13) {
				document.mls_search.submit();
				return false;
			}
		});
	});
});

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
    }
}