/*
*	annoBox (announcement box) jQuery plugin
*	author: Bill Garoutte 
*	change log: 6/23/09 created
*	
*	annoBox converts a div containing child announcement paragraphs into a carousel-type display.
*	If you don't use p tags, you'll need to update the annoElemType option when initializing the annoBox.
*	Usage:
*	(in your HTML):
*	<div id="uniqueIdentifier">
*		<p class="alert"> some text or html here </p>
*		<p class="info"> here is another announcement </p>
*	</div> 
*
*	(in your JS onready statement: only pass options that are different from the defaults):
*	options = {
*		autoCycleSpeed : 6000
*	}
*	$("#uniqueIdentifier").annoBox(options);
*/

(function($){
	var annoIndicators, annoItems, opts, t;
	
	// plugin definition
	$.fn.annoBox = function(options){
		opts = $.extend({}, $.fn.annoBox.defaults, options);
		
		return this.each(function(){
			var obj = $(this);
			
			//Get a collection of announcement elements
			annoItems = $(opts.annoElemType, obj);
			
			//Hide all announcements and display the current one
		   annoItems.hide();
		   setCurrentAnnouncement();

			// Append the announcement box with indicator graphics and light up the selected one
			appendIndicators(obj);
			setCurrentIndicator();
				
			// if the autoCycle option is on , add the hover behavior and start it rolling
			if(opts.autoCycleOn){
				obj.hover(
					function(){clearInterval(t)},
					function(){startRotation()}
				);
				startRotation();
			}
		});
	};

	// exposed plugin defaults
	$.fn.annoBox.defaults = {
		annoElemType : "p",
		annoIndicatorOff : "images/bullet_green.png",
		annoIndicatorOn : "images/bullet_black.png",
		autoCycleOn : true,
		autoCycleSpeed : 5000,
		annoIndx : 0
	};

// private functions follow:
	// adds the indicator bar, with a clickable indicator for each announcement 
	function appendIndicators($obj){
		$obj.prepend('<div id="annoTools"><\/div>');
		
		// for each announcement add an indicator
		annoItems.each(function(){
			$("#annoTools", $obj).append("<img  src='" + opts.annoIndicatorOff + "' alt='Click to jump to this newsitem' \/>");
		});
		
		// provide a reference to the indicator image collection we just created
		annoIndicators = $("#annoTools img", $obj);
		
		// add behavior to the indicators
		annoIndicators.click(function(){
			opts.annoIndx = annoIndicators.index($(this));
			setCurrentIndicator();
			setCurrentAnnouncement();
		});
	};
	
	// hide all announcements and displays the current one
	function setCurrentAnnouncement(){
		annoItems.css("display","none");
		annoItems.eq(opts.annoIndx).fadeIn("medium");
	 };
	
	// sets all indicators to the default image and lights the current one
	function setCurrentIndicator(){
		annoIndicators.attr("src", opts.annoIndicatorOff);
		annoIndicators.eq(opts.annoIndx).attr("src", opts.annoIndicatorOn);
	};
	
	function startRotation(){
		t = setInterval(function(){
			opts.annoIndx < annoItems.length - 1 ?  opts.annoIndx ++ : opts.annoIndx = 0 ;
			setCurrentIndicator();
			setCurrentAnnouncement();
		}, opts.autoCycleSpeed);
	};
})(jQuery);
