/* A Bar is a simple overlay that outlines a lat/lng bounds on the
 * map. It has a border of the given weight and color and can optionally
 * have a semi-transparent background color.
 * @param latlng {GLatLng} Point to place bar at.
 * @param opts {Object Literal} Passes configuration options - 
 *   weight, color, height, width, text, and offset.
 */
function MarkerLight(latlng, opts, label, zbias) {
  this.latlng = latlng;
	this.zbias = zbias;

  if (!opts) opts = {};

  this.height_ = opts.iconSize.height || 32;
  this.width_ = opts.iconSize.width || 32;
  this.anchor_x_ = opts.iconAnchor.x;
  this.anchor_y_ = opts.iconAnchor.y;
  this.image_ = opts.image;
  this.label_ = label || '';
  this.clicked_ = 0;
  this.mouseMoved_ = false;
  this.mouseDownTime_ = 0;
  this.clickListener_ = 0;
  this.mouseDownListener_ = 0;
  this.mouseMoveListener_ = 0;
  this.mouseOverListener_ = 0;
  this.mouseOutListener_ = 0;
}

/* MarkerLight extends GOverlay class from the Google Maps API
 */
MarkerLight.prototype = new GOverlay();

/* Creates the DIV representing this MarkerLight.
 * @param map {GMap2} Map that bar overlay is added to.
 */
MarkerLight.prototype.initialize = function(map) {
  var me = this;

  // Create the DIV representing our MarkerLight
  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.paddingLeft = "0px";
  div.style.cursor = 'pointer';
  div.style.zIndex = /*GOverlay.getZIndex(this.latlng.lat()) +*/ me.zbias;

  var img = document.createElement("img");
  img.src = me.image_;
  img.style.width = me.width_ + "px";
  img.style.height = me.height_ + "px";
  div.appendChild(img);
  
  this.clickListener_ = GEvent.addDomListener(div, "click", function(event) {
	if (!me.mouseMoved_ || (new Date().getTime() < me.mouseDownTime_ + 150))
	{
		me.clicked_ = 1;
	    GEvent.trigger(me, "click");
	}
  });

  if (this.label_ != '')
  {
	  this.mouseOverListener_ = GEvent.addDomListener(div, "mouseover", function(event) {
		GEvent.trigger(map, "hover", me.latlng, me.label_);
	  });
	  
	  this.mouseOutListener_ = GEvent.addDomListener(div, "mouseout", function(event) {
		GEvent.trigger(map, "hover-off");
	  });
  }

  this.mouseDownListener_ = GEvent.addDomListener(div, "mousedown", function(event) {
	me.mouseMoved_ = false;
	me.mouseDownTime_ = new Date().getTime();
  });

  this.mouseMoveListener_ = GEvent.addDomListener(div, "mousemove", function(event) {
	me.mouseMoved_ = true;
  });
  
  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
};

/* Remove the main DIV from the map pane
 */
MarkerLight.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
  GEvent.removeListener(this.clickListener_);
  GEvent.removeListener(this.mouseDownListener_);
  GEvent.removeListener(this.mouseMoveListener_);
  if (this.mouseOverListener_ != 0)
  {
	GEvent.removeListener(this.mouseOverListener_);
  }
  if (this.mouseOutListener_ != 0)
  {
	GEvent.removeListener(this.mouseOutListener_);
  }
};

/* Copy our data to a new MarkerLight
 * @return {MarkerLight} Copy of bar
 */
MarkerLight.prototype.copy = function() {
  var opts = {};
  opts.color = this.color_;
  opts.height = this.height_;
  opts.width = this.width_;
  opts.iconAnchor.x = this.anchor_x_;
  opts.iconAnchor.y = this.anchor_y_;

  opts.image = this.image_;
  return new MarkerLight(this.latlng, opts);
};

/* Redraw the MarkerLight based on the current projection and zoom level
 * @param force {boolean} Helps decide whether to redraw overlay
 */
MarkerLight.prototype.redraw = function(force) {

  // We only need to redraw 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 MarkerLight
  var divPixel = this.map_.fromLatLngToDivPixel(this.latlng);

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (divPixel.x) - this.anchor_x_ + "px"
  this.div_.style.height = (this.height_) + "px";
  this.div_.style.top = (divPixel.y) + this.anchor_y_ - this.height_ + "px";
};

MarkerLight.prototype.getPoint = function() {
  return this.latlng;
}

MarkerLight.prototype.setPoint = function(latlng_) {
  this.latlng = latlng_;
  this.redraw();
}

MarkerLight.prototype.setStyle = function(style) {
  for (s in style) {
    this.div_.style[s] = style[s];
  }
}

MarkerLight.prototype.setImage = function(image) {
  this.div_.style.background = 'url("' + image + '")';
}


