/**
 * @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
 */
var IS_IE = true;
var IS_LT_IE7;

//create the tab(s) for the GInfoWindow
function createTab(marker, content) {
    var element = jsonToDom(
		{ el: 'div', att: { Class: 'googleMarkerTab' }, ch: [
			{ el: 'div', att: { Class: 'content' }, ch: [
				content
			]
			}
		]
		});
    marker.tab = [new GInfoWindowTab('Address', element)];
}

function jsonToDom(e) {
    if (null != e.txt) return document.createTextNode(e.txt);
    if (null == e.el) return null;
    var node = document.createElement(e.el.toUpperCase());
    if (null != e.att) {
        for (var i in e.att) {
            if (IS_IE) {
                node = applyAttribute(node, i, e.att[i]);
            } else {
                applyAttribute(node, i, e.att[i]);
            }
        }
    }
 
    if (null != e.ch) {
        for (var j in e.ch) {
            var childNode = jsonToDom(e.ch[j]);
            if (null !== childNode) node.appendChild(childNode);
        }
    }
    return node;
}
function applyAttribute(node, attribute, value) {
    if (IS_IE) {
        return ieApplyAttribute(node, attribute, value);
    } else {
        node.setAttribute(attribute, value);
    }
}

/**
* @param {HTMLElement} node
* @param {String} attribute
* @param {String} value
* @return {HTMLElement}
*/
function ieApplyAttribute(node, attribute, value) {
    switch (attribute.toLowerCase()) {
        case 'for':
            node.htmlFor = value;
            break;
        case 'class':
            node.className = value;
            break;
        case 'style':
            node.style.textCss = value;
            break;
        case 'name':
            node = document.createElement(node.outerHTML.replace(">", " name=" + value + ">"));
            break;
        default:
            node.setAttribute(attribute, value);
    }
    return node;
}


 
 /*
function Tooltip(marker, text, padding, html){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
	this.innerHTML_ = html;
}
*/
function Tooltip(marker, content, padding) {
    this.marker = marker;
    this.content =content;
    this.padding = padding;
    this.div = null;
    this.map = null;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map) {

    this.div = document.createElement("div");
    var innerContainer = this.div.cloneNode(false);
    this.div.appendChild(innerContainer);
    this.div.style.position = 'absolute';
    this.div.style.visibility = 'hidden';
    this.div.style.border_width = "5px";
    this.div.style.border_style = "Ridge";
    this.div.style.border_color = "Black";
    this.div.style.border_top_style = "solid";
    this.div.style.border_top_color = "Red";
    this.div.style.border_top_width = "5px";


    innerContainer.className = 'tooltip';
/*    var child = typeof this.content == 'string' ?
		document.createTextNode(this.content) :
		this.content;
*/
    var child = typeof this.content == 'string' ?
		document.createTextNode(this.content) :
		this.content;
    
    innerContainer.appendChild(child);
    map.getPane(G_MAP_FLOAT_PANE).appendChild(this.div);
    this.map = map;
}

/*
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';
	div.style.border_width = "10px";
	div.style.border_style = "ridge";
	div.style.border_color = "black";
	div.style.border_top_style = "solid";
	div.style.border_top_color = "red";
	div.style.border_top_width = "5px";
	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;

    //draw tooltip
    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';
    this.div.style.backgroundColor = "#85C1E6";
    this.div.style.color = "Black";
    this.div.style.backgroundColor = "#85C1E6";
    this.div.style.border_width = "5px";
    this.div.style.border_style = "Ridge";
    this.div.style.border_color = "Black";
    this.div.style.border_top_style = "solid";
    this.div.style.border_top_color = "Red";
    this.div.style.border_top_width = "5px";
    this.div.style.width = "100px";
    //    this.div.style.border= "5px";
    this.div.style.border = "2px solid " + "blue";
    
}

/*
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';
    //this.div_.style.width=300;
    this.div_.style.text_decoration = "underline";
    this.div_.style.color = "black";
    this.div_.style.backgroundColor = "#85C1E6";
    this.div_.style.border_width = "10px";
    this.div_.style.border_style = "ridge"; 
    this.div_.style.border_color = "black";
    this.div_.style.border_top_style = "solid";
    this.div_.style.border_top_color = "red";
    this.div_.style.border_top_width = "5px";
    this.div_.innerHtml = this.innerHTML_;
}
*/
Tooltip.prototype.show = function() {
    this.div.style.visibility = 'visible';
}

Tooltip.prototype.hide = function() {
    this.div.style.visibility = 'hidden';
}