/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://www.missingmethod.com/projects/glider/
 * @version 0.0.5
 */
/* Modified by djessup <djessup@usyd.edu.au>  to reset scroll timer when next/prev is user-initiated; some formatting */
/*  Thanks to Andrew Dupont for refactoring help and code cleanup - http://andrewdupont.net/  */

Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
	    this.scrolling  = true;
	    this.wrapper    = $(wrapper);
	    this.scroller   = this.wrapper.down('div.scroller');
	    this.sections   = this.wrapper.getElementsBySelector('div.section');
	    this.options    = Object.extend({ controlsEvent:'click', duration: 1.0, frequency: 3 }, options || {});
	    this.sections.each( function(section, index) {
	      section._index = index;
	    });
	    this.events = { };
	    this.addObservers();
			if(this.options.initialSection) this.moveTo(this.options.initialSection, this.scroller, { duration:this.options.duration });  // initialSection should be the id of the section you want to show up on load
			if(this.options.autoGlide) this.start();
	},	
	addObservers: function() {
		this.controls = this.wrapper.getElementsBySelector('.controls a');
		this.controls.invoke('observe', this.options.controlsEvent, this.events.click);
	},
	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;
	},	
	moveNext: function(manualMove){
		if (manualMove != null) { this.periodicallyUpdate(true); }	  
	    if (this.current) {
	      var currentIndex = this.current._index;
	      var nextIndex = (this.sections.length - 1 == currentIndex) ? 0 : currentIndex + 1;      
	    } else var nextIndex = 1;
	    this.moveTo(this.sections[nextIndex], this.scroller, { duration: this.options.duration });
	},
	movePrevious: function(manualMove){
	  	if (manualMove != null) { this.periodicallyUpdate(true); }		
	    if (this.current) {
	      var currentIndex = this.current._index;
	      var prevIndex = (currentIndex == 0) ? this.sections.length - 1 : 
	       currentIndex - 1;
	    } else var prevIndex = this.sections.length - 1;	    
	    this.moveTo(this.sections[prevIndex], this.scroller, { duration: this.options.duration });
	},
  	next: function() { this.moveNext(true); },	
	previous: function() { this.movePrevious(true); },  
	stop: function() { clearTimeout(this.timer); },	
	start: function() {	this.periodicallyUpdate(); },		
	periodicallyUpdate: function(dontMove) { 
		if (this.timer != null) {
			clearTimeout(this.timer);
			if (dontMove != true) {	this.moveNext(); }
		}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency*1000);
	}
});