function addLoadEvent(func) {

	var oldonload = window.onload;

	if (typeof window.onload != 'function') {

		window.onload = func;

	} else {

		window.onload = function() {

			oldonload();

			func();

		}
	}
}

function insertAfter(newelement,existingelement) {

	var parentelement = existingelement.parentNode;

	if (parentelement.lastChild == existingelement) {

		return parentelement.appendChild(newelement);

	} else {

		return parentelement.insertBefore(newelement,existingelement.nextSibling);

	}
}

/*

makeSlideshow is triggered by another function (see, for example, archivedSites.js).
This function takes three arguments.
The first argument is an array of information.
The function uses this information to create a <div> and an <img>.
These are then instered into the document.
The second argument is an array of coordinates.
The function uses this information to add onmouseover behaviour to a list of links.
The mouseover behaviour invokes the slideElement function (see below).
The third argument is the link list itself.
If that link list doesn't exist, the function returns false.

*/

function makeSlideshow(details,coords,linkholder) {

	if (!document.getElementById) return false;

	if (!document.getElementById(linkholder)) return false;

	var pane = document.createElement('div');
	pane.style.width = details['width'];
	pane.style.height = details['height'];
	pane.style.overflow = 'hidden';
	pane.className = 'screenthumb';

	pane.style.position = 'relative';

	var pic = document.createElement('img');
	pic.setAttribute('id',details['id']);
	pic.setAttribute('src',details['image']);
	pic.setAttribute('alt','');
	pic.style.border = '0';

	pic.style.position = 'absolute';

	pane.appendChild(pic);

	var linklist = document.getElementById(linkholder);

	linklist.parentNode.insertBefore(pane,linklist);

	var lnks = linklist.getElementsByTagName('a');

	for (var i=0;i<lnks.length;i++) {

		var linktext = lnks[i].childNodes[0].nodeValue;

		if (coords[linktext]) {
	
			lnks[i].elementId = details['id'];
			lnks[i].x = coords[linktext][0];
			lnks[i].y = coords[linktext][1];
			lnks[i].sliding = null;
	
			lnks[i].onmouseover = function() {
	
				slideElement(this.elementId,this.x,this.y,6);
	
			}
	
			lnks[i].onfocus = lnks[i].onmouseover;

		}
	}
}

/*

This function changes the 'left' and 'top' properties of an element.
The function takes four arguments.
The first argument is the ID of the element to be moved.
The second argument is the 'left' position that the element should be moved to.
The third argument is the 'top' position that the element should be moved to.
The fourth argument is the increment.
This determines how much the element moves each time.
For instance, sending 2 as the increment will halve the distance travelled by the element each time.
This function invokes itself (with a delay) until the element has reached the desired coordinates.

If you'd like to use this function yourself, go ahead.
Drop me a line to let me know where I can see it in action:
http://adactio.com/contact/

*/


function slideElement(elementId,x,y,inc) {

	if (!document.getElementById) return false;
	if (!document.getElementById(elementId)) return false;

	var element = document.getElementById(elementId);

	if (element.sliding) clearTimeout(element.sliding);
	
	if (!element.xpos) element.xpos = 0;
	if (!element.ypos) element.ypos = 0;

	if (element.xpos == x && element.ypos == y) return true;

	if (element.xpos > x) {

		var dist = Math.ceil((element.xpos-x)/inc);
		element.xpos = element.xpos - dist;

	}

	if (element.xpos < x) {

		var dist = Math.ceil((x-element.xpos)/inc);
		element.xpos = element.xpos + dist;

	}

	if (element.ypos > y) {

		var dist = Math.ceil((element.ypos-y)/inc);
		element.ypos = element.ypos - dist;

	}

	if (element.ypos < y) {

		var dist = Math.ceil((y-element.ypos)/inc);
		element.ypos = element.ypos + dist;

	}

	element.style.left = element.xpos+'px';
	element.style.top = element.ypos+'px';

	element.sliding = setTimeout('slideElement("'+elementId+'",'+x+','+y+','+inc+')',10);

}


