
var HeaderLoop = new Class({
	
	Implements					: Options,
	
	options						:
	{
		'speed'						: 5000
	},
	
	initialize					: function(container,options)
	{
		this.setOptions(options);
		this.images		= $(container).getElements('img');
		this.count		= this.images.length;
		this.current	= 0;
		this.running	= false;
		this.loadImage();
		this.loop = this.loadImage.periodical(this.options.speed,this,'next');
	},
	
	stopLoop					: function()
	{
		$clear(this.loop);
	},
	
	loadImage					: function(neg)
	{	
	
		this.running = true;
		this.images.setStyle('z-index',0);
		
		var current = false;
		
		if (neg) {
			var next = (this.current < this.count - 1) ? this.current + 1 : 0;
			var prev = (this.current == 0) ? this.count - 1 : this.current - 1;
			current			= this.images[this.current];
			var active 		= (neg == 'next') ? this.images[next] : this.images[prev];
			var previous 	= (neg == 'next') ? this.images[prev] : this.images[next];
		} else {
			var active 		= this.images[this.current];
			var previous 	= this.images[this.count - 1];
		}
		
		var tween = new Fx.Tween(active);
		active.setStyle('z-index',2);
		
		if (document.all) {
			active.setStyle('opacity',1);
			if (current) current.setStyle('opacity',0);
		} else {
			tween.start('opacity',0,1).chain(function(){
				if (current) {
					var tween2 = new Fx.Tween(current);
					tween2.start('opacity',1,0);
				}
				//if (current) current.setStyle('opacity',0);
				this.running = false;			
			}.bind(this));
		}
		if (neg)
		{
			if (neg == 'next') {
				this.current++;
				if (this.current == this.count) this.current = 0;
			} else {
				this.current--;
				if (this.current == -1) this.current = this.count - 1;
			}
		}
		
	}
	
});

window.addEvent("domready",function(){
	if ($('header-slideshow')) {
		if ($('header-slideshow').getChildren().length == 1) {
			//console.log('hi');
			$('header-slideshow').getChildren().removeProperty('style');
		} else {
		//console.log('hi');
			var header = new HeaderLoop('header-slideshow');
		}
	}
	
});
