/* QScroller Copyright 2008 Massimo Giagnoni. All rights reserved.

Vesrion 1.0.1 (Mootools 1.11)
QScroller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
var QScroller = new Class({
	options: {
		slides: 'qslide',
		direction: 'h',
		duration: 3000,
		auto: false,
		delay: 1000,
		transition: Fx.Transitions.linear
	},
	initialize: function(wrapper,options) {
		this.setOptions(options);
		this.wrapper = $(wrapper);
		this.wrapper.setStyles({
			position: 'relative',
			overflow: 'hidden'
		});
				
		this.shiftOut = new Element('div').setStyles({
			position: 'absolute',
			overflow: 'hidden',
			top: 0,
			left: 0,
			width: this.wrapper.getStyle('width'),
			height: this.wrapper.getStyle('height')
		}).injectInside(this.wrapper);

		this.shiftIn = this.shiftOut.clone();
		this.shiftIn.injectInside(this.wrapper);
				
		this.slides = $$('.'+this.options.slides);
		
		this.auto = this.options.auto;
		this.idxSlide = 0;
		this.step = 0;
		this.isFirst = true;
	},
	load: function() {
		if(!this.isFirst) {
			this.idxSlide += this.step;
			if(this.idxSlide > this.slides.length-1) {
				this.idxSlide = 0;
			} else if(this.idxSlide < 0) {
				this.idxSlide = this.slides.length-1;
			}
		}
		this.curSlide = this.slides[this.idxSlide].clone();
		this.show();
	},
	show: function() {
		var slide = this.shiftIn.getElement('div');
		if(slide) {
			//slide.replaceWith(this.curSlide);
		} else {
			this.curSlide.injectInside(this.shiftIn);
		}
		this.doEffect();
	},
	doEffect: function() {
		this.fxOn = true;
		var d = this.isFirst ? 0:this.options.duration;
		var t = this.options.transition;

		this.shiftOut.set('tween', {duration: d, transition: t});
		this.shiftIn.set('tween', {duration: d, transition: t});
		/*
		var fxObj = this.shiftIn;
		fxObj.set('tween', {
			duration:d,
			transition: t
		});
		*/
		var inX = 0;
		var inY = 0;
		var outX = 0;
		var outY = 0;
		var ww = this.wrapper.getStyle('width').toInt();
		var wh = this.wrapper.getStyle('height').toInt();
		if(this.step > 0) {
			if(this.options.direction == 'h') {
				inX = ww;
				outX = - ww;
			} else {
				inY = -wh;
				outY = wh;
			}
		} else {
			if(this.options.direction == 'h') {
				inX = ww;
				outX = -ww;
			} else {
				inY =  wh;
				outY = -wh;
			}
		}
		if(this.isFirst) {
			if(this.auto) {
				this.step = 1;
			}
			this.isFirst = false;
		}

		/*
		fxObj.start({
			top: [inY, 0],
			left: [inX, 0]
		});
		*/
		this.shiftIn.tween('left', inX, 0);
		this.shiftOut.tween('left', 0, outX);
		/*
		this.shiftOut.effects({
			duration: d,
			transition: t
		}).start({
			top: [0, outY],
			left: [0, outX]
		});
		*/
		/*
		this.shiftOut.set('tween', {duration: d});
			this.shiftOut.tween('left', outX);
		this.shiftIn.set('tween', {duration: d});
			this.shiftIn.tween('left', outX);
		*/
		this.fxEnd.delay(d + 0, this);
	},
	fxEnd: function() {
		this.fxOn = false;
		this.swapSlides();
		if(this.auto) {
			$clear(this.timer);
			this.timer = this.load.delay(this.options.delay, this);
		}
	},
	swapSlides: function() {
		this.shiftOut.setStyles({
			zIndex: 0,
			opacity: 1
		});
		var t = this.shiftOut;
		this.shiftOut =this.shiftIn;
		this.shiftIn = t;
	}
});
QScroller.implement(new Options, new Events);
