//code info
var version = '1.1';
var timestamp = '11.12.2004-13:13';



//browser detection
var IE = document.all;


//mouse coordinates
var mouseX = 0;
var mouseY = 0;


//stores current opacity values for all popup items
var opacity  = new Array();

//stores fadeout timers for all popup items
var timers   = new Array();

//stores (display) delay timers for all hover popup items
var delays   = new Array();

//stores mouse tracker delays for all popup item
var trackers = new Array();

var locks    = new Array();
var fade_in_lock = 15232;
var fade_out_lock = 15219;

var toggles  = new Array();


//keeps track of the current z-index level
var zindex = 0;


//default display time for popups (in seconds)
var display_time = 5;

if (!IE)
	document.captureEvents(Event.MOUSEMOVE);
	
document.onmousemove = getMouseXY;
document.onkeydown = getKeycode;






function getMouseXY(e)
// grabs the current mouse position and places coordinates
// in mouseX and mouseY
{
//	if (IE)
//	{
//		mouseX = event.clientX + document.body.scrollLeft;
//		mouseY = event.clientY + document.body.scrollTop;
//	}
//	else
//	{
//		mouseX = e.pageX;
//		mouseY = e.pageY;
//	}  

//	if (mouseX < 0){mouseX = 0;}
//	if (mouseY < 0){mouseY = 0;}  

//	return true;
}

function getKeycode(e)
{
//	keycode = e.keyCode;

//	if (keycode =='192')
//		alert('popup.js by Novaurora\nversion ' + version + ' (' + timestamp + ')');
}


function getObj(name)
// cross-platform code used to grab page elements by their ID
//     name:  item ID (NOT class)
{
  if (document.getElementById)
  {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
        this.obj = document.all[name];
        this.style = document.all[name].style;
  }
  else if (document.layers)
  {
        this.obj = document.layers[name];
        this.style = document.layers[name];
  }
}



function drag(item)
// allows users to move specified page elements
//     item: ID (NOT class) of the page element to be moved
{

	if (timers[item])
	//checks to see if any 'fade out' timers are set for the
	//  current item. if there is a timer set, the timer is cleared
	//  and set to a 'paused' state.
	{
		clearTimeout(timers[item]);
		timers[item] = 'paused';	
	}
	
	//grabs the specifed item
	var x = new getObj(item);
	
	
	//gets the current x-coordinate for the item, which is returned as
	//  a string in the form of '150px' - the 'px' must be stripped
	//  from the string
	if (x.style.left != '')
	{
		xlen = x.style.left.length;
		xpos = x.style.left.substr(0,(xlen - 2));
		
		//calculates the 'x-gap' -- the distance between the mouse
		//  and the left edge of the item
		xgap = mouseX - xpos;
	}
	else
	{
		xgap = 0;
	}

	//gets the current y-coordinate for the item, which is returned as
	//  a string in the form of '150px' - the 'px' must be stripped
	//  from the string
	if (x.style.top != '')
	{
		ylen = x.style.top.length;
		ypos = x.style.top.substr(0,(ylen - 2));
		
		//calculates the 'y-gap' -- the distance between the mouse
		//  and the top edge of the item
		ygap = mouseY - ypos;
	}
	else
		ygap = 0;
			
		
	//sets dragging opacity
	alpha(item, 75);
	
	//calls mouse tracking
	trackMouse(item, xgap, ygap);
}



function drop(item)
// releases 'item' from the mouse
//     item: element ID to be dropped
{
	//checks to see if any fadeout timers had been suspended due to dragging
	if (timers[item] == 'paused')
		timers[item] = setTimeout("popdown('"+item+"')", display_time * 1000);

	//cancels mouse tracking		
	loseMouse(item);
	
	//restores item opacity
	alpha(item, 100);
}



