/**
 * Pushbox javascript lib core
 * 
 * @author Rocco Janse, <rocco@efocus.nl>
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @version 1.01
 * @copyrights eFocus Strategy & Webdesign, www.efocus.nl
 * @since oct 2010
 * @uses Mootools 1.1
 */
	
var	Pushbox = new Class({

	// init vars
	container: null,
	indicator: null,
	progressbar: null,
	progressfx: null,
	slides: [],
	navitems: [],
	current: 0,
	interval: 7,
	
	initialize: function(container) {
		
		// set container
		this.container = container;
		
		// sniff and init slides
		viewport = container.getElement('div.viewport');
		if(viewport) {
			slides = viewport.getElements('li');
			if (slides.length > 0) {
				
				// progressbar (if set)
				if (this.container.getElement('div.progressbar')) {
					this.progressbar = this.container.getElement('div.progressbar').getElement('div.innerprogress');
					this.progressbarWidth = this.progressbar.getSize().size.x;
				}
				
				this.slides = slides;
				this.initSlides();
			}
		} else {
			return false;
		}
		
		// sniff and init pushbox navigation
		navigation = this.container.getElement('ul.nav');
		if(navigation) {
			items = navigation.getElements('li');
			if (items.length > 0) {
				this.navitems = items;
				this.navitems.each(function(item, index) {
					item.addEvents({
						'mouseenter': function(event) {
							if (this.current != index) {
								this.timeout = setTimeout(function(){this.showSlide(index)}.bind(this), 250);
							}
						}.bind(this),
						'mouseleave': function(event) {
							if(this.timeout) this.timeout = $clear(this.timeout);
						}.bind(this)
					})
				}.bind(this))
			}
		} else {
			return false;
		}
		
		this.setActive(0);
	},
	
	setPbInterval: function(interval) {
		this.interval = interval;
	},
	
	/**
	 * hides slides which are not displayed
	 * @return void
	 */
	initSlides: function() {
		this.slides.each(function(slide, index) {
			slide.setStyles({
				position: 'absolute',
				top: 0,
				left: 0
			})
			slide.addEvents({
				'mouseenter': function() {
					this.progressfx.stop();
				}.bind(this),
				'mouseleave': function() {
					var intProgress = (this.progressbar.getStyle('left').toInt()) / this.progressbarWidth;
					this.progressfx.options.duration = -(this.progressfx.options.duration * intProgress);
					this.progressfx.start(this.progressbarWidth * intProgress, 0);
				}.bind(this)
			})
			if (this.current != index) {
				new Fx.Style(slide, 'opacity').set(0);
			}
		}.bind(this));
	},
	
	/**
	 * show slide with parameter index
	 * @param integer index of slide to display
	 * @return void
	 */
	showSlide: function(slideindex) {
		
		if(this.slidefx) this.slidefx.stop();
		
		this.slidefx = new Fx.Style(this.slides[this.current], 'opacity', {
			onComplete: function(){
				new Fx.Style(this.slides[slideindex], 'opacity').start(0,1);
				this.setActive(slideindex);
				this.current = slideindex;
				this.start();
			}.bind(this)
		}).start(1,0);
		
	},
	
	/**
	 * sets correct navigation item
	 * @return void
	 */
	setActive: function(slideindex) {
		
		this.navitems.each(function(item, index) {
			if (index != slideindex) {
				item.removeClass('active');
			} else {
				item.addClass('active');
			}
		});
	},
	
	/**
	 * resets progressbar
	 * @return void
	 */
	resetProgressBar: function() {
		if(this.progressbar) {
			this.progressbar.setStyle('left', -this.progressbarWidth);
		}
		if(this.progressfx) {
			this.progressfx.stop();
		}
		
		this.progressfx = new Fx.Style(this.progressbar, 'left', {
			duration: (this.interval*1000).toInt(),
			transition: Fx.Transitions.linear,
			onComplete: function() {
				this.showSlide(nextSlide);
			}.bind(this)
		});
	},
	
	/**
	 * starts auto looping pushbox
	 * @return void
	 */
	start: function() {

		this.resetProgressBar();
		
		nextSlide = this.current+1;
		if (this.current >= this.slides.length-1) {
			nextSlide = 0;
		}
		if (this.current < 0) {
			nextSlide = 0;
		}
		
		this.progressfx.start(-this.progressbarWidth, 0);
	}
});
