/** @package eFocus js lib */

/**
 * Pushbox Class - create pushbox of html elements based on Slideshow
 *
 * @author Rocco Janse <rocco@efocus.nl>
 * @since 1.0, 13 aug, 2009
 * @copyright eFocus
 * @package eFocus js lib
 * @subpackage Pushbox
 * @uses MooTools 1.2.3 More <http://www.mootools.net>
 * @uses Slideshow
 */

var Pushbox = new Class({

	Implements: Options,
	Extends: Slideshow,
	
	options: {
		'navigation': false,
		'delay': 0,
		'prevnext': false
	},

	initialize: function(options) {
		
		this.setOptions(options);
		
		/** @var object navigation */
		this.navigation = this.options.navigation;
		if (!this.navigation.length) return;

		// start parent (slideshow)		
		this.parent();
		
		// init navigation
		this.initNavigation();
		
		// init prev / next buttons
		this.initPrevNextButtons();
		
	},
	
	/**
	* initialises navigation
	*
	* @author Rocco Janse <rocco@efocus.nl>
	* @since 1.0, 13 aug, 2009
	* @return void
	*/

	initNavigation: function() {

		// return if no nav items		
		if (!this.navigation.length) return;
		
		// add events to nav
		this.navigation.each(function(item, index) {
			item.addEvents({
				'click': function(event) {
					event.stop();
					this.pauseSlideshow();
					this.updateNavigation(index);
					this.showSlide(index);
				}.bind(this)								
			});
		}.bind(this));
	},
	
	/**
	* initialises prev / next buttons
	*
	* @author Mirjam <mirjam@efocus.nl>
	* @since 1.0, 30 mar, 2010
	* @return void
	*/

	initPrevNextButtons: function() {
		
		if (!this.options.prevnext.length) return;
		
		for (var i = 0; i < this.options.prevnext.length; i++) {
			if (this.options.prevnext[i].hasClass('nextbutton') == true) {
				this.elNextButton = this.options.prevnext[i];
				this.setNextButton();
			} else if (this.options.prevnext[i].hasClass('prevbutton') == true) {
				this.elPrevButton = this.options.prevnext[i];
				this.setPrevButton();
			}
		}
		
	},
	
	/**
	* initialises next button
	*
	* @author Mirjam <mirjam@efocus.nl>
	* @since 1.0, 30 mar, 2010
	* @return void
	*/

	setNextButton: function() {
		
		this.elNextButton.addEvent('click', function(e){
			e.stop();
			this.showNextSlide();
		}.bind(this));

		
	},
	
	/**
	* initialises next button
	*
	* @author Mirjam <mirjam@efocus.nl>
	* @since 1.0, 30 mar, 2010
	* @return void
	*/

	setPrevButton: function() {
		
		this.elPrevButton.addEvent('click', function(e){
			e.stop();
			this.showPreviousSlide();
		}.bind(this));

		
	},
	
	/**
	 * updates navigation, removes 'active' classes and adds it to clicked item
	 *
	 * @author Rocco Janse <rocco@efocus.nl>
	 * @since 1.0, 13 aug, 2009
	 * @return void
	 */
	
	updateNavigation: function(navnumber) {
	
		this.navigation.each(function(item, index) {
			item.removeClass('active');
			if (index == navnumber) {
				item.addClass('active');
			}
		}.bind(this));
	
	},
	
	/**
	* finds and activates next slide
	*
	* @author Rocco Janse <rocco@efocus.nl>
	* @since 1.0, 5 aug, 2009
	* @return void
	*/

	showNextSlide: function() {

		/** @var integer next slide to activate */
		var nextSlide = this.current + 1;

		// reset next slide to loop slideshow
		if (nextSlide >= this.totalSlides) {
			nextSlide = 0;
		}
		
		this.pauseSlideshow();

		// execute
		this.updateNavigation(nextSlide);
		this.showSlide(nextSlide);
	},

	/**
	* finds and activates previous slide
	*
	* @author Rocco Janse <rocco@efocus.nl>
	* @since 1.0, 5 aug, 2009
	* @return void
	*/

	showPreviousSlide: function() {
		
		/** @var integer previous slide to activate */
		var previousSlide = this.current - 1;
		
		// reset next slide to loop slideshow
		if (previousSlide < 0) {
			previousSlide = this.totalSlides - 1;
		}
		
		this.pauseSlideshow();
		
		// execute
		this.updateNavigation(previousSlide);
		this.showSlide(previousSlide);
	}
});
