﻿// ELabel.js 
//
//   This Javascript is provided by Mike Williams
//   Community Church Javascript Team
//   http://www.bisphamchurch.org.uk/   
//   http://econym.org.uk/gmap/
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/


function ELabel(point, html, contentLength, pixelOffset, markerIconSrc) {
		// Mandatory parameters
    this.point = point;
    this.html = html;
    // Optional parameters
    this.contentLength = contentLength||0;
    this.pixelOffset = pixelOffset||new GSize(0,0);
    this.markerIconSrc = markerIconSrc || '';
    this.hidden = false;
  }

  ELabel.prototype = new GOverlay();

  ELabel.prototype.initialize = function(map) {
  	//closures for dom event handlers  	
  	var eClick = GEvent.callback(this, this.onClick);
  	var eOver = GEvent.callback(this, this.onOver);
  	var eOut = GEvent.callback(this, this.onOut);

  	var div = document.createElement("div");

  	GEvent.clearInstanceListeners(div); //safety 
  	GEvent.addDomListener(div, "click", function(event) { eClick(); });
  	GEvent.addDomListener(div, "mouseover", function() { eOver(); });
  	GEvent.addDomListener(div, "mouseout", function() { eOut(); });

  	div.style.position = "absolute";
  	div.innerHTML = this.html;
  	map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);
  	
  	//add markerIcon
  	document.getElementById('markerId').setAttribute('src', this.markerIconSrc);
  	
  	this.map_ = map;
  	this.div_ = div;
  	
  	if (this.hidden) {
  		this.hide();
  	}


  	window.snpBaloonLabel = this;
  }

  ELabel.prototype.remove = function() {
    this.div_.parentNode.removeChild(this.div_);
  }

  ELabel.prototype.copy = function() {
    return new ELabel(this.point, this.html, null, this.pixelOffset, this.markerIconSrc);
  }

  ELabel.prototype.redraw = function(force) {
  	var p = this.map_.fromLatLngToDivPixel(this.point);

  	var extraContent = this.contentLength - 160;
  	var noOfExtraLines = 0;

  	if ((extraContent > 0) && (extraContent <= 30)) {
  		noOfExtraLines = 1;
  	}
  	else if (extraContent >= 30) {
  		noOfExtraLines = Math.round(extraContent / 30);
  	}

  	this.div_.style.left = (p.x - 175) + "px";

  	var divHeight = 170 + (noOfExtraLines * 10);

  	this.div_.style.top = (20 + p.y - divHeight) + "px";
  	this.div_.style.height = divHeight + "px";
  	this.div_.setAttribute("className", "rtrMarkerMain");
  	this.div_.setAttribute("class", "rtrMarkerMain");
  }

  ELabel.prototype.show = function() {
    if (this.div_) {
      this.div_.style.display="";
      this.redraw();
    }
    this.hidden = false;
  }
  
  ELabel.prototype.hide = function() {
    if (this.div_) {
      this.div_.style.display="none";
    }
    this.hidden = true;
  }
  
  ELabel.prototype.isHidden = function() {
    return this.hidden;
  }
  
  ELabel.prototype.supportsHide = function() {
    return true;
  }

  ELabel.prototype.setContents = function(html) {
    this.html = html;
    this.div_.innerHTML = this.html;
    this.redraw(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;
  }

  ELabel.prototype.setValues = function(point, html, contentLength, pixelOffset, markerIconSrc) {
  	// Mandatory parameters
  	this.point = point;
  	this.html = html;
  	// Optional parameters
  	this.contentLength = contentLength || 0;
  	this.pixelOffset = pixelOffset || new GSize(0, 0);
  	this.hidden = false;
  	this.markerIconSrc = markerIconSrc || '';
  	
  	return this;
  }

  // call this in order to deallocate the resources
  ELabel.prototype.dispose = function() {
  		RemoveCurrentMarker(map);

  		markerInfoLock.unlock();

  		// these properties where set on the click event of the marker (googleMaps.js)
  		window.snpLastMarker.show();
  }
	  
	//Event posters
	ELabel.prototype.onClick = function(){
			GEvent.trigger(this,"click");
	}
	ELabel.prototype.onOver = function(){
			GEvent.trigger(this,"mouseover");
	}
	ELabel.prototype.onOut = function(){
			GEvent.trigger(this,"mouseout");
	}
