/**
 * @author acerutti - Alfredo Cerutti
 * @email : alfredo.cerutti@intercom.it
 * 
 * 
 * semplice slide
 * 
 * 	var slide = new iSlide({
 *		wrapper: 'pagination_content_showroom',
 *		stepping: 500,
 *		iboxItemClass: '.pagination_item_showroom'		
 *		});
 *
 * <a id="go-left" href="#" onclick="slide.previous(); return false;"> indietro </a>
 * <a id="go-right" href="#" onclick="slide.next(); return false;"> avanti </a>
 * 
 */


iSlide = Class.create();

Object.extend(Object.extend(iSlide.prototype, Abstract.prototype), {	
	initialize: function(conf){		
		this.wrapper = $(conf.wrapper) || null;
		this.stepping = conf.stepping || (this.wrapper.getDimensions()[1] / 3)			    
	    this.options    = Object.extend({ duration: 1.0, frequency: 3 }, conf.options || {});
		this.iboxItemClass = conf.iboxItemClass || null;
		this.max_steps = 1;
		this.currentStep = 1; 
	
		if (this.iboxItemClass) this._getContainerWidth();
	},
	
	/**
	 * move the main div
	 * @param {Object} shift : +1 / -1
	 * @param {Object} options
	 */
	moveTo: function(shift, options){
		step = this.currentStep + shift;
		
		// limits				
		if (step < 1 ) step = 1;
		if (step >= this.max_steps) step = this.max_steps;
		
		if (step != this.currentStep) { // allow move?										
			var new_pos = (step > this.currentStep ) ? -this.stepping : this.stepping;				
			new Effect.Move(this.wrapper, { x: new_pos }, options);			
			this.currentStep = step;
		}		
		return false;
	},
		
	/**
	 * move to next step
	 */
	next: function(){		
		this.moveTo( +1, { duration: this.options.duration });
	},
		
	/**
	 * move to previous step
	 */
	previous: function(){
		this.moveTo( -1, { duration: this.options.duration });
	},

	/**
	 * calculate the total width of the items contained in the main div
	 */
	_getContainerWidth: function() {
		var items = $$(this.iboxItemClass);
		w = 0;	
		items.each(function(i){
			w += i.getDimensions().width;
		});
		this.widthContainer = w;
		this._getNSteps();		
	},
	
	/**
	 * calculate the number of steps
	 */
	_getNSteps: function() {
		this.max_steps = Math.ceil(this.widthContainer / this.stepping);
	}

});



