﻿/*
    Rotator
*/
$(function($) {
    var activeItemID = 0;
    var totalItems = 0;
    var timeoutID;
    var items;
    var activeItem = null;
    var lastItem;
    var firstItem;
    var methods = {
        init: function(options) {
            //Add the rotator class
            if (!$(this).hasClass("rotator"))
                $(this).addClass("rotator");

            $("ul:first", $(this)).addClass("rotator-items");
            $(".rotator-items li").hide();

            showItem($(".rotator-items li:first"));

            timeoutID = setTimeout("$('#" + $(this).attr("id") + "').rotator('next')", 7000);
        },
        next: function() {
            showItem($(".rotator-items li.active").next());
            timeoutID = setTimeout("$('#" + $(this).attr("id") + "').rotator('next')", 7000);
        }
    }

    function showItem(item) {
        var active = ($(".rotator-items li.active").size() > 0) ? $(".rotator-items li.active") : false;
        var next = (item.size() == 0) ? $(".rotator-items li:first") : item;

        $(".rotator-items li.active").removeClass("active");

        if (active == false) {
            $(next).fadeIn();
        } else {
            $(active).fadeOut("fast", function() {
                $(next).fadeIn();
            });
        }

        next.addClass("active");
    }

    $.fn.rotator = function(method) {
        if (methods[method]) {
            //Call method 
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            //No method given call the default INIT method
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.rotator');
        }
    }
});	

