
/* Scroller Class

Class that facilitates scrolling background images left, right, up, or down.
By manipulating the background image and the parameters different effects can
be achieved - banners/images that slide into place, banner rotation, scrolling, 
etc.

Parameters: 
	Element ID, Element Width, Element Height, Direction, Increment, Wait Time, 
	Number of Increments

*/

function Scroller (element_name,element_width,element_height,direction,increment,time_to_wait,number_of_frames) {
	this.element_name = element_name; // ID of the element containing the background image
	this.element_width = element_width; // width of the element
	this.element_height = element_height; // height of the element
	this.increment = increment; // number of pixels to shift image
	this.time_to_wait = time_to_wait; // number of milliseconds to wait between shifts
	this.number_of_frames = number_of_frames; // total number of shifts to perform - 0 is infinite
	this.direction = direction; // left,right,up,down
	this.current_frame = 0;
	// Find the current background position so we know where we're starting from
	background_position = document.getElementById(this.element_name).style.backgroundPosition;
	pattern = /(\-{0,1}\d{1,5})\w{0,2} (\-{0,1}\d{1,5})/;
	if (background_position.match(pattern)) {
		positions = background_position.match(pattern);
		this.horizontal_position = parseFloat(positions[1]);
		this.vertical_position = parseFloat(positions[2]);
	}
	else {
		this.horizontal_position = 0;
		this.vertical_position = 0;
	}

	// methods
	this.animate = animate;

	switch(this.direction) {
		case 'left' :
			this.move = move_left;
			this.starting_position = this.horizontal_position;
			this.current_position = this.horizontal_position;
			break;
		case 'right' :
			this.move = move_right;
			this.starting_position = this.horizontal_position;
			this.current_position = this.horizontal_position;
			break;
		case 'up' :
			this.move = move_up;
			this.starting_position = this.vertical_position;
			this.current_position = this.vertical_position;
			break;
		case 'down' :
			this.move = move_down;
			this.starting_position = this.vertical_position;
			this.current_position = this.vertical_position;
			break;
	}
	if (window.object_instances === undefined) {
		object_instances = new Array();
	}
	this.id = object_instances.length;
	object_instances[this.id] = this;
}

function animate() {
	if ( (this.number_of_frames > 0) && (this.current_frame >= this.number_of_frames) ) {
		return 0;
	}
	this.current_frame++;
	/* setTimeout interprets 'this' globally so a global
	reference array (object_instances) is necessary to 
	call an object method */
	setTimeout('object_instances['+this.id+'].move()', this.time_to_wait);
}

function move_left() {
	element = document.getElementById(this.element_name);
	this.current_position -= this.increment;
	if (this.current_position <= (0-this.element_width)) {
		// once the current position is past the element edge we'll reset it
		this.current_position = this.starting_position;
	}
	element.style.backgroundPosition = this.current_position + "px " + this.vertical_position + "px";
	this.animate();
}

function move_right() {
	element = document.getElementById(this.element_name);
	this.current_position += this.increment;
	if (this.current_position >= (this.element_width)) {
		this.current_position = this.starting_position;
	}
	element.style.backgroundPosition = this.current_position + "px " + this.vertical_position + "px";
	this.animate();
}

function move_up() {
	element = document.getElementById(this.element_name);
	this.current_position -= this.increment;
	if (this.current_position <= (0-this.element_height)) {
		this.current_position = this.starting_position;
	}
	element.style.backgroundPosition = this.horizontal_position + "px " + this.current_position + "px";
	this.animate();
}

function move_down() {
	element = document.getElementById(this.element_name);
	this.current_position += this.increment;
	if (this.current_position >= (this.element_height)) {
		this.current_position = this.starting_position;
	}
	element.style.backgroundPosition = this.horizontal_position + "px " + this.current_position + "px";
	this.animate();
}
