/**********************************************************************
 *
 * $Id: kaToolTip.js,v 1.3 2006/08/08 20:51:35 lbecchi Exp $
 *
 * purpose: manage simple tool tips for ka-Map (bug 1374)
 *         
 *
 * author: Lorenzo Becchi & Andrea Cappugi
 *
 * TODO:
 *   - 
 * 
 **********************************************************************/

/******************************************************************************
 * kaToolTip - 
 *
 * To use kaToolTip:
 * 
 * 1) add a script tag to your page:
 * 
 *   <script type="text/javascript" src="tools/kaToolTip.js"></script>
 * 
 * 2) create a new instance of kaToolTip
 * 
 *   var toolTip = new kaToolTip( oMap);
 * 
 * 3) if you want you can set an image (as the tip pointer)
 * 
 *   var offsetX=-6;//offset to move the image left-right
 *   var offsetY=-19;//offset to move the image top-bottom
 *   toolTip.setTipImage('images/tip-red.png',offsetX,offsetY);
 * 
 * 4) Set text
 * 
 *   toolTip.setText('Some text inside the tooltip');
 * 
 * 6) add it to the map
 * 
 *   toolTip.move(x,y);  //pixel coords
 * 
 * or by geo coords:
 * 
 *  toolTip.moveGeo(x,y); //geo coords
 * 
 * 7) hide the tooltip
 * 
 *  toolTip.hide();
 * 
 *
 * 
 *
 *****************************************************************************/
 
 function kaToolTip( oKaMap )
 {
    this.kaMap = oKaMap;
    this.image = null;
    this.domObj = null;
    this.viewport = this.kaMap.domObj;
    this.visible= false;
	this.myMouseOver=false;
	this.EX=-1;
	this.EY=-1;
    this.init();
 };
 
 var ID_TOOL_TIP = 'vfr_toolTip';
 var MIN_DX_EVT = 0;
/**
 * wmsLayer.addRequestParameter( name, parameter )
 *
 * add a parameter to the baseURL safely by checking to see if the parameter
 * exists already.  This is an internal function not intended to be used
 * by other code.
 */
kaToolTip.prototype.init = function()
{
	this.domObj = document.createElement('div');
	this.domObj.setAttribute('id', ID_TOOL_TIP);

	//KL, removed because of firefox flicker problem after scrolling in tooltip
	// this.domObj.onmouseover=this.mouseOver;
	// this.domObj.onmouseout=this.mouseOut;
	
	/* close tool tip on click */
	this.domObj.onclick=this.onclick;
	
	this.minZindex = 1;
	this.coordX = null;
	this.coordY = null;

	var closeButton = document.createElement('div');
	closeButton.setAttribute('id', 'vfr_toolTipCloseButton');
	closeButton.innerHTML = '<img id="winClose" onClick="myToolTip.hide();"  src="images/legend_x.png"/>';
	this.domObj.appendChild(closeButton);

	// KL, create header and content object
	var header = document.createElement('div');
	header.setAttribute('id', 'vfr_toolTipHeader');
	this.domObj.appendChild(header);
	this.domObj.header = header;
	
	var content = document.createElement('div');
	content.setAttribute('id', 'vfr_toolTipContent');
	this.domObj.appendChild(content);
	this.domObj.content = content;
	
//	this.domObj.style.position='absolute';
	this.viewport.appendChild(this.domObj);	

	/*Style features*/
	this.hide();
	this.domObj.toolTip=this;

//	this.kaMap.registerForEvent( KAMAP_MAP_CLICKED, this, this.onclickMap );
	this.kaMap.registerForEvent( KAMAP_EXTENTS_CHANGED, this, this.extentChanged );
};


kaToolTip.prototype.onclickMap=function(e)
{
	e = (e)?e:((event)?event:null);
	this.domObj.toolTip.hide();
};

kaToolTip.prototype.onclick=function(e)
{
	e = (e)?e:((event)?event:null);
	this.toolTip.hide();
};

kaToolTip.prototype.mouseOver=function(e)
{
	e = (e)?e:((event)?event:null);

	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.fromElement;

	if (!reltg) {
		return;
	}
	
//	alert ("mOver, start "  + tg.id + ", " +  reltg.nodeName + ", " + reltg.id);

	while (reltg.id != ID_TOOL_TIP && reltg.nodeName != 'BODY') {
		reltg = reltg.parentNode
//		alert ("mOver, while "  + tg.id + ", " +  reltg.id);
	}
//	alert ("mOver, end "  + tg.id + " " +  reltg.id + ", " + reltg.nodeName + ", " + this.toolTip.myMouseOver);

	if (reltg.nodeName != 'BODY') return;	

	// KL event was triggerd by outside element
	if (this.toolTip.myMouseOver == false) {
// do not switch mode
//		this.style.filter = 'alpha(opacity=100)'; 
//		this.style.opacity = '1.0'; 
//		this.toolTip.myMouseOver = true;
	}
	return;
}

