/*
mooVimgSlide
06/2008 : massacred by christopher wait aka virtualgadjo aka grizzly aka l'ôt fou

s'appuie sur mootools1.2


########## HOW TO ##########
oo -- les Options -- oo

############################

Have swing
*/

var mooVimgSlide = new Class ({

	Implements: [Options, Events],

	options: {
		between   : 5,
		leftPad   : 5,
		topPad    : 5,
		inertie   : 30,
		autoStart : 'left' //(left, right, no)
	},

	initialize: function(container, slide, options){

		this.setOptions(options);
		this.container	= container;
		this.ziLeft		= this.container.getPosition().x;
		this.ziWidth	= this.container.getCoordinates().width;
		this.slide		= slide;
		this.slides		= this.slide.getChildren();
		this.slideW		= this.options.between * this.slides.length * 2;
		this.speed		= - 1 / this.options.inertie;

		this.slides.each(function(el){
			var newSlide = el.clone().inject(slide, 'bottom');
			var elWidth = el.getSize().x;
			this.slideW = this.slideW + (elWidth * 2);
		}.bind(this));

		this.half = this.slideW / 2; //alert (this.half);
		this.initX		= ((this.half) - this.ziWidth) / 2;

		//alert(this.initX);
		this.slide.setStyles({
			'width'    : this.slideW,
			'position' : 'absolute',
			'left'     : - this.initX,
			'top'      : this.options.topPad
		});
		
		this.setMove();
		if (this.move != 0) this.defil();
		this.mousePlay();
	},

	setMove: function(){
		switch (this.options.autoStart){
			case 'left'  : this.move = - 1; break;
			case 'right' : this.move = 1; break;
			case 'no'    : this.move = 0; break;
			default      : this.move = 0; break;
		}
	},

	defil: function(){
		this.slideL = this.slide.getCoordinates(this.container).left;
		this.slide.setStyle('left', this.slideL + this.move);

		if (this.slideL >=  - (this.half / 20)) {
			this.slide.setStyle('left', - (this.half + this.half / 20));
		}
		if (this.slideL <= (- (this.half + (this.half / 4)))) {
			this.slide.setStyle('left', - this.half / 4);
		}
		this.enRoute = this.defil.delay(15, this);
	},

	mousePlay: function(){
		this.container.addEvents({
			'mouseenter'	: function(){
				if (this.options.autoStart == 'no') this.defil();
			}.bind(this),
			'mousemove'		: function(e){
				this.mouseX	= e.client.x;
				this.diff	= (this.mouseX - (this.ziLeft - window.getScroll().x)) - (this.ziWidth / 2);
				this.move	= this.diff * this.speed;
			}.bind(this),
			'mouseleave'	: function(){
				if (this.options.autoStart == 'no'){
					$clear(this.enRoute);
					this.move = 0;
				}
				else {
					this.setMove();
				}
			}.bind(this)
		});
	}

});
