//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, buttonsSelector
		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.menu_buttons = $$(this.options.buttonsSelector);
		this.slides = $$('#' + slider_id + ' .' + slide_class);
		this.total_slides = this.slides.length;
		this.total_width = this.total_slides * this.options.unitWidth;
		
		
		//set up the menu buttons to have a rel attribute which is required for relating them to a slide
		//also set the first menu button as active.  
		for(i = 0; i < this.menu_buttons.length; i++) {
			this.menu_buttons[i].onclick = function() { return false; }
			this.menu_buttons[i].rel = i;
		}
		this.menu_buttons[0].addClassName(this.options.activeClass);
		
		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, 0, ((this.current_unit * this.options.unitWidth) + this.slider.offsetLeft) * -1, { 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, 0, ((this.current_unit * this.options.unitWidth) + this.slider.offsetLeft) * -1, { 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.offsetLeft == (this.total_width - this.options.unitWidth) * -1) { 
			this.current_unit = 0;
			new Effect.MoveBy(this.slider, 0, this.total_width - this.options.unitWidth, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		} else {	
			this.current_unit = ((this.slider.offsetLeft / this.options.unitWidth) -1) * -1;
			new Effect.MoveBy(this.slider, 0, -1 * (this.options.visibleUnits * this.options.unitWidth), { 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.offsetLeft == 0) {
	
			this.current_unit = this.total_units - 1;
			new Effect.MoveBy(this.slider, 0, (this.total_width - this.options.unitWidth) * -1, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) });
		} else {
			this.current_unit = ((this.slider.offsetLeft / this.options.unitWidth) + 1) * -1;
			new Effect.MoveBy(this.slider, 0, this.options.visibleUnits * this.options.unitWidth, { duration: this.options.duration, transition: Effect.Transitions.EaseFromTo, beforeStart: this.beforeSlide.bindAsEventListener(this), afterFinish: this.afterSlide.bindAsEventListener(this) } );
		}
	},
	
	setActiveButton: function() {
		for(i = 0; i < this.menu_buttons.length; i++) {
			this.menu_buttons[i].removeClassName(this.options.activeClass);
		}
		
		this.menu_buttons[this.current_unit].addClassName(this.options.activeClass);
	},
	
	disableButtons: function() {
		Event.stopObserving(this.forward_button, 'click', this.forward_button_cache)
		Event.stopObserving(this.back_button, 'click', this.back_button_cache);
		
		for(i = 0; i < this.menu_buttons.length; i++) {
			Event.stopObserving(this.menu_buttons[i], 'click', this.scrollCache[i]);
		}
	},
	
	enableButtons: function() {
		Event.observe(this.forward_button, 'click', this.forward_button_cache);
		Event.observe(this.back_button, 'click', this.back_button_cache);
		
		for(i = 0; i < this.menu_buttons.length; i++) {
			this.scrollCache[i] = this.scrollToItem.bindAsEventListener(this, this.menu_buttons[i])
			Event.observe(this.menu_buttons[i], 'click', this.scrollCache[i]);
		}
	},
	
	beforeSlide: function() {
		this.setActiveButton();
		this.disableButtons();
		if(this.options.beforeStart) this.options.beforeStart(this);
	},
	
	afterSlide: function() {
		this.enableButtons();
		if(this.options.afterFinish) this.options.afterFinish(this);
	}
	
});

