﻿var map;

/* [listing 9-2] */
//create the ToolTip overlay object
function ToolTip(marker,html,width) {
	this.html_ = html;
	this.width_ = (width ? width + 'px' : 'auto');
	this.marker_ = marker;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.display = 'none';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	
	this.map_ = map;
	this.container_ = div;
}

//akis start--added this Function
function removeHTMLTags(htmlString){
    if(htmlString){
        var mydiv = document.createElement("div");
        var akisRET;

        mydiv.innerHTML = htmlString;
        //akisRET = mydiv.innerHTML.getElementById('akisID');
        if (document.all) // IE Stuff
        {
            //akisRET = akisRET + mydiv.innerText;
            return mydiv.innerText;
        }    
        else // Mozilla does not work with innerText
        {
            //akisRET = akisRET + mydiv.textContent;
            return mydiv.textContent;
        }                            
            //return htmlString;

    }
}
//akis ends--added this Function

ToolTip.prototype.remove = function() {

	this.container_.parentNode.removeChild(this.container_);
}

ToolTip.prototype.copy = function() {
	return new ToolTip(this.html_);
}

ToolTip.prototype.redraw = function(force) {
	if (!force) return;

	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	//akis start mono etsi paizei swsta!! Alliws vgazei provlima ston Iexplorer.
	var akisInner = document.createElement("div");
	akisInner.innerHTML = this.html_;
	this.container_.appendChild(akisInner);
	akisInner.style.font = 'normal 10px/10px verdana, arial, sans';
	akisInner.style.width = '190px';
	akisInner.style.height = '110px';
	//akisInner.style.border = '1px solid #6da0c2';
	akisInner.style.color = '#FFFFFF';
	akisInner.style.background = '#97cbee';
	//akisInner.style.padding = '4px';
	this.container_.style.position = 'absolute';


	this.container_.style.left = pixelLocation.x + "px";
	//var akisaddheight = pixelLocation.y - 30;
	//this.container_.style.top = akisaddheight + "px";
	
	
	//oxivar akiswidth = width + 10;
	//this.container_.style.width = "200px";
	//oxi this.container_.style.width = this.width_;


	this.container_.style.font = 'bold 10px/10px verdana, arial, sans';
	this.container_.style.border = '1px solid #6da0c2';
	this.container_.style.color = '#FFFFFF';
	this.container_.style.background = '#97cbee';
	this.container_.style.padding = '4px';

	//one line to desired width
	//this.container_.style.whiteSpace = 'nowrap';
	if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';
	this.container_.style.display = 'block';

    //extra coding
    var gap = 0;
	//var map = theObj.map;
	var pt  = this.marker_.getPoint();
	
	var theIcon = this.marker_.getIcon();
	
	pixelLocation.y -= Math.floor(theIcon.iconAnchor.y/2);

	var rightSide = true;
	var bounds = this.map_.getBounds();
	var boundsSpan	= bounds.toSpan();
	var longSpan = boundsSpan.lng();
	var mapWidth = map.getSize().width;

    //john
	var bottomSide = true;
	var latSpan = boundsSpan.lat();
	var mapHeight = map.getSize().height;
    
	var tooltipHeightInDeg = (this.container_.offsetHeight + 24 + 6) / mapHeight * latSpan;
	if ( pt.lat() + tooltipHeightInDeg > bounds.getNorthEast().lat() )
		bottomSide = false;
	
	var gama = (theIcon.iconSize.height - theIcon.iconAnchor.y) + gap;
	if (bottomSide)
		pixelLocation.y += gama;
	else
		pixelLocation.y -= gama;
	this.marker_.bottomSide = bottomSide;
	
	//john
    var tooltipWidthInDeg = (this.container_.offsetWidth) / mapWidth * longSpan;
	if ( pt.lng() + tooltipWidthInDeg > bounds.getNorthEast().lng() )
		rightSide = false;
	
	var delta = (theIcon.iconSize.width - theIcon.iconAnchor.x) + gap;
	if (rightSide)
		pixelLocation.x += delta;
	else
		pixelLocation.x -= delta;
	this.marker_.rightSide = rightSide;
	this.marker_.ttLeft = pixelLocation.x;
	this.marker_.ttTop  = pixelLocation.y;
		if (rightSide) {
			this.container_.style.left = pixelLocation.x + "px";
			this.container_.style.right = null;
		}
		else {
			this.container_.style.left = null;
			this.container_.style.right = -pixelLocation.x + "px";
		}
		 
		 
		 if (bottomSide) {
			this.container_.style.bottom = -pixelLocation.y + "px";
			this.container_.style.top = null;
		}
		else {
			this.container_.style.bottom = null;
			this.container_.style.top = pixelLocation.y + "px";
		}

}

GMarker.prototype.ToolTipInstance = null;

GMarker.prototype.openToolTip = function(content) {
	//don't show the tool tip if there is acustom info window
	if(this.ToolTipInstance == null) {
		this.ToolTipInstance = new ToolTip(this,content)
		map.addOverlay(this.ToolTipInstance);
	}
}

GMarker.prototype.closeToolTip = function() {
	if(this.ToolTipInstance != null) {
		map.removeOverlay(this.ToolTipInstance);
		this.ToolTipInstance = null;
	}
}
/* [listing 9-2 end] */

