$(document).ready(function(){	
	$("#headlines").newsticker(); // or $("#news").newsTicker(8000);
	
	$.fn.Story.Ready();
	
	$('#quotes').rotateQuotes({fadeSpeed:600, interval:10000});
});

/****************************
*
* Story Switcher Plugin
*
*****************************/
$.fn.extend({
Story : {
    Ready : function()
		{
		
	  $('#story-selector a.selector')
	    .hover(
	      function() {
	        $(this).addClass('slideHover');
	      },
	      function() {
	        $(this).removeClass('slideHover');
	      }
	    )
	    .click(
	      function(e) {
	      
	      	e.preventDefault();
	      	
	        $.fn.Story.SlideInterupted = true;
	        $('div.story').hide();
	        $('a.selector').removeClass('active');
			
	        $('div#story' + this.id.split("").pop()).show()
	        
	        $(this).addClass('active');
	        
	      }
	    );
	    
		this.SlideCounter = 1;
		this.SlideInterupted = false;
		this.numSlides = $('.story').size();
		this.SlideShow();
		},
		
		SlideShow : function()
		{
			if ($.fn.Story.SlideInterupted) {
				return;
			}
		
			$.fn.Story.SlideLast = $.fn.Story.SlideCounter - 1;
			
			if ($.fn.Story.SlideLast < 1) {
			
				$.fn.Story.SlideLast = $.fn.Story.numSlides;
			}
			
			$('div#story' + $.fn.Story.SlideLast).fadeOut(
			'slow',
				function() {
					$('a#selector' + $.fn.Story.SlideLast).removeClass('active');
					$('a#selector' + $.fn.Story.SlideCounter).addClass('active');
					$('div#story' + $.fn.Story.SlideCounter).fadeIn('slow');
				
					$.fn.Story.SlideCounter++;
				
					if ($.fn.Story.SlideCounter > $.fn.Story.numSlides) {
						$.fn.Story.SlideCounter = 1;
					}
				
					setTimeout('$.fn.Story.SlideShow();', 8000);
				}
			);
		}
	}
});


/************************************************************************************
*
* Quotes Plugin
*
* Usage:
* $('target div').rotateQuotes({
*																fadeSpeed: Speed of fading animation,
*																interval: How long to display a single quote
*															})
*
*************************************************************************************/
(function($){
	$.fn.rotateQuotes = function(config){
		
		//Configuration Settings
		var defaults = {
			fadeSpeed:600, 			//How fast the photos fade
			interval:5000				//Autoplay interval
		}
		config = $.extend(defaults, config);

		//Initialize
		return this.each(function(){
			
			var container = $(this);
			var quotes = new Object();
			var count = 0;
			var current = 1;
			
			//Put some ID's on the quotes
			container.children('p').each(function(){
				count++;
				$(this).attr('id', 'quote_'+count);
			});
			
			//Start autoplay
			if(count > 1){
				window.setInterval(showNext, config.interval);			
			}

			function showNext(){				
				var next = current+1;
				if(next > count){ next = 1; }
				$('#quote_'+current).fadeOut(config.fadeSpeed, function(){
					$('#quote_'+next).fadeIn(config.fadeSpeed);
				});
				current = next;
			}
								
		});
	}
})(jQuery);