var temp;

function scroller(myId, scrolling, leftArrow, rightArrow, direction, maxSpeed) {
	this.scrolling = scrolling;
	this.leftArrow = leftArrow;
	this.rightArrow = rightArrow;
	this.direction = direction;

	this.myId = myId;
	this.movingLeft = false;
	this.movingUp = false;
	this.movingRight = false;
	this.movingDown = false;
	this.speed = 0;
	this.maxSpeed = maxSpeed;

	this.init = function() {
		//set the styles
		$(this.scrolling).css('position', 'relative');
		$(this.scrolling).css('overflow', 'hidden');
		$(this.scrolling + ' table').css('position', 'relative');
		if (this.direction == 'horizontal') {
			$(this.scrolling + ' table').css('left', '0px');
		} else {
			$(this.scrolling + ' table').css('top', '0px');
		}

		//ie fix
		$(this.scrolling).css('z-index', '10');
		$(this.scrolling + ' table').css('z-index', '9');

		//embed id for easy access
		$(this.leftArrow).attr('name', this.myId);
		$(this.rightArrow).attr('name', this.myId);

		if (this.direction == 'horizontal') {
			$(this.leftArrow).hover(function() {				
				eval($(this).attr('name') + '.moveLeft();');
			}, function() {
				eval($(this).attr('name') + '.stopMoving();');
			});
			$(this.rightArrow).hover(function() {
				eval($(this).attr('name') + '.moveRight();');
			}, function() {
				eval($(this).attr('name') + '.stopMoving();');
			});
		} else {
			$(this.leftArrow).hover(function() {
				eval($(this).attr('name') + '.moveUp();');
			}, function() {
				eval($(this).attr('name') + '.stopMoving();');
			});
			$(this.rightArrow).hover(function() {
				eval($(this).attr('name') + '.moveDown();');
			}, function() {
				eval($(this).attr('name') + '.stopMoving();');
			});
		}
	};

	this.moveLeft = function() {
		this.movingLeft = true;

		this.move();
	};

	this.moveUp = function() {
		this.movingUp = true;

		this.move();
	};

	this.moveRight = function() {
		this.movingRight = true;

		this.move();
	}

	this.moveDown = function() {
		this.movingDown = true;

		this.move();
	}

	this.stopMoving = function() {
		this.movingLeft = false;
		this.movingUp = false;
		this.movingRight = false;
		this.movingDown = false;

		this.speed=0;
	};

	this.move = function() {
		this.incSpeed();
		if (this.direction == 'horizontal') {
			if (this.movingLeft && this.canMoveLeft()) {
				this.setLeft(-this.speed);

				setTimeout(this.myId + '.move();', 10);
			} else if (this.movingRight && this.canMoveRight()) {
				this.setLeft(this.speed);

				setTimeout(this.myId + '.move();', 10);
			}
		} else {
			if (this.movingUp && this.canMoveUp()) {
				this.setTop(-this.speed);

				setTimeout(this.myId + '.move();', 10);
			} else if (this.movingDown && this.canMoveDown()) {
				this.setTop(this.speed);

				setTimeout(this.myId + '.move();', 10);
			}
		}
	}

	this.canMoveLeft = function() {
		if (this.getLeft() >= -this.getWidth()) {
			return true;
		}
		this.stopMoving();
		return false;
	};

	this.canMoveRight = function() {
		if (this.getLeft() <= 0) {
			return true;
		}
		this.stopMoving();
		return false;
	};

	this.canMoveUp = function() {
		if (this.getTop() >= -this.getHeight()) {
			return true;
		}
		this.stopMoving();
		return false;
	};

	this.canMoveDown = function() {
		if (this.getTop() <= 0) {
			return true;
		}
		this.stopMoving();
		return false;
	};

	this.getWidth = function() {
		return $(this.scrolling + ' table').width() - ($(this.scrolling).width());
	};

	this.getHeight = function() {
		return $(this.scrolling + ' table').height() - ($(this.scrolling).height());
	};

	this.setLeft = function(amount) {
		$(this.scrolling + ' table').css('left', (this.getLeft() + amount) + 'px');
	};

	this.setTop = function(amount) {
		$(this.scrolling + ' table').css('top', (this.getTop() + amount) + 'px');
	};

	this.getLeft = function() {
		var x = $(this.scrolling + ' table').css('left').split('px');

		return parseInt(x[0]);
	};

	this.getTop = function() {
		var x = $(this.scrolling + ' table').css('top').split('px');

		return parseInt(x[0]);
	};

	this.incSpeed = function() {
		this.speed++;
		if (this.speed > this.maxSpeed) {
			this.speed = this.maxSpeed;
		}
	}
}

