﻿
var slideshow = {

    rotationSpeed: 10000,
    transitionSpeed: 1000,

    rotateSlides: function (newslide) {
        $("#rotator > div:not('#rotator-nav')").fadeOut(slideshow.transitionSpeed);
        $("#rotator div#slide" + (newslide + 1)).fadeIn(slideshow.transitionSpeed);
        $("#rotator-nav li").removeClass("active");
        $("#rotator-nav li#nav" + newslide).addClass("active");
        currentslide = 0;
        slideid = newslide;
        if (newslide < slidecount - 1) {
            currentslide = newslide + 1;
        }
        rt = setTimeout("slideshow.rotateSlides(" + currentslide + ")", slideshow.rotationSpeed);
    },

    initializeNav: function () {
        $("#rotator-nav li:not(#navprev, #navnext)").click(function () {
            clearTimeout(rt);
            var id = this.id.replace("nav", "");
            if (parseInt(slideid) !== parseInt(id)) {
                slideshow.rotateSlides(parseInt(id));
            } else {
                if (slideid < slidecount - 1) {
                    currentslide = slideid + 1;
                }
                rt = setTimeout("slideshow.rotateSlides(" + currentslide + ")", slideshow.rotationSpeed);
            }
        });
        $("#rotator-nav li#navprev").click(function () {
            var newslide = parseInt(currentslide);
            if (newslide === 0) {
                newslide = 8;
            }
            newslide = newslide - 1;
            if (newslide > 0) {
                clearTimeout(rt);
                slideshow.rotateSlides(newslide - 1); ;
            }
        });
        $("#rotator-nav li#navnext").click(function () {
            var newslide = parseInt(currentslide);
            if (newslide > 0) {
                clearTimeout(rt);
                slideshow.rotateSlides(newslide); ;
            }
        });
    },

    initialize: function () {
        var i = 0;
        var navcontainer = $("<div/>").attr("id", "rotator-nav").appendTo("#rotator");
        var nav = $("<ul/>").appendTo(navcontainer).css("display", "none");
        var prev = $("<li/>").attr("id", "navprev").html("Previous").appendTo(nav);
        $("#rotator > div:not('#rotator-nav')").each(function () {
            var navitem = $("<li/>").attr("id", "nav" + i).html(i + 1).appendTo(nav);
            if (i > 0) {
                $(this).css("display", "none");
            } else {
                navitem.addClass("active");
            }
            $(this).css("position", "absolute");
            i++;
        });
        var next = $("<li/>").attr("id", "navnext").html("Next").appendTo(nav);
        slidecount = i;
        slideid = 0;
        currentslide = 1;
        if (slidecount > 1) {
            nav.css("display", "block");
            slideshow.initializeNav();
            rt = setTimeout("slideshow.rotateSlides(1)", slideshow.rotationSpeed);
        }
    }

}

if ($("#rotator").length) {
    slideshow.initialize();
}
