/*
	ANIMATION
	
	(c) Copyright 2003 - Ruben Daniëls, Virtual Cowboys C.V.
*/

//Browser Detectie & Global Variables
var Timer, CURRENT;
var isIE55up = navigator.appVersion.indexOf("MSIE 5.5") != -1 || navigator.appVersion.indexOf("MSIE 6.") != -1;;
var isIE50 = navigator.appVersion.indexOf("MSIE 5.0") != -1;
var isIE50up = isIE50 || isIE55up;
var isIE5x = navigator.appVersion.indexOf("MSIE 5.") != -1;
var isMozilla = !document.layers && !document.all && navigator.userAgent.indexOf("Gecko/") != -1;

Animate = {
	sequences : [],
	register : function(o){
		return this.sequences.push(o) - 1;
	},
	findSeq : function(id){
		return this.sequences[id];
	},
	
	//Animation Modules
	left : function(oHTML, value){oHTML.style.left = value + "px";},
	top : function(oHTML, value){oHTML.style.top = value + "px";},
	width : function(oHTML, value, center){oHTML.style.width = value + "px";},
	height : function(oHTML, value, center){oHTML.style.height = value + "px";},
	
	mleft : function(oHTML, value){oHTML.style.marginLeft = value + "px";},
	
	scrollwidth : function(oHTML, value, center){
		oHTML.style.width = value + "px";
		oHTML.scrollLeft = oHTML.scrollWidth;
	},
	
	clipright : function(oHTML, value, center){
		oHTML.style.clip = "rect(auto, auto, auto, " + value + "px)";
		oHTML.style.marginLeft = (-1*value) + "px";
	},
	
	fade : function(oHTML, value){
		if(isIE50up) oHTML.style.filter = "alpha(opacity=" + parseInt(value*100) + ")";
		else if(isMozilla) oHTML.style.MozOpacity = value-0.000001;
		else oHTML.style.opacity = value;
	}
}

//Object container several AnimationSequences combining to a full Animation
function Animation(frameperiod){
	this.uniqueId = Animate.register(this);
	
	this.frames = [];
	this.sequence = [];
	//this.framerate = framerate;
	this.interval = frameperiod;//parseInt(1000/framerate);
	
	this.addScript = 
	this.addSequence = function(framenr, animseq){
		if(!this.frames[framenr]) this.frames[framenr] = [];
		this.frames[framenr].push(animseq);
	}
	
	this.stripFunction = function(str){
		q = str.replace(/^\s*function\s*\w*\s*\([^\)]*\)\s*\{/, "");
		q = q.replace(/\}$/m, "");
		return q;
	}
	
	this.compile = function(){
		var f = this.frames;
		var q = this.sequence;
		
		for(var i=0;i<f.length;i++){
			if(f[i]){
				for(var k=0;k<f[i].length;k++){
					//Sequence
					if(f[i][k].compile){
						if(!f[i][k].compiled) f[i][k].compile();
					
						for(var j=0;j<f[i][k].steps.length;j++){
							if(!q[i+j]) q[i+j] = [];
							q[i+j].push(this.stripFunction(f[i][k].method.toString()).replace(/value/g, f[i][k].steps[j]).replace(/oHTML/g, f[i][k].oHTMLName));
						}
					}
					//Script
					else{
						if(!q[i]) q[i] = [];
						q[i].push(this.stripFunction(f[i][k].toString()));
					}
				}
			}
		}

		//Finalize Sequence
		for(var i=0;i<q.length;i++)
			q[i] = new Function(q[i] ? q[i].join("\n") : "");
	}
	
	this.step = 0;
	this.timer = null;
	this.play = function(){
		clearInterval(this.timer);
		this.timer = setInterval("var o = Animate.findSeq(" + this.uniqueId + ");if(o.step < o.sequence.length){o.sequence[o.step++]()}else{if(o.onfinish) o.onfinish();o.stop()}", this.interval);
	}
	this.playreverse = function(){
		if(!this.step) this.step = this.sequence.length-1;
		clearInterval(this.timer);
		this.timer = setInterval("var o = Animate.findSeq(" + this.uniqueId + ");if(o.step >= 0){o.sequence[o.step--]()}else{if(o.onfinishreverse) o.onfinishreverse();o.stop()}", this.interval);
	}
	this.stop = function(){
		clearInterval(this.timer);
		this.timer = null;
		this.step = 0;
	}
	
	this.isPlaying = function(){
		return this.timer ? true : false;
	}
}

