function MapSearch(options)
{
	var object = this;
	this.map_canvas = null;
	this.listings = new Array();
	this.mapListings = new Array();
	this.markers = new Array();
	this.markerCount = 0;
	this.totalListings = 0;
	this.max_results = 20;
	this.objHttpListDataRequest = null;
	this.objHttpMapDataRequest = null;
	this.listing_window = null;
	this.search_id = 0;
	this.new_only = false;
	this.referring_agent_id = 0;
	this.is_framed = false;
	this.objHttpAgentDataRequest = null;
	this.agent = null;
	this.saved_options = null;
	this.search_region = null;
	this.iframePopup = null;
	this.pre_search_callback = false;
	this.post_search_callback = false;
	
	this.status_tags = {
		10: '/images/site/tag_available.gif',
		20: '/images/site/tag_pending.gif',
		30: '/images/site/tag_sold.gif',
		40: '/images/site/tag_leased.gif'
	};
	
	this.categories = {
		1: 'Commercial',
		2: 'Land',
		3: 'Residential',
		4: 'Rental'
	};
	
	this.options = jQuery.extend({
			defaultSort: 2,
			map_canvas: 'map_canvas',
			sort: new Array({sort: 'create_date', dir: 'desc'},
								{sort: 'price', dir: 'asc'},
								{sort: 'price', dir: 'desc'},
								{sort: 'city', dir: 'asc'},
								{sort: 'city', dir: 'desc'}),
			center: {lat: 27.412614, lng: -82.492905},
			zoom: 8,
			max_markers: 500,
			map_icon: '/images/site/map/house.png',
			map_icon_alt: '/images/site/map/house.png',
			max_overlay: '/images/site/map/map_max.png',
			state: 'FL', //used for geocodeing
			form: 'search_form',
			animate_options: true,
			results_view: 'more',
			show_status: false,
			show_open_house: false
			}, options || { });
	
	this.map_canvas = document.getElementById(this.options.map_canvas);
	this.form = $j('#'+this.options.form);
	/* setup search criteria */
	if (this.options.animate_options) {
		jQuery('div.option_content').hide().eq(0).show();
		var optTitle = jQuery('div.option_title'); 
		optTitle.click(function(){
				jQuery(this).siblings('.option_content').slideToggle();
			});
		optTitle.css('cursor','pointer');
	}
	
	if (this.options.pre_search_callback) {
		this.pre_search_callback = this.options.pre_search_callback;
	}
	if (this.options.post_search_callback) {
		this.post_search_callback = this.options.post_search_callback;
	}
	
	jQuery('#action_box').bind("mouseleave",function()
		{
			jQuery(this).parent().fadeOut();
			this.selected_property = null;
		});
	
	/* setup the map */
	this._gmap = null;
	if (GBrowserIsCompatible()) {
			this.bounds = null;
			this._gmap = new GMap2(this.map_canvas);
			this._gmap.setCenter(new GLatLng(this.options.center.lat, this.options.center.lng), this.options.zoom);
			this._gmap.addControl(new GLargeMapControl());
			this._gmap.addControl(new GMapTypeControl());
			//this._gmap.addControl(new GOverviewMapControl());
			
			jQuery(document).unload(GUnload);
			
			this.markerIcon = new GIcon(G_DEFAULT_ICON);
			this.markerIcon.image = this.options.map_icon;
			this.markerIcon.shadow = null;//"http://www.google.com/mapfiles/shadow50.png";
			this.markerIcon.iconSize = new GSize(18, 18);
			//this.mapIcon.shadowSize = new GSize(37, 34);
			this.markerIcon.iconAnchor = new GPoint(9, 9);
			//this.mapIcon.infoWindowAnchor = new GPoint(9, 2);
			
			// Set up our GMarkerOptions object
			this.markerOptions = {icon: this.markerIcon };
			this.maxOverlay = new GScreenOverlay(this.options.max_overlay,
					  	new GScreenPoint(.5, .5,'fraction', 'fraction'),  // screenXY
						new GScreenPoint(80, 51),  // overlayXY
						new GScreenSize(160, 102)  // size on screen
					  ); 
			this._gmap.addOverlay(this.maxOverlay);
			this.maxOverlay.hide();
			
			this.loadBGOverlay = new GScreenOverlay('/images/site/map/map_progress.png',
					  	new GScreenPoint(.5, .5,'fraction', 'fraction'),  // screenXY
						new GScreenPoint(80, 51),  // overlayXY
						new GScreenSize(160, 102)  // size on screen
					  );  
			this._gmap.addOverlay(this.loadBGOverlay);
			this.loadBGOverlay.hide();
			
			this.loadingOverlay = new GScreenOverlay('/images/site/loading.gif',
					  	new GScreenPoint(.5, .5, 'fraction', 'fraction'),  // screenXY
						new GScreenPoint(16, 20),  // overlayXY
						new GScreenSize(32, 32)  // size on screen
					  );  
			this._gmap.addOverlay(this.loadingOverlay);
			this.loadingOverlay.hide();
		}
	
	//external objects
	this.iframePopup = new IframePopup();
	this.compare = null;
		
}

