/*************************** Insert After function ************************************/
	
	function insertAfter (newElement, targetElement) {
		var parent = targetElement.parentNode;
		if (parent.lastChild == targetElement) {
			parent.appendChild(newElement);
		} else {
			parent.insertBefore(newElement, targetElement.nextSibling);
		}
	}	
	
/*************************** Get Next Element ************************************/

	function getNextElement(node) {
		if ( node.nodeType == 1 ) {
			return node;
		}
		if ( node.nextSibling ) { 
			return getNextElement(node.nextSibling);
		}
		return null;
	}

/*************************** Add Class ************************************/
	
	/*
		* The addClass function is passed to argument values, elemetn and value.  These contain the element
		  that is to have a class value attached and the value of that class.
		* If the element has no class then it is assigned the newly created class value.
		* Else the existing class value is detected and assigned to the variable newClassName.  The existing 
		  variable is then given a space and then the new class value is placed after the existing value.
		* The element is then assigned this new double class value stored in newClassName, through the className property.
	*/
	
	function addClass(element, value) {
		if ( !element.className) {
			element.className = value;
		} else {
			newClassName = element.className;
			newClassName+= " ";
			newClassName+= value;
			element.className = newClassName;
		}
	}

/*************************** Animation ************************************/


function moveElement(elementId, final_x, final_y, interval) {
	// Test for DOM capabilities	
		if ( !document.getElementById ) return false;
		if ( !document.getElementById(elementId) ) return false;
	
	// Get element to animate
		var elem = document.getElementById(elementId);
		
	// ClearTimeout will stop the animation on one particular element, allowing the next to be carried out.
	// The if statement is used to ensure that a movement has been carried out.
		if (elem.movement) {
			clearTimeout(elem.movement)
		}
										// Dont presume anything
		if (!elem.style.top) {			// If the element does not have a top position, provide it with one.
			elem.style.top = "0px";
		}
		if (!elem.style.left) {			// If the element does not have a left position, provide it with one.
			elem.style.left = "0px";
		}
	
	// Get current position of existing element.  "ParseInt" is used to remove the "px" from the retrieved coordinates
		var xpos = parseInt(elem.style.left); // 
		var ypos = parseInt(elem.style.top);
		
	// Find if the x and y coordinates are equal to the final coordinates, if so leave the function
		if ( xpos == final_x && ypos == final_y ) {
			return true;
		}
	
	// Move animation
		if ( xpos < final_x ) {					// If the x position is less the the final left position 
			var dist = Math.ceil(( final_x - xpos )/10);	// Subtract the x position from the final position and divide this total by 10, Assign this value to the "dist" variable
			xpos = xpos + dist;					// Recalculate x position by adding x position and the distance
		}
			
		if ( xpos > final_x ) {					// If the x position is more the the final left position decrese it by one
			var dist = Math.ceil(( xpos - final_x )/10);
			xpos = xpos - dist;
		}
	
		if ( ypos < final_y ) {	// If the y position is less the the final left position increase it by one
			var dist = Math.ceil(( final_y - ypos )/10);
			ypos = ypos + dist;
		}

		if ( ypos > final_y ) {	// If the y position is less the the final left position decrease it by one
			var dist = Math.ceil(( ypos - final_y )/10);
			ypos = ypos - dist;
		}
					
	// Applt the new x & y coordinates to the style property (px is added to the end as this was the original unit read in
		elem.style.left = xpos + "px";
		elem.style.top = ypos + "px";
		
	// Call the fuction again and pass it the same arguments (need to contained as a string)
	// Assign the concantenated string to a variable "repeat", this makes it easier to work with when dealing with the setTimeout function	
		var repeat = "moveElement('"+elementId+"', "+final_x+", "+final_y+", "+interval+")"; // Recursion (Calling the same function from within itself)
	
		// Set up the timeout function
		elem.movement = setTimeout(repeat, interval);

} 

/*************************** Cross Browser Add Event ************************************/
	
function trace( msg ){
  if( typeof( jsTrace ) != 'undefined' ){
    jsTrace.send( msg );
  }
}