/*case 1:
elm.vSlidex+=scalex*Math.pow(sv,3);elm.vSlidey+=scaley*Math.pow(sv,3);break;
case 2:
elm.vSlidey+=scaley*Math.pow(((steps-count)+1)/steps,3);
if(Math.round(elm.vSlidex)==endx&&Math.round(elm.vSlidey)==endy)count=steps;
break;
case 3:
elm.vSlidex+=distx/steps;elm.vSlidey+=disty/steps;break;
case 4:
var sv=count/steps;
elm.vSlidex=endx*Math.pow(sv,3)+x1*3*Math.pow(sv,2)*(1-sv)+x2*3*sv*Math.pow(1-sv,2)+x3*Math.pow(1-sv,3);
elm.vSlidey=endy*Math.pow(sv,3)+y1*3*Math.pow(sv,2)*(1-sv)+y2*3*sv*Math.pow(1-sv,2)+y3*Math.pow(1-sv,3);*/

//Object containing the information of a single movement
function AnimationSequence(oHTML, type, fromValue, toValue, ease, frames, interval){
	this.uniqueId = Animate.register(this);
	
	this.setObject = function(oHTML){
		if(this.oHTML && this.oHTML.id == "o" + this.uniqueId) this.oHTML.id = "";
		this.oHTML = oHTML;
		if(!this.oHTML.id) this.oHTML.id = "o" + this.uniqueId;
		this.oHTMLName = "document.getElementById('" + this.oHTML.id + "')";
	}
	
	this.type = type;
	this.method = Animate[type];
	if(oHTML) this.setObject(oHTML);
	this.frames = isMozilla ? parseInt(frames) : frames;// /3
	this.interval = interval;
	this.ease = ease;
	
	this.steps = [];
	this.step = 0;
	this.timer = null;
	this.compiled = false;
	
	//Fill the steps - ease currently unsupported
	this.compile = function(ease){
		this.compiled = true;
		//for(var i=0;i<=this.frames;i++)
			//this.steps.push(fromValue + ((toValue - fromValue)/this.frames)*i);
		
		this.steps.push(fromValue);
		
		var steps = this.frames;
		var scalex = (toValue - fromValue+114)/((Math.pow(steps,2)+2*steps+1)/(4*steps));
		for(var i=0;i<=this.frames;i++){
			var sv = i/this.frames;
			//this.steps.push(scalex*Math.pow(sv,3));
			var value = scalex*Math.pow(((steps-i)+1)/steps,3);
			this.steps.push((this.steps[this.steps.length-1] || 0) + value);
			//if(Math.round(elm.vSlidex)==endx&&Math.round(elm.vSlidey)==endy)count=steps;
		}
		
		return this;
	}
	
	this.execute = function(step){
		this.method(this.steps[step]);
	}
	
	this.play = function(step, oHTML){
		this.stop();
		if(oHTML) this.setObject(oHTML);
		
		if(this.step >= this.steps.length || this.step < 0) this.step = 0;
		this.timer = setInterval("var o = Animate.findSeq(" + this.uniqueId + ");if(o.step < o.steps.length){o.method(o.oHTML, o.steps[o.step++])}else{if(o.onfinish) o.onfinish(o.oHTML);o.stop();}", this.interval);
	}
	
	this.reverseplay = function(oHTML){
		this.stop();
		if(oHTML) this.setObject(oHTML);
		
		if(this.step >= this.steps.length || this.step <= 0) this.step = this.steps.length - 1;
		this.timer = setInterval("var o = Animate.findSeq(" + this.uniqueId + ");if(o.step >= 0 && o.step < o.steps.length){o.method(o.oHTML, o.steps[o.step--])}else{if(o.onfinishreverse) o.onfinishreverse(o.oHTML);o.stop();}", this.interval);
	}
	
	this.stop = function(){
		clearInterval(this.timer);
	}
	
	this.reset = function(){
		this.stop();
		this.step = 0;
	}
}



/*
	Mozilla Whitespace workaround
*/

function getNextElement(o){
	do{o = o.nextSibling;}while(o && o.nodeType != 1);
	return o;
}

function getPreviousElement(o){
	do{o = o.previousSibling;}while(o && o.nodeType != 1);
	return o;
}

function fixIEPNG(){
	//Fix for IE PNG support
	if(!isIE50up) return false;

	var imgs = document.getElementsByTagName("img");
	for(var i=0;i<imgs.length;i++){
		if(imgs[i].src.match(/\.png$/)){
			//Disable PNG in IE 5.0
			if(isIE50) imgs[i].src = imgs[i].src.replace(/.png$/, ".gif");
			
			//Enable Alpha Filter for PNG in IE 5.5+
			else{
				imgs[i].style.width = imgs[i].offsetWidth;
				imgs[i].style.height = imgs[i].offsetHeight;
				imgs[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgs[i].src + "', sizingMethod='scale')";
				imgs[i].src = 'spacer.gif';
			}
		}
	}
}