﻿/*
* Additional functions added:
* select - sets the selected image and selected class for the marker (if they have been passed
* in as options)
* deslect - resets the image and class back to their normals states
*
* @constructor
* @param {GPoint} The point where the marker should be located
* param {object} options A container for optional arguments (in addition to the regular GMarker options):
* 	{String} 	iconImage 			Sets the image to be used in the marker icon
* 	{String} 	selectedIconImage 	Sets the image to be used when the marker is selected
* 	{GSize}		iconSize 			A GSize object indicating the width and height of the icon image
* 	{String} 	iconShadow 			The url for an a shadow image for the icon
* 	{GSize}		iconShadowSize 		A GSize object indicating the width and height of the icon shadow
* 	{GPoint}	iconAnchor 		 	A GPoint object indicating the icon anchor for the marker icon
* 	{String}	labelText			The text to display inside of the marker label
* 	{String}	labelClass 			The css class to apply to the marker label
* 	{String}	labelSelectedClass  The css class to apply to the marker label when the marker is selected
* 	{Number}	labelTopOffset 		The amount in pixels to offset the label from the top of the marker
* 	{Number}	labelLeftOffset 	The amount in pixels to offest the label from the left of the marker'
* 	{Object}	labelElement 		A DOM element to use as the label for the marker
* 	{Function}	onclick 			A function to be executed when the marker is clicked on
*/
var Webitects = Webitects || {};
Webitects.GMarkerPlus = function(point, options)
{
	var me 		= this;
    me.options 	= options || {};
    
	arguments[1] = arguments[1] || {};
    arguments[1].icon = me.getIcon();
        
    if (me.options.icon)
        me.options.iconImage = me.options.icon.image;
    
    if (me.options.onclick)	
		GEvent.addListener(this, "click", me.options.onclick); 
    
    GMarker.apply(this, arguments);
};

/* 
* Inherit from a copy of GMarker
*/
Webitects.GMarkerPlus.prototype = new GMarker(new GLatLng(0,0));

/*
* return {GIcon} The icon to be used for the marker
*/
Webitects.GMarkerPlus.prototype.getIcon = function()
{
	var me = this;
	var icon = new GIcon(G_DEFAULT_ICON, me.options.iconImage);
        
    if (me.options.iconSize)
        icon.iconSize = me.options.iconSize;
        

	icon.shadow = me.options.iconShadow;
    
    if (me.options.iconShadowSize)
        icon.shadowSize = me.options.iconShadowSize;
    
    if (me.options.iconAnchor)
        icon.iconAnchor = me.options.iconAnchor;
        
    return icon;
}

/*
* Override inherited initialize function
* @param {GMap} map
*/
Webitects.GMarkerPlus.prototype.initialize = function(map)
{    
    GMarker.prototype.initialize.apply(this, arguments);

    this.map = map;
    
    if (this.options.labelText || this.options.labelElement)
        this.initializeLabel();
    
    if (this.options.infoWindowHtml)
        GEvent.addListener(this, "click", function() {  this.openInfoWindowHtml(this.options.infoWindowHtml); });
    else if (this.options.infoWindowText)
		GEvent.addListener(this, "click", function() {  this.openInfoWindow(this.options.infoWindowText); });
};

/* 
* Initializes the marker label (if there is one)
*/
Webitects.GMarkerPlus.prototype.initializeLabel = function()
{
    this.label = this.options.labelElement || this.getLabelDiv(this.options.labelText);
    this.label.style.zIndex = GOverlay.getZIndex(this.getLatLng().lat()) + 1;
    this.bindEvents(this.label);
    
    this.positionLabel();
    this.map.getPane(G_MAP_MARKER_PANE).appendChild(this.label);
};

/* 
* Override inherited redraw function
* @param {Boolean} force 
*/
Webitects.GMarkerPlus.prototype.redraw = function(force)
{
    GMarker.prototype.redraw.apply(this, arguments);
    
    if (this.options.labelText || this.options.labelElement)
    {
        this.positionLabel();
        this.map.getPane(G_MAP_MARKER_PANE).appendChild(this.label);
    }
};

/* 
* Overrides inherited redraw function
*/
Webitects.GMarkerPlus.prototype.remove = function() 
{ 
    if (this.label)
    {
        GEvent.clearInstanceListeners(this.label);
        this.label.parentNode.removeChild(this.label);
    }
    
    GMarker.prototype.remove.apply(this, arguments);
};

/* 
* @param {String} text The text to fill the label with
* @return the div for the label that will be centered on the pin
*/
Webitects.GMarkerPlus.prototype.getLabelDiv = function(text) 
{
    var div = document.createElement("div");
    div.innerHTML = text;
    div.className = this.options.labelClass || null;    
    return div;
};

/* 
* Binds any events that should trickle down to the original marker
* @param {Object} div The object to bind the events to 
*/
Webitects.GMarkerPlus.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
* @param {Object} div 	The object to bind the event to
* @param {String} event The name of the event to bind to the element
*/
Webitects.GMarkerPlus.prototype.bindEvent = function(div, event)
{
    var me = this;
    GEvent.bindDom(div, event, me, function() { GEvent.trigger(me, event) });
};

/* 
* Positions the label so it's centered on the marker
*/
Webitects.GMarkerPlus.prototype.positionLabel = function()
{
    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
*/
Webitects.GMarkerPlus.prototype.getTop = function()
{
    var offset = this.options.labelTopOffset || 0;
    return (this.map.fromLatLngToDivPixel(this.getPoint()).y + offset) + 'px';
};

/* 
* Get the left pixel position for the label
*/
Webitects.GMarkerPlus.prototype.getLeft = function()
{
    var offset = this.options.labelLeftOffset || 0;
    return (this.map.fromLatLngToDivPixel(this.getPoint()).x + offset) + 'px';
};

/* 
* Set the marker in a selected state
*/
Webitects.GMarkerPlus.prototype.select = function()
{
    if (this.options.labelSelectedClass)
        this.label.className = this.options.labelSelectedClass;
    
    if (this.options.selectedIconImage)
        this.setImage(this.options.selectedIconImage);
        
    this.selected = true;
};

/* 
* Sets the marker back to the non selected state
*/
Webitects.GMarkerPlus.prototype.deselect = function()
{
    if (this.options.labelClass)
        this.label.className = this.options.labelClass;
    
    if (this.options.iconImage)
        this.setImage(this.options.iconImage);
        
    this.selected = false;
};
