// see bottom for initial calls

if (document.layers) document.captureEvents(Event.MOUSEMOVE);
if (!document.all) window.onmousemove=mouseTrack;

var isSpread = (window.location.search.indexOf("action=browsepagespread") > -1 ||
				window.location.search.indexOf("action=browsepagesingle") > -1);
var lastAlignment = 0;
var lastAlignmentY = 0;
var alignment =0;
var alignmentY =0;

var isNav4, isNav6, isIE4;
var mpX, mpY;

var popped = 'null';

           

function mouseTrack(e) 
{
	if (document.all) 
	{
		oScrollCoords = getScrollCoords();
		mpX = event.clientX + oScrollCoords.x + 10;
		mpY = event.clientY + oScrollCoords.y + 20;
	}
	else 
	{
		mpX = e.pageX + 10;
		mpY = e.pageY + 20;
	}
}	


function getScrollCoords () {
  if (typeof window.pageXOffset != 'undefined')
    return {x: window.pageXOffset, y: window.pageYOffset};
  else if ((!document.compatMode || document.compatMode == 'BackCompat')
           && document.body && typeof document.body.scrollLeft != 'undefined')
    return {x: document.body.scrollLeft, y: document.body.scrollTop};
  else if (document.compatMode == 'CSS1Compat' &&
           document.documentElement && typeof document.documentElement.scrollLeft != 'undefined')
    return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
  else
    return null;
}

function getWidth(id) 
{ 
    var x = 0;
    if (document.getElementById) // IE5+/NS6 
    {
		if(document.getElementById(id))
			x = document.getElementById(id).offsetWidth;         
    }
    else if (document.layers) // NS4 
    {
		if(document.layers[id])
			x = document.layers[id].clip.width;     
    }
    else if (document.all)  // IE4 
    {
		if(document.all[id])
			x = document.all[id].offsetWidth;         
    }
        
	return x;
} 


function getHeight(id) 
{ 
    var y = 0;
    if (document.getElementById) // IE5+/NS6 
    {
		if(document.getElementById(id))
			y = document.getElementById(id).offsetHeight;         
    }
    else if (document.layers) // NS4 
    {
		if(document.layers[id])
			y = document.layers[id].clip.height;     
    }
    else if (document.all)  // IE4 
    {
		if(document.all[id])
			y = document.all[id].offsetHeight;         
    }
    
	return y;
} 

function fade(id, show)
{ 
	//display div if show = 1
	var oScrollCoords = getScrollCoords();	
	var browseWidth;
	var browseHeight;
	var divHeight = getHeight(id);
	var pointer = (document.all) ? document.all[id] : document.getElementById(id);	
	
		if(pointer && show.toString() == "1")
		{
			// calculate browser middle point 
			//  - for X
			if(document.layers||(document.getElementById&&!document.all))
			{
				browseWidth=window.outerWidth / 2;
			}
			else if(document.all)
			{
				browseWidth=document.body.clientWidth / 2;
				mpX = event.clientX + oScrollCoords.x;
				mpY = event.clientY + oScrollCoords.y;
			}
			// calculate alignment modifier	
			// -- for X
			if(lastAlignment == 0)
				lastAlignment = ((mpX > browseWidth && !isSpread)) ? -(getWidth(id) + 10) : 10;				 			

			alignment = mpX + lastAlignment;	
			
			//get the document body height
			// --- note of course diff browsers use diff calls but we try to use a common one first
			browseHeight= getHeight(document);
			//--- if that didn't work, try others
			if((browseHeight ==null) || (browseHeight ==0))
				browseHeight=document.body.innerHeight;

			if((browseHeight ==null) || (browseHeight ==0))
				browseHeight=document.documentElement.clientHeight;
			

			//	alert(this.height + " browseheight " + browseHeight + " y " + mpY + " distance " + distance);

			// -- Y
			// -- determine if the y location is too close to the bottom of the page
			var distance = browseHeight - (mpY + divHeight);
			if(distance <= 1)  //too close to the edge so push the div above the pointer
				alignmentY = (mpY - (divHeight + 10));			 			
			else
				alignmentY = mpY + lastAlignmentY;
		}


    
    //if an element is invisible, make it visible
    if((show.toString() == "1"))  //fade in
    { 
    	// position div where the mouse cursor is and make it visible	
		if(pointer)	
		{

			pointer.style.left = alignment + "px";
			pointer.style.top =  alignmentY + "px";
		}	
        changeOpacity(id, 0, 100, 250); 
    }
    else  //fade in
    {
		changeOpacity(id, 100, 0, 200); 
		
    	// move the div out of the way
		if(pointer)	
		{
			pointer.style.left = "-1000px";
			pointer.style.top = "-1000px";
		}	
    }
} 

function changeOpacity(id, opacStart, opacEnd, millisec)
{ 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) //fade out
    { 
        for(i = opacStart; i >= opacEnd; i--) 
        { 
            setTimeout("setOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
    else if(opacStart < opacEnd) //fade in
    { 
        for(i = opacStart; i <= opacEnd; i++) 
        { 
            setTimeout("setOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function setOpac(opacity, id) 
{ 
    var object = (document.all) ? document.all[id] : document.getElementById(id); 

    if(object)
    {

		if(object.style.filter != null)  //MSIE
			object.style.filter = "alpha(opacity=" + opacity + ")"; 
			

		object.style.opacity = (opacity / 100); 	
    }
   
} 

