var temp;

function slider(myId, sliding, leftArrow, rightArrow, direction, maxSpeed) {
	this.sliding = sliding;
	this.leftArrow = leftArrow;
	this.rightArrow = rightArrow;
	this.direction = direction;
	this.paused = false;

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

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

		//ie fix
		$(this.sliding).css('z-index', '10');
		$(this.sliding + ' 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).click(function() {				
				eval($(this).attr('name') + '.moveLeft();');
			});
			$(this.rightArrow).click(function() {
				eval($(this).attr('name') + '.moveRight();');
			});
		} else {
			$(this.leftArrow).click(function() {
				eval($(this).attr('name') + '.moveUp();');
			});
			$(this.rightArrow).click(function() {
				eval($(this).attr('name') + '.moveDown();');
			});
		}

		//calculate the size of the slide
		this.slideDistance = $(this.sliding).width();
		this.calcMax();
		this.slideCount = 0;
		this.controlsEnabled = 1;

		temp = this;
	};

	this.calcMax = function() {
		this.slideMax = Math.floor($(this.sliding + ' table').width() / this.slideDistance);
	};

	this.moveLeft = function() {
		if (this.controlsEnabled == 1) {
			this.movingLeft = true;
			this.move();
			this.controlsEnabled--;
		}
	};

	this.moveUp = function() {
		if (this.controlsEnabled == 1) {
			this.movingUp = true;
			this.move();
			this.controlsEnabled--;
		}
	};

	this.moveRight = function() {
		if (this.controlsEnabled == 1) {
			this.movingRight = true;
			this.move();
			this.controlsEnabled--;
		}
	}

	this.moveDown = function() {
		if (this.controlsEnabled == 1) {
			this.movingDown = true;
			this.move();
			this.controlsEnabled--;
		}
	}

	this.pause = function() {
		this.paused = true;
	}

	this.unpause = function() {
		this.paused = false;
	}

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

		//make sure we snap to a slideDistance
		this.setAbsLeft(- ((this.slideCount) * this.slideDistance));

		this.controlsEnabled++;
	}	

	this.calcSpeed = function() {
		var ret = this.slideDistance / this.maxSpeed;
		return ret;
	}

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

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

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

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

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

		temp = this;
	}

	this.willMoveLeft = function() {
		if (this.controlsEnabled == 1) {
			//first case, detect if the count is correct		
			if (this.slideCount < this.slideMax) {
				return true;
			} else {
				return false;
			}
		} else {
			//detect it through the slide
			if ((this.getLeft() + this.slideDistance) >= -(this.slideDistance * (this.slideCount - 1))) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	};

	this.canMoveLeft = function() {
		if (this.controlsEnabled == 1) {
			//first case, detect if the count is correct		
			if (this.slideCount < this.slideMax) {
				this.slideCount++;
				return true;
			} else {
				return false;
			}
		} else {
			//detect it through the slide
			if ((this.getLeft() + this.slideDistance) >= -(this.slideDistance * (this.slideCount - 1))) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	};

	this.canMoveRight = function() {
		if (this.controlsEnabled == 1) {
			//first case		
			if (this.slideCount > 0) {
				this.slideCount--;
				return true;
			} else {
				return false;
			}
		} else {
			if (-(this.getLeft() + this.slideDistance) > (this.slideDistance * (this.slideCount - 1))) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	};

	this.willMoveRight = function() {
		if (this.controlsEnabled == 1) {
			//first case		
			if (this.slideCount > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			if (-(this.getLeft() + this.slideDistance) > (this.slideDistance * (this.slideCount - 1))) {
				return true;
			} else {
				return false;
			}
		}
		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.sliding + ' table').width() - ($(this.sliding).width());
	};

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

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

	this.setAbsLeft = function(left) {
		if (!this.paused) {
			$(this.sliding + ' table').css('left', left + 'px');
		}
	};

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

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

		return parseInt(x[0]);
	};

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

		return parseInt(x[0]);
	};

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

