
var SlideShow = Class.create();
SlideShow.prototype = {
	initialize: function(id, slides)
	{
		if($(id) && slides)
		{
			this.start_timeout = Math.round(((Math.random()*4)+1)*20)*100;
			this.cycle_time = Math.round(((Math.random()*4)+1)*15)*100;
			this.element = $(id);
			this.slides = slides;
			this.slides_ = [];
			var n = 0;
			for(slide in this.slides)
			{
				this.slides_[n] = slide;
				n++;
			}
			this.spinner = $(document.createElement('div'));
			this.spinner.setStyle({
				backgroundImage: 'url(/img/style/spinner.gif)',
				backgroundColor: 'transparent',
				backgroundPosition: 'center center',
				backgroundRepeat: 'no-repeat',
				position: 'absolute',
				top: '0',
				left: '0',
				zIndex: '100',
				width: '100%',
				height: '100%'
			});
			this.spinner.hide();
			this.element.setStyle({
				overflow: 'hidden'
			});
			this.element.appendChild(this.spinner);
			this.preloader = new imagePreloader();
			this.current = null;
			this.prepare();
		}
		else
		{
			return null;
		}
	},
	
	prepare : function()
	{
		var self = this;
		if(this.slides)
		{
			for(img in this.slides)
			{
				this.spinner.show();
				this.preloader.add(img, function(a,b){self._loaded(a,b)});
			}
		}
	},
	
	_loaded : function(arg1, arg2)
	{		
		if(arg2 == 1 && this.slides[arg1])
		{
			this.slides[arg1].done = true;
			var a = $(document.createElement('a'));
			if(this.slides[arg1].url != '')
			{
				if(this.slides[arg1].url.match(/^http:\/\//))
				{
					$(a).href = this.slides[arg1].url;
				}
				else
				{
					$(a).observe('click', function(){window.open(this.slides[arg1].url, '', 'width=820, height=620'); return false;}.bind(this));
				}
				$(a).setStyle({cursor: 'pointer'});
			}
			a.setStyle({
				display: 'block',
				position: 'absolute',
				top: 0,
				left: this.element.getWidth() + 'px',
				width: '100%',
				height: '100%',
				backgroundImage: 'url(' + arg1 + ')',
				backgroundPosition: 'center center',
				backgroundRepeat: 'no-repeat',	
				zIndex: 90		
			});
			a.hide();
			this.element.appendChild(a);
			this.slides[arg1].element = a;
			this.spinner.hide();
			
			if(this.current == null)
			{
				this._start();
			}
		}
	},
	
	_start : function()
	{
		var self = this
		if(this.current == null)
		{
			for(slide in this.slides)
			{
				if(this.slides[slide].element != undefined)
				{
					this.current = this.slides[slide].element;
					this.slides[slide].element.setStyle({
						top: 0,
						left: 0,
						zIndex: 100
					});
					this.slides[slide].element.setStyle({opacity: 0});
					this.slides[slide].element.show();
					fx = new Fx.Style(this.slides[slide].element, 'opacity', {duration: 1000});
					fx.custom(0,1);
					setTimeout(function(){self._next()}, this.cycle_time);
				}
			}
		}
	},
	
	_next : function()
	{
		var self = this
		var cur = null;
		var old = this.current;
		if(this.slides[this.slides_[this.slides_.length-1]].element == this.current)
		{
			cur = this.slides[this.slides_[0]].element;
		}
		else
		{
			for(var n = 0; n <this.slides_.length; n++)
			{
				if(typeof this.slides[this.slides_[n]].element != "undefined")
				{
					if(this.slides[this.slides_[n]].element == this.current)
					{
						cur = this.slides[this.slides_[n+1]].element;
					}
					else
					{
						this.slides[this.slides_[n]].element.hide();
						this.slides[this.slides_[n]].element.setStyle({left: this.element.getWidth()+'px'});
					}
				}
			}		
		}
		
		if(cur)
		{						
			cur.show();
			var fx = new Fx.Style(old, 'left', {transition: Fx.Transitions.sineInOut, duration: 2000});
			var fx1 = new Fx.Style(cur, 'left', {transition: Fx.Transitions.sineInOut, duration: 2000, onComplete: function(){old.hide();}});
			fx.custom(0, this.element.getWidth()*-1);						
			fx1.custom(this.element.getWidth()-1, 0);			
			this.current = cur;			
		}
		setTimeout(function(){self._next()}, this.start_timeout);
	}
}
