var celciusVertRotator = new Class({
	options: {
		showDuration: '',
		itemHeight: '',
		up: '',
		down: ''
	},
	
	Implements: [Options,Events],

	initialize: function(container,elements,options) {
		this.setOptions(options);
		
		this.container 		= $(container);
		this.elements 		= $$(elements);
		this.total		= this.elements.length - 2;
		this.currentIndex 	= 0;
		this.interval 		= '';
		this.direction 		= 'down';
		this.showDuration 	= this.options.showDuration;
		this.itemHeight 	= this.options.itemHeight;
		this.up			= $(this.options.up);
		this.down		= $(this.options.down);

		if( this.up ){ this.up.addEvent( 'click', function(){ this.prev(); }.bind( this ) ); }
		if( this.down ){ this.down.addEvent( 'click', function(){ this.next(); }.bind( this ) ); }		
	},

	show: function(){
		if( this.direction == 'down' ){ this.currentIndex = (this.currentIndex < this.total - 1 ? this.currentIndex + 1 : 0);
		}else{			        this.currentIndex = (this.currentIndex != 0 ? this.currentIndex - 1 : this.total - 1); }
		myFx = new Fx.Scroll( this.container ).start( 0, (this.itemHeight * this.currentIndex) );
	},

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

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

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

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

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.controls		= $(this.options.controls);

		this.elements.each( function(el,i){ if(i > 0) el.setStyles({'opacity':0, 'visibility':'visible'}); },this );
		
		if( this.elements.length > 1 ){
			(this.controls.getChildren('img'))[0].addEvent( 'click', function(){ this.prev(); }.bind( this ) );
			(this.controls.getChildren('img'))[1].addEvent( 'click', function(){ this.next(); }.bind( this ) );
			this.controls.getChildren('img').addEvents({
				'mouseenter' : function(){ this.setProperty('src', this.getProperty('src').replace('off', 'on' ) ); },
				'mouseleave' : function(){ this.setProperty('src', this.getProperty('src').replace('on', 'off' ) ); }
			});
		}else{
			this.controls.setStyle('display','none');
		}
	},

	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 celciusMenu = new Class({
	options: {
		offSetX : 0,
		offSetY : 0,
		ems: '',
		emsFloat: '',
		emsPrefix: '',
		emsSearch: '',
		btnBg: '',
		btnShadow: '',
		btnText: '',
		btnClose: '',
		siteNav: ''
	},

	Implements: [Options,Events],

	initialize: function( elements, options ){
		this.setOptions(options);
		this.elements 	= $$(elements);
		this.offSetX 	= this.options.offSetX;
		this.offSetY 	= this.options.offSetY;
		this.ems	= this.options.ems;
		this.emsFloat   = this.options.emsFloat;
		this.emsPrefix	= this.options.emsPrefix;
		this.emsSearch  = this.options.emsSearch;
		this.btnBg	= this.options.btnBg;
		this.btnShadow  = this.options.btnShadow;
		this.btnText    = this.options.btnText;
		this.btnClose   = $$(this.options.btnClose);
		this.showingMenu= false;
		this.siteNav	= $$(this.options.siteNav);

		this.elements.each( function( el ){
			el.addEvent( 'click', function( event ){				
				event.stop();
				this.hideAll();				
				this.showMenu( el, this.emsPrefix + el.id );
			}.bind(this) );
		}.bind(this) );

		$(document.window).addEvent('click', function(e){
			if( this.showingMenu && !$(e.target) || this.showingMenu && !$(e.target).hasClass( (this.ems).replace('.','') ) ){ 
				this.hideAll();
			}
		}.bind(this) );		

		this.btnClose.addEvent('click', function(e){
			this.hideAll();
		}.bind(this) );		
		
		this.temp = function(){ this.hideAll(); }.bind(this);
		this.initMouseOverEvents();
	},

	showMenu: function( el, target ){
		el.setStyles({
			'background-color'	: this.btnBg,
			'color'			: this.btnText,
			'-webkit-border-radius'	: '4px 4px 0px 0px',
			'-moz-border-radius'	: '4px 4px 0px 0px',
			'-o-border-radius'	: '4px 4px 0px 0px',
			'-ms-border-radius'	: '4px 4px 0px 0px',
			'-khtml-border-radius'	: '4px 4px 0px 0px',
			'border-radius'		: '4px 4px 0px 0px',
			'-webkit-box-shadow'	: '0px 0px 3px ' + this.btnShadow,
			'-moz-box-shadow'	: '0px 0px 3px ' + this.btnShadow,
			'box-shadow'		: '0px 0px 3px ' + this.btnShadow,
			'position'		: 'relative',
			'z-index'		: '1000'
		});
		
		$(target).setStyles({
			'position'	: 'relative',
			'top'		: (el.getPosition().y).toInt() + this.offSetY.toInt() + "px",
			'left'		: (el.getPosition().x).toInt() + this.offSetX.toInt() + "px",
			'display' 	: 'block',
			'z-index'	: '999'
		});
		
		el.removeEvent('mouseover', this.temp );
		
		//$(target).addEvent( 'mouseleave', function(event){ this.hideMenu( el, target ); }.bind(this) );
		
		this.divideAndConquer( target );
		this.setColumnHeight( target );		
		this.showingMenu = true;
	},

	hideMenu: function( el, target ){ 
		$(target).setStyle( 'display', 'none' ); 
		el.set('style', null); 
		this.showingMenu= false;
		this.initMouseOverEvents();
	},
	
	hideAll: function(){ 
		$$(this.ems).setStyle( 'display', 'none' ); 
		this.elements.set('style', null); 
		this.showingMenu = false; 
		this.initMouseOverEvents();
	},
	
	setColumnHeight: function( target ){
		tc_height = 0;		
		target_columns = '#' + target + " " + this.emsFloat;

		$$( target_columns ).setStyle( 'padding-bottom', '0px' );

		$$( target_columns ).each( function(e){
			if( e.offsetHeight > tc_height ){ tc_height = e.offsetHeight; }
		});

		$$( target_columns ).each( function(e){
			if( e.offsetHeight != tc_height ){
				e.setStyle( 'padding-bottom', tc_height - e.offsetHeight );
			}
		});	
	},
	
	divideAndConquer: function( target ){
		if( $(target).hasClass(this.emsSearch) ){	
			$(target).removeClass(this.emsSearch);
			totalHeight 	= $(target).getStyle('height').toInt();
			heightPerColumn = Math.ceil( totalHeight / 3 );		

			iColomns = [0,0,0];
			iCurCol  = 0;
			iTemp    = 0;

			targets = '#' + target + '-c1 div';
			$$(targets).each( function( el ){
				if( iTemp > heightPerColumn ){
					iCurCol++
					iTemp = 0;
				}
				iColomns[iCurCol] = iColomns[iCurCol] + 1;
				iTemp += el.offsetHeight;
			});
			
			column2 = target + '-c2';
			column3 = target + '-c3';

			for( i = 0; i < iColomns[2]; i++ ){
				($$(targets).getLast()).inject( column3, 'top' );
			}
			
			for( i = 0; i < iColomns[1]; i++ ){
				($$(targets).getLast()).inject( column2, 'top' );
			}			
		}	
	},
	
	initMouseOverEvents: function(){
		$$(this.siteNav).addEvent( 'mouseover', this.temp );
	}
});
