/* 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) {
  this.latlng = latlng;
  if (!opts) opts = {};
  this.height_ = opts.height || 32;
  this.width_ = opts.width || 32;
  this.image_ = opts.image;
  this.imageOver_ = opts.imageOver;
  this.clicked_ = 0;
  this.title = opts.title;
}

/* 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';

  var img = document.createElement("img");
  img.src = me.image_;
  img.style.width = me.width_ + "px";
  img.style.height = me.height_ + "px";
  img.tooltip = me.title;
  img.className = 'map_tooltip';
  div.appendChild(img);

  GEvent.addDomListener(img, "click", function(event) {
    me.clicked_ = 1;
    GEvent.trigger(me, "click");
  });
  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_);
};

/* 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.image = this.image_;
  opts.imageOver = 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) + "px"
  this.div_.style.height = (this.height_) + "px";
  this.div_.style.top = (divPixel.y) - this.height_ + "px";
};

MarkerLight.prototype.getZIndex = function(m) {
  return GOverlay.getZIndex(marker.getPoint().lat())-m.clicked*10000;
}

MarkerLight.prototype.getPoint = function() {
  return this.latlng;
};

MarkerLight.prototype.getInfoWindowPoint = function() {
	var offset = $(this.div_).offset();
	var map_offset = $("#map").offset();
	var left = offset.left - map_offset.left + this.width_ / 2;
	var top = offset.top - map_offset.top + this.height_ / 2;
	var latlng = window.map.fromContainerPixelToLatLng({ x: left, y: top });
  return latlng;
};

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 + '")';
}


//Version 0.0  Initial version
//Version 0.1  10/10/2006 Added E_STYLE_7
//Version 0.2  17/05/2007 Added .isHidden() and .supportsHide()

function EStyle(stemImage, stemSize, boxClass, boxOffset) {
this.stemImage = stemImage;
this.stemSize = stemSize;
this.boxClass = boxClass;
this.boxOffset = boxOffset;
//this.border = border;

// Known fudge factors are:
// Firefox (1.0.6 and 1.5)    5, -1
// IE 6.0                     0, -1
// Opera 8.54                 3, -1
// Opera 9 prev               4, -1
// Netscape (7.2, 8.0)        5, -1
// Safari                     5, -1

var agent = navigator.userAgent.toLowerCase();

var fudge = 5;  // assume Netscape if no match found

if (agent.indexOf("opera") > -1) {
 fudge = 3;
}
if (agent.indexOf("firefox") > -1) {
 fudge = 0;
}
if (agent.indexOf("safari") > -1) {
 fudge = 0;
}
if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
 fudge = 0;
}
this.fudge = fudge;
}

var E_STYLE_0 = new EStyle("/images/map/stem0.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_1 = new EStyle("/images/map/stem1.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_2 = new EStyle("/images/map/stem2.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_3 = new EStyle("/images/map/stem3.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_4 = new EStyle("/images/map/stem4.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_5 = new EStyle("/images/map/stem5.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_6 = new EStyle("/images/map/stem6.png", new GSize(24,24), "estyle2", new GPoint(-10,23));
var E_STYLE_100 = new EStyle("/images/map/stem100.png", new GSize(24,24), "estyle2", new GPoint(-10,23));


function EWindow(map,estyle) {
// parameters
this.map=map;
this.estyle=estyle;
// internal variables
this.visible = false;
// browser - specific variables
this.ie = false;
var agent = navigator.userAgent.toLowerCase();
if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
}

EWindow.prototype = new GOverlay();

EWindow.prototype.initialize = function(map) {
var div1 = document.createElement("div");
div1.style.position = "absolute";
map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div1);
var div2 = document.createElement("div");
div2.style.position = "absolute";
div2.style.width = this.estyle.stemSize.width+"px";
map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div2);
this.div1 = div1;
this.div2 = div2;
}

EWindow.prototype.openOnMap = function(point, html, offset) {
this.offset = offset||new GPoint(0,0);
this.point = point;
this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';
if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
 var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
 this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '" ></div>';
} else {
 this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'" style="margin:0px;padding:0px;">';
}
var z = GOverlay.getZIndex(this.point.lat());
this.div1.style.zIndex = z;
this.div2.style.zIndex = z+1;
this.visible = true;
this.show();
this.redraw(true);
}

EWindow.prototype.openOnMarker = function(marker,html) {
this.openOnMap(marker.getInfoWindowPoint(), html, false);
}

EWindow.prototype.redraw = function(force) {
if (!this.visible) {return;}
var p = this.map.fromLatLngToDivPixel(this.point);
this.div2.style.left   = (p.x + this.offset.x) + "px";
this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
}

EWindow.prototype.remove = function() {
this.div1.parentNode.removeChild(this.div1);
this.div2.parentNode.removeChild(this.div2);
this.visible = false;
}

EWindow.prototype.copy = function() {
return new EWindow(this.map, this.estyle);
}

EWindow.prototype.show = function() {
this.div1.style.display="";
this.div2.style.display="";
this.visible = true;
}

EWindow.prototype.hide = function() {
this.div1.style.display="none";
this.div2.style.display="none";
this.visible = false;
}

EWindow.prototype.isHidden = function() {
return !this.visible;
}

EWindow.prototype.supportsHide = function() {
return true;
}

