/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="google.maps-vsdoc.js" />

twareg = {};
twareg.trop = {};

// class twareg.trop.Marker
twareg.trop.Marker = function(latlng, options) {
  /// <param name="latlng" type="google.maps.Size"/>
  /// <param name="options" type="Object" />

  this.latlng = latlng;
  this.label = options.label || '';
  this.labelClass = options.labelClass || 'label';
  this.labelOffset = options.labelOffset || new google.maps.Size(0, 0);

  var icon = new google.maps.Icon(G_DEFAULT_ICON, '/styles/i/marker.png');
  icon.transparent = '/styles/i/marker_t.png';
  icon.shadow = '';
  icon.printImage = '';
  icon.printShadow = '';
  icon.shadowSize = new google.maps.Size(0, 0);
  icon.iconSize = new google.maps.Size(30, 32);

  options.icon = icon;

  google.maps.Marker.apply(this, arguments);
}

twareg.trop.Marker.prototype = new google.maps.Marker(new google.maps.LatLng(0, 0));

twareg.trop.Marker.prototype.initialize = function(map) {
  google.maps.Marker.prototype.initialize.call(this, map);

  var div = document.createElement("div");
  div.className = this.labelClass;
  div.innerHTML = this.label;
  div.style.color = "#FFF";
  div.style.position = "absolute";
  div.style.marginTop = "-34px";
  div.style.marginLeft = "-9px";
  div.style.lineHeight = "19px";
  div.style.height = "19px";
  div.style.width = "19px";
  div.style.textAlign = "center";
  div.style.fontSize = "11px";
  div.style.fontWeight = "bold";

  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  if (this.clickable) {
    var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
    for (var i = 0; i < eventPassthrus.length; i++) {
      var name = eventPassthrus[i];
      google.maps.Event.addDomListener(div, name, newEventPassthru(this, name));
    }

    div.style.cursor = "pointer";
  }

  this.map = map;
  this.div = div;
}

function newEventPassthru(obj, event) {
  return function() {
    google.maps.Event.trigger(obj, event);
  };
}

twareg.trop.Marker.prototype.redraw = function(force) {
  google.maps.Marker.prototype.redraw.apply(this, arguments);

  if (!force) return;

  var p = this.map.fromLatLngToDivPixel(this.latlng);
  var z = google.maps.Overlay.getZIndex(this.latlng.lat());

  this.div.style.left = (p.x + this.labelOffset.width) + "px";
  this.div.style.top = (p.y + this.labelOffset.height) + "px";
  this.div.style.zIndex = z + 1;
}

twareg.trop.Marker.prototype.remove = function() {
  google.maps.Event.clearInstanceListeners(this.div);
  this.div.parentNode.removeChild(this.div);
  this.div = null;
  google.maps.Marker.prototype.remove.apply(this, arguments);
}

// class twareg.trop.Bubble
twareg.trop.Bubble = function (marker, text) {
  this.marker = marker;
  this.text = text;
}

twareg.trop.Bubble.prototype = new google.maps.Overlay();

twareg.trop.Bubble.prototype.initialize = function(map) {
  var div = document.createElement("div");

  var closeButton = document.createElement('div');
  closeButton.className = 'closeButton';
  closeButton.style.position = 'absolute';
  closeButton.onclick = function() { map.getPane(G_MAP_FLOAT_PANE).innerHTML = ''; };

  var top = document.createElement('div');
  top.className = 'bubbleTop';
  top.innerHTML = this.text;

  var bottom = document.createElement('div');
  bottom.className = 'bubbleBottom';

  div.className = 'bubble';
  div.style.position = 'absolute';
  div.appendChild(closeButton);
  div.appendChild(top);
  div.appendChild(bottom);

  map.getPane(G_MAP_FLOAT_PANE).innerHTML = '';
  map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

  google.maps.Event.bindDom(div, 'mousedown', this, disableEvent);
  google.maps.Event.bindDom(div, 'dblclick', this, disableEvent);
  google.maps.Event.bindDom(div, 'DOMMouseScroll', disableEvent);

  this.map = map;
  this.div = div;
}

function disableEvent(e) {
  if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && document.all) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    e.stopPropagation();
  }
}

twareg.trop.Bubble.prototype.remove = function() {
  this.div.parentNode.removeChild(this.div);
}

twareg.trop.Bubble.prototype.copy = function() {
  return new twareg.trop.Bubble(this.marker, this.text, this.padding);
}

twareg.trop.Bubble.prototype.redraw = function(force) {
  if (!force) return;

  var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  var xPos = markerPos.x;
  var yPos = markerPos.y - this.div.clientHeight;

  this.div.style.top = yPos + 'px';
  this.div.style.left = xPos + 'px';
}

twareg.trop.Bubble.prototype.show = function() {
  if (this.map.getPane(G_MAP_FLOAT_PANE).innerHTML != '')
    return;

  this.map.getPane(G_MAP_FLOAT_PANE).appendChild(this.div);
}
