(function($) {
    $.fn.extend
	(
		{
		    Fadelicious: function(options) {
		        var defaults =
				{
				    itemsSelector: "li",
				    pauseMilliseconds: 3000,
				    fadeSpeedMilliseconds: 1000,
				    makeMePositionRelative: true,
				    doPagination: false
				};

		        var o = $.extend(defaults, options);

		        return this.each
				(
					function() {
					    var elThis = jQuery(this);

					    if (!o.makeMePositionRelative) {
					        elThis.css
					        (
					            {
					                'position': 'absolute'
					            }
					        );
					    }
					    var aryItems = elThis.find(o.itemsSelector);

					    if (jQuery.browser.msie) {
					        var aryImgPng = aryItems.find('img[src*=".png"]');
                             
					        aryImgPng.each
					        (
					            function() {
					                var img = jQuery(this);
					                var span = jQuery(document.createElement('span'));
					                var strStyle = "display: block;";
                                    
                                    // need to do this to get img dimensions
					                var imgCopy = jQuery(document.createElement('img'));
                                    
					                imgCopy.css({ position: "absolute", visibility: "hidden", display: "block" });
					                imgCopy.attr('src', img.attr('src'));
					                jQuery('body').append(imgCopy);

					                strStyle += "width: " + imgCopy.width() + "px;";
					                strStyle += "height: " + imgCopy.height() + "px;";
					                
					                imgCopy.remove();
					                
					                if (jQuery.browser.version < 9)
					                    strStyle += "filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr('src') + "', sizingMethod='crop'));";

					                strStyle += img.attr('style') ? img.attr('style') : "";
					                span.attr('style', strStyle);
					                img.parent().append(span);
					                img.hide();
					            }
					        );
					    }

					    if (aryItems.length > 0) {
					        aryItems.css
					        (
					            {
					                'position': 'absolute',
					                'display': 'none'
					            }
					        );
						    
						if(o.doPagination && aryItems.length > 0)
						{
							var div = jQuery(document.createElement('div'));
							 div.addClass("fadelicious_pagination");
							
							for(var i=0; i < aryItems.length; i++)
							{
								var a = jQuery(document.createElement('a'));
								a.attr('href', 'javascript:void(0);');
								a.attr('item', i);
								a.html(i);
								div.append(a);
							}
							
							elThis.after(div);
						}

						var aryPaginationLinks = elThis.next('.fadelicious_pagination').children('a');
						var iNext = 0;
						var objMyState = {Animating : false};
						
						aryPaginationLinks.click
						(
							function()
							{
								var a = jQuery(this);
								var iCurrent = iNext;
								iNext = parseInt(a.attr('item'));
								objMyState.Animating = true;
								fadeNext(o, aryItems, aryPaginationLinks, iCurrent, iNext, objMyState);
							}
						);
						
					        jQuery(aryItems.get(iNext)).show();
					        jQuery(aryPaginationLinks.get(iNext)).addClass('on');
						
					        setInterval
					        (
					            function() 
							{
								if(!objMyState.Animating)
								{
									var iCurrent = iNext;
									iNext = iNext + 1 > aryItems.length - 1 ? 0 : ++iNext;
									
									fadeNext(o, aryItems, aryPaginationLinks, iCurrent, iNext, objMyState);
								}
					            },
					            o.pauseMilliseconds
					        );
					    }
					}
				);
					
				function fadeNext(o, aryItems, aryPaginationLinks, iCurrent, iNext, objState)
				{
					var current = jQuery(aryItems.get(iCurrent));
					var next = jQuery(aryItems.get(iNext));

					if(aryPaginationLinks.length > 0)
					{
						aryPaginationLinks.removeClass('on');
						jQuery(aryPaginationLinks.get(iNext)).addClass('on');
					}
					
					next.fadeIn
					(
						o.fadeSpeedMilliseconds, 
						function()
						{ 
							objState.Animating = false; 
						}
					);
					current.fadeOut(o.fadeSpeedMilliseconds);
				}
		    }
		}
	);
}
)(jQuery);  
