﻿var CMH = CMH || {};
CMH.LabeledMarker = function(point, options)
{
    GMarker.apply(this, arguments);
    this.options = options;
    this.options.labelColor = this.options.labelColor || "blue";
}

CMH.LabeledMarker.LABEL_STYLE = 
{
	"width": "23px",
	"cursor": "pointer"
}

// Inherit from a copy of GMarker
CMH.LabeledMarker.prototype = new GMarker(new GLatLng(0,0));

// Override inherited initialize function
CMH.LabeledMarker.prototype.initialize = function(map)
{    
    GMarker.prototype.initialize.apply(this, arguments);

    this.map = map;
    this.label = this.getLabelDiv(this.options.labelText, map);
    this.positionLabel(map);
    this.map.getPane(G_MAP_MARKER_PANE).appendChild(this.label);
    
    if (this.options.infoText)
		GEvent.addListener(this, "click", function() {
			//this.openInfoWindow(this.options.infoText, {maxWidth:150});
			this.openExtInfoWindow(
				this.map,
				"gwindow",
				this.options.infoText.innerHTML,
				{beakOffset: 15}
			);
		});
}

// Override inherited redraw function
CMH.LabeledMarker.prototype.redraw = function(force)
{
    GMarker.prototype.redraw.apply(this, arguments);
    
    if (force)
        this.positionLabel();
}

// Override inherited redraw function
CMH.LabeledMarker.prototype.remove = function() 
{ 
    GEvent.clearInstanceListeners(this.label);
    this.label.parentNode.removeChild(this.label);
    this.label = null;
    
    GMarker.prototype.remove.apply(this, arguments);
}

// Returns the div for the label that will be centered on the pin
CMH.LabeledMarker.prototype.getLabelDiv = function(text, map) 
{
    var div = document.createElement("div");
    div.innerHTML = text;
    div.className = this.options.labelClass || null;
    $(div).setStyle(CMH.LabeledMarker.LABEL_STYLE);

    div.style.zIndex = GOverlay.getZIndex(this.getLatLng().lat()) + 1;
	//CMH.Map.recordZIndex(div.style.zIndex);

    this.bindEvents(div);
    
    return div;
}

// Binds any events that should trickle down to the original marker
CMH.LabeledMarker.prototype.bindEvents = function(div)
{
    var events = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];

    for(var i = 0; i < events.length; i++)
        this.bindEvent(div, events[i]);
}

// Bind an event on the the div so it gets triggered on the original marker
CMH.LabeledMarker.prototype.bindEvent = function(div, event)
{
    var me = this;
    GEvent.bindDom(div, event, me, function() { GEvent.trigger(me, event) });
}

// Position the label so it's centered on the marker
CMH.LabeledMarker.prototype.positionLabel = function(map)
{
    this.label.style.position = "absolute";
    this.label.style.top = this.getTop();
    this.label.style.left = this.getLeft();
}

// Get the top pixel position for the label
CMH.LabeledMarker.prototype.getTop = function()
{
    var offset = 32;
    return (this.map.fromLatLngToDivPixel(this.getPoint()).y - offset) + 'px';
}

// Get the left pixel position for the label
CMH.LabeledMarker.prototype.getLeft = function()
{
    var offset = 8;
    return (this.map.fromLatLngToDivPixel(this.getPoint()).x - offset) + 'px';
}

CMH.LabeledMarker.prototype.isSelected = function()
{
    return (this.label.className == this.options.labelSelectedClass);
}

CMH.LabeledMarker.prototype.select = function()
{
    this.label.className = this.options.labelSelectedClass || null;
    this.setImage("/source/images/marker-" + this.options.labelColor + "-selected.png");

    //this.label.style.zIndex = CMH.Map.getMaxZIndex();
    //re-append to show correct zIndex
    //this.map.getPane(G_MAP_MARKER_PANE).appendChild(this.label);

    //this.redraw(true);

    //if(!Prototype.Browser.IE) console.log("label zIndex: %s; pin#: %s", this.label.style.zIndex, this.label.innerHTML)
}

CMH.LabeledMarker.prototype.deselect = function()
{
    this.label.className = this.options.labelClass || null;
    this.setImage("/source/images/marker-" + this.options.labelColor + ".png");
    //this.closeInfoWindow();
    this.closeExtInfoWindow(this.map);
}