/*
* 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/
*/

/***************************************************************/
/**
 * LabelStyle, mantiene una configuracion de estilos.
 * Autor: afierro
 */

(function() {
	PorArriba.LabelStyle = function() {
		this.className = "markerLabel";
        
        this.icon = new GIcon();
        this.icon.iconSize = new GSize(0, 0);
        this.icon.iconAnchor = new GPoint(16, 16);
        this.icon.infoWindowAnchor = new GPoint(25, 7);
        
        this.clickable = false;
        this.labelText = '';
        this.labelOffset = 0;
        this.draggable = false;
		
		this.position = "absolute";
		this.color = "#990000";
		this.backgroundColor = "#FFFFCC";
		this.fontFamily = "verdana";
		this.fontSize = 9;
		this.verticalAlign = "middle";
		this.textAlign = "center";
		this.paddingRight = 4;
		this.paddingLeft = 4;
		this.paddingBottom = 2;
	}
})();

PorArriba.LabelStyle.prototype = new PorArriba.LabelStyle();

/***************************************************************/


/* Constructor */
function LabeledMarker(latlng, divStyle){
	this.divStyle = divStyle;
	
    this.latlng = latlng;
    this.labelText = divStyle.labelText || "";
    this.labelClass = divStyle.labelClass || "markerLabel";
    this.labelOffset = divStyle.labelOffset || new GSize(0, 0);
    this.foregroundColor = divStyle.color || "#000000";
    this.backgroundColor = divStyle.backgroundColor;
    
    this.clickable = divStyle.clickable || true;
    
    if (divStyle.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	divStyle.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, divStyle) {
	if (isUndefined(divStyle)){
		if (isUndefined(this.divStyle)) {
			divStyle = new PorArriba.LabelStyle();
		} else {
			divStyle = this.divStyle;
		}
	}
	
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = divStyle.labelClass;
	div.innerHTML = divStyle.labelText;
	
	div.style.position = divStyle.position;
	div.style.color = divStyle.color;
	div.style.backgroundColor = divStyle.backgroundColor;
	div.style.fontFamily = divStyle.fontFamily;
	div.style.fontSize = divStyle.fontSize;
	div.style.verticalAlign = divStyle.verticalAlign;
	div.style.textAlign = divStyle.textAlign;
	div.style.paddingRight = divStyle.paddingRight;
	div.style.paddingLeft = divStyle.paddingLeft;
	div.style.paddingBottom = divStyle.paddingBottom;
	//div.style.width = this.labelText.length * 6;
	
	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.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);
}

LabeledMarker.prototype.setColor = function(foregroundColor, backgroundColor){
	this.foregroundColor = foregroundColor || "#000000";
    this.backgroundColor = backgroundColor;
	this.div.style.color = this.foregroundColor;
	this.div.style.backgroundColor = this.backgroundColor;
}
	
fileLoaded("labeled_marker.js");
