﻿
(function($) {
    $.fn.extend({ tprotator: function(options) {
        var defaults = { rotationSpeed: 10000, transitionSpeed: 1000, nav: "#rotator-nav", navitem: "#rotator-nav li" }
        var options = $.extend(defaults, options); var o = options; var obj = $(this); var rt; function rotateSlides(newslide) {
            $("#" + obj.attr("id") + " > div:not(" + o.nav + ")").fadeOut(o.transitionSpeed); $("#" + obj.attr("id") + " div.slide" + newslide).fadeIn(o.transitionSpeed); $("#highlight").animate({ left: (newslide * 35) + "px" }); $(o.navitem).removeClass("active"); $(o.navitem + ":nth-child(" + (newslide) + ")").addClass("active"); currentslide = 0; slideid = newslide; if (newslide < slidecount - 1) { currentslide = newslide + 1; }
            rt = setTimeout(function() { rotateSlides(currentslide) }, o.rotationSpeed);
        }
        function initializeNav() {
            $("<div/>").attr("id", "highlight").appendTo(o.nav); $(o.nav).css("display", "block"); $(o.navitem + ":first-child").addClass("active"); $(o.navitem).click(function() {
                clearTimeout(rt); var id = $(this).index(); if (parseInt(slideid) !== parseInt(id)) { rotateSlides(parseInt(id)); } else {
                    if (slideid < slidecount - 1) { currentslide = slideid + 1; }
                    rt = setTimeout(function() { rotateSlides(currentslide) }, o.rotationSpeed);
                } 
            });
        }
        function initialize() {
            var i = 0; obj.children().not(o.nav).each(function() {
                $(this).addClass("slide" + i); if (i > 0) { $(this).css("display", "none"); }
                $(this).css("position", "absolute"); i++;
            }); slidecount = i; slideid = 0; if (slidecount > 1) { initializeNav(); rt = setTimeout(function() { rotateSlides(1) }, o.rotationSpeed); } 
        }
        initialize();
    } 
    });
})(jQuery);

$("#rotator").tprotator({
    rotationSpeed: 6000,
    transitionSpeed: 1000
});
