var ANIMATIONS = new Array ();
var	INTERVALS = new Array();
var TIMEOUTS = new Array();

function homeAnimation (source, startX, startY, borderLeft, borderRight, zOrder, interval, pixelSpeed, bounce, delay, lifeTime) {
	this.startX = startX;  						// X-Start position
	this.startY = startY;						// Y-Start position
	this.borderLeft = borderLeft;				// Left boundary
	this.borderRight = borderRight;				// Right boundary
	this.zOrder = zOrder;						// Z-order
	this.interval = interval;					// interval for animating
	this.speed = pixelSpeed;					// Speed in pixels per interval
	this.bounce = bounce;						// bouncing (true) or not (false)
	this.delay = delay;							// Time when to start (in msec)
	this.lifeTime = lifeTime;					// Time animating (in msec)
	this.HTML = "<img alt=\"\" SRC='" + source + "'>";	// img source
	this.container = null;
	return this;
	if ((navigator.platform.indexOf('Mac')==0) || ((navigator.appName=="Netscape")&&(parseInt(navigator.appVersion) > 4)) || ((navigator.appName.indexOf("Microsoft")!=-1)&&(parseInt(navigator.appVersion.substr(versionIndex, 5)) == 4)) ) {
		this.speed = this.speed * 2;
	}
}

function animContainer (animID) {
		switch(dom) {
			case W3C:
				this.div = document.createElement("DIV");
				this.div.style.position = "absolute";
				this.div.style.visibility = "hidden";
				this.div.setAttribute("id", "animContainer"+animID);
				document.body.appendChild(this.div);
				this.clipRight = function clipAnimContainer(clipRight) {this.div.style.clip = "rect(0px, " + clipRight + "px, 1000px, 0px)";}
				this.unclip = function unclipAnimContainer() {this.div.style.clip = "auto"}
				this.move = function moveAnimContainer(x,y) {this.div.style.left = x+"px"; this.div.style.top = y+"px"; this.x = x; this.y = y;}
				this.hide = function hideAnimContainer() {this.div.style.visibility = "hidden";}
				this.show = function showAnimContainer() {this.div.style.visibility = "visible";}
				this.setZ = function ZAnim(newZ) {this.div.style.zIndex = newZ;}
				this.div.innerHTML = ANIMATIONS[animID].HTML;
				this.width = this.div.offsetWidth;
				break;
			case IE:
				document.body.insertAdjacentHTML("afterBegin", "<DIV id='animContainer"+animID+"' style='position:absolute;visibility:hidden;'></DIV>");
				this.div = document.all["animContainer"+animID];
				this.div.insertAdjacentHTML("afterBegin", ANIMATIONS[animID].HTML);
				this.width = this.div.clientWidth;
				this.move = function moveAnimContainer(x,y) {this.div.style.left = x; this.div.style.top = y; this.x = x; this.y = y;}
				this.hide = function hideAnimContainer() {this.div.style.visibility = "hidden";this.div.style.display = "none";}
				this.show = function showAnimContainer() {this.div.style.visibility = "visible";this.div.style.display = "inline";}
				this.setZ = function ZAnim(newZ) {this.div.style.zIndex = newZ;}
				this.clipRight = function clipAnimContainer(clipRight) {this.div.style.clip = "rect(0 " + clipRight + " " + 1000 + " 0)";}
				this.unclip = function unclipAnimContainer() {this.div.style.clip = "rect(auto)"}				
				break;
			case NS:
				this.layer = new Layer(1);
				this.move = function moveAnimContainer(x,y) {this.layer.moveTo(x,y);this.y = y; this.x = x};
				this.hide = function hideAnimContainer() {this.layer.visibility = "hide";};
				this.show = function showAnimContainer() {this.layer.visibility = "show";};
				this.setZ = function ZAnim(newZ) {this.layer.zIndex = newZ;}
				this.clipRight = function clipAnimContainer(clipRight) {this.layer.clip.right = clipRight;}
				this.unclip = function unclipAnimContainer() {this.layer.clip.right = this.width;}
				this.layer.document.open();
				this.layer.document.write(ANIMATIONS[animID].HTML);
				this.layer.document.close();
				this.width = this.layer.clip.width;
		}
	return this;
}

function addAnimation (source, startX, startY, borderLeft, borderRight, zOrder, interval, pixelSpeed, bounce, delay, lifeTime) {
	var animID = ANIMATIONS.length;

	ANIMATIONS[animID] = new homeAnimation(source, startX, startY, borderLeft, borderRight, zOrder,  interval, pixelSpeed, bounce, delay, lifeTime);
}

function animateThis(animation) {
	var xPos = animation.container.x;
	var yPos = animation.container.y;
	var clipRight;
	
	if (animation.bounce) {
		if (((xPos + animation.container.width) > animation.borderRight) || xPos < animation.borderLeft) {
			animation.speed = -animation.speed;
		}
	}
	else {
		if ((xPos + animation.container.width) > animation.borderRight) {
			clipRight = animation.container.width - ((xPos + animation.container.width) - animation.borderRight)
			animation.container.clipRight(clipRight);
		}
		if (xPos > animation.borderRight) {
			xPos = (0 - animation.container.width);
			animation.container.unclip();
		}
		
	}
	animation.container.move(xPos + animation.speed, yPos);
}

function generateAnimations () {
	for (i=0;i < ANIMATIONS.length; i++) {
		ANIMATIONS[i].container = new animContainer(i);
		ANIMATIONS[i].container.setZ(ANIMATIONS[i].zOrder);
		ANIMATIONS[i].container.move(ANIMATIONS[i].startX,ANIMATIONS[i].startY);
		TIMEOUTS[i] = setTimeout("startAnimation(" + i + ")", ANIMATIONS[i].delay);
	}
}

function startAnimation(i) {
	ANIMATIONS[i].container.show();
	INTERVALS[i] = setInterval("animateThis(ANIMATIONS[" + i + "])", ANIMATIONS[i].interval)
	TIMEOUTS[i] = setTimeout("stopAnimation(" + i + ")", ANIMATIONS[i].lifeTime);
}

function stopAnimation(i) {
	ANIMATIONS[i].container.hide();
	clearInterval(INTERVALS[i]);
	clearTimeout(TIMEOUTS[i]);
}

function stopAllAnimations() {
	for (i=0;i<aNIMATIONS.length;i++) {
		stopAnimation(i);
	}
}

function restart() {
	stopAllAnimations();
	for (i=0;i<aNIMATIONS.length;i++) {
		ANIMATIONS[i].speed < 0 ? ANIMATIONS[i].speed = -ANIMATIONS[i].speed : ANIMATIONS[i].speed;
		ANIMATIONS[i].container.move(ANIMATIONS[i].startX,ANIMATIONS[i].startY);
		ANIMATIONS[i].container.unclip();
		setTimeout("startAnimation(" + i + ")", ANIMATIONS[i].delay);
	}
}