function trackMouse(item, xgap, ygap)
// collects current mouse info and adjusts the position of 'item'
//     item:  element ID of the item to track/reposition
//     xgap:  space between the mouse and left edge of the item
//     ygap:  space between the mouse and top edge of the item
{
	var x = new getObj(item);
	
	//new position is set using the x and y gaps
	x.style.top = mouseY - ygap;
	x.style.left = mouseX - xgap;
	
	//sets next call to 'trackMouse()'
	trackers[item] = setTimeout("trackMouse('"+item+"', "+xgap+", "+ygap+")", 10);
}



function loseMouse(item)
// cancels mouse tracking
//   item:  element ID of the item being tracked
{
	//clears 'trackMouse()' timer
	clearTimeout(trackers[item]);
}



function hover_popup(item, delay, duration)
// displays a popup after the delay time has passed
//     item:  element ID to be poppped
//     delay:  delay before popup is called (in seconds)
//     duration:  (optional) amount of time to display popup before closing (in seconds)
{
	//set default display time
	if (duration == null)
		duration = 0;
	
	//sets a popup delay
	locks[item] = fade_in_lock;
	delays[item] = setTimeout("popup('"+item+"', " + duration + ")", delay * 1000);
}



function hover_popdown(item)
// closes popups called by 'hover_popup()'
//  item:  element ID to be popped
{
	var x = new getObj(item);
	
	//cancels any delays set to display the item (this is in case a mouse
	//  moved from the hover area before the delay time had lapsed)
	if (delays[item])
		clearTimeout(delays[item]);
	
	//this just checks to make sure that the item hasn't been closed already,
	//  or is in the process of closing
	if ((x.style.visibility == 'visible') && (opacity[item] == 100))
	{
		locks[item] = fade_out_lock;
		popdown(item);
	}
}



function popup(item, duration, x_adjust, y_adjust)
// function used to popup any page element
//     item:      item ID (NOT class)
//     duration:  amount of time (in seconds) to display popup,
//                set to '0' for infinite display.  defaults to
//                value of 'display_time' defined above.
{
	//set default display time
	if (duration == null)
		duration = display_time;


	//grab the item to be displayed
	var x = new getObj(item);

		
	switch (typeof(x_adjust))
	{
		case "string":
			x.style.left = x_adjust;
			break;
			
		case "number":
			x.style.left = mouseX + x_adjust;
			break;
			
		default:
			x.style.left = mouseX + 10;
	}
		
	switch (typeof(y_adjust))
	{
		case "string":
			x.style.top = y_adjust;
			break;
			
		case "number":
			x.style.top = mouseY + y_adjust;
			break;
			
		default:
			x.style.top = mouseY;
	}


	//set item's z-index to top-most value
	x.style.zindex = zindex++;


	//this shouldn't be needed, but it is...
	x.style.visibility = 'hidden';


	//display the item
	locks[item] = fade_in_lock;
	fade_in(item);
	
	
	//set a timeout for the popup (if specified)
	if (duration != 0)
		timers[item] = setTimeout("popdown('"+item+"')", duration * 1000);

}



function popdown(item)
// closes the popup
//     item: item ID to be closed (NOT class)
{
	locks[item] = fade_out_lock;
	fade_out(item);
}



function show(item, duration, fade)
// function used to popup any page element
//     item:      item ID (NOT class)
//     duration:  amount of time (in seconds) to display popup,
//                set to '0' for infinite display.  defaults to
//                value of 'display_time' defined above.
{
	//set default display time
	if (duration == null)
		duration = display_time;
		
	if (fade == null)
		fade = 'on';


	//grab the item to be displayed
	var x = new getObj(item);

	//this shouldn't be needed, but if it is...
	x.style.visibility = 'hidden';


	switch (fade)
	{
		case 'on':
			//display the item
			locks[item] = fade_in_lock;
			fade_in(item);
			break;
			
		case 'off':
			//display the item
			hard_in(item);
			break;
	}

	//set a timeout for the popup (if specified)
	if (duration != 0)
		timers[item] = setTimeout("hide('"+item+"')", duration * 1000);
}



