/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 * @version 1.0
 * The Tooltip class is an addon designed for the Google
 * Maps GMarker class.
 */

/**
 * @constructor
 * @param {GMarker} marker
 * @param {String} text
 * @param {Number} padding
 */
function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(this.text_));
	div.className = 'tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

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

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}






var iconRed = new GIcon();
iconRed.image = "/images/icon_red.png";
iconRed.iconSize = new GSize(15, 16);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);

var iconOrange = new GIcon();
iconOrange.image = "/images/icon_orange.png";
iconOrange.iconSize = new GSize(15,16);
iconOrange.iconAnchor = new GPoint(6, 20);
iconOrange.infoWindowAnchor = new GPoint(5, 1);

var iconYellow = new GIcon();
iconYellow.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
iconYellow.iconSize = new GSize(12, 20);
iconYellow.iconAnchor = new GPoint(6, 20);
iconYellow.infoWindowAnchor = new GPoint(5, 1);


var iconGreen = new GIcon();
iconGreen.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
iconGreen.iconSize = new GSize(12, 20);
iconGreen.iconAnchor = new GPoint(6, 20);
iconGreen.infoWindowAnchor = new GPoint(5, 1);

var iconPurple = new GIcon();
iconPurple.image = "/images/icon_purple.png";
iconPurple.iconSize = new GSize(15, 16);
iconPurple.iconAnchor = new GPoint(6, 20);
iconPurple.infoWindowAnchor = new GPoint(5, 1);

var iconBlue = new GIcon();
iconBlue.image = "/images/icon_blue.png";
iconBlue.iconSize = new GSize(15, 16);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);


var customIcons = [];
customIcons["13"] = iconRed;
customIcons["31999"] = iconOrange;
customIcons["32000"] = iconBlue;
customIcons["31998"] = iconPurple;
customIcons["32341"] = iconOrange;
customIcons["32374"] = iconOrange;
customIcons["32680"] = iconOrange;

var markerGroups = [];
markerGroups["0"] = [];
markerGroups["13"] = [];
markerGroups["31999"] = [];
markerGroups["32000"] = [];
markerGroups["31998"] = [];
markerGroups["32341"] = [];
markerGroups["32374"] = [];
markerGroups["32680"] = [];
markerGroups["best"] = [];

var bounds = new GLatLngBounds();


function createMarker(point, name, address, type) {
  var marker = new GMarker(point, customIcons[type]);
  markerGroups[type].push(marker);
  GEvent.addListener(marker, 'click', function() {
    document.location.href = address;
  });
  return marker;
}

function toggleGroup(type, that) {

  for (var i = 0; i < markerGroups[type].length; i++) {
    var marker = markerGroups[type][i];
    if (marker.isHidden()) {
      marker.show();
      that.style.color = '#BBBBBB';
    } else {
      marker.hide();
      that.style.color = '#808080';
    }
  } 
}
