
//
// These routines handle the dynamic display of Dart popup InText ads
//

// Browser type
var sBrowserType = navigator.userAgent.toLowerCase();
var bIsIE = ((sBrowserType.indexOf('msie') != -1) && (sBrowserType.indexOf('opera') == -1));

// Mouse Position
var iMouseX;
var iMouseY;
var iPageScrollX;
var iPageScrollY;
  
// Image Positioning
var iImageX;
var iImageY;
var iClientWidth;
var iClientHeight;
var iOffsetX;
var iOffsetY;
var iOffsetDefault = 24;
var iOffsetMinimum = 16;
// Image is visible semaphore
var bInAdvertisement = false;;

// Image dimensions
var iImageWidth;
var iImageHeight;

// inText Div
var objInTextDiv;
var objInTextStyle;
var iLastSpanId = 0;
var sLastDivId = '';

// Fade delay - Set before calling divFadeIn
var iIEfade;
var dblFFfade;
var iIEfadeInc;
var dblFFfadeInc;
var iDivFadeInTime;
var iDivFadeInTimerID = 0;
// Fade routine semaphore
var bInFade = false;

// Close delay
var iDivHideTimerID = 0;
var iDivHideDelay = 1500;


// Initialize once
function initInText(){
  // Set a document wide mouse move event
  document.onmousemove = getMousePos;
}

// Get the current mouse position
function getMousePos(e)
{
  // Get the window scroll position and the mouse position
  if(bIsIE){
    if(document.compatMode && document.compatMode != "BackCompat"){
      iPageScrollX = document.documentElement.scrollLeft;
      iPageScrollY = document.documentElement.scrollTop;
    }else{
      iPageScrollX = document.body.scrollLeft;
      iPageScrollY = document.body.scrollTop;
    }
    iMouseX = event.x;
    iMouseY = event.y;
  }else{
    iPageScrollX = window.pageXOffset;
    iPageScrollY = window.pageYOffset;
    iMouseX = e.pageX - iPageScrollX;
    iMouseY = e.pageY - iPageScrollY;
  }
  return true;
}

// Move to the mouse position
function moveToMousePos()
{
  // Get browser window size
  if( typeof( window.innerWidth ) == 'number' ) {
    // Non-IE
    iClientWidth = window.innerWidth-26;
    iClientHeight = window.innerHeight-44;
  }else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    // IE 6+ in standards compliant mode
    iClientWidth = document.documentElement.clientWidth-8;
    iClientHeight = document.documentElement.clientHeight-28;
  }else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    // IE 4 compatible
    iClientWidth = document.body.clientWidth-8;
    iClientHeight = document.body.clientHeight-28;
  }
  
  // Positioning conditions
  
  // Default
  iOffsetX = iOffsetDefault; 
  iOffsetY = iOffsetDefault; 

  // Against the right side but above the bottom
  if((iMouseX + iImageWidth > iClientWidth) && (iMouseY + iImageHeight < iClientHeight)){
    // Calculate horizontal offset  
    iOffsetX = -(iMouseX + iImageWidth - iClientWidth);
    // Set default vertical offset  
    iOffsetY = iOffsetDefault;
  }else{
	// Against the bottom but left of the right side
	if((iMouseY + iImageHeight > iClientHeight) && (iMouseX + iImageWidth < iClientWidth)){
	  // Calculate vertical offset  
	  iOffsetY = -(iMouseY + iImageHeight - iClientHeight);
	  // Calculate horizontal offset  
	  iOffsetX = iOffsetDefault;
	}else{
	  // Against the bottom and the right side
	  if((iMouseY + iImageHeight > iClientHeight) && (iMouseX + iImageWidth > iClientWidth)){
	  // Set vertical offset  
	  iOffsetY = -iImageHeight - iOffsetDefault;
	  // Set horizontal offset  
	  iOffsetX = -iImageWidth - iOffsetDefault;
  }}}

  // Image X coordinate
  iImageX = iMouseX + iPageScrollX + iOffsetX
  // Image Y coordinate
  iImageY = iMouseY + iPageScrollY + iOffsetY

  //
  // Set the InText Ad position
  //
  objInTextStyle.left = iImageX  + 'px';
  objInTextStyle.top = iImageY + 'px'; 

}   

