// ELabel.js 
//
//   This Javascript is provided by Mike Williams
//   Blackpool Community Church Javascript Team
//   http://www.blackpoolchurch.org/
//   http://econym.org.uk/gmap/
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.2      Works with GMarkerManager in v2.72, v2.73, v2.74 and v2.75
// version 1.3      add .isHidden()
// version 1.4      permit .hide and .show to be used before addOverlay()
// version 1.5      fix positioning bug while label is hidden
// version 1.6      added .supportsHide()
// version 1.7      fix .supportsHide()
// version 1.8      remove the old GMarkerManager support due to clashes with v2.143


function ELabel(point, html, distance, url, type, pixelOffset, overlap) {
	// Mandatory parameters
	this.point = point;
	this.html = html;
	this.extra = distance + ' <a href="' + url + '" target="_blank">View</a>';
	this.type = type;
	
	// Optional parameters
	this.pixelOffset = pixelOffset||new GSize(0,0);
	if(this.type == "West Park")
		this.overlap= false;
	else
		this.overlap= true;
		
	this.hidden = false;  
	
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.className = "building marker";
	div.innerHTML = '<div>' + this.html + '</div>' ;
	this.div_ = div;
}

ELabel.prototype = new GOverlay();

ELabel.prototype.initialize = function(map) {
	
	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(this.div_);
	this.map_ = map;

	if (this.percentOpacity) {        
		if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
		if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
		if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
		if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
	}
	
	if (this.overlap) {
		var z = GOverlay.getZIndex(this.point.lat());
		this.div_.style.zIndex = z;
	}
	if (this.hidden) {
		this.hide();
	}
}
ELabel.prototype.addMain = function(){
		this.div_.className = "westPark marker";
	this.div_.innerHTML = '<div>West Park</div>' ;	
	this.percentOpacity = 100;
}
	
ELabel.prototype.addBig = function() {
	this.div_.className = "small marker";
	this.div_.innerHTML = '<div>' + this.html + '<br/><br/>' + this.extra + '</div>' ;	
	this.percentOpacity = 100;
}

ELabel.prototype.addSmall = function() {
	this.div_.className = "small marker";
	this.div_.innerHTML = '<div>' + this.html + '</div>' ;	
	this.percentOpacity = 70;
}

ELabel.prototype.addTiny= function() {
	var what = "";

	if(this.type == "golf"){
		what = "<img src='http://www.westpark.co.uk/images/golf-icon.jpg'>";
	}
	else if(this.type == "Attraction"){
		what = "A";
	}
	else if(this.type == "Travel"){
		what = "T";
	}
	else{
		what = "West Park";
	}
	
	this.div_.className = "tiny marker";
	this.div_.innerHTML = '<div>' + what + '</div>' ;	
	this.percentOpacity = 70;
}
ELabel.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}

ELabel.prototype.redraw = function(force) {  
	if (!force) return;
	if(this.type != "West Park"){
		if( this.map_.getZoom() >=15 ) this.addBig();
		else if( this.map_.getZoom() >=12 ) this.addSmall();
		else this.addTiny();
	}
	else{
		this.addMain();
	}
	
	var p = this.map_.fromLatLngToDivPixel(this.point);
	var width = Math.round(this.div_.offsetWidth/2);
	var height = Math.round(this.div_.offsetHeight);
	
	this.div_.style.left = (p.x - width) + "px";
	this.div_.style.top = (p.y - height) + "px";

}

ELabel.prototype.show = function() {
	var y = this.div_.style.top;
	this.div_.show()
		.css('top',( Number( y.replace(/px/,'') ) -10 ) + 'px') // set 10px higher
		.fadeIn( 500 )
		.animate( { top:y }, {queue:false,duration:500} );
	return this;
}

ELabel.prototype.hide = function() {
	this.div.hide();
	return this;
}

ELabel.prototype.isHidden = function() {
	return this.hidden;
}

ELabel.prototype.supportsHide = function() {
	return true;
}

ELabel.prototype.setPoint = function(point) {
	this.point = point;
	if (this.overlap) {
		var z = GOverlay.getZIndex(this.point.lat());
		this.div_.style.zIndex = z;
	}
	this.redraw(true);
}

ELabel.prototype.getPoint = function() {
	return this.point;
}
