
var Banner = new Class({
	
	options: {
		banner: 'banner',
		images: 'bannerimages',
		items: 'bannernav',
		currentClassName: 'current',
		offset: 212,
		slideShow: false,
		refreshRate: 10000,
		useConsole: false
	},
	initialize: function(options) {
			
		// Set the Options.
		this.setOptions(options);
		
		
		// Do this within a try/catch so there are no nasty 'uncaught exception' error messages if anything goes wrong
		try {
		
			// Get the Container, Panels and Tabs as Class properties..
			Banner.container = $(this.options.banner);
			Banner.imagecontainer = $(this.options.images);
			Banner.tabs  = $$('#' + this.options.items + ' li');
			
			count = Banner.tabs.length;
			offset = this.options.offset; 
			currentClassName = this.options.currentClassName;
			
			Banner.tabs.each(function(element, i) {
				
				fx = new Fx.Styles(Banner.imagecontainer, {duration:200, wait:false});
			 
				element.addEvent('mouseenter', function(){
					
					Banner.container.current = i;
					
					fx.start({
						'top': -(offset * i)
					});
					
					Banner.tabs.each(function(el){
						 el.removeClass(currentClassName);											
					});
					
					element.addClass(currentClassName);
				});
			});
			
			// If set in the Options, start a periodical effect.
			if(this.options.slideShow) {
				Banner.interval = this.beginPeriodical();
				Banner.container.addEvent("mouseenter", this.resetPeriodical.bindWithEvent(this));
				Banner.container.addEvent("mouseleave", this.beginPeriodical.bindWithEvent(this));
			}

		} catch(err) {
			if(this.options.useConsole) { console.log(err); }
		}
	
	},
	periodicalBanner: function() {
		
		// Do this within a try/catch so there are no nasty 'uncaught exception' error messages if anything goes wrong
		try {
		
			if(!Banner.container.current){
				Banner.container.current = 0;
			}
			len = Banner.tabs.length;
	
			if(Banner.container.current == len -1) {
				Banner.container.current = 0;
			}else{
				Banner.container.current++;
			}
	
			fx.start({
				'top': -(offset * Banner.container.current)
			});
			
			Banner.tabs.each(function(el){
				 el.removeClass(currentClassName);											
			});
			
			Banner.tabs[Banner.container.current].addClass(currentClassName);

		} catch(err) {
			if(this.options.useConsole) { console.log(err); }
		}
		
	},
	beginPeriodical:function() {
		this.resetPeriodical();
		var myRefreshRate = this.options.refreshRate;
		Banner.interval = this.periodicalBanner.periodical(myRefreshRate);
		return Banner.interval;
	},
	resetPeriodical:function() {
		return $clear(Banner.interval);
	}
	
});

// Extend the Scrollpanel Class with the Options Class.  
Banner.implement(new Options());

// Add the Class init to the DomReady signal.
window.addEvent('domready', function() {
																		 
	var BannerObj = new Banner({
		banner: 'banner',
		images: 'bannerimages',
		items: 'bannernav',
		currentClassName: 'current',
		offset: 212,
		slideShow: true,
		refreshRate: 5000,
		useConsole: false
	});

});
