﻿jQuery.contentRotatorObjects = new Array();
jQuery.fn.contentRotatorInit = function(passedOptions){
	var options = {
		childElementType: "li",
		delay: 4000,
		effect: 'fade',
		effectSpeed: 'slow',
		contentRotatorId: "",
		contentRotatorObjectsIndex: 0,
		items: new Array(),
		currentItemIndex: 0,
		timerId: "",
		
		showCurrent: function(){
			if (options.effect == "show"){
				jQuery(options.items[options.currentItemIndex]).show(options.hideShowSpeed, function(){
					options.hideCurrent();
				});
			}
			else{
				jQuery(options.items[options.currentItemIndex]).fadeIn(options.hideShowSpeed, function(){
					options.hideCurrent();
				});
			}
		},
		
		hideCurrent: function(){
			options.timerId = setTimeout(options.doHide, options.delay);			
		},
		
		doHide: function(){
			if (options.effect == "show"){
				jQuery(options.items[options.currentItemIndex]).hide(options.hideShowSpeed, function(){
					options.prepareForNextShow();
				});
			}
			else{
				jQuery(options.items[options.currentItemIndex]).fadeOut(options.hideShowSpeed, function(){
					options.prepareForNextShow();
				});
			}
		},
		
		prepareForNextShow: function(){
			options.currentItemIndex++;
			if (options.currentItemIndex == options.items.length)
				options.currentItemIndex = 0;
			options.showCurrent();
		}
		
	};
	
	if (passedOptions) {
		jQuery.extend(options, passedOptions);
	}
	
	options.contentRotatorId = this[0].id;
	jQuery.contentRotatorObjects.push(options);
	options.contentRotatorObjectsIndex = jQuery.contentRotatorObjects.length - 1;

	options.items = this.children(options.childElementType);
	options.items.each(function(i){
		jQuery(this).hide();
	});
	
	if (options.items.length > 0){
		if (options.items.length == 1)
			jQuery(options.items[0]).show();
		else
			options.showCurrent();
	}
		
	return this;
};