/**
 * pushbox.class.js
 * 
 * creates a pushbox
 * 
 * @author: Rocco Janse, <rocco@efocus.nl>
 * @version: 1.0
 * @date: 20/09/2010
 * @copyright: eFocus, webdesign & strategy, http://www.efocus.nl
 */
var Pushbox = new Class({
	
	Implements: [Events, Options],
	
	options: {
	/*	onLoaded: $empty(),
	 	onChange: $empty(),
	 	onChangeComplete: $empty(),
	 */
		viewport: false,
		slides: [],
		navigation: [],
		progressBar: false,
		interval: 7,
		showProgress: true
	},	
	
	initialize: function(container, options) {
		
		// init
		this.setOptions(options);
		this.container = container;

		this.progressfx = false;
		this.currentSlide = null;
		
		if(this.options.navigation.length > 0) {
			this.initNavigation();
		}
		
		this.resetProgress();
	//	this.initViewport();
		this.hideSlides();		
		this.showSlide(0);
	},
	
	initViewport: function() {
		this.options.viewport.removeEvents();
		this.options.viewport.addEvents({
			'mouseenter': function() {
				//this.progressfx.pause();
			}.bind(this),
			'mouseleave': function() {
				//this.progressfx.resume();
			}.bind(this)			
		});
	},
	
	initNavigation: function() {
		this.options.navigation.each(function(nav, index) {
			nav.removeEvents();
			nav.addEvents({
				'click': function(event) {
					event.stop();
					this.showSlide(index);
				}.bind(this)
			});
		}.bind(this));
	},
	
	/**
	 * hides all current slides and position them absolute
	 */
	hideSlides: function() {
		this.options.slides.each(function(slide) {
			slide.fade('hide');
			slide.setStyles({
				'position': 'absolute',
				'top': 0,
				'left': 0
			});
			slide.removeEvents();
			slide.addEvents({
				'mouseenter': function() {
					this.progressfx.pause();
				}.bind(this),
				'mouseleave': function() {
					this.progressfx.resume();
				}.bind(this)	
			});
		}.bind(this));
	},
	
	showSlide: function(slideindex) {
		
		this.progressfx.cancel();
		
		if(this.options.navigation.length > 0) {
			this.options.navigation[slideindex].getElement('a').addClass('active');
			if($defined(this.currentSlide)) {
				this.options.navigation[this.currentSlide].getElement('a').removeClass('active');
			}
		}
		
		if (this.currentSlide == null) {
			this.options.slides[slideindex].fade('show');
			this.currentSlide = slideindex;
			this.startProgress();
		} else {
			this.options.slides[this.currentSlide].fade(0);
			this.options.slides[slideindex].fade(1).get('tween').chain(function() {
				this.currentSlide = slideindex;
				this.startProgress();
			}.bind(this));				
		}

	},
	
	resetProgress: function() {
		
		if(this.options.showProgress) {
			this.options.progressBar.setStyle('left', -this.options.viewport.getWidth());
		} else {
			this.options.progressBar.setStyle('display', 'none');
		}
		
		if(this.progressfx) {
			this.progressfx.cancel();
		}
		
		this.progressfx = new Fx.Tween(this.options.progressBar, {
			duration: (this.options.interval*1000).toInt(), 
			property: 'left'
		});		
	
	},
	
	startProgress: function() {
		this.progressfx.start(-this.options.viewport.getWidth(),'0').chain(function() {
			if (this.currentSlide+1 >= this.options.slides.length) {
				newslide = 0;
			} else {
				newslide = this.currentSlide+1;
			}
			this.showSlide(newslide);
		}.bind(this));
	}
	
});
