var HolviksShowcase = Class.create({
	initialize: function(content, options){
		
		this.options = {
			onStageDuration: 3000,
			initialStartDelay: 0,
			backstageSlideOutDuration: 750,
			manualBackstageSlideOutDuration: 400,
			backstageSlideOutCurve: "ease-out",
			stageFadeInDuration: 750,
			manualStageFadeInDuration: 400,
			stageFadeInCurve: "ease-out",
		};
		
		Object.extend(this.options, options || {});
		
		this.inInAnimation=false;
		this.isInStageAnimation=false;
		this.isInBackStageAnimation=false;
		this.content = $(content);
		this.stage=this.content.select(".stage")[0];
		this.actors=this.stage.select("div");
		this.backstage=this.content.select("div.backstage div")[0];
		this.thumbnails=this.backstage.select("a");
		
		this.setBackStage();
		this.startShowTimer();
	},
	setBackStage: function(){
		
			this.thumbnailHeight=this.thumbnails[0].offsetHeight;
			this.currentIndex=1;
			this.currentActorIndex = 1;
			this.maxIndex=this.actors.length-1;
			
			var B=this.thumbnailHeight;
			
			this.thumbnails.each(function(D,C){
				
				D.setStyle({'top': -64 + ((C-1)*B) + 'px' });
			
			});
			
			//this.thumbnails[0].style.bottomInPx(B*(this.thumbnails.length-1));
			this.thumbnails[0].setStyle({'top': ((this.thumbnails.length-1)*B) + 'px' });
			this.thumbnails[0].hide()
		
	},
	startDelay: function(E,C)
	{
		var B=this;
		var D=new Object();
		D.method=E;
		D.duration=C;
		D.currentTime=new Date();
		D.paused=false;
		D.timeout=setTimeout(function(){B[E]()}.bind(B),C);
		return D
	},
	pauseDelay: function(B)
	{
		if(B)
		{
			clearTimeout(B.timeout);
			B.paused=true;
			B.pausedAt=new Date();
			return B
		}
	},
	resumeDelay: function(C,B)
	{
		if(B==null)
		{
			B=0
		}
		var D=C.duration-(C.pausedAt-C.currentTime);
		D=Math.max(D,0);
		D=D+B;
		return(this.startDelay(C.method,D))
	},
	runShow: function(){
		
		if(this.showTimer)
		{
			this.showLoop(1,true);
		}
		
		this.startShowTimer();
	},
	showLoop: function(A,B)
	{
		this.nextCurrentIndex(A);
		this.startingShowLoop();
		this.updatePointers();
		this.introduceNextActor(B);
		this.slideOutThumbnail(B)
	},
	introduceNextActor: function(C)
	{
		this.isInStageAnimation = true;
		var A=C?this.options.stageFadeInDuration:this.options.manualStageFadeInDuration;
		var B=this.options.stageFadeInCurve;
			
		//this.actors.except(this.currentActor,function(D){D.style.opacity=0});
		
		this.actors.each(function(D, I){
		
			if(I != this.currentActorIndex)
			{
				D.hide();
			}
				D.style.zIndex="99";
		
		}.bind(this));
		
			//this.currentActor.show();
			
		//this.nextActor.hide();
		this.nextActor.style.zIndex="100";
		this.nextActor.appear({ duration: .750 });
		//this.nextActor.style.animation_transition("opacity",A,B);
		//this.nextActor.style.opacity=1;
		//this.nextActor.style.pointerEvents="auto"
	},
	updatePointers: function()
	{
		
		//console.log(this.currentActorIndex);
		//this.currentAcorIndex = this.loopingIndex(this.currentActorIndex,0);
		//var test = this.currentActorIndex;
		this.currentActorIndex = this.currentIndex;
		this.currentActor = this.actors[this.currentActorIndex];
		
		this.nextActorIndex = this.loopingIndex(this.nextActorIndex,1);
		this.nextActor = this.actors[this.nextActorIndex];
		
	//	console.log(this.nextActorIndex);
		
		this.lastActorThumbnailIndex = this.loopingIndex(this.lastActorThumbnailIndex,0);
		this.lastActorThumbnail = this.thumbnails[this.lastActorThumbnailIndex];
		this.currentActorThumbnailIndex = this.loopingIndex(this.currentActorThumbnailIndex,1);
		this.currentActorThumbnail = this.thumbnails[this.currentActorThumbnailIndex];
		this.currentThumbnailIndex = this.loopingIndex(this.currentThumbnailIndex,2);
		this.currentThumbnail = this.thumbnails[this.currentThumbnailIndex];
	},
	loopingIndex: function(A,B)
	{
		return this.roundToIndex(this.currentIndex+B);
	},
	startingShowLoop: function()
	{
		this.inInAnimation=true
	},
	startShowTimer: function()
	{
		this.showTimer=this.startDelay("runShow",this.options.onStageDuration);
	},
	stopShowTimer: function()
	{
		this.pauseDelay(this.showTimer);
	},
	nextCurrentIndex: function(A)
	{
		this.previousIndex = this.currentIndex;
		this.currentIndex = this.roundToIndex(this.currentIndex+A);
	},
	afterIntroduceNextActor: function(A)
	{
		this.isInStageAnimation = false;
	},
	direction: function(A)
	{
		var B = this.currentIndex - this.previousIndex;
			
		if(B<-1)
		{
			B=1;
		}
			
		if(B>1)
		{
			B=-1;
		}
			
		return B;
	},
	finishedShowLoopPart: function()
	{
		this.finishedShowLoop()
	},
	finishedShowLoop: function(){
		this.inInAnimation=false;
	},
	slideOutThumbnail: function(B)
	{
		var self = this;
		this.isInBackStageAnimation=true;
			
		var A=B?this.options.backstageSlideOutDuration:this.options.manualBackstageSlideOutDuration;
		
		//this.backstage.style.animation_transition("transform",A,this.props.backstageSlideOutCurve);
					
		if(this.direction()<0)
		{
			this.currentThumbnail.show();
			this.currentThumbnail.setStyle({'top': '64px'});
		}
			
			var val = (this.direction()*this.thumbnailHeight);
			new Effect.Move(this.backstage, { 
				x: 0, 
				y: val,
				duration: 0.9,
				transition: Effect.Transitions.EaseFromTo,
				afterFinish: function()
				{
					var C=self.thumbnailHeight;
					var D=self.currentThumbnailIndex;
					var BB=self.maxIndex;
					
					self.backstage.setStyle({'top':'-64px'});
					
					self.thumbnails.each(function(E,F)
					{
						var G=F-D;
						
						if(G<0)
						{
							G=(BB-Math.abs(G)+1)
						}
						
						E.setStyle({'top': (C*G) + 'px'});
						
						self.lastActorThumbnail.show();
						self.currentActorThumbnail.hide();
						self.isInBackStageAnimation=false;
						self.finishedShowLoopPart();
					});

				} 
			});
			
		
		
	},
	roundToIndex: function(A)
	{
		var B=A%(this.maxIndex+1);
		if(B<0)
		{
			B=this.maxIndex+B+1
		}
		return B;
	}
});

