/* defines an object that will prepare a group of boxes to be scrolled through */
function pspr_scroller(obj_name, parent_id, sections_class, transition_time, transition_wait_time) {
	this.obj_name = obj_name;  // needs to be same as variable storing this object, used by setTimeouts

	this.parent_div = document.getElementById(parent_id);	// The container holding the sections
	this.sections = Array();				// The sections to be scrolled through
	divs = this.parent_div.getElementsByTagName('div');
	for(var i=0; i < divs.length; i++) {
		cls = divs[i].className.split(' ');
		for(var j=0; j < cls.length; j++ ) {
			if(cls[j] == sections_class) {
				this.sections.push(divs[i]);
			}
		}
	}
	this.index = 0;						// Keeps track of which ones have been & will be scrolled
	this.parent_width = 0;
	this.transition_time = transition_time;
	this.transition_wait_time = transition_wait_time;
	this.time_btwn_mv = 0;					// How often settimeout fires
	this.timeouts = Array();				// Keep track of timeouts so we can cancel them if needed
	this.is_rotating = false;				// Is true until we stop continuous scrolling

	// calculations
	this.parent_width = this.parent_div.clientWidth;
	this.time_btwn_mv = this.transition_time / this.parent_width;

	// absolutely position the sections
	for(var i=0; i < this.sections.length; i++) {
		this.sections[i].style.position = 'absolute';
		this.sections[i].style.top = '0px';
		if(i == 0) {
			this.sections[i].style.left = '0px';
		} else {
			this.sections[i].style.left = (this.parent_width + 'px');
		}
	}
		
	//Create navigation buttons
	this.nav = document.createElement('div');
	this.navl = document.createElement('div');
	this.navr = document.createElement('div');
	this.nav.className = 'scroller_nav';
	this.navl.className = 'scroller_nav_l';
	this.navr.className = 'scroller_nav_r';
	this.navl.id = this.obj_name + '_nav_l';
	this.navr.id = this.obj_name + '_nav_l';
	this.nav.appendChild(this.navl);
	this.nav.appendChild(this.navr);
	this.navl.onclick = function() { s = eval(this.id.substr(0, this.id.search('_'))); s.scroll('left'); }
	this.navr.onclick = function() { s = eval(this.id.substr(0, this.id.search('_'))); s.scroll('right'); }
	this.parent_div.appendChild(this.nav);
	
	// This function will perform a single scrolling motion to the left or right.
	this.scroll = function(direction, keep_rotating) {
		if(!keep_rotating) this.is_rotating = false;
		this.clearScrolling();
		cur_section = this.index; // the section moving out
		if(direction == 'left') {
			this.index = this.index - 1 >= 0 ? this.index - 1 : this.sections.length - 1;
		} else {
			this.index = this.index + 1 < this.sections.length ? this.index + 1 : 0;
		}
		next_section = this.index; // the section moving in

		// Move both boxes to the appropriate starting positions
		this.sections[cur_section].style.left = '0px';
		if(direction == 'left') {
			this.sections[next_section].style.left = ((0 - this.parent_width) + 'px');
		} else {
			this.sections[next_section].style.left = (this.parent_width + 'px');
		}

		// Perform the scrolling
		if(direction == 'left') {
			for(var i = 0; i <= this.parent_width; i++) {
				var a = setTimeout(this.obj_name + '.sections[' + cur_section + '].style.left = "' + (0 + i) + 'px";', (i * this.time_btwn_mv));
				var b = setTimeout(this.obj_name + '.sections[' + next_section + '].style.left = "' + (i - this.parent_width) + 'px";', (i * this.time_btwn_mv));
				this.timeouts.push(a);
				this.timeouts.push(b);
			}
		} else {
			for(var i = 0; i <= this.parent_width; i++) {
				var a = setTimeout(this.obj_name + '.sections[' + cur_section + '].style.left = "' + (0 - i) + 'px";', (i * this.time_btwn_mv));
				var b = setTimeout(this.obj_name + '.sections[' + next_section + '].style.left = "' + (this.parent_width - i) + 'px";', (i * this.time_btwn_mv));
				this.timeouts.push(a);
				this.timeouts.push(b);
			}
		}
	}

	// This function starts the repeated scrolling
	this.rotate = function() {
		this.is_rotating = true;
		setTimeout(this.obj_name + '.keep_rotating()', this.transition_wait_time);
	}


	// This function keeps the scrolling going
	this.keep_rotating = function(){
		if(this.is_rotating){
			this.scroll('right', true);
			setTimeout(this.obj_name + '.keep_rotating()', this.transition_time + this.transition_wait_time);
		}
	}
	this.clearScrolling = function() {
		for(var i = 0; i < this.timeouts.length; i++) {
			clearTimeout(this.timeouts[i]);
		}
		this.timeouts = Array();
		for(var i = 0; i < this.sections.length; i++ ) {
			if(i == this.index) {
				this.sections[i].style.left = '0px';
			} else {
				this.sections[i].style.left = ('-' + this.parent_width + 'px');
			}
		}

		
	}
}

