/* Alpha release of the static pager, developed and released by Jay Michael Del Greco, December 14, 2010.
Feel free to use and re-use it at will. Just give credit where it's due. Thanks. */
$.fn.jayCarousel=function(settings){
	settings = $.extend({}, $.fn.jayCarousel.defaults, settings);
	size = settings.size;
	increment = settings.increment;
	carWidth = settings.carWidth;
	carHeight = settings.carHeight;
	sectionWidth = settings.sectionWidth;
	var masterContainer = $(this).attr('id');
	//If we are set to full sceen we need to grab the window dimensions. If not, we're just going to use the passed ones or defaults
	if (size == 'full'){
		var uagent = navigator.userAgent.toLowerCase();
		if (uagent.indexOf('msie') != -1) {
			//If we're IE we'll need to grab the browser dimentions this way
			var yourWidth = document.documentElement.clientWidth;
			var yourHeight = document.documentElement.clientHeight;
		} else {
			//Everybody else works with this
			var yourWidth = window.innerWidth;
			var yourHeight = window.innerHeight;
		}
	} else {
		var yourWidth = carWidth;
		var yourHeight = carHeight;
		$('#'+masterContainer).css('width',carWidth+'px');
	}
	//Now for the stuff that we need to do regardless
	var sectionWidth = sectionWidth;//Set block width
	var sectionCount = $('#'+masterContainer+' .section').length;//Count our blocks
	var sliderWidth = sectionCount * sectionWidth;//Calculate a new width for the sliding container
	//Apply necessary IDs to tabs
	for (var i=1; i<=sectionCount; i++){
		if (i==1){
			$('#'+masterContainer+' .nav').children('li:first').children('.tab').addClass('tab'+i).attr('href','#tab'+i);
		} else {
			$('#'+masterContainer+' .nav').children('li:eq('+(i-1)+')').children('.tab').addClass('tab'+i).attr('href','#tab'+i);
		}
	}
	$('#'+masterContainer+' .arrows').children('li:first').children('a').addClass('disabled');
	if(increment >= sectionCount){
		$('#'+masterContainer+' .arrows').children('li:last').children('a').addClass('disabled');
	}
	$('#'+masterContainer+' .section').wrapAll('<div class="slider"></div>');
	$('#'+masterContainer+' .slider').wrap('<div class="slideWrapper"></div>');
	$('#'+masterContainer+' .slideWrapper').css('width',carWidth+'px').css('height',yourHeight+'px');
	//Apply potentially necessary/useful IDs to blocks
	for (var i=1; i<=sectionCount; i++){
		if (i==1){
			$('#'+masterContainer+' .slider').children('.section:first').attr('id','article'+i);
		} else {
			$('#'+masterContainer+' .slider').children('.section:eq('+(i-1)+')').attr('id','article'+i);
		}
	}
	$('#'+masterContainer+', #'+masterContainer+' .slider, #'+masterContainer+'  .block').css('height',yourHeight+'px');//Force all heights to the browser window height
	$('#'+masterContainer+' .section').css('width',sectionWidth+'px');//Apply block widths to individual blocks
	$('#'+masterContainer+' .slider').css('width',sliderWidth+'px').css('position','absolute','').css('left','0px').css('top','0px');//Apply newly caluculated slider width
	$('#'+masterContainer+' .nav li:first').addClass('selected');
	var chunkCount = Math.ceil(sectionCount/increment);
	var chunkWidth = sectionWidth * increment;
	var chunkPositions = new Array();//chunkPositions[0]=0; I removed this because it was unnecessary
	for (i=1;i<=chunkCount;i++){
		if (i==1){
			chunkPositions[i]=0;
		} else {
			chunkPositions[i]= chunkWidth-(i*chunkWidth);
		}
	}
	var chunkShown = 1;
	//Click function for prev and next links
	$('#'+masterContainer+' .arrows a').live('click', function(){
		if ($('#'+masterContainer).data("IsEnabled") == true){
			$('#'+masterContainer).data("IsEnabled",false);
			var theSlider = $('#'+masterContainer+' .slider');
			var sliderPosition = theSlider.position();
			if($(this).attr('class') == 'prev'){
				$('#'+masterContainer+' .arrows li a').removeClass('disabled');
				var chunkRevealed = chunkShown-1;
				if(chunkRevealed < 1){
					chunkRevealed = chunkCount;
				}
				$('#'+masterContainer+' .slider').animate({
					left: chunkPositions[chunkRevealed]+'px'
				}, 1000, function() {
					//Animation complete, update our variable and add appropriate disabled class if needed
					chunkShown = chunkRevealed;
					if (chunkShown == 1){
						$('#'+masterContainer+' .arrows li .prev').addClass('disabled');
					}
					$('#'+masterContainer).data("IsEnabled",true);
				});
				//alert('Previous: '+sliderPosition.left);
			} else if ($(this).attr('class') == 'next'){
				$('#'+masterContainer+' .arrows li a').removeClass('disabled');
				var chunkRevealed = chunkShown+1;
				if(chunkRevealed > chunkCount){
					chunkRevealed = 1;
				}
				$('#'+masterContainer+' .slider').animate({
					left: chunkPositions[chunkRevealed]+'px'
				}, 1000, function() {
					//Animation complete, update our variable and add appropriate disabled class if needed
					chunkShown = chunkRevealed;
					if (chunkShown == chunkCount){
						$('#'+masterContainer+' .arrows li .next').addClass('disabled');
					}
					$('#'+masterContainer).data("IsEnabled",true);
				});
				//alert('Next: '+sliderPosition.left);
			} else {
				//alert('Disabled class is present. Do Nothing.');
				$('#'+masterContainer).data("IsEnabled",true);
			}
		}
		return false;
	}).parent().parent().parent().data("IsEnabled",true);
	$('#'+masterContainer+' .nav .tab').live('click', function(){
		var clickedId = $(this).attr('href');
		var tabNumber = clickedId.replace('#tab','');
		var clickedNumber = parseInt(tabNumber);
		var displacement = sectionWidth-(clickedNumber*sectionWidth);
		$('#'+masterContainer+' .slider').animate({
			left: displacement+'px'
		}, 1000, function() {
			//Animation complete, maybe have a party or sum'um
			$('#'+masterContainer+' .tab').parent('li').removeClass('selected');
			$('#'+masterContainer+' .tab'+clickedNumber).parent('li').addClass('selected');
		});
		return false;
	});
};
//Default values for the parameters in the extend.
$.fn.jayCarousel.defaults = {
	size				:	'full',
	increment			:	3,
	carWidth			:	800,
	carHeight			:	600,
	sectionWidth		:	800
};

