// JavaScript Document

/*
scrollers behaviour


*/

var JScrollers = new Class({

    getOptions: function(){
        return {
        };
    },
    initialize: function(dlist, options){
        this.setOptions(this.getOptions(), options);
        this.dlist = $(dlist);
				
		if ( this.dlist.hasClass('clickScroller') )
		{
		    this.inner = dlist.getFirst('div');
		    this.outerSize = dlist.getSize();
		   //alert('out width=' + this.innerSize.size.x + ' inner height=' + this.innerSize.size.y);
		    //alert('outer width=' + this.outerSize.size.x + ' offset size=' + this.offsetWidth);
		    this.innerSize = this.inner.getSize();
		    this.currLeft = 0;
		    this.currTop = 0;
			
			this.firstpane = this.inner.getFirst('div');
			this.margin = this.firstpane.getStyle('margin').toInt();
			this.direction = 'horizontal';
			if ( this.dlist.hasClass('vertical') )
			{
				this.direction = 'vertical';
			}

						
            this.forwardButton = new Element('div', {'html':'&nbsp;'}).injectAfter(this.dlist).addClass('forwardButton');
			this.forwardButton.setStyle('cursor', 'pointer');
			this.forwardButton.addClass(this.dlist.id + '_forward' );
			this.forwardButton.addClass(this.direction);
			if ( this.direction == 'horizontal')
			{			
			   this.forwardButton.addEvent('click', this.scrollForward.bind(this));
			}
			else
			{
			   this.forwardButton.addEvent('click', this.scrollDown.bind(this));
			}
			
            this.backButton = new Element('div',  {'html':'&nbsp;'}).injectBefore(this.dlist).addClass('backButton');
			this.backButton.setStyle('cursor', 'pointer');			
			this.backButton.addClass(this.dlist.id + '_back' );
			this.backButton.addClass(this.direction);	
			if ( this.direction == 'horizontal')
			{
			    this.backButton.addEvent('click', this.scrollBack.bind(this));
			}
			else
			{
			    this.backButton.addEvent('click', this.scrollUp.bind(this));
			}
            this.scroller = new Fx.Scroll(this.dlist, {offset: { 'x': 0, 'y': 0 }});			
		}
		else
		{
           this.scroller = new Scroller(this.dlist, {area: 100, velocity: 1});
           this.dlist.addEvent('mouseover', this.scroller.start.bind(this.scroller));
           this.dlist.addEvent('mouseout', this.scroller.stop.bind(this.scroller));	
           this.dlist.setStyle('cursor', 'move');		   
		}
		
	},
	scrollForward : function(){
		if ( (this.currLeft + this.outerSize.size.x ) < this.innerSize.scrollSize.x )
		{
			this.currLeft = this.currLeft + this.outerSize.size.x-this.margin;
		}
		this.scroller.scrollTo( this.currLeft, this.currTop );
		//alert('inner scroll left=' + this.currLeft + ' inner scroll height=' + this.currTop);	
		
	},
	scrollBack : function(){
		
		if ( (this.currLeft - this.outerSize.size.x ) > 0 )
		{
			this.currLeft = this.currLeft - this.outerSize.size.x+this.margin;
		}
		else
		{
		  this.currLeft = 0;	
		}
		
		this.scroller.scrollTo( this.currLeft, this.currTop );
		
		
	},
	scrollDown : function(){
		
		if ( (this.currTop + this.outerSize.size.y) < this.innerSize.scrollSize.y )
		{
			this.currTop = this.currTop + this.outerSize.size.y;
		}

		this.scroller.scrollTo( this.currLeft, this.currTop );
		//alert('inner scroll left=' + this.currLeft + ' inner scroll height=' + this.currTop);	
		
	},
	scrollUp : function(){
		
		if ( (this.currTop - this.outerSize.size.y) > 0 )
		{
			this.currTop = this.currTop - this.outerSize.size.y;
		}
		else
		{
			this.currTop = 0;
		}
		this.scroller.scrollTo( this.currLeft, this.currTop );
		
		
	}


});

JScrollers.implement(new Events);
JScrollers.implement(new Options);

//alert('scrollpane included');

