/*
Copyright 2005-2007 James Tolley - http://www.bitperfect.com | http://www.gmaptools.com
All rights reserved.
This software is released under the LGPL. (http://www.opensource.org/licenses/lgpl-license.php)

  BpOverlay - some good utility methods for all overlays

This class inherits from GOverlay, adding some basic, useful functionality.
This class is subclassed by BpLabel and BpMinimalMarker, which are
implementations of this using HTML and an image, respecitvely.
These classes are meant to be fast, clean, open-source implementations
built for speed.

initialize returns the div that should be used by the inheriting class,
  and that class should append that div to a map pane.

These are the methods that should be implemented in this class:
("!" means that it's tested)
! get/setPane
! getMap/isMapped
! show/hide/isVisible
! get/setPoint
! getId
! get/setUserData
! getDiv
! copy
! remove
! initialize
! redraw
! get/setCursor
! get/setZIndex
! get/setOpacity

TODO:
  getEventTarget (getDiv?)
  openInfoWindowHtml

*/

(function() {

var id = 0;

function BpOverlay(latlng,opts) {
  GOverlay.apply(this,arguments);

  this._latlng = latlng;
  if(opts)
    this._opts = opts;
  else
    this._opts = {};

  this._id = ++id;

  this._pane = G_MAP_MARKER_PANE;
  this._cursor = false;
  this._opacity = 80;
  this._userData = {};
}

BpOverlay.prototype = new GOverlay(new GLatLng(0,0));
BpOverlay.prototype.constructor = BpOverlay;


BpOverlay.prototype.initialize = function(map) {
  this._map = map;

  // if we're doing this for the first time...
  if(!this._div) {
    this._div = document.createElement('div');
    this._div.style.position = 'absolute';
    this._div.style.zIndex = GOverlay.getZIndex(this.getPoint().lat());
    this.redraw();
  }

  if (this._cursor)
    this.setCursor(this._cursor);

  this.setOpacity(this._opacity);
  this.setZIndex(this.getZIndex());

  map.getPane(this._pane).appendChild(this._div);

  return this._div;
};

BpOverlay.prototype.remove = function() {
  this._div.parentNode.removeChild(this._div);
  delete this._map;
};

// this is a bare-bones implementation which anchors on the NW corner
BpOverlay.prototype.redraw = function() {
  var divPx = this._map.fromLatLngToDivPixel(this._latlng);

  this._div.style.left = divPx.x + 'px';
  this._div.style.top  = divPx.y + 'px';
};

// point, opts (not userData, pane, cursor, innerHTML, opacity, zIndex)
BpOverlay.prototype.copy = function() {
  return new BpOverlay(this._latlng,this._opts);
};

BpOverlay.prototype.isMapped = function() {
  return this._map ? true : false;
};

BpOverlay.prototype.getMap = function() {
  return this._map;
};

BpOverlay.prototype.show = function() {
  this._div.style.display = '';
};

BpOverlay.prototype.hide = function() {
  this._div.style.display = 'none';
};

BpOverlay.prototype.isVisible = function() {
  if (!this.isMapped() || !this._div || !this._div.parentNode)
    return false;

  return this._div.style.display != 'none';
};

BpOverlay.prototype.getUserData = function() {
  return this._userData;
};

BpOverlay.prototype.setUserData = function(userData) {
  this._userData = userData;
};

BpOverlay.prototype.getZIndex = function() {
  if (! this._div) {
    if (typeof(this._zIndex) != 'undefined')
      return this._zIndex;
    else
      return GOverlay.getZIndex(this.getPoint().lat());
  }

  return this._div.style.zIndex;
};

BpOverlay.prototype.setZIndex = function(zIndex) {
  if (arguments.length == 0)
    zIndex = GOverlay.getZIndex(this.getPoint().lat());

  this._zIndex = zIndex;

  if (this._div)
    this._div.style.zIndex = this._zIndex;
};

BpOverlay.prototype.getOpacity = function() {
  return this._opacity;
};

BpOverlay.prototype.setOpacity = function(opacity) {
	if(opacity < 0)   opacity = 0;
	if(opacity > 100) opacity = 100;

  this._opacity = opacity;
  
  if(this._div) {
    this._div.style.filter       = 'alpha(opacity:' + this._opacity + ')';
    this._div.style.KHTMLOpacity = this._opacity / 100;
    this._div.style.MozOpacity   = this._opacity / 100;
    this._div.style.opacity      = this._opacity / 100;
  }
};

BpOverlay.prototype.getCursor = function() {
  return this._cursor;
};

BpOverlay.prototype.setCursor = function(cursor) {
  this._cursor = cursor;

  if(this._div)
    this._div.style.cursor = this._cursor;
};

BpOverlay.prototype.getPane = function() {
  return this._pane;
};

// only works before the overlay is added to the map
BpOverlay.prototype.setPane = function(pane) {
  this._pane = pane;
};

BpOverlay.prototype.getDiv = function() {
  return this._div;
};

BpOverlay.prototype.getPoint = function() {
  return this._latlng;
};

BpOverlay.prototype.setPoint = function(latlng) {
  this._latlng = latlng;
  this.redraw();
};

BpOverlay.prototype.getId = function() {
  return this._id;
};

BpOverlay.prototype.getDiv = function() {
  return this._div;
};

BpOverlay.prototype.getOptions = function() {
  return this._opts;
};

window.BpOverlay = BpOverlay;
})();

