/**
 * @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/  */
/* Modified by Pius Jeon <pjeon@usyd.edu.au> to add option to loop through sections instead of rewinding*/

Glider = Class.create();
Object.extend(Object.extend(Glider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options, loop){
	    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.loop       = loop;
	    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;
	      var currentIndex = 0;
	    }
	    this.moveTo(this.sections[nextIndex], this.scroller, { duration: this.options.duration });
		
		/* move section to the end*/
		if (this.loop) {
		  //this.repositionElement('next',currentIndex);
		  setTimeout(this.repositionElement.bind(this, 'next', currentIndex),this.options.duration * 1000);
		}
	},
	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);
	},
	repositionElement: function(mode, index){
		var prevSection = this.sections[index];
		var prevSectionContent = prevSection.innerHTML;
		var prevSectionId = prevSection.id
		var parentElem = prevSection.parentNode;
		var newSection = document.createElement('div');
		newSection.setAttribute('class','section');
		newSection.setAttribute('id',prevSectionId);
		newSection.innerHTML = prevSectionContent;
		if (mode == 'next') 
		  parentElem.appendChild(newSection);
		else // mode == 'prev'
		  parentElem.appendChild(newSection);
		this.sections[index] = newSection;
		this.sections[index]._index = index;
		//parentElem.removeChild(prevSection);
	}
});