// JavaScript Document
// Created: August 19th, 2008
// This is a base library that should be included on all sites that use JavaScript and the DOM
// It should be the first .js file linked to in an (X)HTML document


//This function allows the addition of any number of event handlers to the window.onload event
function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//This function allows you to insert an element node after another. Similar to insertBefore()
function insertAfter(newElement, targetElement)
{
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement)
	{
		parent.appendChild(newElement);
	}
	else
	{
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}
//This function allows you to add a CSS class to an element.  It will check to see if there is already a class assigned to the 
//element, and if so, will properly add a space before appending the second class name. 
function addClass(element, value)
{
	if(!element.className)
	{
		element.className = value;
	}
	else 
	{
		newClassName = element.className;//Extract the current class name
		newClassName += " "; //Add a space before inserting new class name 'value'
		newClassName += value;
		element.className = newClassName;
	}
}
//This function creates animation on a specified element.  The division by "10" is abrbitrary and can be adjusted to increase or 
//decrease the rate of animation.
function moveElement(elementID,final_x,final_y,interval) {
  if (!document.getElementById) return false;
  if (!document.getElementById(elementID)) return false;
  var elem = document.getElementById(elementID);
  if (elem.movement) {
    clearTimeout(elem.movement); //If the element undergoing animation already has a setTimeout ID then clear it first
  }
  if (!elem.style.left) {
    elem.style.left = "0px";
  }
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
  var xpos = parseInt(elem.style.left);
  var ypos = parseInt(elem.style.top);
  if (xpos == final_x && ypos == final_y) {
    return true;
  }
  if (xpos < final_x) {
    var dist = Math.ceil((final_x - xpos)/10);
    xpos = xpos + dist;
  }
  if (xpos > final_x) {
    var dist = Math.ceil((xpos - final_x)/10);
    xpos = xpos - dist;
  }
  if (ypos < final_y) {
    var dist = Math.ceil((final_y - ypos)/10);
    ypos = ypos + dist;
  }
  if (ypos > final_y) {
    var dist = Math.ceil((ypos - final_y)/10);
    ypos = ypos - dist;
  }
  elem.style.left = xpos + "px";
  elem.style.top = ypos + "px";
  var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
  elem.movement = setTimeout(repeat,interval); //Assign timeout ID as a property to check for dual setTimeouts on the same element
}