/*
	Author: Paul Spaulding, Vanillaware LLC
	Requires: jQuery
*/
var VSlider = function(containerId, idleDelay) {
	this.$container = $('#' + containerId);
	this.width = this.$container.width();
	this.height = this.$container.height();
	this.slides = $('.slide', this.$container);
	var self = this;
	$(this.slides).each(function(idx, el) {
		$(el).css({position: 'absolute', top: (idx == 0 ? 0 : self.height)});
	});
	this.current = 0;
	this.speed = 1000;
	this.idleDelay = idleDelay || 5000;
	this.blnAnimating = false;
};

VSlider.prototype.prev = function() {
	if (this.blnAnimating) return;
	this.blnAnimating = true;
	var self = this;
	var $current = $(this.slides[this.current]);
	var prevId = this.current <= 0 ? this.slides.length - 1 : this.current - 1;
	var $prev = $(this.slides[prevId]);
	$prev.css({top: -this.height});
	$current.animate({top: '+=' + this.height}, this.speed);
	$prev.animate({top: '+=' + this.height}, this.speed, function() {self.blnAnimating = false;});
	this.current = prevId;
	return this;
};

VSlider.prototype.next = function() {
	if (this.blnAnimating) return;
	this.blnAnimating = true;
	var self = this;
	var $current = $(this.slides[this.current]);
	var nextId = this.current < this.slides.length - 1 ? this.current + 1 : 0;
	var $next = $(this.slides[nextId]);
	$next.css({top: this.height});
	$current.animate({top: '-=' + this.height}, this.speed);
	$next.animate({top: '-=' + this.height}, this.speed, function() {self.blnAnimating = false;});
	this.current = nextId;
	return this;
};

VSlider.prototype.play = function() {
	var self = this;
	this.idle = setTimeout(function () {
		self.next();
		self.play();
	}, this.idleDelay);
};

VSlider.prototype.pause = function() {
	if (this.idle) {
		clearTimeout(this.idle);
		this.idle = null;
	}
};
