/* Begin: TragicMedia.com - JQuery Scroller
/* created 3/09 by Rich Rudzinski
/*------------------------------------------*/
//var constants = {container:"", arrowLeft:"", arrowRight:"", itemClass:"", slideNumber:#, numberShown:#, startPosition:#, speed:#};
//var scroll = new jScroll(constants);
//Notes: speed works as a "duration". The smaller the number, the faster it will animate

var $j = jQuery.noConflict();

function jScroll(constants) {
	this.container = $j('#' + constants.container);
	this.frame = this.container.parent();
	this.slideItems = $j('.' + constants.itemClass);
	this.buttonLeft = $j('#' + constants.arrowLeft);
	this.buttonRight = $j('#' + constants.arrowRight);
	this.slideNumber = (constants.slideNumber == undefined) ? this.slideNumber = 1 : this.slideNumber = constants.slideNumber;
	this.sideButtons = (constants.sideButtons == undefined) ? this.sideButtons = true : this.sideButtons = constants.sideButtons;
	this.numberShown = (constants.numberShown == undefined) ? this.numberShown = this.slideNumber : this.numberShown = constants.numberShown;
	this.startPosition = (constants.startPosition == undefined) ? this.startPosition = 1 : this.startPosition = constants.startPosition;
	this.speed = (constants.speed == undefined) ? this.speed = 300 : this.speed = constants.speed;
	if(this.slideItems.length%this.numberShown != 0) this.addNodes(this.slideNumber - this.slideItems.length%this.numberShown);
	this.itemWidth = this.slideItems[0].offsetWidth + parseInt($j(this.slideItems[0]).css('marginRight'));
	this.itemHeight = this.slideItems[0].offsetHeight;
	this.totalWidth = this.itemWidth * this.slideItems.length;	
	this.totalSlide = this.slideNumber * this.itemWidth;
	this.animate = false;
	this.createScroll();
}
// add nodes to slideItems when not enough elements for pages
jScroll.prototype.addNodes = function(number) {
	for(i=0; i<number; i++) {
		var tempNode = this.slideItems[this.slideItems.length-1].cloneNode(false);
		this.container.append(tempNode);
	}
	this.slideItems = $j('.' + constants.itemClass);
}
// Set start position and adjust css for container and frame
jScroll.prototype.createScroll =  function() {
	this.container.css('width', this.totalWidth + 'px');
	this.container.left = 0;
	this.container.css('height', this.itemHeight + 'px');
//	this.frame.css('width', this.itemWidth * this.numberShown);
//	this.frame.css('height', this.itemHeight + 'px');
	this.adjustStartPosition(); // adjust start position
	this.addArrowEvent();	// add arrow events
}
// adjust start position
jScroll.prototype.adjustStartPosition =  function() {	
	if(this.startPosition != 1) {	
		var shiftNum = this.startPosition - 1;
		if(shiftNum >= this.slideItems.length) shiftNum = shiftNum%this.slideItems.length;
		if(shiftNum > 0) this.shiftElementsRight(shiftNum);
		this.container.left = 0;
		this.container.css('left', 0);
	} 
}
// add click event
jScroll.prototype.addArrowEvent = function() {
	var obj = this;
	this.buttonLeft.click(function() {
		if(!obj.animate) {
			obj.animate = true;
			obj.shiftElementsLeft(obj.slideNumber);
			obj.slideLeft();		
		}
		return false;
	});
	this.buttonRight.click(function() {
		if(!obj.animate) {
			obj.animate = true;
			obj.slideRight();
		}
		return false;
	});
}
// moves the elements on the page in preparation for the slide
jScroll.prototype.shiftElementsLeft = function(shiftNumber) {
	this.container.left = this.container.left - shiftNumber * this.itemWidth;
	this.container.css('left', this.container.left + 'px'); 
	for(i=shiftNumber; i>0; i--) {
		$j(this.slideItems[this.slideItems.length - 1]).insertBefore(this.slideItems[0]);
		this.slideItems = $j('.' + constants.itemClass);
	} 
}
jScroll.prototype.shiftElementsRight = function(shiftNumber) {
	this.container.left = this.container.left + shiftNumber * this.itemWidth;
	this.container.css('left', this.container.left + 'px'); 
	for(i=shiftNumber; i>0; i--) {
		$j(this.slideItems[0]).insertAfter(this.slideItems[this.slideItems.length - 1]);
		this.slideItems = $j('.' + constants.itemClass);
	}
}
// slide elements left
jScroll.prototype.slideLeft = function() {
	var obj = this;
	this.container.left = this.container.left + this.totalSlide;
	this.container.animate({
		left: this.container.left
	}, this.speed*this.slideNumber, function() {obj.animate = false;});		
}
// slide elements right
jScroll.prototype.slideRight = function() {
	var obj = this;
	this.container.left = this.container.left - this.totalSlide;
	this.container.animate({
		left: this.container.left
	}, this.speed*this.slideNumber, function() {obj.shiftElementsRight(obj.slideNumber); obj.animate = false;});
}