/*
 * jQuery Featured LIVESTRONG Plugin 
 * Version: 1.0 (Feb 03 2009)
 * Requires: jQuery v1.3.1
*/


(function($){

$.fn.feature = function(options) {
	// Extend our default options with those provided.
	var opts = $.extend({}, $.fn.feature.defaults, options);

	// Local Variables
	var items			= $(this).find("li");
	var timeoutID		= null;
	var current_index	= 0;
	var controls 		= document.createElement("div");
	var parent 			= this;
	
	// Add the controls to the element.
	controls.className = "controls";
	this[0].appendChild(controls);

	this.show_item = function(index, func) {
		this.hide_item(current_index);
		
		if(index >= items.length) {
			index = 0;
		}
		
		if(index < 0) {
			index = items.length-1;
		} 
		
		// Set the current index to the new value.
		current_index = index;
		
		// $(items[index].shadow_box).hide();
		$(items[index]).animate({ 
			opacity: "show"
		}, opts.animation_duration, function(){
			this.control.src = opts.bullet_on;
		});
	}

	this.hide_item = function(index) {
		// Check if the index actually exists.
		if(!items[index]) {
			index = 0;
		} 
		
		$(items[index]).animate({ 
			opacity: "hide"
		}, opts.animation_duration, function(){
			this.control.src = opts.bullet_off;
		});
	}

	this.show_next = function() {
		this.show_item(current_index + 1);
	}

	this.show_previous = function() {
		this.show_item(current_index - 1);
	}

	this.rotate = function() {
		if(opts.pause == true) {
			return;
		}
		this.show_next();
		this.start_rotate();
	}

	this.start_rotate = function() {
		var self = this;
		timeoutID = window.setTimeout(function(){ self.rotate(); }, opts.rotate_delay);
	}

	this.stop_rotate = function() {
		window.clearTimeout(timeoutID);
	}

	$(items).each(function(index) {
		// Create a control element for each featured module
		var img = document.createElement("img");
		img.src = index == 0 ? opts.bullet_on : opts.bullet_off;
		img.index = index;
		img.onclick = function() {
			parent.show_item(this.index);
		}
		controls.appendChild(img);
		this.control = img;
		
		if(opts.show_controls == false) {
			controls.style.display = "none";
		}
		
		this.title_obj = $(this).find(".title")[0];
		this.description_obj = $(this).find(".description")[0];
		
		var shadow_box = document.createElement("div");
		$(shadow_box).css({
				opacity: 0.4, 
				zIndex: 70, 
				position: "absolute", 
				left: "0px", 
				bottom: "0px", 
				width: $(items).width() + "px", 
				height: 80 + "px", 
				background: "#000000"
			});
		
		this.appendChild(shadow_box);
		this.shadow_box = shadow_box;
	});
	
	this.start_rotate();

	return(this);
};

// override these globally if you like (they are all optional)
$.fn.feature.defaults = {
	rotate_delay:			5000,
	pause:					false,
	show_controls:			true,
	animation_duration:		100,
	bullet_off:				"img/white_bullet.gif",
	bullet_on:				"img/yellow_bullet.gif"
};

})(jQuery);

