/**
* Banner rotation script
*
* @copyright Squarepage Systems Ltd
*/

var BannerRotate = Class.create({

	banners:null,					// currently displayed banner
	container:null,					// containing element
	currentbanner:0,				// index of currently selected banner
	duration:1.0,					// fade duration
	isrotating:false,				// is rotating flag
	timer:null,						// timer
	timerinterval:8,				// timer interval
	wrapper:null,					// wrapper element
	
	_btnNext:null,					// next button
	_btnPrev:null,					// prev button
	
	/**
	* construct object
	*/
	initialize: function(container) {
		this.container = $(container);
		document.observe('dom:loaded', function() {
			this._build();
			this._listen();
			this._start();
		}.bind(this));
	},
		
	/**
	* build any interface elements
	*/
	_build: function() {
		this.wrapper = new Element('div', {className:'bannerrotate'});
		this.container.wrap(this.wrapper);
		this.banners = this.container.select('p').invoke('hide');
		this.banners.first().show();
		this.btnNext = new Element('button', {className:'next'}).update('next');
		this.btnPrev = new Element('button', {className:'prev'}).update('prev');
		this.wrapper.insert(this.btnPrev, {position:'top'});
		this.wrapper.insert(this.btnNext, {position:'bottom'});
	},
	
	/**
	* handle next click
	*/
	_eventBtnNextClick: function(event) {
		Event.stop(event);

		if(this.isrotating)
			return;
			
		if(this.banners.length == 1)
			return;
		
		if(this.timer)
			this.timer.stop();

		this._next();
	},

	/**
	* handle prev click
	*/
	_eventBtnPrevClick: function(event) {
		Event.stop(event);

		if(this.isrotating)
			return;
			
		if(this.banners.length == 1)
			return;
		
		if(this.timer)
			this.timer.stop();
			
		this._prev();
	},
	
	/**
	* handle after rotate event
	*/
	_eventRotateFinish: function(event) {
		this.isrotating = false;
	},

	/**
	* set up event listeners
	*/
	_listen: function() {
		this.btnNext.observe('click', this._eventBtnNextClick.bindAsEventListener(this));
		this.btnPrev.observe('click', this._eventBtnPrevClick.bindAsEventListener(this));
	},
	
	/**
	* move to next banner
	*/
	_next: function() {
		this.isrotating = true;
		
		// fade out current banner
		this.banners[this.currentbanner].fade({duration:this.duration});

		// get next banner index
		this.currentbanner = this.banners.length > this.currentbanner + 1 ? this.currentbanner + 1 : 0;
		
		// fade in next banner
		this.banners[this.currentbanner].appear({afterFinish:this._eventRotateFinish.bindAsEventListener(this), duration:this.duration});
	},
		
	/**
	* move to previous banner
	*/
	_prev: function() {
		this.isrotating = true;
		
		// fade out current banner
		this.banners[this.currentbanner].fade({duration:this.duration});

		// get next banner index
		this.currentbanner = this.currentbanner == 0 ? this.banners.length - 1 : this.currentbanner - 1;
		
		// fade in next banner
		this.banners[this.currentbanner].appear({afterFinish:this._eventRotateFinish.bindAsEventListener(this), duration:this.duration});
	},
	
	/**
	* start timer
	*/
	_start: function() {
		if(this.banners.length > 1)
			this.timer = new PeriodicalExecuter(this._next.bind(this), this.timerinterval);
	}
	
});