/*****************
 *
 * Created by Dylan Maxwell Reilly http://atariland.net .
 * Feel free to reuse so long as the comments and this header remain intact.
 *
 *****************/
 
var CMN_ANIM_STEP 	= 50;			//!< Animations run once every this many MS.
var CMN_FADE_TIMERS	= 100;
var cmnFadeIntr		= new Array(CMN_FADE_TIMERS);	//!< Fade animation function pointer. 
var cmnFadeTime		= new Array(CMN_FADE_TIMERS);	//!< Fade animation current time. 
for (var i = 0; i < CMN_FADE_TIMERS; i++) cmnFadeTime[i] = 0;

var cmnArVersion = navigator.appVersion.split("MSIE")
var cmnVersion 	 = parseFloat(cmnArVersion[1])
 
//! Browser type.
var ns4, ns6, ie4, ie5, ie6 = false;

if(document.body){
     // ie6 and ns6 are the same for me here
     ns6 = ie6 = true;
}
else if(document.getElementById){
    ie5 = true;
}
else if(document.all){
    ie4 = true;
}
else if(document.layers){
    ns4 = true;
}
else {
}

/*!
 * Returns the object reference for a object's style
 * takes a string of the object name as fist argument
 * and the window name as the second or null if this window
 */
function getStyle(obj) {
  if (ns4)
      return obj;
  else if ((ie5 || ie6 || ns6) && obj)
      return obj.style;
  else
  	return null;
}

/*!
 * Returns the object reference for a layer
 * takes a string of the layer name as first argument
 * and the window name as the second or null if this window
 */
function getLayer(name, base) {
  if (base == null)
      base = document;

  if (ns4)
      return base.layers[name];
  if (ie5 || ie6 || ns6)
      return base.getElementById(name);
}

/*!
 * Returns the object reference for an item (non layer)
 * takes a string of the layer name as first argument 
 * and the window name as the second or null if this window 
 */
function getItem(name, base) { 
  if (base == null) 
      base = document; 

  if (ns4) {
      return document.eval(name);
  }
  if (ie5 || ie6 || ns6) {
     return base.getElementById(name);
  }
} 

/*!
 * Returns a handle to the style of the given object. 
 * Same as calling (getStyle(getItem("thingy")).
 */
function getItemStyle(name)
{
	var item = getItem(name);
	if (item != null) return getStyle(item);
	return null;
}

/*!
 * Returns a reference to the style of an image. This takes into account the image
 * not actually being an image but also if the image is defined as a background in css.
 */
function getImageStyle(name)
{
	var item = document.images[name];
	if (item == null)
		item = getItemStyle(name);
	return item;
}

/*!
 * Returns the width of an image. A safe call. Handles width defined as a css property as well
 * and automatically removes and string formatting (i.e., "px").
 */
function getImageW(name)
{
	return parseInt(getImageStyle(name).width);
}

/*!
 * Returns the height of an image. A safe call. Handles height defined as a css property as well
 * and automatically removes and string formatting (i.e., "px").
 */
function getImageH(name)
{
	return parseInt(getImageStyle(name).height);
}

/*!
 * Returns the object reference for a layer
 * takes a string of the layer name as argument
 */
function getItemLayers(name) {
  // Netscape section
  if (ns4) {
      return eval('document.layers.' + name);
  }
  // NS6 section
  if (ns6) {
     return eval('document.getElementById("' + name + '").style');
  }
  // IE section
  if (ie4) {
      return eval('document.all.' + name);
  }
}

/*!
 * Returns the dimmensions of the current document as a Rect();
 * This many return the dimmensions of the browser window in some browsers. Oh well.
 * \return Dimmensions of the current document.
 */
function getDocRect()
{
	var dim = new Rect(0, 0, document.documentElement.clientWidth, document.documentElement.clientHeight);
	
	// Mozilla and etc.
	if(typeof(window.innerWidth) == 'number') {
		dim.Width	= window.innerWidth;
		dim.Height	= window.innerHeight;
	} 
	//IE 6+ in 'standards compliant mode'
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		dim.Width	= document.documentElement.clientWidth;
		dim.Height	= document.documentElement.clientHeight;
	} 
	//IE 4 compatible
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		dim.Width	= document.body.clientWidth;
		dim.Height	= document.body.clientHeight;
	}
  
	return dim;
}

/*!
 * Determines the current mouse position and returns is as an array.
 * \param e Mouse move event param.
 * \return Point() of current mouse position.
 */
