/*
	Filename         :	mt.lib.animation.js
	Created by       :	Mark Thomas
	Last modified by :	Mark Thomas
	Created          :	28 April 2006 09:35:48
	Last Updated     :	28 April 2006 11:00:03
	Comments         :	Animation Object
*/

oMTLib.animation = function(o){
	
	this.nDelay = 20;
	
	this.initialise	= function(o){
		if(typeof document.aMTLibAnimations == "undefined") document.aMTLibAnimations = [];
		if(typeof o == 'string'){
			this.sID = o;
			o = document.getElementById(o);
		}else{
			this.sID = o.id || "animobject" + document.aMTLibAnimations.length
		};
		if(o.bInitalised) return document.aMTLibAnimations[this.sID];
		o.bInitalised = 1;
		this.dom.sID	= this.sID;
		this.obj 		= this.dom.obj = o;
		document.aMTLibAnimations[this.sID] = this;
		document.aMTLibAnimations[document.aMTLibAnimations.length] = this;
		return this;
	};

	this.animation = function(sStyle, sUnit, nStart, nEnd, nDuration, sType){
		if(this["_slide" + sStyle]) window.clearInterval(this["_slide" + sStyle]);
		var nTime = 0;
		var _self = this;
		sType = sType || "easeInOutQuad";
		nDuration = nDuration / this.nDelay;
		this["_slide" + sStyle] = window.setInterval(
			function(){
				if(_self.onanimate) _self.onanimate();
				_self.obj.style[sStyle] = _self[sType](nTime++, nStart, nEnd-nStart, nDuration) + sUnit;
				if(nTime >= nDuration){
					_self.obj.style[sStyle] = nEnd + sUnit;
					window.clearInterval(_self["_slide" + sStyle]);
					if(_self["onanimationend" + sStyle]) _self["onanimationend" + sStyle]();
				};
			}
			, this.nDelay);
	};
	
		this.linearTween  = function(t, b, c, d){
			return c*t/d+b;
		};
		this.easeInQuad = function(t, b, c, d){
			return c*(t/=d)*t + b;
		};
		this.easeOutQuad = function(t, b, c, d){
			return -c*(t /= d)*(t-2)+b;
		};
		this.easeInOutQuad = function(t, b, c, d){
			if((t /= d/2)<1) return c/2*t*t+b;
			return -c/2*((--t)*(t-2)-1)+b;
		};
		this.easeInSine = function(t, b, c, d){
			return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
		};
		this.easeOutSine = function(t, b, c, d){
			return c * Math.sin(t/d * (Math.PI/2)) + b;
		};
		this.easeInOutSine = function(t, b, c, d){
			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
		};
		this.easeInCirc = function(t, b, c, d){
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		};
		this.easeOutCirc = function(t, b, c, d){
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		};
		this.easeInOutCirc = function(t, b, c, d){
			if((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
			return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
		};
		this.easeInBounce = function(t, b, c, d){
			return c - this.easeOutBounce (d-t, 0, c, d) + b;
		};
		this.easeOutBounce = function(t, b, c, d){
			if((t/=d) < (1/2.75)){
				return c*(7.5625*t*t) + b;
			}else if(t < (2/2.75)){
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			}else if(t < (2.5/2.75)){
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			}else{
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		};
		this.easeInOutBounce = function(t, b, c, d){
			if(t < d/2) return this.easeInBounce(t*2, 0, c, d) * .5 + b;
			return this.easeOutBounce(t*2-d, 0, c, d) * .5 + c*.5 + b;
		};
	
	this.fade = function(nTime, bIn, nFinalOpacity){
		if(!nFinalOpacity) nFinalOpacity = 100;
		if(!bIn) bIn = 0;
		clearTimeout(this.oFadeTimer);
		if(typeof this.dom.nOpacity == "undefined") this.dom.nOpacity = 95;
		if(bIn && this.dom.nOpacity == nFinalOpacity){
			if(this.onfadeend) this.onfadeend();
			return;
		};
		if(!bIn && (this.dom.nOpacity == 0 || this.dom.nOpacity == nFinalOpacity)){
			if(this.onfadeend) this.onfadeend();
			return;
		};
		this.dom.setOpacity(this.dom.nOpacity + ((bIn)?5:-5));
		this.oFadeTimer = window.setTimeout("document.aMTLibAnimations['" + this.sID + "'].fade(" + nTime + ", " + bIn + ", " + nFinalOpacity + ")", nTime/20);
	};
	
	this.highlightFade = function(nTime, sStyle, aFadeColours){
		clearTimeout(this["oHighlightFadeTimer" + sStyle]);
		if(aFadeColours){
			this["aFadeColours" + sStyle] = aFadeColours;
			this["nFadeColourInc" + sStyle] = 0;
		};
		if(typeof this["nFadeColourInc" + sStyle] == "undefined") this["nFadeColourInc" + sStyle] = 0;
		this.obj.style[sStyle] = "#" + this["aFadeColours" + sStyle][this["nFadeColourInc" + sStyle]];
		if(this["nFadeColourInc" + sStyle] >= this["aFadeColours" + sStyle].length - 1){
			return;
		};
		this["nFadeColourInc" + sStyle] ++;
		this["oHighlightFadeTimer" + sStyle] = window.setTimeout("document.aMTLibAnimations['" + this.sID + "'].highlightFade(" + nTime + ", '" + sStyle + "')", nTime/this["aFadeColours" + sStyle].length);
	};
	
	this.collapse = function(nTime, bVertical, bHorizontal, sType){
		this.overflow 	= this.dom.getComputedStyle("overflow");
		this.obj.style.overflow = "hidden";
		if(!this.obj.nHeightStart){
			this.obj.nHeightStart = this.obj.offsetHeight;
			this.obj.nWidthStart = this.obj.offsetWidth;
		};
		if(bVertical){
			this.animation("height", "px", this.obj.offsetHeight, 0, nTime, sType);
		};
		if(bHorizontal){
			this.animation("width", "px", this.obj.offsetWidth, 0, nTime, sType);
		};
	};
	this.expand = function(nTime, sType){
		this.animation("height", "px", this.obj.offsetHeight, this.obj.nHeightStart, nTime, sType);
		this.animation("width", "px", this.obj.offsetWidth, this.obj.nWidthStart, nTime, sType);
	};
	
	this.dom ={
		setOpacity : function(nOpacity){
			this.obj.style.filter 		= "alpha(opacity=" + nOpacity + ")";
			this.obj.style.opacity 		= nOpacity/100;
			this.obj.style.visibility 	= (!nOpacity) ? "hidden" : "visible";
			this.nOpacity = nOpacity;
		},
		toggle	: function(){
			this.obj.style.display = (this.getComputedStyle("display") == "none") ? "" : "none";
		},
		getComputedStyle : function(sProperty){			
			if(document.defaultView){
				return document.defaultView.getComputedStyle(this.obj, null).getPropertyValue(sProperty);
			}else if(this.obj.currentStyle){
				sProperty = sProperty.replace(/-\D/gi, function(sMatch){
					return sMatch.charAt(sMatch.length - 1).toUpperCase();
				});
				return this.obj.currentStyle[sProperty];
			};
			return null;
		}
	};
	
	return this.initialise(o);

};