
	var fadeDuration = 400;
	var slideDuration = 1000;
	
	var itemWidth = 232;
	var slidePosition = 0;
	
	var btnRightEnabled = true;
	var btnLeftEnabled = false;

	var bannerBackgroundIndex = 1;
	var bannerChapIndex = 1;
	var bannerTextIndex = 1;
	

	window.addEvent('domready', initHomepage);
	
	function initHomepage()
	{
		$('btnRight').addEvent('mouseenter',btnRight_MouseEnter);		
		$('btnRight').addEvent('mouseleave',btnRight_MouseLeave);		
		
		$('btnLeft').addEvent('mouseenter',btnLeft_MouseEnter);		
		$('btnLeft').addEvent('mouseleave',btnLeft_MouseLeave);		

		$('btnRight').getChildren()[0].set('morph',{duration:fadeDuration, transition:Fx.Transitions.Sine.easeInOut, link:'cancel'});
		$('btnLeft').getChildren()[0].set('morph',{duration:fadeDuration, transition:Fx.Transitions.Sine.easeInOut, link:'cancel'});
		
		$('buttons').set('morph',{duration:fadeDuration, transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
		$('buttons').set({'opacity':0});

		$('categoriesHeader').set('morph',{duration:fadeDuration, transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
		$('categoriesHeader').set({'opacity':0});

		$('homepageAnimation').set('morph',{duration:(slideDuration), onComplete:showBanner, transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
		$('homepageAnimation').morph({'left':0, 'width':952});
		
		(function(){$('buttons').morph({'opacity':1})}).delay(2000);
		(function(){$('categoriesHeader').morph({'opacity':1})}).delay(2000);

		(function(){switchBannerBackground()}).delay(Math.floor(Math.random()*5000)+5000);
		(function(){switchBannerChap()}).delay(Math.floor(Math.random()*5000)+5000);
		(function(){switchBannerText()}).delay(Math.floor(Math.random()*5000)+5000);
		
		var arrItems = $$('.item');
		for(var intLp=arrItems.length-1; intLp>=0; intLp--)
		{
			var obj = arrItems[intLp];
			obj.set({'opacity':0.5});
			obj.setStyles({'left':-1*itemWidth});
			obj.store('pos',intLp*itemWidth);
			obj.store('index',intLp);

			(function()
			{
				this.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
				this.morph({'left':this.retrieve('pos'), 'opacity':1});		
			}).delay(((slideDuration/8)*(arrItems.length-intLp+1))+2000, obj);
		}
	}
	
	function switchBannerBackground()
	{
		
		var temp = bannerBackgroundIndex;
		while(temp==bannerBackgroundIndex)
		{
			temp = Math.floor(Math.random()*3+1);
		}
		bannerBackgroundIndex = temp;
		
		var background = new Asset.image('/_sys/images/bannerBackground'+bannerBackgroundIndex+'.jpg', {'onload':function()
		{
			$('bannerBackground').set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			$('bannerBackground').morph({'top':-282});
			(function()
			{
				$('bannerBackground').src = background.src;
				$('bannerBackground').morph({'top':11});
				(function(){switchBannerBackground()}).delay(Math.floor(Math.random()*5000)+5000);
			}).delay(slideDuration);
		}});
		
	}

	function switchBannerChap()
	{
		
		var temp = bannerChapIndex;
		while(temp==bannerChapIndex)
		{
			temp = Math.floor(Math.random()*3+1);
		}
		bannerChapIndex = temp;
		
		var chap = new Asset.image('/_sys/images/bannerChap'+bannerChapIndex+'.png', {'onload':function()
		{
			$('bannerChap').set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			$('bannerChap').morph({'left':952});
			(function()
			{
				$('bannerChap').src = chap.src;
				$('bannerChap').morph({'left':0});
				(function(){switchBannerChap()}).delay(Math.floor(Math.random()*5000)+5000);
			}).delay(slideDuration);
		}});
		
	}
	
	function switchBannerText()
	{
		$('bannerText'+bannerTextIndex).morph({'opacity':0});
		
		var temp = bannerTextIndex;
		while(temp==bannerTextIndex)
		{
			temp = Math.floor(Math.random()*3+1);
		}
		bannerTextIndex = temp;
		
		(function()
		{
			$('bannerText'+bannerTextIndex).set('opacity',0);
			$('bannerText'+bannerTextIndex).setStyles({'zIndex':1});
			$('bannerText'+bannerTextIndex).set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			$('bannerText'+bannerTextIndex).morph({'opacity':1});	
			(function(){switchBannerText()}).delay(Math.floor(Math.random()*5000)+5000);
		}).delay(slideDuration);
	}

	function showBanner()
	{
		bannerChapIndex = Math.floor(Math.random()*3+1);
		bannerTextIndex = Math.floor(Math.random()*3+1);

		showBannerBackground();
	}
	
	function showBannerBackground()
	{
		var background = new Asset.image('/_sys/images/bannerBackground'+bannerBackgroundIndex+'.jpg', {'id':'bannerBackground', 'onload':function()
		{
			this.setStyles({'position':'absolute', 'left':11, 'top':-260});
			this.inject($('homepageAnimation'));
			this.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			this.morph({'top':11});
			showBannerOverlay();
		}});
	}
	
	function showBannerOverlay()
	{
		var chap= new Asset.image('/_sys/images/bannerBlackOverlay.png', {'id':'bannerOverlay', 'onload':function()
		{
			this.setStyles({'position':'absolute', 'left':-952, 'top':11});
			this.inject($('homepageAnimation'));
			this.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			this.morph({'left':11});
			showBannerChap();
			}});
	}
	
	function showBannerChap()
	{
		var chap= new Asset.image('/_sys/images/bannerChap'+bannerChapIndex+'.png', {'id':'bannerChap', 'onload':function()
		{
			this.setStyles({'position':'absolute', 'left':952, 'top':11});
			this.inject($('homepageAnimation'));
			this.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			this.morph({'left':11});
			showBannerLogo();
		}});
	}
	
	function showBannerLogo()
	{
		var logo = new Asset.image('/_sys/images/bannerLogo.png', {'id':'bannerLogo', 'onload':function()
		{
			this.setStyles({'position':'absolute', 'left':11, 'top':282});
			this.inject($('homepageAnimation'));
			this.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
			this.morph({'top':11});
			showBannerText();
		}});
	}
	
	function showBannerText()
	{
		var obj = $('bannerText'+bannerTextIndex);
		obj.set('opacity',0);
		obj.setStyles({'zIndex':1});
		obj.set('morph',{duration:(slideDuration), transition:Fx.Transitions.Quad.easeInOut, link:'cancel'});
		obj.morph({'opacity':1});	
	}
	
	function scrollLeft()
	{
		if(btnRightEnabled)
		{
			slidePosition = slidePosition-(itemWidth*2);
			if (slidePosition<=(($$('.item').length)-4)*-1*itemWidth) btnRight_Disable();
			btnLeft_Enable();
			var index=0;
			$$('.item').each(function(el)
			{
				var pos = slidePosition + (itemWidth* el.retrieve('index'));
				(function(){el.morph({'left':pos})}).delay(index*100);
				index++;
			});		
		}
	}
	
	function scrollRight()
	{
		if(btnLeftEnabled)
		{
			slidePosition = slidePosition+(itemWidth*2);
			if (slidePosition>=0) btnLeft_Disable();
			btnRight_Enable();
			var index=0;
			$$('.item').each(function(el)
			{
				var pos = slidePosition + (itemWidth* el.retrieve('index'));
				(function(){el.morph({'left':pos})}).delay(($$('.item').length-index)*100);
				index++;
			});			
		}
	}
	
	function btnRight_Enable()
	{
		btnRightEnabled = true;
		$('btnRight').removeClass('disabled');
	}
	
	function btnRight_Disable()
	{
		btnRightEnabled = false;
		btnRight_MouseLeave();
		$('btnRight').addClass('disabled');
	}
	
	function btnLeft_Enable()
	{
		btnLeftEnabled = true;
		$('btnLeft').removeClass('disabled');
	}
	
	function btnLeft_Disable()
	{
		btnLeftEnabled = false;
		btnLeft_MouseLeave();
		$('btnLeft').addClass('disabled');
	}

	function btnRight_MouseEnter()
	{
		if(btnRightEnabled)
		{
			$('btnRight').getChildren()[0].get('morph').cancel();
			$('btnRight').getChildren()[0].set({'opacity':1});
		}
	}
	
	function btnRight_MouseLeave()
	{
		$('btnRight').getChildren()[0].morph({'opacity':0});
	}

	function btnLeft_MouseEnter()
	{
		if(btnLeftEnabled)
		{
			$('btnLeft').getChildren()[0].get('morph').cancel();
			$('btnLeft').getChildren()[0].set({'opacity':1});
		}
	}
	
	function btnLeft_MouseLeave()
	{
		$('btnLeft').getChildren()[0].morph({'opacity':0});
	}
	
