/*
 *
 *	jQuery Rotating Feature Plugin v0.9
 *		Dave Augustine (daugust@wested.org)
 *		August 19, 2008
 *
 *		@requires: jQuery v1.2 or later
 *				   jQuery Timer plugin v0.1 or later
 *
 *		spotlight() takes a HTML & CSS sfgate.com style feature
 *	    and rotates through at the specified interval w/ fades, etc.
 *	    
 *      Options that can be set:
 *			selector: the container div - default of ".spot"
 *			timer_interval: the amount of milliseconds between features - default of "5000"
 *			startindex: which slide to start on - default of "0"
 *			fadeDuration: how long the fade should last in milliseconds - default of "1000"
 *
 */

(function($){  
	
	$.fn.spotlight = function(options) {
		  
		var opts =  $.extend({}, $.fn.spotlight.defaults, options);
		
	    return this.each(function() {
		
				$this = $(this);
				
				//grab all the default settings if none were specified 
				var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
				var t_int = o.timer_interval;
				var selector = o.selector;
				var index = o.startindex;
				var fadeDuration = o.fadeDuration;
				
				//the number of features based on how many tabs there are
				numslides = $(".slidelink").length;

				//holds the timer object so we can stop it elsewhere
				var timer;
				
				//the offset used to show new slides			
				var offset = 0;
				
				//a boolean of whether or not IE is being used
				var ie = (navigator.userAgent.indexOf("MSIE") == -1)?false:true;
				
				//IE computes the offset wrong by 4 pixels
				var iefix = 4;
				
				//take out the id anchors so when you click a tab
				//the offset isn't all screwed up and so that the
				//view doesn't jump on the user
				$(selector).find(".slidenav").find("a").attr("href","#");
				
				//set the first slide if variable is passed in
				setFirstSlide(index, numslides, ie, iefix);
				
				//enter the timer
				//every interval, t_int default of 5000ms, it runs the following code	
				timer = $.timer(t_int, function (timer) {
					
					//stop the timer, otherwise it will run while the fades are going
					timer.stop();
					
					//get the active tab number
					index = $(".slidelink").index( $(".active") );
					
					//if the slide is not the last then,
					//move onto the next and increment the index
					if( index < numslides - 1 )
					{	
						//the offset is the CSS Top property, and is moves the top property by the height of the div
						offset -= $(".slide").height();
						
						//IE has a bug that doesn't compute the offset correctly
						//add 4 pixels to fix it
						if(ie)
							offset -= iefix;
						
						//set the new index
						index++;
					}
					//reset it all to 0 if its back to the first sldie
					else
					{
						offset = 0;
						index = 0;
					}
					
					//fade to 0 AND	
					$(".spotview ul").fadeTo(fadeDuration,0, function(){
						
						//make the current slide the active one
						$(selector).find(".active").removeClass("active");
						
						//grab the next tab and make it active
						$(".slidelink").eq(index).addClass("active");
						
						//move the slide into position
						$(".spotview ul").css("top", offset);
						
					}).fadeTo(fadeDuration,1);
					
					//start the timer over
					timer.reset(t_int);
					
					return timer;	
				});
											
				//when you click a tab, go there stop the timed show
				$(".slidelink").click(function(){	
			
					
					//stop the timer 
					timer.stop();
					
					//grab the tab
					index = $(".slidelink").index(this);
					
					if ( $(this).hasClass("active") )
					{
						//do nothing if user clicks the active tab
					}
					else
					{
						//stop an animation if it is going
						$(".spotview ul").stop();
						
						//the first tab is at 0					
						offset = 0 - (index * $(".spotview").height());
					
						//IE has a bug that doesn't compute the offset correctly
						//add 4 pixels to fix it if we are not on the first tab
						if(ie)
							offset -= iefix * index;
					
						//remove the active class from the previous tab
						$(this).parent().find(".active").removeClass("active");
					
						//make this tab the active one
						$(this).addClass("active");
					
						//fade out AND
						$(".spotview ul").fadeTo(fadeDuration,0, function(){
						
							// move the slide into position
							$(".spotview ul").css("top", offset);				
							
						//fade back in
						}).fadeTo(fadeDuration,1);
					}
					
				});						
		
		    });  
	 	};  
	
		function setFirstSlide(index, max, ie, iefix){
			
			//set the first active tab as long as it is a real tab
			if(index > (max-1))
				index = 0;
			
			//grab the correct slide and make it active
			var slide = $(".slidelink").get(index);
			$(slide).addClass("active");
		
			//the first tab is at 0					
			offset = 0 - (index * $(".spotview").height());
		
			//IE has a bug that doesn't compute the offset correctly
			//add 4 pixels to fix it if we are not on the first tab
			if(ie)
				offset -= iefix * index;

			//move the slide into position
			$(".spotview ul").css("top", offset);
		}

		//defaults	
		$.fn.spotlight.defaults = {
			selector: ".spot",
			timer_interval: "5000",
			startindex: "0",
			fadeDuration: "1000"
		};
		
})(jQuery);

// 
// 

// 	
// });