MapSearch.prototype = {
	doSearch: function(page, get_total, refresh_map, data_options)
	{
		if(this.pre_search_callback) this.pre_search_callback();
	
		if(refresh_map != false)
		{
			this.clearMap();
			refresh_map = true;
		}
		else
		{
			refresh_map = false;
		}
		
		if (data_options == undefined || data_options == null) {
			if (this.saved_options != undefined && this.saved_options != null) {
				data_options = this.saved_options;
			}
		} else {
			this.saved_options = data_options;
		}
		
		$j('#less_container, #more_container').html('<img src="/images/site/loading.gif" border="0" />&nbsp;&nbsp;&nbsp;&nbsp;<span style="line-height: 32px; font-size: 14px; font-weight: bold;">Searching...</span>');
		var request_data = jQuery.extend(this.getData(page, refresh_map), data_options || {});
		
		//new search
		if(get_total != false)
		{
			request_data.get_total = true;
		}
				
		this.max_results = parseInt(jQuery('#max_per_page').val(), 10);
		request_data.max_results = this.max_results;
		
		if (this.objHttpListDataRequest)
		{
			// Abort the AJAX request.
			this.objHttpListDataRequest.abort();
			this.objHttpListDataRequest = null;
		}
		
		var object = this;
		request_data.refresh_map = false;
		
		if (request_data.region) {
			var object = this;
			jQuery.getJSON('/ajax/get_search_region_by_id.php', 
					{"search_region_id":request_data.region}, 
					function(data, textStatus){object.setSearchRegion(data);});
			
			object.waitForSearchRegion(object, request_data, 0, refresh_map);
		} else {
			object.doAjaxSearch(object, request_data, refresh_map);
		}
		
		if (this.referring_agent_id) {
			this.objHttpAgentDataRequest = jQuery.getJSON('/ajax/get_agent_by_id.php', {"agent_id":this.referring_agent_id}, function(data, textStatus){object.setAgent(data);});
		}
	},
	waitForSearchRegion: function(object, request_data, iteration, refresh_map) {
		if (iteration < 20) {
			if (object.search_region == null) {
				iteration++;
				setTimeout(function(){object.waitForSearchRegion(object, request_data, iteration, refresh_map);},500);
			} else {
				// Do search
				request_data = this.parseSearchRegion(object.search_region, request_data)
				object.doAjaxSearch(object, request_data, refresh_map);
				object.search_region = null;
			}
		} else {
			alert("Failed to search the specified location.");
			object.doAjaxSearch(object, request_data, refresh_map);
		}
	},
	parseSearchRegion: function(search_region, request_data) 
	{
		var points = new Array();
		var point = null;
		var lineArray = null;
	    var lat = null;
	    var lng = null;
		var lines = search_region.data.split("\n");
		for (var i=0; i<lines.length; i++) {
			lineArray = lines[i].split(",");
		    lng = lineArray[0]*1;
		    lat = lineArray[1]*1;
		    if(!isNaN(lat+lng)){
		    	point = new GLatLng(lat,lng);
		    	points.push(point);
		    }
		}
		
		var polygon = new GPolygon(points);
		var bounds = polygon.getBounds();
		var ne = bounds.getNorthEast();
		request_data.top_lat = ne.lat();
		request_data.top_lng = ne.lng();
		
		var sw = bounds.getSouthWest();		
		request_data.btm_lat = sw.lat();
		request_data.btm_lng = sw.lng();
		
		request_data.search_region = search_region.data;

		this._gmap.setCenter(bounds.getCenter());
		this._gmap.setZoom(search_region.zoom * 1);
		
		return request_data;
	},
	doAjaxSearch: function(object, request_data, refresh_map)
	{
		object.objHttpListDataRequest = jQuery.getJSON('/ajax/map_search_results.php', 
				request_data, 
				function(data, textStatus){
					object.doSearchCB(data, textStatus, refresh_map, request_data);
				});
	},
	getSearchFunction: function(object, request_data, iteration)
	{
		return function() {
			object.objHttpListDataRequest = jQuery.getJSON('/ajax/map_search_results.php', 
				request_data, 
				function(data, textStatus){object.doSearchCB(data, textStatus, request_data.refresh_map, request_data);});
		}
	},
	setSearchRegion: function(data) {
		this.search_region = data;
	},
	setAgent: function(data) {
		this.agent = data;
	},
	doPagination: function(page)
	{
		this.doSearch(page, false, false);
		
		return false;
	},
	doSearchCB: function(data, textStatus, refresh_map, request_data)
	{
		var object = this;
		this.listings = data.listings;
		$j(this.map_canvas).trigger('search-finished');

		if(data.total != -1)
		{
			this.totalListings = data.total;
			
			if(data.total <= this.max_results)
			{
				jQuery("#pagination_container").html('');
			}
			else
			{
				//set up pagin
				jQuery("#pagination_container").pagination(data.total, {
						num_edge_entries: 1,
						num_display_entries: 2,
						items_per_page: this.max_results,
						callback: function(page, jq){object.doPagination(page);},
						link_to: 'javascript: function(){return false;}'
					});
			}
			
			
			this.loadBGOverlay.show()
			this.loadingOverlay.show()
			if (this.objHttpMapDataRequest)
			{
				// Abort the AJAX request.
				this.objHttpMapDataRequest.abort();
				this.objHttpMapDataRequest = null;
			}
			request_data.refresh_map = true;
			request_data.max_results = request_data.max_markers;
			this.objHttpMapDataRequest = jQuery.getJSON(
					'/ajax/map_search_results.php', 
					request_data, 
					function(data, textStatus){
						object.doMapSearchCB(data, textStatus);

						if(object.post_search_callback) object.post_search_callback();
					});
			
		}
		
		if (request_data.region)
		{
			jQuery('#search_region_display').html(jQuery('#region option[@value='+request_data.region+']').html());
		}
		else
		{
			jQuery('#search_region_display').html('&nbsp;');
		}
		
		if(data.size > 0)
		{
			var less = new Array();
			var more = new Array();
			var size = this.listings.length;
			for(var i = 0; i < size; i++)
			{
				var listing = this.listings[i];
				var cnames = 'list_details';
				if (this.options.show_open_house) {
					cnames += ' open_house';
				}
				if(i%2 == 0)
				{
					cnames += ' alt_details'
				}
				
				var broker = listing.realtor;
				if(broker.indexOf('Sig') == 0)
				{
					broker = '<div class="sig_logo"></div>';
				}
				else
				{
					broker = '<div class="realtor"><img src="/images/site/map/idx.gif" border="0" /></div>';
				}
				

				if (this.options.results_view != 'wide') {
					less.push(
						'<div class="',cnames,'">',
							'<div style="width: 64px;" class="listing-image" pid="',listing.property_id,'"><img border="0" src="',listing.image,'" width="',listing.image_small[0],'" height="',listing.image_small[1],'"/></div>',
							'<div style="width: 60px;"><div class="action" pid="',listing.property_id,'"></div><label>MLS</label>#',listing.mls,'</div>',
							'<div style="width: 75px;"><label>Listing Price</label>$',listing.price,'</div>',
							'<div style="width: 105px;"><label>Location</label>',listing.city,', ',listing.state,'</div>',
							(parseInt(listing.sq_feet) > 0 ? '<div style="width: 35px;"><label>Sq. ft.</label>'+listing.sq_feet+'</div>' : ''),
							(listing.category == 1 ? (parseInt(listing.lot_size) > 0 ? '<div style="width: 30px;"><label>Lot Size</label>'+listing.lot_size+'</div>' : '')
							 : '<div style="width: 30px;"><label>Beds</label>'+listing.beds+'</div>'+
							'<div style="width: 45px;"><label>Baths</label>'+listing.baths+'</div>'),
							'<div style="width: 136px;">',broker,'</div>',
						'</div>');

					more.push(
					'<div class="',cnames,' more_details">',
						'<div style="width: 110px;" class="listing-image" pid="',listing.property_id,'"><img border="0" src="',listing.image,'" width="',listing.image_medium[0],'" height="',listing.image_medium[1],'"/></div>',
						'<div style="width: 85px;">',
							'<label>Listing Price</label>$',listing.price,
							'<label style="margin-top: 5px;">MLS</label>#',listing.mls,
						'</div>',
						'<div style="width: 115px;"><label>Location</label>',(listing.address == 'None' ? '' : listing.address+'<br/>'),listing.city,', ',listing.state,'</div>',
						'<div style="width: 70px;">',
							'<label>Details</label>',
							(parseInt(listing.sq_feet) > 0 ? 'Sq.ft.: '+listing.sq_feet+'<br/>' : ''),
							(listing.category == 1 ? (parseInt(listing.lot_size) > 0 ? 'Lot Size: '+listing.lot_size : '')
							 : 'Beds: '+listing.beds+'<br />'+'Baths: '+listing.baths),
						'</div>',
						'<div style="width: 185px;">',
							listing.description ? '<label>Description</label>' : '',
							listing.description, 
						'</div>',
						'<div class="icon_container" pid="',listing.property_id,'">',
							'<input type="hidden" name="property_id" id="property_id" value="',listing.property_id,'" />',
							'<div style="float: left; padding-top: 10px;"><table cellpadding="0" cellspacing="0" border="0"><tr>',
							'<td><div class="icon-map" action="map-popup"');
					if(listing.hide_map)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
							'<td><div class="icon-photos" action="photos"></div></td>',
							'<td><div class="icon-details" action="details"></div></td>',
							'<td><div class="icon-compare" action="compare"');
					if(listing.hide_compare)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
							'<td><div class="icon-save" action="save"');
					if(listing.hide_save || this.is_framed)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
							'</tr></table></div>',
							'<div style="width: 136px; float: right;">',
							broker);
					if (this.options.show_status) {
						more.push('<div>');
						if (this.status_tags[listing.status_id]) {
							more.push('<img src="',
									this.status_tags[listing.status_id],
									'" alt="Status" />');
						}
						more.push('</div>');
					}
					more.push('</div>',
						'</div>',
					'</div>');
				} else {
					more.push(
					'<div class="',cnames,' more_details">',
						'<div style="cursor: pointer; width: 110px; height: 60px; overflow: hidden;" class="listing-image" pid="',listing.property_id,'"><img border="0" src="',listing.image,'" width="',listing.image_medium[0],'" height="',listing.image_medium[1],'"/></div>',
						'<div style="width: 85px;">',
							'<label>Listing Price</label>$',listing.price,
						'</div>',
						'<div style="width: 85px;">',
							'<label>MLS</label>#',listing.mls,
						'</div>','<div class="icon_container" pid="',listing.property_id,'">',
						'<input type="hidden" name="property_id" id="property_id" value="',listing.property_id,'" />',
						'<div style="float: left; padding-top: 10px;"><table cellpadding="0" cellspacing="0" border="0"><tr>',
						'<td><div class="icon-map" action="map-popup"');
					if(listing.hide_map)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
							'<td><div class="icon-photos" action="photos"></div></td>',
							'<td><div class="icon-details" action="details"></div></td>',
							'<td><div class="icon-compare" action="compare"');
					if(listing.hide_compare)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
							'<td><div class="icon-save" action="save"');
					if(listing.hide_save || this.is_framed)
					{
						more.push('style="display: none;"');
					}
					more.push('></div></td>',
						'</tr></table></div>',
						'</div>',
						'<div style="width: 115px;"><label>Location</label>',(listing.address == 'None' ? '' : listing.address+'<br/>'),listing.city,', ',listing.state,'</div>',
						'<div style="width: 70px;">',
							'<label>Details</label>',
							(parseInt(listing.sq_feet) > 0 ? 'Sq.ft.: '+listing.sq_feet+'<br/>' : ''),
							(listing.category == 1 ? (parseInt(listing.lot_size) > 0 ? 'Lot Size: '+listing.lot_size : '')
									 : 'Beds: '+listing.beds+'<br />'+'Baths: '+listing.baths),
						'</div>',
						'<div style="width: 185px;">',
							listing.description ? '<label>Description</label>' : '',
							listing.description, 
						'</div>',
						'<div style="width: 146px; float: right; margin: 0px;">',
						broker);
					if (this.options.show_status) {
						more.push('<div>');
						if (this.status_tags[listing.status_id]) {
							more.push('<img src="',
									this.status_tags[listing.status_id],
									'" alt="Status" />');
						}
						more.push('</div>');
					}
					more.push('</div>');
					
					if (this.options.show_open_house) {
						if (listing.open_house_date) {
							more.push('<div style="clear: both; width: 100%; margin-top: 5px;">',
							'<img style="vertical-align: middle;" src="/images/site/open_house.gif" alt="Open House" border="0" width="24" height="19" />',
							'The next open house is on <b>',listing.open_house_date,'</b>.');
							
							if (listing.address != 'None') {
								more.push('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull;',
										'Get <a href="http://maps.google.com/?hl=en&daddr=',
										escape(listing.address+' '+listing.city+', '+listing.state+' '+listing.zip),
										'" target="_blank">driving directions</a>.');
							}
							more.push('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&bull;',
									'See <a href="viewlisting.php?property_id=',listing.property_id,
									'" target="_blank">listing details</a> for directions and complete schedule.</div>');	
						}
					}
					
					more.push('</div>','</div>');
				}
			}
				
			$j('#less_container').html(less.join(''));
			$j('#more_container').html(more.join(''));

			if($j('#more_container').html() == '') {
				// weird IE fix
				document.getElementById('more_container').innerHTML = more.join('');
			}
			
			if (this.options.results_view != 'wide'){
				/* initialize mouseovers for action */
				$j('.action').mouseover(function()
					{
						var o = $j(this).offset();
						object.selected_property = jQuery(this).attr('pid');
						var l = object.getListing(object.selected_property);
						if(l != null)
						{
							if(l.hide_compare)
							{
								jQuery('#icon-compare').hide();
							}
							else
							{
								jQuery('#icon-compare').show();
							}
							
							if(l.hide_save)
							{
								jQuery('#icon-save').hide();
							}
							else
							{
								jQuery('#icon-save').show();
							}
							
							if(l.hide_map)
							{
								jQuery('#icon-map').hide();
							}
							else
							{
								jQuery('#icon-map').show();
							}
						}
						else
						{
							jQuery('#icon-save, #icon-compare').show();
						}
						
						$j('#action_box').parent().css({top: (o.top-7), left: (o.left-6)}).fadeIn(100);
					});
			}
			
			$j('#more_container .listing-image').click(function(event){
				object.openListing(jQuery(this).attr('pid'), object.referring_agent_id, object.is_framed);
			});
			$j('#less_container .listing-image').click(function(event){
				object.openListing(jQuery(this).attr('pid'), object.referring_agent_id, object.is_framed);
			});
			
			$j('.icon-map,.icon-photos,.icon-details,.icon-compare,.icon-save').hover(
				function(){
					var  c = $j(this).attr('class');
					$j(this).addClass(c+'-hover');
				}, 
				function(){
					var c = $j(this).attr('class').split(' ');
					$j(this).removeClass(c[0]+'-hover');
				}).click(function(e){ e.preventDefault(); object.doIconAction(this); });
			
			var page = parseInt(data.page);
			
			var start = page*this.max_results;
			var end = start+size;

			if(request_data.featured)
			{
				if(this.agent == null)
				{
					$j('#list_counter').html('Featured Properties: '+(start+1)+' - '+end+' <b>of</b> '+ this.totalListings);
				}
				else
				{
					$j('#list_counter').html(this.agent['first_name']+' '+this.agent['last_name']+'\'s Featured Properties: '+(start+1)+' - '+end+' <b>of</b> '+ this.totalListings);
				}
			}
			else
			{
				if(this.agent == null)
				{
					$j('#list_counter').html('Properties: '+(start+1)+' - '+end+' <b>of</b> '+ this.totalListings);
				}
				else
				{
					$j('#list_counter').html(this.agent['first_name']+' '+this.agent['last_name']+'\'s Featured Properties: '+(start+1)+' - '+end+' <b>of</b> '+ this.totalListings);
				}
			}
		}
		else
		{
			$j('#list_counter').html('Properties: 0');
			$j('#less_container, #more_container').html('No listings match your search');
		}
	},
	doMapSearchCB: function(data, textStatus)
	{
		var object = this;
		this.mapListings = data.listings;
		
		$j('#map_counter').html('');
		if(data.size > 0 && data.total <= this.options.max_markers)
		{
			//add icons to map
			if(data.total > 0 && data.total <= this.options.max_markers) //we only do this on the intial view
			{
				$j('#map_counter').html(' (Mapped: '+this.mapListings.length+')');
				var size = this.mapListings.length;
				for( var i = 0; i < size; i++)
				{
					var l = this.mapListings[i];
					if(l.hide_map) continue;
					this.addMarker(l, i);
				}
			}
		}
		else if(data.total > this.options.max_markers)
		{
			//display 500 min overlay
			this.maxOverlay.show();
		}
		
		this.loadBGOverlay.hide();
		this.loadingOverlay.hide();
	},
	addMarker: function(l)
	{
		var latlng = new GLatLng(l.lat, l.lng);
		if(this.markerCount < this.markers.length)
		{
			var marker = this.markers[this.markerCount];
			this.markerCount++;
			
			marker.setLatLng(latlng);
			marker.listing = l;
			l.marker = marker;
			marker.show();
		}
		else
		{
			var object = this;
			var marker = new GMarker(latlng, this.markerOptions);
			marker.listing = l;
			l.marker = marker;
			
			GEvent.addListener(marker, "mouseover", function() { object.showInfoWindow(marker); });
			this._gmap.addOverlay(marker);
			
			this.markers[this.markerCount] = marker;
			this.markerCount++;
			
		}
		
		if(l.is_affiliate == 'true')
		{
			marker.setImage(this.options.map_icon);
		}
		else
		{
			marker.setImage(this.options.map_icon_alt);
		}
		
		return marker;
	},
	clearMap: function()
	{
		this.maxOverlay.hide();
		this.loadBGOverlay.hide()
		this.loadingOverlay.hide()
		this._gmap.closeExtInfoWindow();
		
		var size = this.markers.length;
		for(var i = 0; i < size; i++)
		{
			var marker = this.markers[i];
			if(marker.isHidden())
			{
				break;
			}
			
			marker.hide();
		}
		
		this.markerCount = 0;
	},
	showInfoWindow: function(marker)
	{
		var l = marker.listing;
		
		var content = '<div style="clear: both; color: black;">'+
				'<div style="float: left; padding: 5px;"><img id="info_window_img" pid="'+l.property_id+'" style="cursor: pointer;" class="listing-image" src="'+l.image+'" border="0" width="'+l.image_small[0]+'" height="'+l.image_small[1]+'"/></div>'+
				'<div style="float: left; padding: 5px;"><img id="info_window_actions" style="cursor: pointer; margin-bottom: 10px;" src="/images/site/map/actions.png" pid="'+l.property_id+'" /><br /><strong>MLS</strong><br />'+l.mls+'</div>'+
				'<div style="float: left; padding: 5px;">&nbsp;<br /><strong>Listing Price</strong><br />'+l.price+'</div>'+
			'</div>'+
			'<div style="clear: both; color: black;">'+
				'<div style="float: left; padding: 5px;"><strong>Sq. ft.</strong><br />'+l.sq_feet+'</div>'+
				'<div style="float: left; padding: 5px;"><strong>Beds</strong><br />'+l.beds+'</div>'+
				'<div style="float: left; padding: 5px;"><strong>Baths</strong><br />'+l.baths+'</div>'+
				'<div style="float: left; padding: 5px;"><strong>Location</strong><br />'+l.city+', '+l.state+'</div>'+
			'</div>';
			//'<div style="clear: both; text-align: center;">'+
			//	'<img src="/images/map_search/agency-logo.png" alt="Our Agency" />'+
			//'</div>';
		
		marker.openExtInfoWindow(this._gmap,
				'infoWindow',
				content,
				{beakOffset: 6}
			);
		
		var object = this; 
		$j('#info_window_actions').mouseover(function() {
				var o = $j(this).offset();
				object.selected_property = jQuery(this).attr('pid');
				var l = object.getListing(object.selected_property);
				if(l != null)
				{
					if(l.hide_compare)
					{
						jQuery('#icon-compare').hide();
					}
					else
					{
						jQuery('#icon-compare').show();
					}
					
					if(l.hide_save)
					{
						jQuery('#icon-save').hide();
					}
					else
					{
						jQuery('#icon-save').show();
					}
					
					jQuery('#icon-map').hide();
				}
				else
				{
					jQuery('#icon-save, #icon-compare').show();
				}
				
				$j('#action_box').parent().css({top: (o.top-7), left: (o.left-6)}).fadeIn(100);
			});
		
		$j('#info_window_img').click(function(event){
			object.openListing(jQuery(this).attr('pid'), object.referring_agent_id, object.is_framed);
		});
	},
	getListing: function(id)
	{
		var size = this.listings.length;
		for(var i = 0; i < size; i++)
		{
			if(this.listings[i].property_id == id)
			{
				return this.listings[i];
			}
		}
		
		return null;
	},
	getMapListing: function(id)
	{
		var size = this.mapListings.length;
		for(var i = 0; i < size; i++)
		{
			if(this.mapListings[i].property_id == id)
			{
				return this.mapListings[i];
			}
		}
		
		return null;
	},
	doKeyword: function(keywords)
	{
	},
	doSave: function()
	{
		var object = this;
		var data = this.getData();
		if(this.search_id != 0)
		{
			data.update = 1;
			data.search_id = this.search_id;
		}
		else
		{
			data.save = 1;
		}
		
		data.page = 1;
		data._is_post_ = 1;
		jQuery.getJSON('/ajax/search_ajax.php', data, function(data, status){ object.doSaveCB(data, status);} );
	},
	doSaveCB: function(data, status)
	{
		if(data.status == 'error')
		{
			showMessage('error', data.message);
		}
		else
		{
			showMessage('info', data.message);
		}
		
	},
	getData: function(page, new_search)
	{
		var data = {};
		var select = $j('[name=sort_option]')[0];
		if(select != undefined)
		{
			var val = select.options[select.selectedIndex].value;
			if(val.length > 0)
			{
				data = jQuery.extend(data, this.options.sort[val]);
			}
			else
			{
				data = jQuery.extend(data, this.options.sort[this.options.defaultSort]); //default option
			}
			data.sort_option = val;
		}
		else
		{
			data = jQuery.extend(data, this.options.sort[this.options.defaultSort]); //default option
			data.sort_option = this.options.defaultSort;
		}
		
		if(page == undefined) { data.page = 0; } else { data.page = page }
		
		var inputs = $j(':input', this.form);
		for(var i = 0;i < inputs.length; i++)
		{
			var input = inputs[i];
			if(input.nodeName == 'SELECT')
			{
				var value = input.options[input.selectedIndex].value;
			}
			else if(input.type == 'checkbox' && input.checked == false)
			{
				continue;
			}
			else
			{
				var value = input.value;
			}
			if(value.length > 0)
			{
				//see if we need to handle multiselect values
				if(data[input.name] != undefined)
				{
					var a = data[input.name];
					if(typeof a == 'object')
					{
						data[input.name][a.length] = value;
					}
					else
					{
						data[input.name] = new Array(data[input.name], value);
					}
				}
				else
				{
					data[input.name] = value;
				}
			}			
		}
		
		if(this.new_only)
		{
			data.new_only = this.last_search;
		}
		
		// get map bounds
		if(new_search) //only get boundaries we need to refresh the map, or else we mess up the results
		{
			//if location has been changed
			if(data.location != undefined && data.location.length != 0)
			{
				var llz = data.location.split(',');
				var zoom = parseInt(llz[2], 10);
				if(zoom == 0) zoom = 12;
				
				this._gmap.setCenter(new GLatLng(parseFloat(llz[0]), parseFloat(llz[1])), zoom); 
			}
			
			this.bounds = this._gmap.getBounds();
			this.zoom_level = this._gmap.getZoom();
			this.center = this._gmap.getCenter();
		}
		var ne = this.bounds.getNorthEast();
		data.top_lat = ne.lat();
		data.top_lng = ne.lng();
		
		var sw = this.bounds.getSouthWest();		
		data.btm_lat = sw.lat();
		data.btm_lng = sw.lng();
		
		data.zoom = this.zoom_level;
		data.center_lat = this.center.lat();
		data.center_lng = this.center.lng();
		
		data.max_markers = this.options.max_markers;
		
		return data;
	},
	doReset: function()
	{
		$j('.criteria_box > div').hide();
		$j('.criteria_box').toggleClass('criteria_open');
		var selects = $j(':input', this.form);
		for(var i = 0;i < selects.length; i++)
		{
			if(selects[i].nodeName.toLowerCase() == 'select')
			{
				selects[i].options[0].selected = true;
			}
		}
		
		$j('#exclude_mls')[0].checked = true;
		  
		this.search_id = 0;
		
		$j('#btn_save_text').html('SAVE');
		this.doSearch();
	},
	loadSearch: function(search_id, new_only, last_search)
	{
		this.search_id = search_id;
		this.new_only = new_only;
		this.last_search = last_search
	},
	selectOption: function(select, value)
	{
		var options = select.options;
		for(var i = 0; i < options.length; i++)
		{
			if(options[i].value == value)
			{
				options[i].selected = true;
				select.selectedIndex = i;
			}
		}
	},
	addProperty: function(div, property_id)
	{
		var object = this;
		data = {add_property: property_id};
		jQuery.getJSON('/ajax/search_ajax.php', data, function(data, status){ object.addPropertyCB(data, status, div, property_id);} );
	},
	addPropertyCB: function(data, status, div, property_id)
	{
		var object = this;
		if(data.status == 'error')
		{
			showMessage('error', data.message);
		}
		else
		{
			var l = this.getListing(property_id);
			l.hide_save = true;
			$j(div).hide();
			showMessage('info', data.message);
		}
	},
	removeProperty: function(btn, property_id)
	{
		var object = this;
		data = {remove_property: property_id};
		jQuery.getJSON('/ajax/search_ajax.php', data, function(data, status){ object.removePropertyCB(data, status, btn, property_id);} );
	},
	removePropertyCB: function(data, status, btn, property_id)
	{
		var object = this;
		if(data.status == 'error')
		{
			showMessage('error', data.message);
		}
		else
		{
			showMessage('info', data.message);
			$j(btn).css('width', '125px').click(function(){ object.removeProperty(btn, property_id) }).children().html('Save to My Properties');
		}
	},
	doIconAction: function(div)
	{
		var object = this;
		var action = jQuery(div).attr('action');
		var pid = jQuery(div).parents('.icon_container').attr('pid');
		if(pid == null || pid == undefined)
		{
			pid = this.selected_property;
		}
		
		if(action == 'details')
		{
			this.openListing(pid, this.referring_agent_id, this.is_framed);
		}
		else if(action == 'photos')
		{
			this.iframePopup.show('Property Photos', '/property_photos.php?property_id='+pid, 425, 500);
		}
		else if(action == 'compare')
		{
			this.compare.addProperty(pid, function(rv){object.compareCB(rv, div, pid);});
		}
		else if(action == 'save')
		{
			this.addProperty(div, pid);
		}
		else if(action == 'map')
		{
			this._gmap.closeExtInfoWindow();
			var zoom = this._gmap.getZoom(); 
			if(zoom < 15) //zoom in
			{
				zoom = 15;
			}
			var l = this.getMapListing(pid);
			if(l != null && l.marker != null)
			{
				this.showInfoWindow(l.marker);
				//this._gmap.setZoom(zoom);
			}
			else //most likely in a too many results state
			{
				this.clearMap();
				l = this.getListing(pid);
				this._gmap.setCenter(new GLatLng(l.lat, l.lng), 15);
				var marker = this.addMarker(l);				
				this.showInfoWindow(marker);
			}
		}
		else if(action == 'map-popup')
		{
			this.iframePopup.show('Map This Property', '/map_property.php?property_id='+pid,625,475)
		}
	},
	compareCB: function(rv, div, pid)
	{
		if(rv)
		{
			var l = this.getListing(pid);
			l.hide_compare = true;
			jQuery(div).hide();
		}
	},
	openListing: function(prop_id, referring_agent_id, is_framed)
	{
		var location = 'viewlisting.php?property_id='+prop_id;
		if(referring_agent_id != 0)
		{
			location += '&agent_id='+referring_agent_id;
		}
		if(is_framed == true)
		{
			location += '&is_framed=1';
		}
		this.listing_window = window.open(location, 'window_'+prop_id, 'resizable=yes,scrollbars=yes');
		if(this.listing_window)
		{
			this.listing_window.focus();
		}
		else
		{
			alert('Please disable your popup blocker for this page.');
		}
	},
	showAddress: function(address, lat, lng, zoom, refresh) {
	 var object = this;
	 if(lat == undefined || lat == 0)
	 {
		 var geocoder = new GClientGeocoder();
		 geocoder.getLatLng(address+', '+this.options.state, function(point) 
			 {
				 if(!point) 
				 {
					 alert(address + " not found.");
				 }
				 else 
				 {
					 object._gmap.setCenter(point, parseInt(zoom, 10));
					 if(refresh == true)
					 {
						 object.doSearch();
					 }
				 }
			 });
		}
		else
		{
			this._gmap.setCenter(new GLatLng(lat, lng), parseInt(zoom, 10));
			if(refresh == true)
			 {
				 this.doSearch();
			 }
		}
	},
	setCenter: function(lat, lng, zoom, refresh)
	{
		this._gmap.setCenter(new GLatLng(lat, lng), parseInt(zoom, 10));
		if(refresh == true)
		{
			this.doSearch();
		}
	}
}