function hide(item, fade)
// closes the popup
//     item: item ID to be closed (NOT class)
{
	if (fade == null)
		fade = 'on';

	switch (fade)
	{
		case 'on':
			//display the item
			locks[item] = fade_out_lock;
			fade_out(item);
			break;
			
		case 'off':
			//display the item
			hard_out(item);
			break;
	}
}



function toggle(item)
{

	switch (toggles[item])
	{
		case "on":
			hide(item, 'off');
			toggles[item] = 'off';
			break;
			
		case "off":
			show(item, 0, 'off');
			toggles[item] = 'on';
			break;
			
		default:
			show(item, 0, 'off');
			toggles[item] = 'on';
	}
}



function fade_in(item)
// adjusts the alpha values and visibility of a page element to
// produce a 'fade in' effect
//     item: item ID of the element to be 'faded out' (NOT class)
{

	if (locks[item] == fade_in_lock)
	{

		//grab the page element
		var x = new getObj(item);
		
		if (x.style.visibility == 'hidden')
		//this code block should execute on the first call to 'fade_in()'
		{
			//define and set the item's opacity to 0
			opacity[item] = 0;
			alpha(item, opacity[item]);

			//move the item to the top of the stack		
			x.style.zindex = zindex++;
			
			//display the element
			x.style.display = 'block';
			x.style.visibility = 'visible';
		}//end if
	
	
		if (opacity[item] < 100)
		//if the element is still partially transparent...
		{
		
			//adjust and set the opacity ten percent darker
			opacity[item] += 10;
			alpha(item, opacity[item]);
			
			//repeat 20 milliseconds later
			setTimeout("fade_in('"+item+"')", 20);
		}//end if
	}
		
}//end fade_in()



function fade_out(item)
// adjusts the alpha values and visibility of a page element to
// produce a 'fade out' effect
//     item: item ID of the element to be 'faded out' (NOT class)
{
	if (locks[item] == fade_out_lock)
	{
		//grabs the specified page element
		var x = new getObj(item);	

		if (opacity[item] > 0)
		//if you can still see the element...
		{
			//adjust and set the opacity ten percent lighter
			opacity[item] -= 10;
			alpha(item, opacity[item]);
			
			//repeat 20 milliseconds later...
			setTimeout("fade_out('"+item+"')", 20);
		}
		else
		//if the element has 0 opacity...
		{
			//cancel any pending requests for a fade_out
			clearTimeout(timers[item]);
			
			//officially hide the element and knock it's z-index down a notch
			x.style.visibility = 'hidden';
			x.style.display = 'none';
			x.style.zindex = --zindex;
			
			loseMouse(item);
		}	
	}
}



function hard_in(item)
// displays the item without any tansitions (no fading)
//     item: id of the element to be shown (NOT class)
{
	//grab the page element
	var x = new getObj(item);
	
	//set opacity to 100% (just in case)
	opacity[item] = 100;
	alpha(item, opacity[item]);
	
	
	//move the item to the top of the stack		
	x.style.zindex = zindex++;
	
	
	//display the element
	x.style.display = 'block';
	x.style.visibility = 'visible';
}



function hard_out(item)
// hides the 'item' without any transitions (no fading)
//     item: id of the element to be hidden (NOT class)
{
	//grabs the specified page element
	var x = new getObj(item);	


	//hide the element
	x.style.visibility = 'hidden';
	x.style.display = 'none';
	
	//knock it down a notch on the stack
	x.style.zindex = --zindex;
			
	//set opacity to 0% (so it plays nice with faders)
	opacity[item] == 0;
	alpha(item, opacity[item]);

	//stop mouse tracking (in case this is a draggable element)
	loseMouse(item);
}



function alpha(item, alpha_value)
// adjusts the specified item's opacity
//     item: item ID of the page element to be adjusted (NOT class)
//     alpha_value: opacity value from 0 to 100 (0 = invisible, 100 = fully visible)
{

	//grab the page element
	var x = new getObj(item);

	//set opacity
	if (IE)
		x.obj.filters.alpha.opacity = alpha_value;
	else
		x.style.opacity = alpha_value/100;
}
