/* 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.jayCarouselE=function(settings){
	settings = $.extend({}, $.fn.jayCarouselE.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 sectionCountE = $('#'+masterContainer+' .section').length;//Count our blocks
	var sliderWidthE = sectionCountE * sectionWidth;//Calculate a new width for the sliding container
	$('#'+masterContainer+' .arrows').children('li:first').children('a').addClass('disabled');
	if(increment >= sectionCountE){
		$('#'+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<=sectionCountE; i++){
		if (i==1){
			$('#'+masterContainer+' .slider').children('.section:first').attr('id','event'+i);
		} else {
			$('#'+masterContainer+' .slider').children('.section:eq('+(i-1)+')').attr('id','event'+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',sliderWidthE+'px').css('position','absolute','').css('left','0px').css('top','0px');//Apply newly caluculated slider width
	var chunkCountE = Math.ceil(sectionCountE/increment);
	var chunkWidthE = sectionWidth * increment;
	var chunkPositionsE = new Array();//chunkPositionsE[0]=0; I removed this because it was unnecessary
	for (i=1;i<=chunkCountE;i++){
		if (i==1){
			chunkPositionsE[i]=0;
		} else {
			chunkPositionsE[i]= chunkWidthE-(i*chunkWidthE);
		}
	}
	var chunkShownE = 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 chunkRevealedE = chunkShownE-1;
				if(chunkRevealedE < 1){
					chunkRevealedE = chunkCountE;
				}
				$('#'+masterContainer+' .slider').animate({
					left: chunkPositionsE[chunkRevealedE]+'px'
				}, 1000, function() {
					//Animation complete, update our variable and add appropriate disabled class if needed
					chunkShownE = chunkRevealedE;
					if (chunkShownE == 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 chunkRevealedE = chunkShownE+1;
				if(chunkRevealedE > chunkCountE){
					chunkRevealedE = 1;
				}
				$('#'+masterContainer+' .slider').animate({
					left: chunkPositionsE[chunkRevealedE]+'px'
				}, 1000, function() {
					//Animation complete, update our variable and add appropriate disabled class if needed
					chunkShownE = chunkRevealedE;
					if (chunkShownE == chunkCountE){
						$('#'+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);
};
//Default values for the parameters in the extend.
$.fn.jayCarouselE.defaults = {
	size				:	'full',
	increment			:	3,
	carWidth			:	800,
	carHeight			:	600,
	sectionWidth		:	800
};

