//this function is required by the content slider class
//i broke it out of the class because it's generally kinda
//useful, but included it in this file cause like i said, it's required
function getURLVar(urlVarName) {
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');

		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

ContentSlider = Class.create({
	initialize: function(slider_id, slide_class, options) {
		
		//set the default options
		//other available options not listed here include: 
		//beforeStart, afterFinish
		this.options = {
			backButton: 'button-back',
			forwardButton: 'button-forward',
			unitWidth: 800,
			visibleUnits: 1,
			duration: 1,
			activeClass: 'active'
		}
		Object.extend(this.options, options || {});
		
		//set up the initial methods/vars
		this.current_unit = 0;
		this.slider = $(slider_id);
		this.back_button = $(this.options.backButton);
		this.forward_button = $(this.options.forwardButton);
		this.slides = $$('#' + slider_id + ' .' + slide_class);
		this.total_slides = Math.ceil(this.slides.length / 3);
		this.total_width = this.total_slides * this.options.unitWidth;
				
		this.back_button.onclick = function() { return false; }
		this.forward_button.onclick = function() { return false; }
		
		//set up the event caches
		//this is so stopObserving will work
		this.scrollCache = new Array();
		this.forward_button_cache = this.forwardButtonPressed.bindAsEventListener(this)
		this.back_button_cache = this.backButtonPressed.bindAsEventListener(this)
		
		//enable the buttons
		this.enableButtons();
		
		//scroll to the slide identified in the URL if available
		this.slideByNumber();
	},
	
	slideByNumber: function() {
		if (getURLVar('slide')) {
			this.current_unit = getURLVar('slide');
			new Effect.MoveBy(this.slider, ((this.current_unit * this.options.unitWidth) + this.slider.offsetTop) * -1, 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		}
	},
	
	scrollToItem: function(event, link) {
		//self explanatory, scroll to the item based on which button was pressed.
		this.current_unit = link.rel
		new Effect.MoveBy(this.slider, ((this.current_unit * this.options.unitWidth) + this.slider.offsetTop) * -1, 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
	},
	
	forwardButtonPressed: function() {
		//zoom back to the beginning if we're at the end
		if (this.slider.offsetTop == (this.total_width - this.options.unitWidth) * -1) { 
			this.current_unit = 0;
			new Effect.MoveBy(this.slider, this.total_width - this.options.unitWidth, 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		} else {	
			this.current_unit = ((this.slider.offsetTop / this.options.unitWidth) -1) * -1;
			new Effect.MoveBy(this.slider, -1 * (this.options.visibleUnits * this.options.unitWidth), 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		}
	},
	
	backButtonPressed: function() {
		//in the case of vertical sliders, backward is up
		// 
		//zoom all the way to the end if we're at the beginning
		if (this.slider.offsetTop == 0) {
			this.current_unit = this.total_units - 1;
			new Effect.MoveBy(this.slider, (this.total_width - this.options.unitWidth) * -1, 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		} else {
			this.current_unit = ((this.slider.offsetTop / this.options.unitWidth) + 1) * -1;
			new Effect.MoveBy(this.slider, this.options.visibleUnits * this.options.unitWidth, 0, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) } );
		}
	},
	
	disableButtons: function() {
		Event.stopObserving(this.forward_button, 'click', this.forward_button_cache)
		Event.stopObserving(this.back_button, 'click', this.back_button_cache);
	},
	
	enableButtons: function() {
		Event.observe(this.forward_button, 'click', this.forward_button_cache);
		Event.observe(this.back_button, 'click', this.back_button_cache);
	},
	
	beforeSlide: function() {
		this.disableButtons();
		if(this.options.beforeStart) this.options.beforeStart(this);
	},
	
	afterSlide: function() {
		this.enableButtons();
		if(this.options.afterFinish) this.options.afterFinish(this);
	}
	
});

