(function($) {
	$.fn.slideshow = function(delay, fadeSpeed, prevLinkID, nextLinkID, navListID) {
	    if ($(this).children().length > 0) {
            $(this).children().hide();
            $(this).children().eq(0).show();

            if ($(this).children().length > 1) {
                $(this).attr("doAuto", "1");
                $(this).attr("doClick", "1");

                if (typeof(delay) == 'undefined') { delay = 5000; }
                $(this).attr("delay", delay);

                if (typeof(fadeSpeed) == 'undefined') { fadeSpeed = 'slow'; }
                $(this).attr("fadeSpeed", fadeSpeed);

                var containerid = $(this).attr("id");

                if (prevLinkID) {
                    $("#" + prevLinkID).attr("container", containerid);
                    $("#" + prevLinkID).click(function() { slideshow_clickItem($(this).attr("container"), slideshow_getCurrentVisibleChild($(this).attr("container")) - 1); return false; });
                }

                if (nextLinkID) {
                    $("#" + nextLinkID).attr("container", containerid);
                    $("#" + nextLinkID).click(function() { slideshow_clickItem($(this).attr("container"), slideshow_getCurrentVisibleChild($(this).attr("container")) + 1); return false; });
                }

                if (navListID) {
                    $(this).attr("navListID", navListID);
                    var ictr = 0;
                    $("#" + navListID + " a.slide_item").each(function() {
                        $(this).attr("container", containerid);
                        $(this).attr("navID", ictr);
                        if (ictr == 0) { $(this).parent().addClass("active"); }
                        ictr++;
                    });

                    $("#" + navListID + " a.slide_item").click(function() { 
                        if (!$(this).parent().hasClass("active")) {
                            slideshow_clickItem($(this).attr("container"), $(this).attr("navID"));  
                        }

                        return false;
                    });
                } else {
                    $(this).attr("navListID", "_");
                }

                if (delay != 0) {
                    slideshow_rotateChildrenTimer(containerid);
                }
            } else {
                if (prevLinkID) {
                    $("#" + prevLinkID).hide();
                }

                if (nextLinkID) {
                    $("#" + nextLinkID).hide();
                }

                if (navListID) {
                    $("#" + navListID).hide();
                }
            }
        }
    };
})(jQuery);


function slideshow_rotateChildrenTimer(container) {
    var delay = $("#" + container).attr("delay");
    window.setTimeout("slideshow_rotateDiv('" + container + "')", delay);
}

function slideshow_rotateDiv(container) {
	var containerEl = $("#" + container);

	if (containerEl.attr("doAuto") == "1") {
        containerEl.attr("doClick", "0");

        var delay = containerEl.attr("delay");
        var fadeSpeed = containerEl.attr("fadeSpeed");
	    var currentVisible = slideshow_getCurrentVisibleChild(container);
	    var nextVisible = currentVisible + 1;
	    if (nextVisible >= containerEl.children().length) { nextVisible = 0; }

        if (fadeSpeed == "0" || fadeSpeed == 0) {
            containerEl.children(":visible").hide();
            containerEl.children().eq(nextVisible).show();
            containerEl.attr("doClick", "1");
            slideshow_rotateChildrenTimer(container);
            slideshow_setNavListItem(container);
        }
        else {
            containerEl.children().eq(currentVisible).fadeOut(fadeSpeed);
            containerEl.children().eq(nextVisible).fadeIn(fadeSpeed, function() {
                containerEl.attr("doClick", "1");
                slideshow_setNavListItem(container);
                slideshow_rotateChildrenTimer(container);
            });
        }
    }
}

function slideshow_getCurrentVisibleChild(container) {
	for (var i = 0; i < $("#" + container).children().length; i++) {
		var el = $("#" + container).children().eq(i);
		var disp = el.css("display");
		if (disp != "none") { return i; }
	}

	return 0;
}

function slideshow_clickItem(container, itemid) {
    var containerEl = $("#" + container);

    if (containerEl.attr("doClick") == "1") {
        containerEl.attr("doAuto", "0");
        containerEl.attr("doClick", "0");

        if (itemid >= containerEl.children().length) { itemid = 0; }
        if (itemid < 0) { itemid = containerEl.children().length - 1; }
        var fadeSpeed = containerEl.attr("fadeSpeed");

        if (fadeSpeed == "0" || fadeSpeed == 0) {
            containerEl.children(":visible").hide();
            containerEl.children().eq(itemid).show();
            containerEl.attr("doClick", "1");
            slideshow_setNavListItem(container);
        }
        else {
            containerEl.children(":visible").fadeOut(fadeSpeed);
            containerEl.children().eq(itemid).fadeIn(fadeSpeed, function() {
                containerEl.attr("doClick", "1");
                slideshow_setNavListItem(container);
            });
        }
    }
}

function slideshow_setNavListItem(container) {
    var containerEl = $("#" + container);
    var navListID = containerEl.attr("navListID");

    if (navListID != "_") {
        var navListEls = $("#" + navListID + " a.slide_item");
        navListEls.parent().removeClass("active");
        var itemid = slideshow_getCurrentVisibleChild(container);
        navListEls.eq(itemid).parent().addClass("active");
    }
}
