﻿//// Google Map Solution ////
/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

/* Constructor */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything if the coordinate system has changed
	if (!force) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.width = "150px";
	this.div.style.zIndex = z - 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}

var map, manager;

// This displays a little window
function createMarkerClickHandler(marker, text, link) {
	return function() {
		marker.openInfoWindowHtml(
			'<h3>' + text + '</h3>' +
			'<p><a href="' + link + '">Drilldown &raquo;</a><br/>' + 
			'<a href="' + link + '">View All Properties for ' + text + '&raquo;</a></p>'
			
		);
		return false;
	};
}

// This is used for just clicking
function createClickHandler(marker, html, link) {
	return function() {
		document.location.href = link;
/*		if (!html) {
		} else {
			marker.openInfoWindowHtml(html, { maxWidth : 400 });
		}
*/
		return false;
			
	};
}

var highestZ = 1000000;
var currentZ = 0;
function createMarker(pointData) {
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = iconFileName;
	icon.iconSize = iconFileName.indexOf("a_icon")>-1?new GSize(19,19):new GSize(22, 25) ;
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(25, 7);

	opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.abbr.length>0?'<div class="mapIcon"><span>'+pointData.abbr+'</span></div>':'',
		"labelOffset": new GSize(-16, -16)
	};
	var marker = new LabeledMarker(latlng, opts);
	//var handler = createMarkerClickHandler(marker, pointData.name, pointData.wp);
	var handler = createClickHandler(marker, pointData.html, pointData.wp);
	
	GEvent.addListener(marker, "click", handler);
	GEvent.addListener(marker, "mouseover", function() {
      currentZ = GOverlay.getZIndex(this.latlng.x);  ;
      this.div.style.zIndex = highestZ;   
      var image = getMarkerImage(this);
      image.style.zIndex = highestZ + 1;      
     
	  if (pointData.abbr.length>0) {
	      this.div.firstChild.className += ' mapIconOver';
		}
	  if (pointData.html) {
		marker.openInfoWindowHtml(pointData.html, { maxWidth : 400 });
	}
    });
    
    GEvent.addListener(marker, "mouseout", function() {
      //this.div.firstChild.className = 'mapIcon';
      this.div.style.zIndex = currentZ;   
      var image = getMarkerImage(this);
      //this.Rj.style.zIndex = currentZ + 1;
      image.style.zIndex = currentZ + 1;
    });

	return marker;
}

function getMarkerImage(marker)
{
    for (var memberName in marker)
    {
        var memberObj = marker[memberName];
    
        if ((memberObj) && (memberObj.tagName == "IMG"))
        {
            return memberObj;            
        }
            
    }
}


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 init() {
	//handleResize();

	map = new GMap(document.getElementById("gMap"));
	//if (showZoom) map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	//map.addControl(new GMapTypeControl());
	map.addControl(new GSmallZoomControl());

	manager = new GMarkerManager(map);
	
	// This is a sorting trick, don't worry too much about it.
	markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
	batch = [];
	for(id in markers) 
	{
	    var newMarker = createMarker(markers[id]);
		batch.push(newMarker);
    
	}
	manager.addMarkers(batch, 6);
	manager.refresh();
	
}

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

