var celciusRotator = new Class({
	options: {
		showDuration: '',
		itemWidth: ''
	},
	
	Implements: [Options,Events],

	initialize: function(container,elements,options) {
		this.setOptions(options);
		
		this.container 		= $(container);
		this.elements 		= $$(elements);
		this.currentIndex 	= 0;
		this.interval 		= '';
		this.direction 		= 'right';
		this.showDuration 	= this.options.showDuration;
		this.itemWidth 		= this.options.itemWidth;
	},

	show: function() {
		if( this.direction == 'right' ){
			test = this.currentIndex + 1;
			if( ( test > this.elements.length ) || ( test == ( this.elements.length - 2 ) ) ){
				this.currentIndex = 0;
			}else{
				this.currentIndex = this.currentIndex + 1;
			}
			myFx = new Fx.Scroll( this.container ).start( (this.itemWidth * this.currentIndex), 0 );
		}else{
			test = this.currentIndex - 1;
			if( test < 0 ){
				this.currentIndex = this.elements.length;
			}else if( 
				( test == ( this.elements.length - 1 ) ) || 
				( test == ( this.elements.length - 2 ) )
			){
				this.currentIndex = this.elements.length - 4;
			}else{
				this.currentIndex = this.currentIndex - 1;
			}	

			myFx = new Fx.Scroll( this.container ).start( (this.itemWidth * this.currentIndex), 0 );
		}
	},

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

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

	next: function() {
		this.stop();
		this.direction = 'right';
		this.show();
	},

	prev: function() {
		this.stop();
		this.direction = 'left';
		this.show();
	}
});

var celciusSlideshow = new Class({
    options: {
	showDuration: 5000
    },
    
    Implements: [Options,Events],
    
    initialize: function(container,elements,options) {
	this.container = $(container);
	this.elements = $$(elements);
	this.currentIndex = 0;
	this.interval = '';
	
	this.elements.each(function(el,i){
	    if(i > 0) el.set('opacity',0);
	},this);
	
	this.container.addEvents({
	    mouseenter: function() { this.stop(); }.bind(this),
	    mouseleave: function() { this.start(); }.bind(this)
	});

	if( this.elements.length > 1 ){
		legTarg = "legend" + this.currentIndex;
		if( $(legTarg) ){ $(legTarg).setStyle('background-color', '#ffffff' ); }
	}	
    },
    
    show: function(to) {
    	if( this.elements.length > 1 ){
		legTarg = "legend" + this.currentIndex;
		if( $(legTarg) ){ $(legTarg).setStyle('background-color', '#a6a6a6' ); }
   	}
   	
	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.elements.length > 1 ){
		legTarg = "legend" + this.currentIndex;
		if( $(legTarg) ){ $(legTarg).setStyle('background-color', '#ffffff' ); }
	}
    },
    
    start: function() {
	this.interval = this.show.bind(this).periodical(this.options.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 celciusOverlay = new Class({
    options: {
	'parentHeight' 		: 'foodpairing',
	'extraHeight'  		: '',
	'prev' 			: '',
	'next' 			: '',
	'cta'  			: '',
	'close'			: '',
	'closeXtra'		: '',
	'overlay_container' 	: '',
	'overlay_pages' 	: '',
	'indicators' 		: '',
    },
    
    Implements: [Options,Events],
    
    initialize: function(overlay_bg,overlay,options) {
    	this.setOptions(options);
    
	this.overlay_bg 	= $(overlay_bg);
	this.overlay  		= $(overlay);
	this.parentHeight 	= this.options.parentHeight;
	this.extraHeight 	= this.options.extraHeight;
	this.prev 		= $(this.options.prev);
	this.next 		= $(this.options.next);
	this.cta 		= $(this.options.cta);
	this.close 		= $(this.options.close);
	this.closeXtra 		= $(this.options.closeXtra);
	this.overlay_container  = $(this.options.overlay_container);
	this.overlay_pages	= $$(this.options.overlay_pages);
	this.indicators 	= this.options.indicators;
		
	$(this.overlay_bg).setStyles({
		'display' : 'block',
		'z-index' : '99998',
		'opacity' : '0.90',
		'height'  : $(this.parentHeight).getSize().y + this.extraHeight,
		'width'	  : window.getScrollSize().x
	});

	$(this.overlay).setStyles({
		'display' : 'block',
		'z-index' : '99999'
	});
	
	this.close.addEvent( 'click', function(){ $$(this.overlay_bg, this.overlay).setStyle('display','none' ); }.bind(this) );
	this.closeXtra.addEvent( 'click', function(){ $$(this.overlay_bg, this.overlay).setStyle('display','none' ); }.bind(this) );
	new celciusOverlaySlideshow( this.overlay_container, this.overlay_pages, { 'indicators' : this.indicators, 'btnPrev' : this.prev, 'btnNext' : this.next, 'btnCTA' : this.cta } );	
    }
});

var celciusOverlaySlideshow = new Class({
    options: {
    	'indicators' 	: '',
   	'btnPrev' 	: '',
	'btnNext' 	: '',
	'btnCTA' 	: ''
    },
    
    Implements: [Options,Events],
    
    initialize: function(container,elements,options) {
    	this.setOptions(options);
    	
	this.container 		= $(container);
	this.elements 		= $$(elements);
	this.currentIndex 	= 0;
	this.indicators 	= $$(this.options.indicators);
	this.btnPrev 		= this.options.btnPrev;
	this.btnNext 		= this.options.btnNext;	
	this.btnCTA 		= this.options.btnCTA;
	
	this.elements.each(function(el,i){ if(i > 0) el.set('opacity',0); },this);
	this.indicators[this.currentIndex].removeClass('overlay_indicator_off').addClass('overlay_indicator_on');
	
	this.btnPrev.addEvent('click', function(){ this.prev(); }.bind(this) );
	this.btnNext.addEvent('click', function(){ this.next(); }.bind(this) );
	
	this.btnPrev.setStyle('display', 'none');
    },
    
    show: function(to) {
    	this.indicators[this.currentIndex].removeClass('overlay_indicator_on').addClass('overlay_indicator_off');
    	
	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');
	
	this.indicators[this.currentIndex].removeClass('overlay_indicator_off').addClass('overlay_indicator_on');
	
	if( this.currentIndex > 0 ){ this.btnPrev.setStyle('display', 'block');
	}else{ this.btnPrev.setStyle('display', 'none'); }

	if( this.currentIndex == ( this.indicators.length - 1 ) ){
		this.btnNext.setStyle('display', 'none');
		this.btnCTA.setStyle('display', 'block');
	}else{
		this.btnNext.setStyle('display', 'block');
		this.btnCTA.setStyle('display', 'none');
	}	
    },
    
    next: function() {
    	this.show();
    },
    
    prev: function() {
	this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
    }
});
