var MovieSlideshow = new Class({
							   
	initialize:function(e)
	{
		this.e = e;

		this.duration = 5000;
		this.startIndex = 150;
		this.images = this.e.getElements('.movieSlideshowImage');
		this.length = this.images.length;
		this.currentImage = 0;
		this.width = this.styleToInt(this.images[this.currentImage], 'width');
		this.height = this.styleToInt(this.images[this.currentImage], 'height');
		this.setEdges();
		this.disabled = false;
		this.setIndex();		
		if(this.length > 1)
		{
			this.setTimer();
			this.setIndicator();
			this.animateIndicator();
			this.setOverlay();
			this.setEvents();
		}
	},
	
	setEvents: function()
	{
		this.overlay.addEvents
		({
			'mouseover': this.doMouseover.bind(this),
			'mouseout': this.doMouseout.bind(this),
			'click': this.doClick.bind(this)
		});
		
		this.overlay.onclick = function()
		{
			return false;
		};	
		
		this.overlay.onmouseover = function()
		{
			return false;
		};
		
		this.overlay.onmouseout = function()
		{
			return false;
		};
	},
	
	doMouseover: function(ev)
	{
		this.overlay.setStyle('cursor', 'pointer');
	},
	
	doMouseout: function(ev)
	{
		
	},
	
	doClick: function()
	{
		this.timerAction();
		this.setTimer();
	},
	
	animateImage:function()
	{
		this.slide = new Fx.Tween(this.images[this.currentImage],{'duration':800, 'transition': Fx.Transitions.Cubic.easeOut });//this.duration	
		
		this.slide.addEvents
		({
			'complete': this.imageAnimationComplete.bind(this)
		});
		
		this.slide.start('width', 0);
	},
	
	imageAnimationComplete: function(event)
	{
		var image = this.images[this.currentImage];
		this.images.shift();
		this.images.push(image);
		this.setIndex();
		this.images[(this.length-1)].setStyle('width', this.width);	
	},
	
	animateIndicator: function()
	{
		if(this.indicatorTween){this.indicatorTween.cancel();}
		this.indicatorTween = new Fx.Tween(this.indicator, {'duration': this.duration, 'transition': 'linear' });//this.duration	
		this.indicatorTween.set('width', '0px');
		this.indicatorTween.start('width', this.width);
	},
	
	setIndex: function()
	{
		this.images.each( function(e, i)
		{
			e.setStyles(
			{
				'position':'absolute', 
				'z-index': this.startIndex - i
			});
		}.bind(this));
	},
	
	setTimer: function()
	{
		$clear(this.timer);
		this.timer = this.timerAction.periodical(this.duration, this);//this is neccessary
	},
	
	clearTimer: function()
	{
		$clear(this.timer);
	},
	
	timerAction: function()
	{
		this.animateImage();
		this.animateIndicator();
	},
	
	setEdges:function()
	{
		var positions = Array('TL','TR','BR','BL');
		
		for(var i=0; i < positions.length; i++)
		{
			var which = Math.floor(Math.random()*2)+1;
			
			switch(positions[i])
			{
				case 'TL':
				var bgpos = 'left top';
				break;
				case 'TR':
				var bgpos = 'right top';
				break;
				case 'BR':
				var bgpos = 'right bottom ';
				break;
				case 'BL':
				var bgpos = 'left bottom';
				break;
			}
			if(which <= 2)
			{
				var overlay = new Element('div', 
				{
					'styles': 
					{
						'display' : 'block',
						'position' : 'absolute',
						'width' : this.width + 'px',
						'height' : this.height + 'px',
						'background' : 'url("/site/img/edge' + positions[i] + which + '.png")',
						'background-position' : bgpos,
						'background-repeat' : 'no-repeat',
						'z-index' :(this.startIndex+i+1)
					}
				});
				
				overlay.inject(this.e, 'before');
			}
		}
	},
	
	setOverlay:function()
	{
	
		this.overlay = new Element('div', 
		{
			'styles': 
			{
				'display' : 'block',
				'position' : 'absolute',
				'width' : this.width + 'px',
				'height' : this.height + 'px',
				'z-index' :(this.startIndex+6)
			}
		});
		
		this.overlay.inject(this.e, 'before');
		
	},
	
	setIndicator:function()
	{
		var bg = new Element('div', 
		{
			'styles': 
			{
				'display' : 'block',
				'position' : 'absolute',
				'width' : this.width + 'px',
				'height' : '1px',
				'z-index' :(this.startIndex + 5)
			}
		});
		
		bg.inject(this.e, 'after');
		
		this.indicator = new Element('div', 
		{
			'styles': 
			{
				'display' : 'block',
				'position' : 'absolute',
				'width' : '0px',
				'height' : '1px',
				'background' : 'white',
				'z-index' :(this.startIndex + 6)
			}
		});
		
		this.indicator.inject(this.e, 'after');
		
		
	},
	
	styleToInt: function(e, style)
	{
		var styleAsString = e.getStyle(style);
		var styleAsInt = parseInt(styleAsString.substring(0, (styleAsString.length-2)));
		return styleAsInt;
	}

})
