/* Begin: TragicMedia.com - JQuery Scroller
/* created 3/09 by Rich Rudzinski
/* modified 4/10 for MJD
/*------------------------------------------*/
//var constants = {container:"", buttonClass:"", itemClass:"", slideNumber:#, numberShown:#, speed:#};
//var scroll = new jScroll(constants);
//Notes: speed works as a "duration". The smaller the number, the faster it will animate

function jScroll(initVars) {
	var obj = this;
	this.container = $('#' + initVars.container);
	this.frame = this.container.parent();
	this.itemClass = initVars.itemClass;
	this.slideItems = $('.' + initVars.itemClass);
	this.timer = (initVars.timer) ? setInterval(function() { obj.changeOrder(); }, 6000) : false;
	this.buttons = (initVars.buttonClass) ? $('.' + initVars.buttonClass) : [];
	this.slideNumber = (initVars.slideNumber == undefined) ? this.slideNumber = 1 : this.slideNumber = initVars.slideNumber;
	this.numberShown = (initVars.numberShown == undefined) ? this.numberShown = this.slideNumber : this.numberShown = initVars.numberShown;
	if(this.itemClass.match('text-box')) {
		var textUrl = document.location.href.split('#')[1];
		if(textUrl) url = decodeUrl(textUrl);
		this.startPosition = (textUrl) ? url : 1;
	} else {
		this.startPosition = (document.location.href.split('#')[1]) ? Number(document.location.href.split('#')[1]) : 1;
	}
	this.speed = (initVars.speed == undefined) ? this.speed = 300 : this.speed = initVars.speed;
	if(this.slideItems.length%this.slideNumber != 0) this.addNodes(this.slideItems.length%this.slideNumber);
	this.itemWidth = this.slideItems[0].offsetWidth;
	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 = $('.' + this.itemClass);
}
// Set start position and adjust css for container and frame
jScroll.prototype.createScroll =  function() {
	this.container.css({'width': this.totalWidth + 'px',
						'height': this.itemHeight + 'px',
						'position': 'absolute',
						'left': 0});
	this.frame.css({'width': this.itemWidth * this.numberShown,
					'height': this.itemHeight + 'px',
					'position': 'relative',
					'overflow': 'hidden'});
	this.slideItems.css({'width': this.itemWidth + 'px',
					'height': this.itemHeight + 'px',
					'position': 'relative',
					'float': 'left'});
	this.container.left = 0;
	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);
	} else if(this.startPosition == 0) {
		this.startPosition = 1;
	}
	this.container.left = 0;
	this.container.css('left', 0);
	this.current = this.startPosition;
}
// add click event
jScroll.prototype.addArrowEvent = function() {
	var obj = this;
	for(i=0; i<this.buttons.length; i++) {
		$(this.buttons[i]).click(function() {
			if(!obj.animate) {
				obj.animate = true;
				// clear timer
				if(obj.itemClass.match('scroll-pane') && obj.timer != false) { 
					clearInterval(obj.timer);
				} 
				if(obj.itemClass.match('text-box')) {
					var textNum = $(this).attr('href').split('#')[1];
					num = decodeUrl(textNum);
					var textUrl = document.location.href.split('#')[1];
					url = decodeUrl(textUrl);
				} else {
					var num = Number($(this).attr('href').split('#')[1]);
					var url = Number(document.location.href.split('#')[1]);
				}
				if(url != num) {
					if(textNum) document.location.href = document.location.href.split('#')[0] + '#' + textNum;
					else document.location.href = document.location.href.split('#')[0] + '#' + num;
					obj.slideNumber = num - obj.current;
					if(obj.slideNumber > 0) {
						obj.totalSlide = obj.slideNumber * obj.itemWidth;
						obj.slideRight();
					} else if(obj.slideNumber == 0) {
						obj.animate = false;
					} else {
						obj.slideNumber = Math.abs(obj.slideNumber);
						obj.totalSlide = obj.slideNumber * obj.itemWidth;
						obj.shiftElementsLeft(obj.slideNumber);
						obj.slideLeft();
					}
					obj.current = num;
				}
				if(obj.timer != false) {
					obj.timer = setInterval(function() { obj.changeOrder(); }, 50000);
				}
				obj.adjustActive();
			}
			return false;
		});
	}
		// Add left/right scroll functionality for bottom scroll
	if($(this.slideItems[0]).hasClass('btm-item')) {
		$('.btn-prev').click(function() {
			if(!obj.animate) {
				obj.animate = true;
				obj.shiftElementsLeft(obj.slideNumber);
				obj.slideLeft();		
			}
			return false;
		});
		$('.btn-next').click(function() {
			if(!obj.animate) {
				obj.animate = true;
				obj.slideRight();
			}
			return false;
		});
	}
}
jScroll.prototype.adjustActive = function() {
	if($('#nav-services li').length) {
		var mainNav = $('#nav-services li');
		mainNav.removeClass('active');
		$(mainNav[this.current - 1]).addClass('active');
	}
	for(i=0; i<this.buttons.length; i++) {
		if((this.current-1) == i) $(this.buttons[i]).parent().addClass('active');
		else $(this.buttons[i]).parent().removeClass('active');
	}
}
// 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); 
	for(i=shiftNumber; i>0; i--) {
		$(this.slideItems[this.slideItems.length - 1]).insertBefore(this.slideItems[0]);
		this.slideItems = $('.' + this.itemClass);
	} 
}
jScroll.prototype.shiftElementsRight = function(shiftNumber) {
	this.container.left = this.container.left + shiftNumber * this.itemWidth;
	this.container.css('left', this.container.left); 
	for(i=shiftNumber; i>0; i--) {
		$(this.slideItems[0]).insertAfter(this.slideItems[this.slideItems.length - 1]);
		this.slideItems = $('.' + this.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; });
}
jScroll.prototype.changeOrder = function() {
	this.slideNumber = 1;
	this.totalSlide = this.slideNumber * this.itemWidth;
	this.slideRight();
	this.current = (this.current == this.slideItems.length) ? 1 : this.current + 1;
	document.location.href = document.location.href.split('#')[0] + '#' + this.current;
	this.adjustActive();
}
// decode url for services page
function decodeUrl(url) {
	switch(url) {
		case 'subnav1':
			url = 1;
		break;
		case 'subnav2':
			url = 2;
		break;
		case 'subnav3':
			url = 3;
		break;
		case 'subnav4':
			url = 4;
		break;
		case 'subnav5':
			url = 5;
		break;
		case 'subnav6':
			url = 6;
		break
		case 'subnav7':
			url = 7;
		break;
		case 'subnav8':
			url = 8;
		break;
		case 'subnav9':
			url = 9;
		break;
	}
	return url;
}


