var map;
var centerLatitude = 42.69363;
var centerLongitude = -88.531554;
var startZoom = 10;

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


var DisclaimerControl = function(url) {
this.url_ = url;
};
 
DisclaimerControl.prototype = new GControl(true);
 
DisclaimerControl.prototype.initialize = function(map) {
var container = document.createElement("div");
container.innerHTML = '* Sales data provided by Metro MLS, Inc. and county public records. These entities do not guarantee result accuracies. This may not include all market transactions. Data is from 1/1/2009 - 12/31/2009.';
container.style.width='400px';
container.style.height='42px';
container.style.background='#FFFFFF';
container.style.filter='alpha(opacity=70)';
container.style.padding='2';
container.style.overflow='auto';
container.style.font='11px Ariel';
container.style.border='1px solid #666666';



 
url = this.url_;
 
GEvent.addDomListener(container, "click", function() {
document.location = url;
});
 
map.getContainer().appendChild(container);
 
return container;
};
 
DisclaimerControl.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(180, 0));
};


var PromoControl = function(url) {
this.url_ = url;
};
 
PromoControl.prototype = new GControl(true);
 
PromoControl.prototype.initialize = function(map) {
var container = document.createElement("div");
container.innerHTML = '<img style="cursor:pointer" src="http://www.keeferealestate.com/lakemaps/images/keefe_logo.gif" border="0">';
container.style.width='100px';
container.style.height='93px';
 
url = this.url_;
 
GEvent.addDomListener(container, "click", function() {
document.location = url;
});
 
map.getContainer().appendChild(container);
 
return container;
};
 
PromoControl.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70, 0));
};


var deselectCurrent = function() {};

var icon = new GIcon();
icon.image = "http://www.keeferealestate.com/lakemaps/images/marker.png";
icon.shadow = "http://www.keeferealestate.com/lakemaps/images/shadow.png";
icon.iconSize = new GSize(44, 50);
icon.shadowSize = new GSize(75,56);
icon.iconAnchor = new GPoint(22, 50);
icon.infoWindowAnchor = new GPoint(22, 1);
 
function initializePoint(pointData) {
var point = new GLatLng(pointData.latitude, pointData.longitude);
var marker = new GMarker(point,{title:pointData.lake, icon:icon});
var listItem = document.createElement('li');
var listItemLink = listItem.appendChild(document.createElement('a'));
var visible = false;

listItemLink.href = "#";
listItemLink.innerHTML = '<strong>' + pointData.lake + ' </strong><span>County: ' + pointData.county + '</span>';
	var focusPoint = function() {
deselectCurrent();
listItem.className = 'current';
deselectCurrent = function() { listItem.className = ''; }
		marker.openInfoWindowHtml(pointData.lake);
map.addOverlay(marker);
var infoTabs = [
new GInfoWindowTab("Real Estate", '<div class="infowindow"><strong>' + pointData.lake + ' Sales</strong>*<br>Average:	$' + addCommas(pointData.average_price) + '<br>Median:	$' + addCommas(pointData.median_price) + '<br>High:	$' + addCommas(
pointData.high_price) + '<br>Low:	$' + addCommas(pointData.low_price) + '<br>' + pointData.note + '<br><a href="http://tacoma.redata.com/vp/SearchServlet?SITE=KEEFE&Search=true&ImprovementType=3&ListingStatus=Active&fg_WaterAccess=Y&&Search_Page=SEARCH_BLANK&ScreenID=ListingSummary_blank&servlet_alt=showListingDetail_NS&ScreenID_Alt=DETAIL_BLANK&ScreenID_Cur=SEARCH_LISTING_P&sortCriteria=Listing_Price%20ASC&numRecordsToShow=25&' + pointData.searchparam +'" target="_blank">View Current Listings</a><br /><a href="lake_report.php?lake=' + pointData.lake + '" target="_blank">Printable Lake Info</a><br /><br /><div id="detailmap" align="center"></div></div>'),

new GInfoWindowTab("Lake", '<div class="infowindow"><div align="center"><strong>' + pointData.lake + '</strong><br /><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="201" height="131" id="' + pointData.flash + '" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="flash/' + pointData.flash + '/lake.swf" /><param name="base" value="." /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed base="." src="flash/' + pointData.flash + '/lake.swf" quality="high" bgcolor="#ffffff" width="201" height="131" name="' + pointData.flash + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></div><br />' + pointData.LakeDescription + '<br /><br /><strong>Area</strong> (acres): ' + addCommas(pointData.LakeAcreage) +'<br /><strong>Max Depth</strong> (ft.): ' + pointData.LakeDepth +'</div>')

];
map.closeInfoWindow();
marker.openInfoWindowTabsHtml(infoTabs,{
selectedTab:0,
maxWidth:250
}

);

		{
		var dMapDiv = document.getElementById("detailmap");
		var detailmap = new GMap2(dMapDiv);		
		detailmap.addControl(new GSmallZoomControl());
		detailmap.setCenter(point , 11);
		detailmap.setMapType(G_HYBRID_TYPE);
		}
		return false;
		
		GEvent.addListener(marker,'click',function() {	
		map.closeInfoWindow();
		marker.openInfoWindowTabsHtml(infoTabs);

});

map.panTo(point);
return false;
}


GEvent.addListener(marker, 'click', focusPoint);
listItemLink.onclick = focusPoint;
 
pointData.show = function() {
if (!visible) {
document.getElementById('sidebar-list').appendChild(listItem);
map.addOverlay(marker);
visible = true;
}
}
pointData.hide = function() {
if (visible) {
document.getElementById('sidebar-list').removeChild(listItem);
map.removeOverlay(marker);
visible = false;
}
}
 
pointData.show();
}
 
function initializeSortTab(county) {
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	 
	listItemLink.href = "#";
	listItemLink.innerHTML = county;
	listItemLink.onclick = function() {
	changeBodyClass('standby', 'loading');
	listItemLink;
	 
	for(id in markers) {
	if (markers[id].county == county || 'All' == county)
	markers[id].show();
	else
	markers[id].hide();
	}
	 
	changeBodyClass('loading', 'standby');
	 
	return false;
}
 
document.getElementById('filters').appendChild(listItem);
}
 
function windowHeight() {
// Standard browsers (Mozilla, Safari, etc.)
if (self.innerHeight)
return self.innerHeight;
// IE 6
if (document.documentElement && document.documentElement.clientHeight)
return document.documentElement.clientHeight;
// IE 5
if (document.body)
return document.body.clientHeight;
// Just in case.
return 0;
}
 
function handleResize() {
var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 120;
document.getElementById('map').style.height = height + 'px';
document.getElementById('sidebar').style.height = height + 'px';
}
 
function changeBodyClass(from, to) {
document.body.className = document.body.className.replace(from, to);
return false;
}



 
function init() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new PromoControl('http://www.keeferealestate.com'));
map.addControl(new DisclaimerControl('http://www.keeferealestate.com'));
var location = new GLatLng(centerLatitude, centerLongitude);
map.setCenter(location, startZoom);

var county;
var allTypes = { 'All':[] };
 
document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };
document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };
handleResize();
 
 
for(id in markers) {
initializePoint(markers[id]);
allTypes[markers[id].county] = true;
}
 
for(county in allTypes) {
initializeSortTab(county);
}
 
changeBodyClass('loading', 'standby');
}

}
 
window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;