// Enter the keyword span and show the image
function inSpan(sKeywordDivId,ih,iw,iSpanId,sDartSource,sFrameId){

  //alert('InSpan');
  // Check same span rentry and image semaphore
  if(iSpanId==iLastSpanId || bInAdvertisement)
    return;
    
  // If not the first image - Hide the previous image 
  if( sLastDivId != ''){
    clearTimeout(iDivFadeInTimerID);
    clearTimeout(iDivHideTimerID);
    divHide();
  }

  // Set semaphore
  bInAdvertisement = true;
    
  // Set last spanId and divId
  iLastSpanId = iSpanId;
  sLastDivId = sKeywordDivId;

  // Get the div style var
  objInTextDiv = document.getElementById(sKeywordDivId);
  objInTextStyle = objInTextDiv.style;

  // Set the image dimensions
  iImageHeight = ih;
  iImageWidth = iw;
  
  // Move the objInTextDiv to the mouse position offset
  moveToMousePos();

  // Freeze the last mouse position during the fade in
  mouseFadeX = iMouseX;
  mouseFadeY = iMouseY;

  // Dynamically set the iframe location
  top.frames[sFrameId].location.replace(sDartSource);
  
  // Set the fade interval settings
  iIEfade = 50;
  dblFFfade = .50;
  iIEfadeInc = 5;
  dblFFfadeInc = .05;
  iDivFadeInTime = 50;
  
  // Show the div
  divShow()

  // Fade in the div
  divFadeIn();
}

// Exit the keyword span - Set divHide with delay
function outSpan(){
  //alert('OutSpan');
  bInAdvertisement = false;
  iDivHideTimerID = setTimeout('divHide()',iDivHideDelay);
}

// Enter the IFrameCell - Clear divHide timeout
function inDartContainer(){
  //alert('InDart');
  bInAdvertisement = true;
  clearTimeout(iDivHideTimerID);
}

// Exit the DartContainer - Set divHide with delay
function outDartContainer(){
  //alert('OutDart');
  bInAdvertisement = false;
  iDivHideTimerID = setTimeout('divHide()',iDivHideDelay);
}

// Show the div
function divShow(){
  // Show the div
  objInTextStyle.display='block';
  objInTextStyle.visibility = 'visible';
}

// Fade in a div while sliding into place
function divFadeIn(){

  // Prevent rentry to inSpan() during a fade
  bInFade = true;

  // Increment the transparency step
  iIEfade += iIEfadeInc;
  dblFFfade += dblFFfadeInc;
	
  // Set the transparency
  if(bIsIE){
	objInTextStyle.filter = 'alpha(opacity=' + iIEfade + ')';
  }else{
    objInTextStyle.opacity=dblFFfade;
  }
  
  // Slide in the div
  if(Math.abs(iOffsetX)>iOffsetMinimum){
    if(iIEfade % 10 == 0){
      if(iOffsetX < 0){
        iOffsetX += 1;
      }else{
        iOffsetX -= 1;
      }
      iImageX = mouseFadeX + iPageScrollX + iOffsetX;

     if(iOffsetY < 0){
       iOffsetY += 1;
     }else{
       iOffsetY -= 1;
     }
     iImageY = mouseFadeY + iPageScrollY + iOffsetY
   }
 }
   
  objInTextStyle.left = iImageX  + 'px';
  objInTextStyle.top = iImageY + 'px'; 
  
  // Set the fade time
  if(iIEfade <= 100-iIEfadeInc){
    iDivFadeInTimerID = setTimeout('divFadeIn()',iDivFadeInTime);
  }else{
    // Fade all done - clear semaphore
    bInFade = false;
  }
}

// Hide the div
function divHide(){

  // Check semaphores
  if(bInAdvertisement || sLastDivId=='')
    return;
    
  // Set the div style var
  objInTextDiv = document.getElementById(sLastDivId);
  objInTextStyle = objInTextDiv.style;
  
  // Do not display
  objInTextStyle.display = 'none';
  objInTextStyle.visibility = 'visible';
  
  // Clear sLastDivId and iLastSpanId
  sLastDivId = '';
  iLastSpanId = 0;

  // Div hidden - clear semaphore
  bInAdvertisement = false;
}

// Close button - Hide the div
function divCloseButton(){

  // Check the fade semaphore
  if(bInFade)
    return;

  // Set the div style var
  objInTextDiv = document.getElementById(sLastDivId);
  objInTextStyle = objInTextDiv.style;
  
  // Do not display
  objInTextStyle.display = 'none';
  objInTextStyle.visibility = 'visible';
  
  // Clear sLastDivId and iLastSpanId
  sLastDivId = '';
  iLastSpanId = 0;

  // Div hidden - clear semaphore
  bInAdvertisement = false;
}

// RUN ON LOAD - Add initInText to the onload event 
if (window.addEventListener)
	window.addEventListener('load', initInText, false);
else if (window.attachEvent)
	window.attachEvent('onload', initInText);
else if (document.getElementById)
	window.onload=initInText;
