/* Slider v1.0 by JD Neff
	Basic slider class created for use with script.aculo.us (http://script.aculo.us/) and prototype (http://prototypejs.org/) libraries.
*/
var Slider = Class.create();
Slider.prototype = {
	animating : false,
	effects : [],
	initialize : function(container, options) {
		this.container = container;
		this.options = Object.extend({
			forwardName : 'forward',
			backName : 'backward',
			outerName : 'sliderOuter',
			innerName : 'sliderInner',
			itemName : 'sliderItem',
			disabledName : 'disabled',
			direction : 'horizontal',
			backTitle : 'Previous',
			forwardTitle : 'Next',
			sizeUnit : 'px',
			size : 1
		}, options || {});
		this.count = $$('#'+this.container+' .'+this.options.itemName).length;
		this.innerContainer = $$('#'+this.container+' .'+this.options.innerName)[0];
		this.back = $$('#'+this.container+' .'+this.options.backName)[0];
		this.forward = $$('#'+this.container+' .'+this.options.forwardName)[0];
	},

	activate : function() {
		Event.observe(this.back, 'click', this.goBack.bind(this), false);
		Event.observe(this.forward, 'click', this.goForward.bind(this), false);
		this.checkButtons();
	},

	goBack : function() {
		if (Math.abs(parseFloat(this.innerContainer.style.left || 0)) / this.options.size < this.count - 1 && ! this.animating){
			this.animating = true;
			this.effects.push(new Effect.MoveBy(this.innerContainer, 0, 0 - this.options.size));
			new Effect.Parallel(this.effects, {afterFinish:function(){this.animating=false;this.checkButtons()}.bind(this)});
			this.effects = [];
		}
	},

	goForward : function() {
		if (Math.abs(parseFloat(this.innerContainer.style.left || 0)) / this.options.size > 0 && ! this.animating){
			this.animating = true;
			this.effects.push(new Effect.MoveBy(this.innerContainer, 0, this.options.size));
			new Effect.Parallel(this.effects, {afterFinish:function(){this.animating=false;this.checkButtons()}.bind(this)});
			this.effects = [];
		}
	},

	checkButtons : function() {
		if (Math.abs(parseFloat(this.innerContainer.style.left || 0)) / this.options.size == 0){this.forward.addClassName(this.options.disabledName);this.forward.title='';}else{this.forward.removeClassName(this.options.disabledName);this.forward.title=this.options.forwardTitle;}
		if (Math.abs(parseFloat(this.innerContainer.style.left || 0)) / this.options.size == this.count - 1){this.back.addClassName(this.options.disabledName);this.back.title='';}else{this.back.removeClassName(this.options.disabledName);this.back.title=this.options.backTitle;}
	}
}
