/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
/*
* This code modified so that when the cursor is over my map img element is figures
* out the position of the cursor within the map and how close it is to any of the 
* rose rays being displayed. Only if the cursor is within 6 pixels of a ray, is a 
* tool tip displayed and the tooltip is derived from the name of the target that
* the ray points at.
*/


var offsetxpoint=-60 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody() {
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

// this is the tooltip display string
var strToolTipPeakInfo = 'not initialized';

function ddrivetip(thetext, thecolor, thewidth) {
  if (ns6||ie) {
    if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
    if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
    tipobj.style.fontFamily='Verdana';
    tipobj.style.fontSize='12px';
    tipobj.innerHTML=thetext
    enabletip=true
    return false
  }
}

function positiontip(e) {
  if (enabletip) {
    var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

    //Find out how close the mouse is to the corner of the window
    var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
    var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

    var el = document.getElementById( 'map' );
    var rect = el.getBoundingClientRect();

    // get mouse cursor loc relative to the map

    var curXX = curX - rect.left;
    var curYY = curY - rect.top;
    // adjust for scrolling (it dif for ns6 vs ie vs (doctype = strict && ie)
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    scrollX=document.all? iebody.scrollLeft : pageXOffset;
    scrollY=document.all? iebody.scrollTop : pageYOffset;

    curXX -= scrollX;
    curYY -= scrollY;

    // NOTE - google offsets the rose rays (static map path points) about 1 pixel to the NE  at zoom
    // level 12.  by zoom level 15 it's noticable.  we end up thinking the rose ray lines are 8 pixels
    // SE of where they are on the display map. so we fix it with a fudge factor here.

    var iFudgeY = -2 / fMultiplier; 
    var iFudgeX = 0.65 / fMultiplier;

    var Center = GetLatLong( strCenter );
    var Viewpoint = GetLatLong( strViewpoint );
    var size = GetSize( strSize );
    var viewpointY = (size.y / 2) + iFudgeY + 320.0 * ( Center.lat - Viewpoint.lat ) / ( fLatitudeExtent * fMultiplier );
    var viewpointX = (size.x / 2) + iFudgeX + 320.0 * ( Viewpoint.long - Center.long ) / ( fDegreeWidthAt640 * fMultiplier );
    var i;
    var Target;
    var bNotOnTheLine = true;
    var distance = 999.0;
    for ( i = 0; i < VisiblePoints.length; i++ ) {
      // convert Target location from degrees to pixels
      Target = VisiblePoints[i];
      // convert target lat/long to pixel X,Y
      var TargetY = (size.y / 2) + iFudgeY + 320.0 * ( Center.lat - Target.loc.lat ) / ( fLatitudeExtent * fMultiplier );
      var TargetX = (size.x / 2) + iFudgeX + 320.0 * ( Target.loc.long - Center.long ) / ( fDegreeWidthAt640 * fMultiplier );
      //alert("" + Target.loc.lat + ',' + Target.loc.long + ',' + Viewpoint.lat + ',' + Viewpoint.long );
      //alert("x = " + TargetX + ',' + TargetY );
      //alert('cursor = ' + curXX + ',' + curYY );
      bNotOnTheLine = IsPastTheEndOrBeforeTheBeginning( viewpointX, viewpointY, TargetX, TargetY, curXX, curYY  );
      if ( bNotOnTheLine )
        continue;
      distance = CalcDistance( viewpointX, viewpointY, TargetX, TargetY, curXX, curYY );
      //alert( 'name = ' + Target.name + ' dist = ' + distance + ' TargetXY = ' + TargetX + ',' + TargetY);
      //alert('dist = ' + distance );
      if ( distance <= 6.0 ) {
        break;
      }
    } // end for loop
    if ( bNotOnTheLine || distance > 6.0 ) {
      // the mouse isn't close to any ray so turn off the tooltip
      hideddrivetip();
      ddrivetip(strToolTipPeakInfo, "cyan", 300);
      return;
    }

    strToolTipPeakInfo = Target.name + ' E:' + Target.elevation + ' ft D:' + Target.distance + ' miles VA:' + Target.verticalangle + ' H:' + Target.heading + 'TN'; // sure would like the degree char in there °°°°°°
    ddrivetip(strToolTipPeakInfo, "cyan", 300);

    var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

    //if the horizontal distance isn't enough to accomodate the width of the context menu
    if (rightedge<tipobj.offsetWidth)
      //move the horizontal position of the menu to the left by it's width
      tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
    else if (curX<leftedge)
      tipobj.style.left="5px"
    else
      //position the horizontal position of the menu where the mouse is positioned
      tipobj.style.left=curX+offsetxpoint+"px"

    //same concept with the vertical position
    if (bottomedge<tipobj.offsetHeight)
      tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
    else
      tipobj.style.top=curY+offsetypoint+"px"
    tipobj.style.visibility="visible"
  } // end if enabletip
} // end positiontip()

// DESCRIPTION:
//  reset the tooltip.
// NOTE:
//  you'll prob need to call ddrivetip() once to get the visual tooltip to go away

function hideddrivetip(){
  if (ns6||ie){
    enabletip=false
    tipobj.style.visibility="hidden"
    tipobj.style.left="-1000px"
    tipobj.style.backgroundColor=''
    tipobj.style.width=''
  }
  strToolTipPeakInfo = "xxx";
}

// DESCRIPTION:
//  calculate the closest distance from a point to a line (ie. the length of a line
//  perpendicular to the given line that passes through the given point.
// CALL:
//  X1,Y1 = viewpoint pixel X,Y location (floating).
//  X2,Y2 = a target pixel X,Y location (floating).
//  curX, curY = cursor position (pixel X,Y location) (floating). 
// NOTE:
//  the line (the ray line) is from the viewpoint (X1,Y1) to the target (X2,Y2).
//  the point for which we want to know the distance to the line is curX,curY
// NOTE:
//  this alogorithm taken from http://www.worsleyschool.net/science/files/linepoint/method5.html

function CalcDistance( X1, Y1, X2, Y2, curX, curY ) {
  var m1 = (Y1 - Y2) / (X1 - X2);
  var C1 = Y1 - m1 * X1;
  // Y1 = m1*X1 + C1
  // 0 = m1*X1 -1*Y1 + C1
  // AX + BY + C = 0
  // A=m1, B=-1, C=C1

  // dist = abs( A*curX + B*curY + c ) / sqrt( A*A + B*B )

  var dist = Math.abs( m1 * curX + -1.0 * curY + C1 ) / Math.sqrt( m1 * m1 + 1.0 );

  return dist;
}

// DESCRIPTION:
//  test if the mouse position is beyond the end of a ray or before the beginning.
// NOTE:
//  this is a rough test of whether the mouse pos is beyond the ends of the rose ray line. 
//  Since being "on the line" at all is a fairly rough test to begin with, then this test
//  can be kind of "rough" too.
// NOTE:
//  in this case the test is all just comparisons of point locations and doesn't involve
//  the much slower calculations of real distances.

function IsPastTheEndOrBeforeTheBeginning( ViewpointX, ViewpointY, TargetX, TargetY, MouseX, MouseY ) {
  // if the mouse is on the "backside" of the viewpoint location from the ray, we return TRUE
  // ie. the mouse is "before the beginning"

  if ( TargetX > ViewpointX && MouseX < ViewpointX )
    return true;
  if ( TargetX < ViewpointX && MouseX > ViewpointX )
    return true;
  if ( TargetY > ViewpointY && MouseY < ViewpointY )
    return true;
  if ( TargetY < ViewpointY && MouseY > ViewpointY )
    return true;

  // if the mouse is farther away from the viewpoint than the target is from the viewpoint
  // (in the same direction) then we return true. NOTE - this isn't a very exact test of 
  // "farther" but since we're only looking for the mouse being "close to the line" it works.

  if ( TargetX > ViewpointX && MouseX > TargetX )
    return true;
  if ( TargetX < ViewpointX && MouseX < TargetX )
    return true;
  if ( TargetY > ViewpointY && MouseY > TargetY )
    return true;
  if ( TargetY < ViewpointY && MouseY < TargetY )
    return true;

  return false;
}

document.onmousemove=positiontip
document.onclick=positiontip

