/**
 * Based on MarkerLight: http://code.google.com/p/gmaps-samples/source/browse/trunk/youtube/markerlight.js?r=2300
 *
 * @param latlng {GLatLng} Point to place bar at.
 * @param opts {Object Literal} Passes configuration options - icon
 */
function HiveMapsMarker(latlng, opt) {
  this.latlng = latlng;
  this.icon = opt.icon;
  this.height_ = this.icon.iconSize.height;
  this.width_ = this.icon.iconSize.width;
  this.image_ = this.icon.image;
  this.clicked_ = 0;
}
HiveMapsMarker.prototype = new GOverlay();

HiveMapsMarker.prototype.initialize = function(map) {
  var me = this;

  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.padding = "0px";
  div.style.cursor = "pointer";

  var img = document.createElement("img");
  img.src = me.image_;
  img.style.width = me.width_ + "px";
  img.style.height = me.height_ + "px";
  div.appendChild(img);  

  GEvent.addDomListener(div, "click", function(event) {
    me.clicked_ = 1;
    GEvent.trigger(me, "click");
  });
  GEvent.addDomListener(div, "mouseover", function() {
    me.bringToFront_();
  });
  GEvent.addDomListener(div, "mouseout", function() {
    me.sendBack_();
  });
  map.getPane(G_MAP_MARKER_PANE).appendChild(div);
  this.map_ = map;
  this.div_ = div;
};

HiveMapsMarker.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
};

HiveMapsMarker.prototype.copy = function() {
  return new HiveMapsMarker(this.latlng, this.icon);
};

HiveMapsMarker.prototype.redraw = function(force) {
  if (!force) return;
  var divPixel = this.map_.fromLatLngToDivPixel(this.latlng);
  if (isNaN(divPixel.x) || isNaN(divPixel.y))
    return this.hide();
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (divPixel.x - (this.width_>>1)) + "px"
  this.div_.style.height = this.height_ + "px";
  this.div_.style.top = (divPixel.y - (this.height_>>1)) + "px";
  this.show();
};

HiveMapsMarker.prototype.bringToFront_ = function() {
  var z = GOverlay.getZIndex(this.latlng.lat());
  this.div_.style.zIndex = 1e9;
}

HiveMapsMarker.prototype.sendBack_ = function() {
  var z = GOverlay.getZIndex(this.latlng.lat());
  this.div_.style.zIndex = z;
}

/*
HiveMapsMarker.prototype.getZIndex = function(m) {
  return GOverlay.getZIndex(marker.getPoint().lat())-m.clicked*10000;
}
*/

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

HiveMapsMarker.prototype.getLatLng = function() {
  return this.latlng;
};

HiveMapsMarker.prototype.isHidden = function() {
  return this.div_.style.display == 'none';
};

HiveMapsMarker.prototype.show = function() {
  this.div_.style.display = 'block';
};

HiveMapsMarker.prototype.hide = function() {
  this.div_.style.display = 'none';
};

HiveMapsMarker.prototype.getIcon = function() {
  return this.icon;
};


