var map;
var centerLatitude = 49.533453;
var centerLongitude = -124.728813;
var startZoom = 12;
var deselectCurrent = function() {};

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.canadaplacemarks.com/images/coast_realty_gmap_logo.png" border = \"0\">';
container.style.width = '63px';
container.style.height = '42px';
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, 5));
};


 
function initializePoint(pointData) {
var point = new GLatLng(pointData.latitude, pointData.longitude);
var marker = new GMarker(point);
var listItem = document.createElement('li');
var listItemLink = listItem.appendChild(document.createElement('a'));
var visible = false;
 
listItemLink.href = "#";
listItemLink.innerHTML = '<strong>' + pointData.address + ', ' + pointData.city + ' </strong><span>' + pointData.price + '</span>';
 
var focusPoint = function() {
deselectCurrent();
listItem.className = 'current';
deselectCurrent = function() { listItem.className = ''; }
marker.openInfoWindowHtml(pointData.address + "<br>" + pointData.price + "<br><a target = '_blank' href ='../../details.php?ML_Number=" + pointData.ML_Number + "'>Get more details and photos</a>" + "<br><br><a target = '_blank' href ='http://www.canadaplacemarks.com/real_estate/realtor_move.php?ML_Number=" + pointData.ML_Number + "'>Move it. (Realtor Only)</a>" + "<br><br><a href ='mailto: info@canadaplacemarks.com?subject=tech%20support%20for%20MLS%20Number " + pointData.ML_Number + "'>Contact tech support (Anyone)</a>");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(cat) {
var listItem = document.createElement('li');
var listItemLink = listItem.appendChild(document.createElement('a'));
 
listItemLink.href = "#";
listItemLink.innerHTML = cat;
listItemLink.onclick = function() {
changeBodyClass('standby', 'loading');
 
for(id in markers) {
if (markers[id].cat == cat || 'All' == cat)
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 - 30;
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() {
var cat;
var allCats = { '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();
 
map = new GMap(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom); 
map.addControl(new GMapTypeControl());
map.addControl(new PromoControl('http://www.denmanhornbyrealestate.com/'));


 
for(id in markers) {
initializePoint(markers[id]);
allCats[markers[id].cat] = true;
}
 
for(cat in allCats) {
initializeSortTab(cat);
}
 
changeBodyClass('loading', 'standby');
}
 
window.onresize = handleResize;
window.onload = init;