/**
 * @author Morgan Roderick <morgan@roderick.dk>
 * @copyright 2007 Roderick IT
 * @package Scroller
 * @license MIT
 * @url http://roderick.dk/projects/scroller/
 * @version 0.0.1
 * @dependencies prototype.js 1.5.1+, effects.js, Effect.SmoothScroll by Bruno Bornsztein
 */

Scroller = Class.create();
Object.extend( Object.extend( Scroller.prototype, Abstract.prototype), {

	initialize : function( wrapper, options ) {
		this.scrolling  = false;
	    this.wrapper    = $(wrapper);
	    this.scroller   = this.wrapper.down( 'div.scroller' );
	    this.lines   	= this.wrapper.getElementsBySelector('li');
	    this.options    = Object.extend({ duration: 1.0, frequency: 3 }, options || {});
		this.area 		= {
			top_left : {
				x : Number( Position.cumulativeOffset( this.wrapper )[0] ),
				y : Number( Position.cumulativeOffset( this.wrapper )[1] )
			},
			bottom_right : {
				x : Position.cumulativeOffset( this.wrapper )[0] + this.wrapper.getDimensions().width,
				y : Position.cumulativeOffset( this.wrapper )[1] + this.wrapper.getDimensions().height
			}
		};

	    this.lines.each( function( line, index ) { line._index = index; } );    

	    this.events = { 
			mouseover : this.mouseover.bind( this ),
			mouseout : this.mouseout.bind( this ) 
		};

	    this.addObservers();
		if( this.options.initialLine ) {
			this.moveTo( this.options.initialLine, this.scroller, { duration : this.options.duration } );  // initialLine should be the id of the line you want to show up on load
		}
		if( this.options.autoScroll ) {
			this.start();
		}
	},
	
	addObservers : function() {
		Event.observe( this.wrapper, "mouseover", this.events.mouseover );
		Event.observe( this.wrapper, "mouseout", this.events.mouseout );
	},	

	mouseover : function( event ) {
		this.stop();
		if ( this.scrolling ) {
			this.scrolling.cancel();
		}

	    Event.stop(event);
	},
	
	mouseout : function ( event ) {
		if ( !this.mouseOverArea( event, this.area ) ) {
			this.start();	
		}		
	},
	
	next : function() {
		if ( this.current ) {
			var currentIndex = this.current._index;
			var nextIndex = ( this.lines.length - 1 == currentIndex ) ? 0 : currentIndex + 1;      
		} else {
			var nextIndex = 1;	
		}

		this.moveTo( this.lines[ nextIndex ], this.scroller, { 
	    	duration: this.options.duration
	  	});
	},

	previous : function() {
		if ( this.current ) {
			var currentIndex = this.current._index;
	    	var prevIndex = ( currentIndex == 0 ) ? this.sections.length - 1 : 
	     	currentIndex - 1;
	  	} else {
			var prevIndex = this.lines.length - 1;	
		}
	  	this.moveTo( this.lines[ prevIndex ], this.scroller, { 
			duration: this.options.duration
		});
	},
	
	
	moveTo: function( element, container, options ) {
		this.current = $(element);
		Position.prepare();
	    var containerOffset = Position.cumulativeOffset( container );
		elementOffset = Position.cumulativeOffset($(element));

		this.scrolling 	= new Effect.SmoothScroll( container, { duration: options.duration, x:(elementOffset[0]-containerOffset[0]), y:(elementOffset[1]-containerOffset[1])});
		return false;
	},

	stop : function() {
		clearTimeout( this.timer );
	},
	
	start : function() {
		this.periodicallyUpdate();
	},
		
	periodicallyUpdate : function()	{ 
		if ( this.timer != null ) {
			clearTimeout( this.timer );
			this.next();
		}
		this.timer = setTimeout( this.periodicallyUpdate.bind( this ), this.options.frequency * 1000 );
	},
	
	mouseOverArea : function( event, area ){
		var x = Event.pointerX( event );
		var y = Event.pointerY( event );
		return ( ( area.top_left.x <= x && x <= area.bottom_right.x ) && ( area.top_left.y <= y && y <= area.bottom_right.y ) );
	}
});

/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://www.missingmethod.com/projects/glider/
*/
Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
   
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
   
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } 
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});