function getMouseXY(e) {
	var pos = new Point(0,0);

	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	
	{
		pos.x = e.pageX;
		pos.y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	
	{
		pos.x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		pos.y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	return pos;
}

/*!
 * Squares the given value and returns it.
 */
function sqr(a)
{
	return a * a;
}

/*!
 * Point object.
 * \param x X coordinate.
 * \param y Y coordinate.
 */
function Point(x, y)
{
	this.x = x;
	this.y = y;
}

/*!
 * Rectangle object.
 * \param x X coordinate of upper left point.
 * \param y Y coordinate of upper left point.
 * \param width Rectangle width.
 * \param height Rectangle height.
 */
function Rect(x, y, width, height)
{
	this.x		= x;
	this.y		= y;
	this.width	= width;
	this.height	= height;
	
	//! Returns the x coordinate of the right edge of the rectangle.
	this.right = function()
	{
		return x + width;
	};
	
	//! Returns the y coordinate of the bottom edge of the rectangle.
	this.bottom = function()
	{
		return y + height;
	}
}

/******************************
 * moves DIV's about the screen
 ******************************/
function slide(area, xend, yend, step, speed) {

   var x = move(area, 'x', xend, step);
   var y = move(area, 'y', yend, step);

   if (x || y) {
      setTimeout("slide(area," + xend + "," + yend + "," + step + "," + speed + ")", 20);
   }
} 


/*!
 * helper function for slide
 */
function move(area, axis, end, step) {

   switch(axis) {
     case 'x':
      pos = parseInt(area.left);
      break;
     case 'y':
      pos = parseInt(area.top);
      break;
     return false;
   }

   if (pos < end) {
      pos += step;
      pos  = (pos > end)? end:pos;
   }
   else if (pos > end) {
      pos -= step;
      pos  = (pos < end)? end:pos;
   }
   else { return false; }

   switch(axis) {
     case "x":
      area.left = pos;
      return true;
     case "y":
      area.top  = pos;
      return true;
   }
}

/*!
 * Sets the opacity of the given element.
 * \param item Item to effect.
 * \param value opacity value.
 */
function setOpacity(item, value) {
	var style = getItemStyle(item);
	if (style == null) return;
	style.opacity = value / 10;					// Mozilla based browsers.
	style.filter = 'alpha(opacity=' + value * 10 + ')';	// IE.
}

/*!
 * Fades the given item using alpha over time from start to end.
 * \param item document element.
 * \param alphaS Starting alpha value.
 * \param alphaE Ending alpha value.
 * \param time Total time in ms over which to fade.
 * \param id Specify a fade handler id, or -1 to automatically find one.
 */
function alphaFade(item, alphaS, alphaE, time, id)
{
	var alphaC;
	// Nothing to do.
	if (alphaS == alphaE) return;
	// Break condition.
	if (cmnFadeTime[id] >= time) 
	{
		 window.clearInterval(cmnFadeIntr[id]);
		 cmnFadeTime[id] = 0;
		 return;
	}

	// Find an available id.
	if (id == null || id == -1)
	{
		for (var i = 0; i < CMN_FADE_TIMERS; i++)
		{
			if (cmnFadeTime[i] == 0) 
			{
				id = i;
			}
		}
	}
	
	if (id == -1) return id;
	
	if (cmnFadeTime[id] == 0)
		cmnFadeIntr[id] = window.setInterval("alphaFade('" + item + "'," + alphaS + "," + alphaE + "," + time + "," + id + ")", CMN_ANIM_STEP);

	// Incremnet current time counter for this fade.
	cmnFadeTime[id] += CMN_ANIM_STEP;
	// Calculate new alpha value via interpolation.
	alphaC = alphaS + (alphaE - alphaS) * (cmnFadeTime[id] / time);
	// Set the new opacity.
	setOpacity(item, alphaC);

	// Toggle visible/hidden valuse as appropriate.
	if (getItemStyle(item).visibility == "hidden" && alphaC != 0)
		getItemStyle(item).visibility = "visible";
	if (alphaC == 0)
		getItemStyle(item).visibility = "hidden";

	return id;
}

function abortFade(id)
{
	if (cmnFadeIntr[id] != null)
	{
		window.clearInterval(cmnFadeIntr[id]);
		cmnFadeIntr[id] = null;
		cmnFadeTime[id] = 0;
	}
				 
}

/*!
 * A rectangle.
 */
function Rectangle(x, y, dx, dy)
{
	this.x 	= x;
	this.y 	= y;
	this.dx 	= dx;
	this.dy 	= dy; 
	
	/*!
	 * Given a point as a Point, returns true if the point is inside the rectangle.
	 */
	this.contains = function(point)
	{
		return(point.x >= x && point.x <= x + dx && point.y >= y && point.y <= y + dy); 
	} 
}

/*!
 * Print something into a debug location. Requires that there exists an HTML form element with id "debug."
 * Appends the given string and adds a line break.
 * \param s String to print.
 */
function dprint(s)
{
	try
	{
		getItem("debug").value += s + "\n";
	}
	catch(Exception){ }
}

/*!
 * Allows PNG's with alpha layers to be properly displayed in IE 5/6.
 * Created by Fabrice Baranski, http://homepage.ntlworld.com/bobosola 
 */
function correctPNG() 
{

   if ((cmnVersion >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img 	= document.images[i]
         var imgName 	= img.src.toUpperCase()

         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID 	 = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 

            if (img.align == "left")    imgStyle = "float:left;" + imgStyle
            if (img.align == "right")   imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	    
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	    
            img.outerHTML = strNewHTML

            i = i-1
         }
      }
   }    
}
if (cmnVersion >= 5.5) window.attachEvent("onload", correctPNG);

