/*
 Eine mischung von PdMarker(http://www.pixeldevelopment.com/pdmarker.asp) und labeled Marker (http://uwmike.com)
*/


/* Constructor */
function apAdvMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
	this.labelClassHover = options.labelClassHover || "markerLabelHover";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    this.draggable = options.draggable || true;
    
	this.setImageOn = true
    /*
	if (options.draggable) {
    	// This version of apAdvMarker doesn't support dragging.
    	options.draggable = false;
    }
	*/
	
	this.oldImagePath = options.icon.image;
	
	
	GEvent.bindDom(this, "mouseover", this, this.onMouseOver);
	GEvent.bindDom(this, "mouseout",  this, this.onMouseOut);
	
    
    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. */
apAdvMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
apAdvMarker.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
apAdvMarker.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
apAdvMarker.prototype.remove = function() {
		
		try{
			GEvent.clearInstanceListeners(this.div);
			this.div.parentNode.removeChild(this.div);
			this.div = null;
			GMarker.prototype.remove.apply(this, arguments);
		}catch(e){
			//document.getElementById("debug").innerHTML += this.classNAme
		}
		
	
}

apAdvMarker.prototype.setImage = function(a) {
	//if (this.mouseOutEnabled && this.setImageOn)
		GMarker.prototype.setImage.call(this, a);
}

apAdvMarker.prototype.restoreImage = function() {
	//if (this.mouseOutEnabled && this.setImageOn && this.oldImagePath.length > 0)
		this.setImage(this.oldImagePath);
}


apAdvMarker.prototype.setIcon = function(a) {
	//this.remove();
	this.icon = a;
	//this.initialize(this.map);
	this.redraw(true); 
}

apAdvMarker.prototype.setHoverImage = function(a) {
	this.hoverImage = a;
}

var inMouseOver = false;

apAdvMarker.prototype.onMouseOver = function() {
	if (inMouseOver) return;
	inMouseOver = true;
	if (this.hoverImage) this.setImage(this.hoverImage);
	this.div.className = this.labelClassHover;
	inMouseOver = false;
}

apAdvMarker.prototype.onMouseOut = function() {
	if (this.hoverImage) this.restoreImage();
	this.div.className = this.labelClass;
}

