var ydi = {};

var Home = Class.create({

	initialize: function() {
		
		this.initBannerRotate();		

	},

	initBannerRotate: function()
	{	
		//number of different contents that are rotating
		this.numOfContents = 6 
		this.videoContentId = false;
		this.so = null;
		this.isRotating = true;
		this.rotationSpeed = 8000;
		this.videoActive = false;
		this.rotationImages = new Array("banner1", "banner2", "banner3", "banner4", "banner5", "banner6");
		this.currentImg = 0;
		this.nextImg = 0;
		this.neutralImg = 2; // this.numOfContents - 1; //	
			
		this.resetBannerRotate();
		
		this.timer = window.setTimeout(this.rotateBannerImg.bindAsEventListener(this), this.rotationSpeed);			
	},
	
	stopRotation: function() {
		this.isRotating = false;
		this.timer = window.clearTimeout(this.timer);		
	},

	playFade: function (imgFrom, imgTo, outImg, inImg, videoPlaying)
	{
		var alphaSpeed = 0.6;
		
		var color = $(imgTo).getStyle('background-color');

		this.fadeContent(imgFrom, alphaSpeed);

		$('banner').morph({backgroundColor: color}, {
		queue: 'end', duration: 0.6});
			
		this.appearContent(imgTo, .4);
	},
	
	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)
		});		
	},

	rotateBannerImg: function ()
	{	
		if (this.isRotating) {
			this.updateIdentifiers();

			this.setRotationContent(this.rotationImages, true);		
		
			this.currentImg = this.nextImg;	
		
			this.timer = window.setTimeout(this.rotateBannerImg.bindAsEventListener(this), this.rotationSpeed);
		}	
	},

	setRotationContent: function (contents, animate) {
		var outContent = contents[this.currentImg];
		var inContent = contents[this.nextImg];		
		var neutralContent = contents[this.neutralImg];
		
		$(neutralContent).setStyle({'zIndex': this.numOfContents - 2}); //1});
		$(inContent).setStyle({'visibility':'visible','zIndex': this.numOfContents - 1}); //2});
		$(outContent).setStyle({'zIndex': this.numOfContents}); //3});

		if (animate) {
			this.playFade(outContent, inContent, outContent, inContent, false);
		} else {
			for(var i = 0; i < this.numOfContents; i++) {
				$(contents[i]).hide();
			}
			$(contents[this.nextImg]).show();
			$(contents[this.nextImg]).setStyle({'visibility':'visible', 'height':'auto'});
		}

	},

	resetBannerRotate: function ()
	{
		for (var i = 0; i < this.numOfContents; i++)
		{
			$(this.rotationImages[i]).hide();
		}
		$(this.rotationImages[0]).show();
			
		this.currentImg = 0;
		this.nextImg = 0;
		this.neutralImg = 2;		
	},	
	
	updateIdentifiers: function()
	{
		if(this.currentImg == 0) {
			this.resetBannerRotate();
		} else {
			this.neutralImg = this.currentImg-1;
		}
		
		if(this.currentImg == this.numOfContents - 1) {
			this.nextImg = 0;
		} else {
			this.nextImg++;
		}		
	}
});

function initpage(){

	ydi.home = new Home();

}

document.observe("dom:loaded",initpage);

