
var map;var minimap;var CRITICAL_ZOOM=12;var CURRENT_CENTERPOINT;var LISTINGS_PER_PAGE=10;var IMG_BASE_URL="/images/gmap/";var BOUNDS_LOCKED=false;var MAP_RESET=false;var VIEWING_RESULTS_SET=false;var SAVED_SEARCH=false;var FILTER_MENU_TIMER;var CITY_MARKERS=new Object();var LISTING_MARKERS=new Object();var CURRENT_LISTING_ID="";var CURRENT_PAGE;var FILTERS=new Object();var baseCityIcon=new GIcon();baseCityIcon.shadow=IMG_BASE_URL+"city_shdw.png";baseCityIcon.iconSize=new GSize(27,31);baseCityIcon.shadowSize=new GSize(56,31);baseCityIcon.iconAnchor=new GPoint(17,31);baseCityIcon.infoWindowAnchor=new GPoint(19,12);function zoomBoxCtrl(){}
zoomBoxCtrl.prototype=new GControl();zoomBoxCtrl.prototype.initialize=function(map){var agent=navigator.userAgent.toLowerCase();if((agent.indexOf("msie")>-1)&&(agent.indexOf("opera")<1)){this.ie=true}else{this.ie=false}
var container=document.createElement("div");container.id="ZoomBox";container.style.display="none";if(this.ie){var loader="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+IMG_BASE_URL+"zoom_box.png', sizingMethod='scale');";container.innerHTML='<div style="'+loader+'" ></div>';}else{container.innerHTML='<img src="'+IMG_BASE_URL+'zoom_box.png" />';}
container.innerHTML+='<span id="ZoomBoxText"><span id="ZoomBoxCount">n</span> listings<br />Zoom in to view them.</span>';GEvent.addDomListener(container,"click",function(){map.zoomIn();});map.getContainer().appendChild(container);return container;}
zoomBoxCtrl.prototype.getDefaultPosition=function(){var ctrl_left_offset=Math.round(map.getSize().width/2)-88;var ctrl_top_offset=Math.round(map.getSize().height/2)-38;return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(ctrl_left_offset,ctrl_top_offset));}
function repositionZoomBox(){var ctrl_left_offset=Math.round(map.getSize().width/2)-88;var ctrl_top_offset=Math.round(map.getSize().height/2)-38;$("ZoomBox").setStyle({left:ctrl_left_offset+'px',top:ctrl_top_offset+'px'});}
function toggleFrame(frameid){var tabObj=$("MS_Tab"+frameid);if(tabObj.className!="ms_tab_selected"){$$('#mapSearchTabs a.ms_tab_selected').each(function(s){s.className="ms_tab";});tabObj.className="ms_tab_selected";if(frameid=="Results"){BOUNDS_LOCKED=true;showMapAndResults();$("filterBar").show();$("mapResultsBar").show();$("Map").show();if($("MS_Details").visible()&&VIEWING_RESULTS_SET){}else{refreshListingResults();}
$("MS_Details").hide();expandResults();}
if(frameid=="Map"){BOUNDS_LOCKED=false;VIEWING_RESULTS_SET=false;showMapAndResults();$("Map").show();$("filterBar").show();$("mapResultsBar").show();$("MS_Details").hide();collapseResults();}
if(frameid=="Details"){$("MS_TabDetails").show();$("MS_Details").show();hideMapAndResults();$("filterBar").hide();$("mapResultsBar").hide();collapseResults();$("Map").hide();}}}
function hideMapAndResults(){$("mapAndResults").setStyle({height:'1px',visibility:'hidden'});}
function showMapAndResults(){$("mapAndResults").setStyle({height:'472px',visibility:'visible'});}
function findMarker(mls_id,mls_no,lat,lng,showdetails){var listing_id=mls_id+"_"+mls_no;if(showdetails==true){showMoreInfo(listing_id);toggleFrame("Details");}
if(map.getZoom()<CRITICAL_ZOOM){map.setZoom(CRITICAL_ZOOM);}
if(!LISTING_MARKERS[listing_id]){addListingMarker(mls_id,mls_no,lat,lng);}
GEvent.trigger(LISTING_MARKERS[listing_id],"click");}
function mouseoverMarker(listing_id,lat,lng){if(LISTING_MARKERS[listing_id]&&isInBounds(lat,lng)&&(map.getZoom()>=CRITICAL_ZOOM)){GEvent.trigger(LISTING_MARKERS[listing_id],"mouseover");}else{highlightListing(listing_id);}}
function mouseoutMarker(listing_id,lat,lng){if(LISTING_MARKERS[listing_id]&&isInBounds(lat,lng)&&(map.getZoom()>=CRITICAL_ZOOM)){GEvent.trigger(LISTING_MARKERS[listing_id],"mouseout");}else{deselectListing(listing_id);}}
function highlightListing(id){var mylisting=$(id);if(mylisting){mylisting.setAttribute('class','listing_tile_highlighted');mylisting.className="listing_tile_highlighted";}}
function selectListing(id){var mylisting=$(id);if(mylisting){mylisting.setAttribute('class','listing_tile_selected');mylisting.className="listing_tile_selected";}}
function deselectListing(id){var mylisting=$(id);if(mylisting){mylisting.setAttribute('class','listing_tile');mylisting.className="listing_tile";}}
function collapseResults(){$("MS_Results").hide();$("mapAndResults").setStyle({paddingLeft:'0px'});if(Prototype.Browser.WebKit){var mapwidth=$("mapAndResults").getWidth()+'px';$("Map").setStyle({width:mapwidth});}
map.checkResize();repositionZoomBox();map.setCenter(CURRENT_CENTERPOINT);}
function expandResults(){$("MS_Results").show();$("mapAndResults").setStyle({paddingLeft:'300px'});if(Prototype.Browser.WebKit){var mapwidth=($("mapAndResults").getWidth()-300)+'px';$("Map").setStyle({width:mapwidth});}
map.checkResize();repositionZoomBox();map.setCenter(CURRENT_CENTERPOINT);}
function toggleFilterMenu(){if(!$("filterMenu").visible()){$("filterMenu").show();$("SEARCH_city").focus();}else{$("filterMenu").hide();}}
function isInBounds(lat,lng){var thispoint=new GLatLng(lat,lng);var themap=map.getBounds();if(themap.contains(thispoint))return true;}
function resetMap(){MAP_RESET=true;toggleFrame("Map");var ctrpt=$F("avg_city_centerpoint").split(",");var myzoom=parseInt($F("avg_city_zoom"));map.setCenter(new GLatLng(parseFloat(ctrpt[0]),parseFloat(ctrpt[1])),myzoom);}
function showMoreInfo(listing_id){if(listing_id){selectListing(listing_id);}else{alert("cannot show details because no listing was specified");return;}
var mlsparts=listing_id.split("_",2);var mls_id=mlsparts[0];var mls_no=mlsparts[1];var details_url="?";new Ajax.Request(details_url,{method:'post',parameters:{controller:'Search',action:'details',mls_id:mls_id,mls_no:mls_no,from:'map_search'},onSuccess:function(transport){var resultsHTML=transport.responseText;if(resultsHTML!="register"){$("MS_Details").update(resultsHTML);attachListingDetailEvents("map");}else{$("MS_Details").update("<div style='padding:20px; text-align:center; font-weight:bold;'>Please register or login to continue to view listing details.<br />Redirecting to the registration page now.</div>");window.location="/"+ACNT+"/Login/register_enduser/";}},onFailure:function(){alert("The ajax request failed - was trying to show the details for a listing.");}});}
function createListingMarker(mls_id,mls_no,lat,lng){var point=new GLatLng(lat,lng);var listingmarker=new GMarker(point);listingmarker.id=mls_id+"_"+mls_no;listingmarker.active=false;GEvent.addListener(listingmarker,"mouseover",function(){if(listingmarker.active!=true){listingmarker.setImage(IMG_BASE_URL+"marker_teal.png");highlightListing(listingmarker.id);}});GEvent.addListener(listingmarker,"mouseout",function(){if(listingmarker.active!=true){listingmarker.setImage(G_DEFAULT_ICON.image);deselectListing(listingmarker.id);}});GEvent.addListener(listingmarker,"click",function(){if(listingmarker.active!=true){CURRENT_LISTING_ID=listingmarker.id;listingmarker.active=true;listingmarker.setImage(IMG_BASE_URL+"marker_green.png");selectListing(listingmarker.id);var infowindow_html;var infowindow_url="?";new Ajax.Request(infowindow_url,{method:'post',parameters:{controller:'Search',action:'details',mls_id:mls_id,mls_no:mls_no,infowindow:1,from:'map_search'},onSuccess:function(transport){infowindow_html=transport.responseText;listingmarker.openInfoWindowHtml(infowindow_html);$("MS_TabDetails").show();},onFailure:function(){alert("The ajax request failed - was trying to retrieve this listing's information to show in the info window.");}});}});GEvent.addListener(listingmarker,"infowindowclose",function(){if(listingmarker.active==true){listingmarker.active=false;listingmarker.setImage(G_DEFAULT_ICON.image);deselectListing(listingmarker.id);$("MS_TabDetails").hide();}else{listingmarker.active=false;}});return listingmarker;}
function refreshListingResults(pagenumber){$("ZoomBox").hide();hideCityMarkers();$("MS_ResultsMsg").update("Use the Locate buttons to find the listings on the map.");var mypage;if(pagenumber){mypage=pagenumber;}else{mypage=1;}
var filters=$("filterForm").serialize(true);if(mypage!=CURRENT_PAGE){filter_has_changed=true;}else{$H(filters).each(function(pair){if($H(FILTERS).get(pair.key)!=pair.value){filter_has_changed=true;throw $break;}});}
saveGlobalFilters();CURRENT_PAGE=mypage;VIEWING_RESULTS_SET=true;map.closeInfoWindow();var ajax_url="?";var filters2=$("filterForm").serialize();ajax_url+=filters2;new Ajax.Request(ajax_url,{method:'post',parameters:{controller:'Search',action:'results',SEARCH_viewport:$("SEARCH_viewport").getValue(),SEARCH__page__:mypage,SEARCH__lpp__:LISTINGS_PER_PAGE,map_results:1,from:'map_search'},onSuccess:function(transport){var resultsHTML=transport.responseText;$("MS_Results").update(resultsHTML);},onFailure:function(){$("MS_Results").update("Ajax failed");}});}
function addListingMarker(mls_id,mls_no,lat,lng){var listing_id=mls_id+"_"+mls_no;if(!LISTING_MARKERS[listing_id]){var marker=createListingMarker(mls_id,mls_no,lat,lng);LISTING_MARKERS[listing_id]=marker;map.addOverlay(marker);}}
function refreshListingMarkers(){if(MAP_RESET==true){$("MS_ResultsMsg").update("To see listings, please choose a city or <a href='' onclick='map.zoomIn(); return false'>zoom in</a>.");$("ZoomBox").hide();resetFilters();showCityMarkers();for(x in LISTING_MARKERS){map.removeOverlay(LISTING_MARKERS[x]);delete LISTING_MARKERS[x];}
$("MS_Loading").hide();MAP_RESET=false;return;}
if(filtersHaveChanged()){saveGlobalFilters();$("MS_LoadingMsg").update("Updating map... please wait.");$("MS_Loading").show();var json_url="?";var filters=$("filterForm").serialize();json_url+="&"+filters;new Ajax.Request(json_url,{method:'post',parameters:{controller:'Search',action:'results',from:'map_search'},onSuccess:function(transport){var json=transport.responseJSON;var total_listings=json.total_listings;var listings=json.listings;$("ZoomBoxCount").update(total_listings);if(listings==0){if(total_listings==0){$("MS_ResultsMsg").update("<strong>"+total_listings+"</strong> listings in the current map area. Please <a href='' onclick='map.zoomOut(); return false'>zoom out</a> or select different search filters.");$("ZoomBox").hide();}else{if(!$("ZoomBox").visible()){$("ZoomBox").show();}
$("MS_ResultsMsg").update("<strong>"+total_listings+"</strong> listings in the current map area. Please continue to <a href='' onclick='map.zoomIn(); return false'>zoom in</a>.");}
showCityMarkers();for(x in LISTING_MARKERS){map.removeOverlay(LISTING_MARKERS[x]);delete LISTING_MARKERS[x];}
$("MS_Loading").hide();}else{if($("ZoomBox").visible()){$("ZoomBox").hide();}
$("MS_ResultsMsg").update("<strong>"+total_listings+"</strong> listings showing on the map.");hideCityMarkers();var batch=new Object;$(listings).each(function(s){var listing_id=s.mls_id+"_"+s.mls_no;batch[listing_id]=true;if(!LISTING_MARKERS[listing_id]){var marker=createListingMarker(s.mls_id,s.mls_no,s.lat,s.lng);LISTING_MARKERS[listing_id]=marker;map.addOverlay(marker);}});for(x in LISTING_MARKERS){if(!batch[x]){map.removeOverlay(LISTING_MARKERS[x]);delete LISTING_MARKERS[x];}}
$("MS_Loading").hide();}},onFailure:function(){alert('The ajax request failed - was trying to create/refresh listing markers on the map.');$("MS_Loading").hide();}});}}
function createCityMarker(cityname,citypoint,listingcount){var cityIcon=new GIcon(baseCityIcon);cityIcon.image=IMG_BASE_URL+"city_red.png";var markerOptions={icon:cityIcon,title:cityname};var citymarker=new GMarker(citypoint,markerOptions);citymarker.cityinfohtml='<div class="cityname">'+cityname+'</div><div class="listingcount">'+listingcount+' listings</div>';GEvent.addListener(citymarker,"mouseover",function(){citymarker.setImage(IMG_BASE_URL+"city_green.png");showCityInfo(citymarker);});GEvent.addListener(citymarker,"mouseout",function(){citymarker.setImage(IMG_BASE_URL+"city_red.png");hideCityInfo();});GEvent.addListener(citymarker,"click",function(){map.closeInfoWindow();hideCityInfo();FILTERS.city=cityname;$("SEARCH_city").value=cityname;map.setCenter(citypoint,CRITICAL_ZOOM);refreshCrumbtrail();});return citymarker;}
function setupCityMarkers(){$("MS_LoadingMsg").update("Loading Cities... please wait.");var json_url="?";new Ajax.Request(json_url,{method:'post',parameters:{controller:'Search',action:'get_cities',from:'map_search'},onSuccess:function(transport){var cities=transport.responseJSON;if(cities!=""){var cities_count=$(cities).size();var last_city_name=$(cities).last().name;var all_city_bounds=new GLatLngBounds();var all_city_zoom;var city_count=0;function loadMarkerBatch(marker_batch){$(marker_batch).each(function(s,index){var name_in_caps=s.name.toUpperCase();if(s.lat&&s.lng&&!CITY_MARKERS[name_in_caps]){var citypoint=new GLatLng(parseFloat(s.lat),parseFloat(s.lng));if(!all_city_bounds.containsLatLng(citypoint)){all_city_bounds.extend(citypoint);}
var marker=createCityMarker(s.name,citypoint,s.latlng_cnt);CITY_MARKERS[name_in_caps]=marker;map.addOverlay(marker);city_count++;}else{city_count++;}
if(last_city_name==s.name){all_city_zoom=map.getBoundsZoomLevel(all_city_bounds);$("avg_city_zoom").value=all_city_zoom;if(SAVED_SEARCH==true){refreshCrumbtrail();refreshListingMarkers();}else{MAP_RESET=true;map.setZoom(all_city_zoom+1);$("MS_Loading").hide();CITY_MARKERS_VISIBLE=true;}}});}
$(cities).eachSlice(20,function(citygroup,groupindex){loadMarkerBatch.delay(groupindex*0.75,citygroup);});}else{MAP_RESET=true;$("MS_Loading").hide();CITY_MARKERS_VISIBLE=true;}},onFailure:function(){alert('The ajax request failed - was trying to create city markers on the map.');$("MS_Loading").hide();}});}
function hideCityMarkers(){for(x in CITY_MARKERS){$(CITY_MARKERS[x]).hide();}
CITY_MARKERS_VISIBLE=false;}
function showCityMarkers(){for(x in CITY_MARKERS){$(CITY_MARKERS[x]).show();}
CITY_MARKERS_VISIBLE=true;}
function showCityInfo(marker){cityinfowindow.innerHTML=marker.cityinfohtml;var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());var iconanchor=marker.getIcon().iconAnchor;var width=marker.getIcon().iconSize.width;var pos=new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(offset.x-point.x-iconanchor.x+width,-offset.y+point.y+iconanchor.y));pos.apply(cityinfowindow);cityinfowindow.show();}
function hideCityInfo(){cityinfowindow.hide();}
function saveGlobalFilters(){FILTERS=$("filterForm").serialize(getHash=true);}
function filtersHaveChanged(){var changedStatus=false;var filters=$("filterForm").serialize(true);$H(filters).each(function(pair){if($H(FILTERS).get(pair.key)!=pair.value){changedStatus=pair.key;throw $break;}});return changedStatus;}
function resetFilters(){$("SEARCH_city").value='';$("SEARCH_minprice").options[0].selected=true;$("SEARCH_maxprice").options[0].selected=true;$("SEARCH_bedrooms").options[0].selected=true;$("SEARCH_baths").options[0].selected=true;$("SEARCH_sqft").options[0].selected=true;refreshCrumbtrail();}
function refreshCrumbtrail(){var city,minprice,maxprice,bedrooms,baths,sqft,type;city=$("SEARCH_city").value;minprice=$("SEARCH_minprice").options[$("SEARCH_minprice").selectedIndex].text;maxprice=$("SEARCH_maxprice").options[$("SEARCH_maxprice").selectedIndex].text;bedrooms=$("SEARCH_bedrooms").options[$("SEARCH_bedrooms").selectedIndex].text;baths=$("SEARCH_baths").options[$("SEARCH_baths").selectedIndex].text;sqft=$("SEARCH_sqft").options[$("SEARCH_sqft").selectedIndex].text;$("CRUMB_city").update("city:&nbsp;<strong>"+city+"</strong>;&nbsp; ");$("CRUMB_minprice").update("min.&nbsp;price:&nbsp;<strong>"+minprice+"</strong>;&nbsp; ");$("CRUMB_maxprice").update("max.&nbsp;price:&nbsp;<strong>"+maxprice+"</strong>;&nbsp; ");$("CRUMB_bedrooms").update("beds:&nbsp;<strong>"+bedrooms+"</strong>;&nbsp; ");$("CRUMB_baths").update("baths:&nbsp;<strong>"+baths+"</strong>;&nbsp; ");$("CRUMB_sqft").update("sq.&nbsp;ft.:&nbsp;<strong>"+sqft+"</strong>;&nbsp; ");if(city==""){$("CRUMB_city").hide();}else{$("CRUMB_city").show();}
if(minprice=="No Min."){$("CRUMB_minprice").hide();}else{$("CRUMB_minprice").show();}
if(maxprice=="No Max."){$("CRUMB_maxprice").hide();}else{$("CRUMB_maxprice").show();}
if(bedrooms=="Any"){$("CRUMB_bedrooms").hide();}else{$("CRUMB_bedrooms").show();}
if(baths=="Any"){$("CRUMB_baths").hide();}else{$("CRUMB_baths").show();}
if(sqft=="Any"){$("CRUMB_sqft").hide();}else{$("CRUMB_sqft").show();}}
function load(){if(GBrowserIsCompatible()){$("MS_LoadingMsg").update("Map is loading... please wait.");$("MS_Loading").show();var starting_zoom;var starting_centerpoint;var starting_type=G_NORMAL_MAP;if($F("SEARCH_centerpoint")!=""&&$F("SEARCH_zoom")!=''){STARTING_CENTERPOINT=$F("SEARCH_centerpoint");starting_zoom=parseInt($F("SEARCH_zoom"));SAVED_SEARCH=true;}else{STARTING_CENTERPOINT=$F("avg_city_centerpoint");starting_zoom=8;}
var ctrpt=STARTING_CENTERPOINT.split(",");map=new GMap2($("Map"));map.setCenter(new GLatLng(parseFloat(ctrpt[0]),parseFloat(ctrpt[1])),starting_zoom,starting_type);map.addControl(new GLargeMapControl());map.addControl(new GMapTypeControl());minimap=new GOverviewMapControl();map.addControl(minimap);minimap.hide();map.addControl(new zoomBoxCtrl());cityinfowindow=new Element('div',{'id':'CityInfo'}).hide();$("Map").insert(cityinfowindow);GEvent.addListener(map,"zoomend",function(){CURRENT_CENTERPOINT=map.getCenter();$("zoom_level").innerHTML=map.getZoom();$("ctr_coords").innerHTML=map.getCenter().toUrlValue();$("bounds_coords").innerHTML=map.getBounds();if(BOUNDS_LOCKED==false){$("SEARCH_viewport").value=map.getBounds();$("SEARCH_centerpoint").value=map.getCenter().toUrlValue();$("SEARCH_zoom").value=map.getZoom();}});GEvent.addListener(map,"moveend",function(){CURRENT_CENTERPOINT=map.getCenter();$("ctr_coords").innerHTML=map.getCenter().toUrlValue();$("bounds_coords").innerHTML=map.getBounds();if(BOUNDS_LOCKED==false){$("SEARCH_viewport").value=map.getBounds();$("SEARCH_centerpoint").value=map.getCenter().toUrlValue();}
refreshListingMarkers();});CURRENT_CENTERPOINT=map.getCenter();$("SEARCH_viewport").value=map.getBounds();$("SEARCH_centerpoint").value=map.getCenter().toUrlValue();$("SEARCH_zoom").value=map.getZoom();$("zoom_level").innerHTML=map.getZoom();$("bounds_coords").innerHTML=map.getBounds();$("ctr_coords").innerHTML=map.getCenter().toUrlValue();GEvent.addListener(map,"click",function(overlay,point){var coordinates;if(!overlay){coordinates=point;}else{if(!overlay.getLatLng){coordinates=point;}else{coordinates=overlay.getLatLng();}}
$("click_coords").innerHTML=coordinates;});attachMapSearchEvents();setupCityMarkers.delay(3);}}
function timedhidemenu(){FILTER_MENU_TIMER=Element.hide.delay(1,'filterMenu');}
function attachMapSearchEvents(){$('MS_TabResults').observe('click',function(event){Event.stop(event);toggleFrame('Results');});$('MS_TabMap').observe('click',function(event){toggleFrame('Map');Event.stop(event);});$('MS_TabDetails').observe('click',function(event){toggleFrame('Details');showMoreInfo(CURRENT_LISTING_ID);Event.stop(event);});$('btn_ResetMap').observe('click',function(event){resetMap();Event.stop(event);});$('btn_FilterMenu').observe('click',function(event){window.clearTimeout(FILTER_MENU_TIMER);toggleFilterMenu();});$('btn_FilterMenu').observe('mouseover',function(event){window.clearTimeout(FILTER_MENU_TIMER);});$('btn_FilterMenu').observe('mouseout',timedhidemenu);$('filterMenu').observe('mouseover',function(event){window.clearTimeout(FILTER_MENU_TIMER);$("filterMenu").show();});$('filterMenu').observe('mouseout',timedhidemenu);$('btn_RefreshResults').observe('click',function(event){toggleFilterMenu();refreshCrumbtrail();if(filtersHaveChanged()=='SEARCH_city'&&$("SEARCH_city").value!=''){var city_marker_index=$("SEARCH_city").value.toUpperCase();if(CITY_MARKERS[city_marker_index]){GEvent.trigger(CITY_MARKERS[city_marker_index],"click");}}else{refreshListingMarkers();}
if($("MS_Results").visible()){refreshListingResults();}
Event.stop(event);});if(Prototype.Browser.IE){$$('#filterMenu select').each(function(sel){$(sel).observe('focus',function(){$('filterMenu').stopObserving('mouseout',timedhidemenu);});$(sel).observe('blur',function(){$('filterMenu').observe('mouseout',timedhidemenu);});$(sel).observe('change',function(){$('filterMenu').observe('mouseout',timedhidemenu);});});}
['SEARCH_minprice','SEARCH_maxprice','SEARCH_bedrooms','SEARCH_baths','SEARCH_sqft'].each(function(s){$(s).observe('change',function(){setTimeout('window.clearTimeout(FILTER_MENU_TIMER);',500);});});}
Event.observe(window,'load',function(){load();});Event.observe(window,'unload',function(){GUnload();});