/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @author Joe Cai <joey.cai@gmail.com>
 * @copyright 2007 Curbly LLC
 * @package Glider
 * @license MIT
 * @url http://www.missingmethod.com/projects/glider/
 * @version 0.0.5
 * @dependencies prototype.js 1.5.1+, effects.js
 */
/*  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.wrapper    = $(wrapper);
	    this.sections   = this.wrapper.getElementsBySelector('div.section');
	    this.options    = Object.extend({ controlsEvent:'click', duration: 1.0, frequency: 3 }, options || {});
	    
	    this.wrapper.down('.controls').setStyle({zIndex: this.sections.length});

	    for(var i = 0; i < this.sections.length; i ++) {
	    	this.sections[i]._index = i;
	    	this.sections[i].setStyle({zIndex: this.sections.length - i});
	    	if(i > 0) {
	    		this.sections[i].setOpacity(0);
	    	}
	    }
	    
	    this.events = {
	    	click: this.click.bind(this)
	    };

	    this.addObservers();
		if(this.options.initialSection) this.switchTo(this.options.initialSection);  // 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);
	},	

    click: function(event) {
		this.stop();
		var element = Event.findElement(event, 'a');
		
		switchTo = element.href.split("#")[1];
		this.switchTo(switchTo);
		Event.stop(event);
	},

	switchTo: function(element) {
		oldId = (this.current ? this.current._index : 0) + 1;
		old = $('section' + oldId);
		this.current = $(element);
		if(old == this.current) {
			return false;
		}
				
		this.controls.each(function(control) {
			var theTarget = control.href.split("#")[1];
			if (theTarget == $(element).id) {
				control.addClassName("active")
			} else {
				control.removeClassName("active")
			}
		});
		
		Effect.Fade(old);
		currentDiv = this.current;
		setTimeout("Effect.Appear(currentDiv);", 0);
		
		return false;
	},
		
	next: function(){
		if (this.current) {
			var currentIndex = this.current._index;
			var nextIndex = (this.sections.length - 1 == currentIndex) ? 0 : currentIndex + 1;      
		} else { 
			var nextIndex = 1;
		}
		
		this.switchTo(this.sections[nextIndex]);
	},
	
	previous: function() {
		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.switchTo(this.sections[prevIndex]);
    },

	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);
	}
});