kaToolTip.prototype.mouseOut=function(e)
{
	e = (e)?e:((event)?event:null);

	var tg = (window.event) ? e.srcElement : e.target;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

//	alert ("mOut, start "  + tg.id + ", " +  reltg.nodeName + ", " + reltg.id);
	if (!reltg) {
		return;
	}

//	if (	( Math.abs(e.clientX - this.toolTip.EX) <= MIN_DX_EVT) && 
//			( Math.abs(e.clientY - this.toolTip.EY) <= MIN_DX_EVT)) {
//		return
//	}
//	this.toolTip.EX = e.clientX;
//	this.toolTip.EY = e.clientY;

	while (reltg.id != ID_TOOL_TIP && reltg.nodeName != 'BODY') {
		reltg = reltg.parentNode
//		alert ("mOut, while "  + tg.id + ", " +  reltg.id);
	}
//	alert ("mOut, end "  + tg.id + " " +  reltg.id);
	// event was triggerd by ID_TOOL_TIP or a child of ID_TOOL_TIP
	if (reltg.id == ID_TOOL_TIP) return;	

	if (this.toolTip.myMouseOver == true) {
		this.style.filter = 'alpha(opacity=75)'; 
		this.style.opacity = '0.75'; 
		this.toolTip.myMouseOver = false;
	}
	return;
}

kaToolTip.prototype.setText = function(header, text)
{
	//KL, set content and compute size of tooltip
	this.domObj.header.innerHTML = header;
	this.domObj.content.innerHTML = text;

	// compute max height of toolTip (45%, so that it will fit into one quadrant of the parent window)
	var viewPortHeight = myKaMap.getObjectHeight(myKaMap.getRawObject('viewport'));
	var maxHeight = viewPortHeight / 100 * 45;

	var hHeader = myKaMap.getObjectHeight(this.domObj.header);
	var hContent = myKaMap.getObjectHeight(this.domObj.content);
	var hToolTip = myKaMap.getObjectHeight(this.domObj);
	
	if ( (hContent + hHeader) > maxHeight ) {
		hContent = maxHeight - hHeader;
		this.domObj.content.style.height = hContent + "px";
	}
	this.domObj.style.height = (hContent + hHeader) + "px";
};

//move the tooltip using geo coords
kaToolTip.prototype.moveGeo = function()
{
	//(int [x],int [y])
	var x = parseInt(arguments[0]);
	var	y = parseInt(arguments[1]);
	var pixPos = this.kaMap.geoToPix(x,y);
	var nPixPos = this.kaMap.currentTool.adjustPixPosition( pixPos[0]*(-1), pixPos[1]*(-1) );
	var newX =nPixPos[0];
	var newY = nPixPos[1];
	
	this.move(newX,newY);
};

kaToolTip.prototype.hide = function()
{
 	var x = 0;
	var y = 0;
	var aPixPos = 0;
	this.visible = false;
	
	this.domObj.style.top='-10000px';
	this.domObj.style.left= '-10000px';
	
	aPixPos = this.kaMap.currentTool.adjustPixPosition( parseInt(x) , parseInt(y) );
	var geoCoords = this.kaMap.pixToGeo( aPixPos[0],aPixPos[1]);
	this.coordX = geoCoords[0];
	this.coordY = geoCoords[1];
}

//move the tooltip in pixel space
kaToolTip.prototype.move = function()
{
	// (int [x],int [y])
 	var x = 0;
	var y = 0;
	var aPixPos = 0;
	var geoPix = 0;
	var noRecenter = false;
	
	if(arguments.length<2){
		alert("not allowed");
	} else {
		x = parseInt(arguments[0]);
		y = parseInt(arguments[1]);
		
		this.visible = true;
	
		aPixPos = this.kaMap.currentTool.adjustPixPosition( parseInt(x) , parseInt(y) );
		var geoCoords = this.kaMap.pixToGeo( aPixPos[0],aPixPos[1]);
		this.coordX = geoCoords[0];
		this.coordY = geoCoords[1];

		// KL, offset of viewport within brwoser window, do not know hoe to get it from the dom model
		var xOffset = this.kaMap.xOffsetViewport;
		var yOffset = this.kaMap.yOffsetViewport;
		
		var xRel = x - xOffset;
		var yRel = y - yOffset;
		
		var wCanvas = getObjectWidth('viewport');
		var hCanvas = getObjectHeight('viewport');

		var wToolTip = getObjectWidth(this.domObj);
		var hToolTip = getObjectHeight(this.domObj);

		// KL, correct position of tooltip, with respect of the qudarant of the click
		if ( yRel > hCanvas/2 ) {
			// lower half of window
			yOffset += hToolTip + 10; 
		} else {
			// upper half of window
			yOffset += - 10; 
		}
		if ( xRel > wCanvas/2 ) {
			// right half of window
			xOffset += wToolTip + 10; 
		} else {
			// left half of window
			xOffset += - 10; 
		}
		
		this.domObj.style.left=(x-xOffset)+'px';
		this.domObj.style.top=(y-yOffset)+'px';
	}
};

kaToolTip.prototype.extentChanged=function()
{
	if (myToolTip) { myToolTip.hide(); }
};
