
//
// These routines handle the dynamic display of Dart popup InText ads
//

// Browser type
var sBrowserType = navigator.userAgent.toLowerCase();
var bIsIE = document.all ? true : false;

// 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 = 16;
var iOffsetMinimum = 1;
// Image is visible semaphore
var bInAdvertisement = false;;

// Custom adjustments for CSS Divs
var iClientWidthAdjust = 785;
var iOffsetAdjustX = 140;
var iOffsetAdjustY = 385;

// 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) {
	//if (!e) var e = window.event;

    // Get the window scroll position
    iPageScrollX = f_scrollLeft();
    iPageScrollY = f_scrollTop();

    // Get the mouse position
	if (bIsIE) {
		iMouseX = event.clientX;
		iMouseY = event.clientY;
	} else {
        iMouseX = e.pageX - iPageScrollX;
        iMouseY = e.pageY - iPageScrollY;
	}
    if (iMouseX < 0){iMouseX = 0;}
    if (iMouseY < 0){iMouseY = 0;}  
    return true;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	if (bIsIE) {
	    return (n_docel) ? n_docel : n_body;
	}
	else {
	    return n_win ;
	}
}


// Move to the mouse position

function moveToMousePos()
{
  // Get browser window size
  //iClientWidth = f_clientWidth();
  iClientWidth = iClientWidthAdjust;
  iClientHeight = f_clientHeight();
  
  if (bIsIE) {
    iClientWidth -= 1;
    iClientHeight -= 28;
  } else {
    iClientWidth -= 1;
    iClientHeight -= 44;
  }
  
  // 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 - iOffsetAdjustX
  // Image Y coordinate
  iImageY = iMouseY + iPageScrollY + iOffsetY - iOffsetAdjustY

  // Freeze the last mouse position during the fade in
  mouseFadeX = iMouseX;
  mouseFadeY = iMouseY;

  //
  // 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();

  // 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 - iOffsetAdjustX;

     if(iOffsetY < 0){
       iOffsetY += 1;
     }else{
       iOffsetY -= 1;
     }
     iImageY = mouseFadeY + iPageScrollY + iOffsetY - iOffsetAdjustY
   }
 }
   
  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;
