
/* TheDenverChannel.com scripts */
// Begin national js scripts


/** 
 * @fileoverview  IbsSlider: moves a tray of nested slides to the right or left.
 * Programmer can specify distance of each movement, how many slides to move,
 * and speed of movement.  This object does not care what is inside the container
 * to be moved. Requires a function in the global scope: addEvent(obj, eventName, func)
 *
 * @author breisinger / Internet Broadcasting, Inc.
 */


/**
 * Construct a new IbsSlider
 * @class This is the object that contains copies of all of the functions and 
 * 	variables it needs to work. 
 * @constructor
 * @param {HtmlObject} slideContainer Element containing slides
 * @param {int} numSlides Use array.length or something to pass number of slides
 * @param {HtmlObject} leftControl Element to click on to move slides to the left
 * @param {HtmlObject} rightControl Element to click on to move slides to the right
 * @param {int} speed Speed of movement. Between 10 - 50 is recommended.
 * @param {int} slidesToAdvance Number of slides to advance after one click. Usually just 1. 
 * @param {int} slideWidth Pixel width of a single slide.
 * @param {HtmlObject} currentSlideContainer Block element to write current position to.
 * @return A new instance of IbsSlider
 */
function IbsSlider(slideContainer,numSlides,leftControl,rightControl,speed,slidesToAdvance,	slideWidth,currentSlideContainer) {
	//vars based on parameters
	var self = this;
	this.speed = speed;				//between 5 and 15 seems ok
	this.slidesToAdvance = slidesToAdvance; 	//1 or 2
	this.slideWidth = slideWidth;			//pixel width of each slide (integer only)
	this.r = rightControl;				//right and left control buttons
	this.l = leftControl;
	this.horiz = slideContainer;			//parent container of slides
	this.currentSlideContainer = currentSlideContainer ? currentSlideContainer : false; //where to report current position
	
	//other members
	this.threshold = 0;				//current position during slide animation
	this.timer = "";				//timer for slide animation
	this.numSlides = numSlides;			//total of slides in element
	this.currentSlide = 1;				//the current slide (left-most slide in display)
	this.rightStop = 0;				//width in pixels at which point the container should not move right
	this.lastLeftValue = 0; 
	this.movementWidth = this.slidesToAdvance * this.slideWidth;  
	//this.movementWidth = this.movementWidth - this.speed;
	
	/******
	/ width in pixels at which point the container should not respond to the
	/ right() function.
	/*****/
	this.rightStop = (this.movementWidth * this.numSlides) - this.movementWidth - this.speed;
	
	/*****
	/ Initiate position reporting text 
	/****/
	if(this.currentSlideContainer) {
		this.currentSlideContainer.innerHTML = "1 of " + numSlides;
	}
	
	/*****
	/ Event handlers for controls
	/****/
	addEvent(this.r, "click", function() {
		////debug("right onclick");
		self.clearTimer();
		if(!self.isEnd()) {
			self.lastLeftValue = parseInt(self.horiz.style.left);
			self.timer = window.setInterval(self.right,20);
			return false;
		}
	});
	addEvent(this.l, "click", function() {
		self.clearTimer();
		if(parseInt(self.horiz.style.left) != 0) {
			self.lastLeftValue = parseInt(self.horiz.style.left);
			self.timer = window.setInterval(self.left,20);
			return false;
		}
	});
	
	/*****
	/ Report current slide ("4 of 10" for example)
	/****/
	this.reportPos = function() {
		if(this.currentSlideContainer) {
			this.currentSlideContainer.innerHTML = this.currentSlide + " of " + this.numSlides;
		}
	}
	
	/*****
	/ Kill the sliding function
	/****/
	this.clearTimer = function() {
		if(this.timer != "undefined") {
			window.clearInterval(this.timer);
		}
	}
	
	/*****
	/ detect if the current position is the end (on the right)
	/****/
	this.isEnd = function() {
		if(this.currentSlide == this.numSlides) {
			return true;
		}
		return false;
	}
	
	/*****
	/ Move container left
	/****/
	this.left = function() {
		if(self.threshold < self.movementWidth && (self.speed + self.threshold <= self.movementWidth)) {
			self.horiz.style.left = parseInt(self.horiz.style.left) + self.speed + "px";
			self.threshold += self.speed;
		} else {
			self.horiz.style.left = self.lastLeftValue + self.movementWidth + "px";
			self.threshold = 0;
			self.clearTimer();
			self.currentSlide--;
			self.reportPos();
			
		}
	}
	
	/*****
	/ Move container right
	/****/
	this.right = function() {
		if(self.threshold >= (self.movementWidth * -1) && (self.threshold - self.speed >= self.movementWidth * -1)) {
			self.horiz.style.left = parseInt(self.horiz.style.left) - self.speed + "px";
			self.threshold -= self.speed;
		} else {
			self.horiz.style.left = self.lastLeftValue - self.movementWidth + "px";
			self.threshold = 0;
			self.clearTimer();
			self.currentSlide++;
			self.reportPos();
		}
		
	}
}


// End national_js scripts

