var bt = {};

var Home = Class.create({

	initialize: function() {

		this.initEpRotation();	
		
	},

	initEpRotation: function()
	{	
		this.numOfItems = 5; 
		this.so = null;
		this.isRotating = true;
		this.rotationSpeed = 10000;
		this.rotationImages = new Array("btMainContainer1", "btMainContainer2", "btMainContainer3", "btMainContainer4", "btMainContainer5");
		this.rotationLeftCont = new Array("btLeftContainer1","btLeftContainer2","btLeftContainer3", "btLeftContainer4", "btLeftContainer5");
		this.rotationButtons = new Array("btn1", "btn2", "btn3", "btn4", "btn5");

		this.currentEp = 0;
		this.nextEp = 0;
		this.neutralEp = 2;
						
		this.resetEpRotation();
		this.addButtonListeners();
		this.adjustRotateButtons();
		
		this.timer = window.setTimeout(this.rotateFullEp.bindAsEventListener(this), this.rotationSpeed);			
	},
	
	addButtonListeners: function ()
	{
		for(var i = 0; i < this.numOfItems; i++) {
			$( this.rotationButtons[i] ).observe('click', this.onButtonClicked.bindAsEventListener(this, i));
		}
	},
	
	onButtonClicked: function (event, contentId)
	{
		Event.stop(event);
		this.stopRotation();
		this.activateContent(contentId);
		return false;
	},

	activateContent: function (contentId)
	{
		var outId = this.currentEp;
		
		var outEp = this.rotationImages[this.currentEp];
		var inEp = this.rotationImages[contentId];
			
		this.currentEp = contentId;
		this.adjustRotateButtons();
			
		$(inEp).setStyle({
			'zIndex': this.numOfItems - 1,
			'visibility': 'visible'
		});
		$(outEp).setStyle({
			'zIndex': this.numOfItems
		});

		this.playFade(outEp, inEp, outId, contentId);	
		
		for (var i = 0; i < this.numOfItems; i++) {
			$(this.rotationLeftCont[i]).hide();
		}
			
		$(this.rotationLeftCont[contentId]).show();
		$(this.rotationLeftCont[contentId]).setStyle({'visibility':'visible','height':'auto'});
	},
	
	stopRotation: function() {
		this.isRotating = false;
		this.timer = window.clearTimeout(this.timer);		
	},

	playFade: function (imgFrom, imgTo, outEp, inEp)
	{
		var alphaSpeed = 0.3;

			this.fadeContent(imgFrom, alphaSpeed);
			this.appearContent(imgTo, alphaSpeed);
		
	},
	
	fadeContent: function(content, duration)
	{
		new Effect.Fade(content, {
			duration: duration, 
			queue: 'end', 
			limit: 1, 
			afterFinish: function(){
			}.bind(this)
		});			
	},
	
	appearContent: function(content, duration) 
	{
		new Effect.Appear(content, {
			duration: duration, 
			queue: 'end', 
			limit: 1, 
			afterFinish: function(){
			}.bind(this)
		});		
	},

	rotateFullEp: function ()
	{	
		if (this.isRotating) {
			this.updateEpIdentifiers();

			this.setRotationContent(this.rotationImages, true);
			this.setRotationContent(this.rotationLeftCont, false);		
		
			this.currentEp = this.nextEp;			
			this.adjustRotateButtons();
		
			this.timer = window.setTimeout(this.rotateFullEp.bindAsEventListener(this), this.rotationSpeed);
		}	
	},

	setRotationContent: function (contents, animate) {
		var outContent = contents[this.currentEp];
		var inContent = contents[this.nextEp];		
		var neutralContent = contents[this.neutralEp];
		
		$(neutralContent).setStyle({'zIndex': this.numOfItems - 2}); //1});
		$(inContent).setStyle({'visibility':'visible','zIndex': this.numOfItems - 1}); //2});
		$(outContent).setStyle({'zIndex': this.numOfItems}); //3});

		if (animate) {
			this.playFade(outContent, inContent, outContent, inContent, false);
		} else {
			for(var i = 0; i < this.numOfItems; i++) {
				$(contents[i]).hide();
			}
			$(contents[this.nextEp]).show();
			$(contents[this.nextEp]).setStyle({'visibility':'visible', 'height':'auto'});
		}

	},

	resetEpRotation: function ()
	{
		for (var i = 0; i < this.numOfItems; i++)
		{
			$(this.rotationImages[i]).hide();
			$(this.rotationLeftCont[i]).hide();
		}
		$(this.rotationImages[0]).show();
		$(this.rotationLeftCont[0]).show();
			
		this.currentEp = 0;
		this.nextEp = 0;
		this.neutralEp = 2;		
	},	
	
	updateEpIdentifiers: function()
	{
		if(this.currentEp == 0) {
			this.resetEpRotation();
		} else {
			this.neutralEp = this.currentEp-1;
		}
		
		if(this.currentEp == this.numOfItems - 1) {
			this.nextEp = 0;
		} else {
			this.nextEp++;
		}		
	},
	
	adjustRotateButtons: function ()
	{	
		for(var i=0; i < this.numOfItems; i++)
		{
			var tmpBtn = this.rotationButtons[i];
			if($(tmpBtn).hasClassName("on")) {
				$(tmpBtn).removeClassName("on");
			}
		}
		
		var currentBtn = this.rotationButtons[this.currentEp];
		$(currentBtn).addClassName("on");
	}

});

function initpage(){
	var numOfTsEntries = 5;
	bt.base = new Base(numOfTsEntries);
	bt.formValidator = new FormValidator();
	bt.home = new Home();
	
}

document.observe("dom:loaded",initpage);