/*

JQUERY TAB SLIDER SCRIPT
Author: Abendago Media Group
Version 0.2
NOTE: This script uses operations that require jQuery 1.3 or later

0.2 (14/12/2009)
-------------------------------------------------------------------------------------------------------------
	+ Removed:	interval based callback function. this was way to CPU intensive.
	+ Added:	listener callback function to replace the previous interval based callback function. the function sends a string to the listener to alert a new process.
	+ Added:	ability to disable and enable the autoscroll feature on the fly.

*/

(function($) {
	
	var options = null;
	var nCurrPos = null;
	var oTimeout = null;

	var defaultSettings = {  
		
		slideTime: 1000, // time it takes to slide slabs in miliseconds
		autoscrollLength: 3000, // time it takes to autoscroll to the next slide //zero disables the autoscroll feature
		easeType: "linear", // this option may require a plugin
		nMargin: 0, // slab horizontal margin in pixels
		nSelected: 0, // initial blurb to be selected
		bPrepare: true, // prepare the slider then initiate once finished
		listener: function(e,o){}, // e = event switch string // o = event variables
		oSlideHolder: "div#JQTS_slides",
		oSlide: "div.JQTS_slide",
		oSlideButton: "a.JQTS_button",
		oCanvas: "div#JQTS_canvas",
		oImageHolder: "div.JQTS_images",
		oImageHolderMask: "div.JQTS_mask",
		classActive: "JQTS_active"

	};

	$.fn.extend({

		tabslider: function(customSettings) {
			
			var JSTS = $(this);

			// default settings
			options = jQuery.extend(defaultSettings,customSettings);

			$(options.oSlideButton).click(function(){
				
				JSTS.listener("BUBBLE_CLICK");

				var oParent = $(this).parent();
				var nPos = $(options.oSlide).index(oParent);

				JSTS.runSlide(nPos);

				return false;

			});

			if (options.bPrepare) {			

				$(options.oSlide).each(function(i){
					
					// set the container width
					var nWidth = $(options.oImageHolderMask).width();
					var oChildren = $(options.oImageHolder).children();
					$(options.oImageHolder).width(nWidth*oChildren.length);
					oChildren.width(nWidth);

					// align the blurbs to their proper spot
					var oSlideButton = $(this).find(options.oSlideButton).eq(0);
					var nLeft = (oSlideButton.width()+options.nMargin)*i;
					$(this).css({'left':nLeft});

					if ((i+1)==$(options.oImageHolder).children().length) {
						
						// setTimeout(function(){ $(options.oSlideHolder).runSlide(options.nSelected); },1000);
						$(window).load(function(){ JSTS.runSlide(options.nSelected); });

					}

				});

			} else {

				JSTS.runSlide(options.nSelected);
			
			}

		},

		listener: function(e){
			options.listener.call(this,e,{ 'slideTime':options.slideTime,'autoscrollLength':options.autoscrollLength });
		},

		enableAutoscroll: function(e){
			this.listener("AUTOSCROLL_ENABLED");
			options.autoscrollLength = e;
			this.autoScroll();
		},
			
		disableAutoscroll: function(){
			this.listener("AUTOSCROLL_DISABLED");
			options.autoscrollLength = 0;
		},

		runSlide: function(nPos) {

			clearTimeout(oTimeout);
			
			// if autoscroll is set then begin the timeout sequence
			if (options.autoscrollLength>0) {

				var JSTS = $(this);
				JSTS.listener("SLIDE_START");

				$(options.oSlideHolder).slideBubble(nPos);
				$(options.oCanvas).slideBanner(nPos);
				
				oTimeout = setTimeout(function(){
					JSTS.listener("SLIDE_END");
					JSTS.autoScroll();
				},options.slideTime);

			}

		},

		autoScroll: function() {
			
			clearTimeout(oTimeout);
			
			// if autoscroll is set then begin the timeout sequence
			if (options.autoscrollLength>0) {

				var JSTS = $(this);
				JSTS.listener("AUTOSCROLL_START");

				var nNextPos = nCurrPos+1;
				var nChildLen = $(options.oImageHolder).children().length;
				if (nNextPos==nChildLen) nNextPos = 0; // if we have reached the end
				
				oTimeout = setTimeout(function(){ 
					JSTS.listener("AUTOSCROLL_END");
					JSTS.runSlide(nNextPos);
				},options.autoscrollLength);

			}

		},

		slideBubble: function(nPos) {

			var oParent = $(this);
			var oSlide = oParent.find(options.oSlide).eq(nPos);
			
			if (nCurrPos==null) nCurrPos = nPos;

			// if our active bubble is before the bubble we clicked // slide left
			if (nCurrPos<nPos) {

				oParent.find(options.oSlide).each(function(){
					
					var oTempBlurb = $(this);
					var oTempBubble = oTempBlurb.find(options.oSlideButton).eq(0);
					var nTempPos = $(options.oSlide).index(oTempBlurb);
					var nTempLeft = nTempPos*(oTempBubble.width()+options.nMargin);

					oTempBlurb.removeClass(options.classActive);
					
					//alert(nTempPos+"<="+nPos+"&&"+nTempPos+">"+nCurrPos);
					if (nTempPos<=nPos&&nTempPos>nCurrPos) {
						oTempBlurb.animate({'left':nTempLeft},options.slideTime,options.easeType);
					}
				
				});
			
			// if our active bubble is after the bubble we clicked // slide right
			} else {
				
				oParent.find(options.oSlide).reverse().each(function(i){
				
					var oTempBlurb = $(this);
					var oTempBubble = oTempBlurb.find(options.oSlideButton).eq(0);
					var nTempPos = $(options.oSlide).index(oTempBlurb);
					var nTempLeft = oParent.width()-((i+1)*(oTempBubble.width()+options.nMargin))+options.nMargin;

					oTempBlurb.removeClass(options.classActive);
					
					//alert(nTempPos+">"+nPos);
					if (nTempPos>nPos) {
						oTempBlurb.animate({'left':nTempLeft},options.slideTime,options.easeType);
					}
				
				});

			}

			oSlide.addClass(options.classActive);
			nCurrPos = nPos;

		},

		slideBanner: function(nPos) {

			var oParent = $(this);
			var oMask = oParent.find(options.oImageHolderMask).eq(0);
			var oContainer = oMask.find(options.oImageHolder).eq(0);
			var nTempLeft = -(nPos*oMask.width());

			oContainer.animate({'left':nTempLeft},options.slideTime,options.easeType);

		},

		reverse: function() {
			return this.pushStack(this.get().reverse(),arguments);
		}

	});

})(jQuery);
