/** 
    Popups used by IBI in Traffic Scotland and Freight scotland transfered over to IS
*/

// TODO: the risk of badly named global variables
// TODO: convert to class to avoid code scattering implementing specific information window interface
// TODO: Obfuscate all of this

//Browsers
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie = document.all;
var _def_mozilla = ns4 || ns6; 

var _global_rollovers_bIsContainer = true;
var _global_rollovers_xcorrection = 0; 
var _global_rollovers_ycorrection = 0;

//Current coordinates
var x, y;

//Tooltip style object 
var toolTipSTYLE="";

//Area to draw the tool tips over
var imgMap = null;


//Find X position of current object
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{ //if offset parent exist then add its coordinates to the results
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


//Find Y poisition of the current object
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//Check if the corner is moving out from the browser window regarding X axis
function /*bool*/ isMovingOutX(iTopX, iWidth)
{    
   var bNeedRellocateX = false;
   
   if (_def_mozilla)
   {  // Mozilla
	  if ( iTopX <= window.pageXOffset)
		bNeedRellocateX = true;
   }
   else
   {  //IE
      if ( iTopX <= document.body.scrollLeft)
		bNeedRellocateX = true;
   }  
      
   var divPos = iTopX + iWidth;
   var parentWidth;

   if (_def_mozilla)
      parentWidth = window.pageXOffset + window.innerWidth;
   else
      parentWidth = document.body.offsetWidth + document.body.scrollLeft;   
   
   if ( divPos >= parentWidth)
	 bNeedRellocateX = true;
   
   return bNeedRellocateX; 
}


//Check if the corner is moving out from the browser window regarding Y axis
function isMovingOutY(iTopY, iHeight)
{
   var divPos = iTopY + iHeight;
     
   var bNeedRellocateY = false;
   
   if (_def_mozilla)
   {        
      var parentHeight = window.pageYOffset + window.innerHeight;
      if ( divPos >= parentHeight)     
		bNeedRellocateY = true;	 
   }
   else
   {// IE
      var parentHeight = document.body.offsetHeight + document.body.scrollTop - 20;	  
      if ( parentHeight > 0)
      {// Which means we have legitimate data
	    if ( divPos >= parentHeight)
		  bNeedRellocateY = true;
	  }//Else leaving as it is because we don't have legitimate data from the browser
   }
	
   if (_def_mozilla)
   {
	  if ( iTopY <= window.pageYOffset)
		bNeedRellocateY = true;
   }
   else
   {// IE
      if ( iTopY <= document.body.scrollTop)
		bNeedRellocateY = true;
   }  
	
   return bNeedRellocateY;
}

//Select given  position
function /* void */ selectPosition(iTopX, iTopY)
{
   toolTipSTYLE.left = iTopX + "px";
   toolTipSTYLE.top = iTopY + "px";
}

function /* double */ calculateIntersectionAxis(iOverlayCoord, iOverlaySize, iBaseCoord, iBaseSize)
{
   if ( iOverlayCoord < iBaseCoord)
   { //First point before
   
       if ( iOverlayCoord + iOverlaySize < iBaseCoord) 
		   return 0; //out of bound (second point before beginning of the area)
		   
	   if ( iOverlayCoord + iOverlaySize < iBaseCoord + iBaseSize)
		  //second point inside the area
		   return (iOverlayCoord + iOverlaySize - iBaseCoord);
	   
	   if ( iOverlayCoord + iOverlaySize >= iBaseCoord + iBaseSize) 
		  //second point after
		   return iBaseSize;		   
   }
   
   else if (iOverlayCoord >= iBaseCoord + iBaseSize) 
	   return 0; //out of bound == (first point after)
	   
   else if ( iOverlayCoord >= iBaseCoord)
   { //first point inside
   
	   if ( iOverlayCoord + iOverlaySize < iBaseCoord + iBaseSize)
	   //second point inside the area
		   return iOverlaySize;
	   
	   if (iOverlayCoord + iOverlaySize >= iBaseCoord + iBaseSize)
	   //second point after
		   return iBaseCoord + iBaseSize - iOverlayCoord;
   }
         
}

function /* double */ calculateIntersectionArea(iTopX, iTopY, iWidth, iHeight)
{		
	var imgHeight = imgMap.clientHeight;
	var imgWidth = imgMap.clientWidth;

	var offsetX = findPosX(imgMap);
	var offsetY = findPosY(imgMap);
		
	var intersectW = calculateIntersectionAxis(iTopX, iWidth, offsetX, imgWidth);
	var intersectH = calculateIntersectionAxis(iTopY, iHeight, offsetY, imgHeight);
	
	return intersectW * intersectH;
}

function /* bool */ isMovingOut(iTopX, iTopY, iWidth, iHeight)
{
    //either of them moves out - means moves out
    return (isMovingOutX(iTopX, iWidth) || isMovingOutY(iTopY, iHeight));
}

function /* void */ positionToolTip()
{
   var iXd = 10;
   var iYd = 10;
		
   //Examining four states of tooltip box
   //x, y contain the coordinates of the pointer
   var divHeight = document.getElementById("toolTipLayer").clientHeight;
   var divWidth = document.getElementById("toolTipLayer").clientWidth;
      
   //All the possible TopX, TopY
   var arrTopX = new Array();
   var arrTopY = new Array();
   
   //Top in X,Y
   //arrTopX[0] = x + 15;
   //arrTopY[0] = y + 15;
   arrTopX[0] = x + iXd;
   arrTopY[0] = y + iYd;
   
   //Top in X - width, Y
   arrTopX[1] = x - divWidth - iXd;
   arrTopY[1] = y + iYd;
   
   //(X - Width, Y - height)
   arrTopX[2] = x - divWidth - iXd;
   arrTopY[2] = y - divHeight - iYd;
   
   //(X, Y - height)
   arrTopX[3] = x + iXd;
   arrTopY[3] = y - divHeight - iYd;
        
   var arrInClientArea = new Array();   
   var BAtLeastOneAvailable = false;
   
   var i;
   
   for (i = 0; i < arrTopX.length; i++)
   {
      arrInClientArea[i] = ! isMovingOut(arrTopX[i], arrTopY[i], divWidth, divHeight);
      if ( arrInClientArea[i])
         BAtLeastOneAvailable = true;
   }
   
   if ( BAtLeastOneAvailable )
   { //Calculating intersection areas
      
      var maxArea = 0;
      var maxAreaToolTipId = 0;
                    
      for (i = 0; i< arrTopX.length; i++)
       if (arrInClientArea[i])
       {
          var currArea = 
             calculateIntersectionArea
                (arrTopX[i], arrTopY[i], divWidth, divHeight);
          if ( currArea >= maxArea) 
          {
             maxArea = currArea;
             maxAreaToolTipId = i;
          }
	   }
         
	  selectPosition(arrTopX[maxAreaToolTipId], arrTopY[maxAreaToolTipId]);
   }
   else
	  selectPosition(arrTopX[0], arrTopY[0]);
}




function initToolTips(smapid, isContainer, x_correction, y_correction)
{    
    //Reading parameters
    var mapid = smapid;
    if (mapid == null) mapid = "map";
    if ( isContainer != null && isContainer == false)
	    _global_rollovers_bIsContainer = false;	
	if ( x_correction != null)
	   _global_rollovers_xcorrection = x_correction; 
	if ( y_correction != null)	
       _global_rollovers_ycorrection = y_correction;


    toolTipSTYLE = document.getElementById("toolTipLayer").style;               
    toolTipSTYLE.visibility = "visible";
    toolTipSTYLE.display = "none";    
    document.onmousemove = moveToMouseLoc;      
    imgMap = document.getElementById(mapid);
}


function toolTip(msg1,msg,txt,txt2, fg, bg)
{

  if ( imgMap == null) 
    //We are not yet initialised, so just returning
    return;

  if(toolTip.arguments.length < 1)   
  { // hide  
    toolTipSTYLE.display = "none";
  }
  else 
  { // show
  
    // default values
    if(!fg) fg = "#777777";
    if(!bg) bg = "#FFFFFF";
	if(!txt) txt = "#FFFFFF";
	if(!txt) txt2 = "#FFFFFF";
	 
    var content =
    '<table  width="250" border="0" cellspacing="0" cellpadding="0" bgcolor="' + fg + '"><td>' +
    '<table  width="250" border="1" bordercolor="#272473"cellspacing="2" cellpadding="2" bgcolor="' + bg + 
    '"><tr ><td align="center"bgcolor="'+ fg +'"><font '+
    'color="' + txt + '">'+ msg1 +
    '</font></td></tr>'+
	'<tr><td ><font  color="' + txt2 + '"' +
    '">'+ msg +
    '</font></td></tr></table></td></table>';
    
    document.getElementById("toolTipLayer").innerHTML = content;
    toolTipSTYLE.display='block'
    positionToolTip();   
  }
}


function moveToMouseLoc(e)
{
      //Tracking the position of the mouse on the screen
       
      if(ns4||ns6)
      { // Mozilla
        x = e.pageX;
        y = e.pageY;
      }
      else
      { // IE
	    if ( _global_rollovers_bIsContainer )
	    {
	      /*
          x = event.x + document.body.scrollLeft - 10 + findPosX(imgMap);
          y = event.y + document.body.scrollTop - 10 + findPosY(imgMap);
          */
          
          x = event.x + document.body.scrollLeft + _global_rollovers_xcorrection + findPosX(imgMap);
          y = event.y + document.body.scrollTop + _global_rollovers_ycorrection + findPosY(imgMap);
        }
        else
	    {
          x = event.x + document.body.scrollLeft - 10;
          y = event.y + document.body.scrollTop - 10;
	    }
      }
      
      //x = x + offsetX;
      //y = y + offsetY;
      
      return true;
}

