var celciusSlideshow = new Class({
	options: {
		showDuration: ''
	},

	Implements: [Options,Events],

	initialize: function(container,elements,options) {
		this.setOptions(options);

		this.container 		= $(container);
		this.elements 		= $$(elements);
		this.currentIndex 	= 0;
		this.interval 		= '';
		this.showDuration 	= this.options.showDuration;

		this.elements.each( function(el,i){ if(i > 0) el.setStyles({'opacity':0, 'visibility':'visible'}); },this );
	},

	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex + 1 : 0));
		this.elements[this.currentIndex].fade('in');
	},

	start: function() {
		this.interval = this.show.bind(this).periodical(this.showDuration);
	},

	stop: function() {
		$clear(this.interval);
	},

	next: function() {
		this.stop();
		this.show();
	},

	prev: function() {
		this.stop();
		this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
	}
});

var celciusFullScreenBG = new Class({
	options: {
		showDuration	: '',
		backgrounds 	: '',
		container	: ''
	},

	Implements: [Options,Events],

	initialize: function( options ){
		this.currentIndex 	= 0;
		this.interval 		= '';
		this.showDuration 	= options.showDuration;
		this.backgrounds 	= options.backgrounds;
		this.container		= $(options.container);
		this.elements		= [];

		this.initImages();		
	},

	initImages: function(){
		if( this.backgrounds.length > 0 ){
			this.backgrounds.each( function( el, i ){
				if( i != this.currentIndex){ opac = 0; }else{ opac = 1; }
				theImg = new Element( 'img', {
					"src"	: el,
					"styles": {
						'margin'	: '0',
						'padding'	: '0',
						'position'	: 'fixed',
						'z-index'	: '-2',
						'opacity'	: opac
					},
					"events":{
						'load' : function(){ this.resizing( i ); }.bind(this)
					}
				} ).inject( $(document.body), 'top');
				this.elements.combine( [theImg] ).erase("");
			}.bind( this ) );
			
			window.addEvent('resize', function(){
				this.elements.each( function(el, i){ this.resizing( i ); }.bind(this) );
			}.bind( this ));
		}
	},

	resizing: function( i ){
		ww 			= $(window).getSize().x;
		wh 			= this.container.getSize().y;
		iw			= this.elements[i].getSize().x;
		ih			= this.elements[i].getSize().y;
		rw 			= wh / ww;
		ri 			= ih / iw;
		newWidth	= '';
		newHeight	= '';
		newLeft		= '';
		newTop		= '';

		if( rw > ri ){
			newWidth = wh / ri;
			newHeight= wh;
		}else{
			newWidth = ww;
			newHeight= ww * ri;
		}

		this.elements[i].setStyles({
			'width'	: newWidth + 'px',
			'height': newHeight + 'px',
			'left'	: ( ww - newWidth ) / 2 + 'px',
			'top'	: ( wh - newHeight ) / 2 + 'px'
		});
	},

	show: function(to){
		this.elements[this.currentIndex].fade('out');
		this.currentIndex = ( $defined(to) ? to : ( this.currentIndex < this.elements.length - 1 ? this.currentIndex + 1 : 0 ) );
		this.elements[this.currentIndex].fade('in');
		if( this.footer ){
			this.footer.set('html', this.elements[this.currentIndex].getProperty('title') );
		}
	},

	start: function(){
		if( this.elements.length > 1 ){
			this.interval = this.show.bind(this).periodical( this.showDuration );
		}
	},

	stop: function(){
		$clear(this.interval);
	},

	next: function(){
		this.stop();
		this.show();
	},

	prev: function(){
		this.stop();
		this.show( this.currentIndex != 0 ? this.currentIndex - 1 : this.elements.length - 1 );
	}
});
