/************************************************
*   scrollerMoo v.0                             *
*   Http: WwW.developer.ps/moo/scrollermoo      *
*   Dirar Abu Kteish dirar@zanstudio.com        *
*   The code was inspired from jd.gallery.js    *
*    and jd.gallery.css. WebSite:               *
*    smoothgallery.jondesign.net                *
/***********************************************/

var scrollerMoo = new Class({
    initialize: function(element, options) {
		this.setOptions({
			steps: 1, 
			wrapperClass: 'div.scrollerMooWrapper',
			scrollerItemSelector: 'div.scrollerMooItem',
			direction:1,
			numdiv : 3
	    }, options);
	    this.currentPos = 0;
	    this.scrollerItems = null;
	    this.scrollerElement = element;
	    this.wrapperDiv = element.getElement(this.options.wrapperClass);
	    this.scrollEff = new Fx.Scroll(this.wrapperDiv, {wait: false,duration: 1000,
                                        offset: {'x': -3, 'y': 0},
                                        transition: Fx.Transitions.Quad.easeInOut
                        });
	    this.initScrollerMoo();
		periodical = this.scrollToItem.periodical(2000,this);
		
		
	    
	},
	initScrollerMoo: function() {
	    var ele = this.scrollerElement;
		var options = this.options;
		this.scrollerItems = this.wrapperDiv.getElements(options.scrollerItemSelector);
		if (this.scrollerItems.length > 1)
		{
			new Element('a').addClass('left').addEvent(
				'click',function(){this.options.direction = -1; this.scrollToItem(); $clear(periodical);}.bind(this)).injectBefore(this.wrapperDiv);
			new Element('a').addClass('right').addEvent(
				'click',function(){
					this.options.direction = 1; 
					this.scrollToItem(); 
					$clear(periodical);
					}.bind(this)).injectAfter(this.wrapperDiv);
		}		
	},
	scrollToItem: function() {
        var move = this.options.direction * this.options.steps;
        var index = (this.scrollerItems[this.currentPos+move]) ? this.currentPos+move : ((this.currentPos+move) > 0) ? this.scrollerItems.length-1 : 0;
        //alert(index);
        if(((index + 1) >= this.scrollerItems.length) && (index % this.options.steps) != 0){
			if (this.options.direction==1) {
				this.options.direction = -1;
				index = this.currentPos - 1;
			}
			else {
				this.options.direction = 1;
				index = this.currentPos + 1;
			};
			this.scrollEff.toElement(this.scrollerItems[index]);
			this.currentPos = index;
			return;
		}
		else {
			if ((this.scrollerItems.length - this.options.numdiv) == this.currentPos) {
				index = 0;
			};
		}
        this.scrollEff.toElement(this.scrollerItems[index]);
        this.currentPos = index;
	},
	stopscroll: function () {
		$clear(periodical);
	}
	
});
scrollerMoo.implement(new Options);

