/**
 * basic pushbox functionality
 *
 * @author Rocco Janse <rocco@efocus.nl>
 * @since 1.0, 04 march 2010
 * @package efocus js lib
 */
 
 var Pushbox = new Class({
 	
 	Implements: Options,
 	
 	options: {
		viewport: null,
		slidelist: null,
		nav: {
			back: null,
			forward: null
		}
 	},
 	
 	arrSlides: [],
 	intCurrentIndex: 0,
 	
 	initialize: function(options) {
 		
 		// parse options
 		this.setOptions(options);
 		
 		// init slides
 		this.arrSlides = this.getSlides();
 		this.initSlideList(); 	
 		
 		// init nav
 		this.initNavigation();

 	},

 	/**
 	 * gets all slides sources and widths
 	 * @return array slides
 	 */
 	getSlides: function() {
  		var arrSlides = [];
 		this.options.slidelist.getChildren('li').each(function(elSlide) {
 			var arrResult = [elSlide,elSlide.getWidth().toInt()];
 			arrSlides.include(arrResult);
 		});
 		return arrSlides;
 	},
 	
 	/**
 	 * sets width of slidelist based on detected amount of slides
 	 * @return void
 	 */
 	initSlideList: function() {
 		var intNewWidth = 0;
 		this.arrSlides.each(function(arrSlidedata) {
 			intNewWidth += arrSlidedata[1];
 		});
 		this.options.slidelist.setStyle('width',intNewWidth);
 	},
 	
 	/**
 	 * handles disabled/enabled states of navigation
 	 * @return void
 	 */
 	initNavigation: function() {
 		if (this.intCurrentIndex >= this.arrSlides.length-1) {
 			this.options.nav.forward.addClass('disabled');
 		} else {
 			this.options.nav.forward.removeClass('disabled');
 		}
		if (this.intCurrentIndex <= 0) {
			this.options.nav.back.addClass('disabled');
		} else {
			this.options.nav.back.removeClass('disabled');
		}
 	},
 
 	/**
 	 * go to previous slide
 	 * @return void
 	 */
 	gotoPrevious: function() {
 		if (this.intCurrentIndex > 0) {
 			this.showSlide(this.intCurrentIndex-1);
 		}
 	},

 	/**
 	 * go to next slide
 	 * @return void
 	 */
 	gotoNext: function() {
 		if (this.intCurrentIndex < this.arrSlides.length-1) {
 			this.showSlide(this.intCurrentIndex+1);
 		}
 	},

 	/**
 	 * displays requested slide and re-inits
 	 * @return void
 	 */
 	showSlide: function(intSlideIndex) {

 		var objScroll = new Fx.Scroll(this.options.viewport, {
 		    wheelStops: false,
 		    onComplete: function() {
 				this.initialize();
 			}.bind(this)
 		});
 		
 		var intStartPos = this.getPosition(this.intCurrentIndex);
 		var intScrollAmount = this.getPositionDifference(intSlideIndex);
 		this.intCurrentIndex = intSlideIndex;
 		objScroll.start(intScrollAmount, 0);
 
 	},

 	/**
 	 * get position of slide in pixels
 	 * @param integer index of slide to get position of
 	 */
 	getPosition: function(intIndex) {
		var intPosition = 0;
 		for(i = 0; i < intIndex; i++) {
 			intPosition += this.arrSlides[i][1];
 		} 
 		return intPosition;
 	},
 	
 	/**
 	 * get amount to scroll in pixels
 	 * @param integer index of slide to scroll to
 	 */
 	getPositionDifference: function(intIndex) {
 		var intAmount = 0;

 		if (this.intCurrentIndex < intIndex) {
 	 		for(i = 0; i < intIndex; i++) {
 	 			intAmount += this.arrSlides[i][1];
	 		} 
 		}
 		if (this.intCurrentIndex > intIndex) {
 			intAmount = this.getPosition(this.intCurrentIndex-1);
 			for(i = intIndex; i > this.intCurrentIndex; i--) {
 	 			intAmount -= this.arrSlides[i][1];
	 		} 
 		}
 		return intAmount;
 	}
 	
 });