var ImageSwapper = new Class(
{
	imagesSwap:null,
	
	initialize: function()
	{
		this.initImageSwapper();
		this.initNewsItemSwapper();
	},
	
	initImageSwapper: function()
	{
		this.imagesSwap = $$('span.swapimage');
		
		if(this.imagesSwap.length > 0)
		{
		
			var imagesSwapIndex = new Element('span');
			imagesSwapIndex.id = 'imagesSwapIndex';
			imagesSwapIndex.setText("1");
			imagesSwapIndex.injectBefore(this.imagesSwap[0]);
			
			var swapImage = function()
			{
				var box = $('topimage');
				var fx = box.effects({duration: 2000, transition: Fx.Transitions.Quart.easeInOut});
				var imagesSwap = this.imagesSwap;
		
				fx.start({
					'opacity': 0
				}).chain(function(){
				
					var imagesSwapIndex = $('imagesSwapIndex').getText();
					imagesSwapIndex = parseInt(imagesSwapIndex)
					var imageSwap = imagesSwap[imagesSwapIndex];
					imagesSwapIndex = imagesSwapIndex + 1;
					if(imagesSwapIndex > 9)
					{
						imagesSwapIndex = 0;
					}
					$('imagesSwapIndex').setText(imagesSwapIndex);
					
					box.setStyles({"background-image":"url(styles/images/" + imageSwap.getText() + ")"});
					
					this.start({
						'opacity': 1
					});
				});
			}
			
			swapImage.periodical(10000, this);
		
		}
	},
	
	initNewsItemSwapper: function()
	{
		if($chk($('boxNewsDeskContent')))
		{
			var newsSwapIndex = new Element('span');
			newsSwapIndex.setStyles({'display':'none'});
			newsSwapIndex.id = 'newsSwapIndex';
			newsSwapIndex.setText("0");
			newsSwapIndex.injectInside($('boxNewsDeskContent'));
			
			var swapNews = function()
			{
				var newsBoxItems = $$('div.newsboxitem');
				
				var newsSwapIndex = $('newsSwapIndex').getText();
				newsSwapIndex = parseInt(newsSwapIndex)
				
				if($chk(newsBoxItems[newsSwapIndex]))
				{
					newsBoxItems[newsSwapIndex].setStyles({'display':'block', 'opacity':'1'});
					
					var fx = newsBoxItems[newsSwapIndex].effects({duration: 2000, transition: Fx.Transitions.Quart.easeInOut});
					fx.start({
						'opacity': 0
					}).chain(function()
					{
						newsBoxItems[newsSwapIndex].setStyles({'display':'none', 'opacity':'0'});
						
						newsSwapIndex++;
						if(newsSwapIndex > 2)
						{
							newsSwapIndex = 0;
						}
						
						$('newsSwapIndex').setText(newsSwapIndex);
						
						newsBoxItems[newsSwapIndex].setStyles({'display':'block', 'opacity':'0'});
						var fx = newsBoxItems[newsSwapIndex].effects({duration: 2000, transition: Fx.Transitions.Quart.easeInOut});
						fx.start({
							'opacity': 1
						});
					});
				}
			}
			
			swapNews.periodical(5000, this);
		}
	}
});

window.addEvent('domready', function()
{
	var imageSwapper = new ImageSwapper();
});