/* ****************************************************************************
          Denne scriptfil indeholder motoren til en billedrotator,
           som kan etableres med webparten 'Indholdsredigering'.

                       NB: DENNE FIL MÅ IKKE SLETTES
          ========================================================
		                   Ole Clausen - 2010
**************************************************************************** */

AESPictureRotator.easingType = "SineEaseInOut";

(function(){
	function Animator(/* nFramesSec */){
		this._interval = typeof arguments[0]=="number" ? 1000/arguments[0] : 20;
	};
	var p = Animator.prototype;
	p.onchange = function(){};
	p.onfinish = function(){};
	p._startTime = 0;
	p._timer = null;
	p._tweens = null;
	p.start = function() {
		var me = this;
		clearInterval(this._timer);
		this._loadTweens(arguments);
		this._startTime = this._time();
		this._timer = setInterval(function(){me._anim()}, this._interval);
		this._stopTime = null;
	};
	p.stop = function() {
		clearInterval(this._timer);
		this._stopTime = this._time()-this._startTime;
	};
	p.resume = function() {
		if (!this._stopTime) return;
		var me = this;
		this._startTime = this._time() - this._stopTime;
		this._timer = setInterval(function(){me._anim()}, this._interval);
		this._stopTime = null;
	};
	p.dispose = function() {
		var x = null;
		this._emptyTweens();
		for (x in this) {
			if (typeof this[x]=="function") this[x] = function(){};
			else {
				this[x] = null;
				delete this[x];
			}
		}
	};
	p._time = function() {
		return new Date().getTime();
	};
	p._loadTweens = function(aTweens) {
		var oT = aT = null;
		if (this._tweens) this._emptyTweens();
		else this._tweens = [];
		for (var i=0,j=aTweens.length; i<j; i++) {
			oT = aTweens[i];
			aT = oT[1].split("Ease");
			this._tweens[i] = {
				name:oT[0],
				type:aT[0],
				begin:oT[2],
				change:oT[3],
				dur:oT[4],
				ease:Easing[aT[0]]["ease"+aT[1]],
				enabled:true,
				prev:null
			};
			if (aT[0]=="Elastic") {
				if (typeof oT[5]!=undefined) this._tweens[i].ampl = oT[5];
				if (typeof oT[6]!=undefined) this._tweens[i].period = oT[6];
			}
			if (aT[0]=="Back" && typeof oT[5]!=undefined) this._tweens[i].over = oT[5];
		}
	}
	p._anim = function() {
		var oT = oRet = null, nNew = 0, nTime = this._time();
		for (var x=i=0,j=this._tweens.length; i<j; i++) {
			oT = this._tweens[i];
			if (!oT.enabled) continue;
			if (nTime-this._startTime>=oT.dur && oT.enabled) {
				nNew = oT.begin + oT.change;
				if (nNew!=oT.prev) {
					if (!oRet) oRet = {};
					oRet[oT.name] = nNew;
					oT.prev = nNew;
				}
				oT.enabled = false;
			}
			else if (oT.enabled) {
				nNew = Math.round(oT.ease(nTime-this._startTime, oT.begin, oT.change, oT.dur));
				if (nNew!=oT.prev) {
					if (!oRet) oRet = {};
					oRet[oT.name] = nNew;
					oT.prev = nNew;
				}
				x++;
			}
		}
		if (oRet) this.onchange(oRet);
		if (x<1) {
			this.stop();
			this.onfinish();
		}
	};
	p._emptyTweens = function() {
		var x = null;
		for (var i=this._tweens.length-1; i>=0; i--) {
			for (x in this._tweens[i]) {
				if (typeof this._tweens[i][x]=="function") this._tweens[i][x] = function(){};
				else {
					this._tweens[i][x] = null;
					delete this._tweens[i][x];
				}
			}
			delete this._tweens[i];
		}
	};

/* ****************************************************************************
	Easing object.
	
	Parameters:
		*) nT : (Number) Current time (now-time minus start-time)
		*) nB : (Number) Begin value
		*) nC : (Number) Change in value
		*) nD : (Number) Duration
		
	  Elastic methods only:
		*) nA : (Number) Amplitude [Optional]
		*) nP : (Number) Period [Optional]
		
	  Back methods only:
		*) nO : (Number) Overshoot [Optional]
	
	  Credits:
		Easing Equations by Robert Penner
		- http://www.robertpenner.com/easing/
		- modified and translated into JS

**************************************************************************** */
var Easing={},a=["Linear","Quad","Cubic","Quart","Quint","Expo","Sine","Circ","Bounce","Elastic","Back"],
aa=[[function(t,b,c,d){return c*t/d+b},function(t,b,c,d){return c*t/d+b},function(t,b,c,d){return c*t/d+b}],
[function(t,b,c,d){return c*(t/=d)*t+b},function(t,b,c,d){return -c *(t/=d)*(t-2) + b},function(t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return -c/2*((--t)*(t-2)-1)+b}],
[function(t,b,c,d){return c*(t/=d)*t*t+b},function(t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},function(t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b}],
[function(t,b,c,d){return c*(t/=d)*t*t*t+b},function(t,b,c,d){return -c*((t=t/d-1)*t*t*t-1)+b},function(t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return -c/2*((t-=2)*t*t*t-2)+b}],
[function(t,b,c,d){return c*(t/=d)*t*t*t*t+b},function(t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},function(t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b}],
[function(t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},function(t,b,c,d){return (t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},function(t,b,c,d){if(t==0)return b;if(t==d)return b+c;if((t/=d/2)<1)return c/2*Math.pow(2,10*(t-1))+b;return c/2*(-Math.pow(2,-10*--t)+2)+b}],
[function(t,b,c,d){return -c*Math.cos(t/d*(Math.PI/2))+c+b},function(t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},function(t,b,c,d){return -c/2*(Math.cos(Math.PI*t/d)-1)+b}],
[function(t,b,c,d){return -c*(Math.sqrt(1-(t/=d)*t)-1)+b},function(t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},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}],
[function(t,b,c,d){return c-Easing.Bounce.easeOut(d-t,0,c,d)+b},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},																																																																																																		function(t,b,c,d){if(t<d/2)return Easing.Bounce.easeIn(t*2,0,c,d)*.5+b;else return Easing.Bounce.easeOut(t*2-d,0,c,d)*.5+c*.5+b}],
[function(t,b,c,d,a,p){if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(!a||a<Math.abs(c)){ a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return -(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p ))+b},function(t,b,c,d,a,p){if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(!a||a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return(a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p )+c+b)},function(t,b,c,d,a,p){if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(!a||a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return -.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p ))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p )*.5+c+b}],
[function(t,b,c,d,o){if(typeof o==undefined)o=1.70158;return c*(t/=d)*t*((o+1)*t-o)+b},function(t,b,c,d,o){if(typeof o==undefined)o=1.70158;return c*((t=t/d-1)*t*((o+1)*t+o)+1)+b},function(t,b,c,d,o){if(typeof o==undefined)o=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((o*=(1.525))+1)*t-o))+b;return c/2*((t-=2)*t*(((o*=(1.525))+1)*t+o)+2)+b}]];for(var i=aa.length-1;i>=0;i--)Easing[a[i]]={easeIn:aa[i][0],easeOut:aa[i][1],easeInOut:aa[i][2]};


	var x, css, oImg, aImgElms = [], nActivePic = 0,
	aPics = AESPictureRotator.pictures.replace(/ /g,"").split(","),
	sPath = AESPictureRotator.pathToPictureFolder.replace(/\/?$/,"/"),
	oPicHolder = document.getElementById("AESPicHolder"),
	sLastPic = sPath+aPics.pop(),
	oStyles = {
		position: "absolute",
		top: 0,
		left: 0,
		margin: 0,
		filter: "alpha(opacity=100)",
		opacity: 1,
		mozOpacity: 1
	};
	
	while(oPicHolder.firstChild) oPicHolder.removeChild(oPicHolder.firstChild);
	
	for (var i=aPics.length-1; i>=0; i--) {
		oImg = document.createElement("img");
		oImg.setAttribute("src", sPath + aPics[i]);
		css = oImg.style;
		for (x in oStyles) css[x] = oStyles[x];
		css.width = AESPictureRotator.width + "px";
		css.height = AESPictureRotator.height + "px";
		oPicHolder.appendChild(oImg);
		aImgElms[i] = oImg;
	}
	css = oPicHolder.style
	css.background = "url("+sLastPic+") no-repeat";
	css.position = "relative";
	css.width = AESPictureRotator.width + "px";
	css.height = AESPictureRotator.height + "px";
	
	if (AESPictureRotator.clickURL.replace(/\s/g,"").length>0) {
		var oWin = null;
		oPicHolder.style.cursor = "pointer";
		oPicHolder.onclick = function () {
			if (AESPictureRotator.openInNewWindow) {
				oWin = window.open(AESPictureRotator.clickURL,"AESPicRotatorPop");
			}
			else window.location.href = AESPictureRotator.clickURL;
		}
	}
	
	if (AESPictureRotator.toolTip && AESPictureRotator.toolTip.replace(/\s/g,"").length>0) {
		oPicHolder.setAttribute("title", AESPictureRotator.toolTip);
	}
	
	var oAnim = new Animator();
	
	oAnim.onfinish = function() {
		var css = aImgElms[nActivePic].style;
		if (css.opacity<=0.01) {
			css.filter = "alpha(opacity=0)";
			css.opacity = css.mozOpacity = 0;
		}
		if (nActivePic==0 && css.opacity>0.9) {
			for (var i=0,j=aImgElms.length; i<j; i++) {
				css = aImgElms[i].style;
				css.filter = "alpha(opacity=100)";
				css.opacity = css.mozOpacity = 1;
			}
			nActivePic = -1;
		}
		if (aImgElms[++nActivePic]) setTimeout(function(){oAnim.start(['anim', AESPictureRotator.easingType, 100, -100, AESPictureRotator.crossFadeDuration*1000])}, AESPictureRotator.delay*1000);
		else {
			nActivePic = 0;
			setTimeout(function(){oAnim.start(['anim', AESPictureRotator.easingType, 0, 100, AESPictureRotator.crossFadeDuration*1000])}, AESPictureRotator.delay*1000);
		}
		//oAnim.dispose();
	}
	oAnim.onchange = function(oChange) {
		if (oChange.anim) {
			var o = aImgElms[nActivePic], n = oChange.anim;
			o.style.filter = "alpha(opacity="+n+")";
			o.style.mozOpacity = o.style.opacity = n/100;
		}
	}
	
	
	setTimeout(function(){oAnim.start(['anim', AESPictureRotator.easingType, 100, -100, AESPictureRotator.crossFadeDuration*1000])}, AESPictureRotator.initialDelay*1000);
	
})();
