﻿

    var totalSlides = 0;
    var currentSlide = 1;
    var contentSlides = "";

    $(document).ready(function () {
        $("#slideshow-previous").click(showPreviousSlide);
        $("#slideshow-next").click(showNextSlide);
        $(".slideshow-content").click(alertme);
        $(".slideshowjump").click(function () {
            goto($(".slideshowjump").index(this));
        });
        $(".slideshowjump").eq(1).addClass("slideshowjumphover");

        // Run our swapImages() function every 5secs
        setInterval('swapImages()', 15000);


        var totalWidth = 0;
        contentSlides = $(".slideshow-content");
        contentSlides.each(function (i) {
            totalWidth += this.clientWidth;
            totalSlides++;
        });
        $("#slideshow-holder").width(totalWidth);
        $("#slideshow-scroller").attr({ scrollLeft: 0 });
        updateButtons(1000);
    });

    function swapImages() {
        showNextSlide();
    }
    function showPreviousSlide() {
        if (currentSlide > 0) {
            currentSlide--;
            
        }
        else {
            currentslide = totalSlides - 1;
        }
        updateContentHolder(1000);
            updateButtons();
    }

    function showNextSlide() {
        if(currentSlide==(totalSlides-1)) {
            currentSlide++;
            updateContentHolder(1000);
        }

        if (currentSlide < totalSlides) {
            currentSlide++;
        }
        else {
            currentSlide = 1;
        }
        updateContentHolder(1000);
            updateButtons();
    }

    function updateContentHolder(animateTime) {
        var scrollAmount = 0;
        //alert(totalSlides + ' cur ' + currentSlide);
        //if (currentSlide == totalSlides) {
        //    $("#slideshow-scroller").animate({ opacity: 0 }, 1000, function (i) {
        //        currentSlide = 0;
        //        updateContentHolder();
        //        $("#slideshow-scroller").animate({ opacity: 1 }, 1000);
        //    });
        //}
        //else {
            contentSlides.each(function (i) {
                if (currentSlide - 1 > i) {
                    scrollAmount += this.clientWidth;
                }
            });

            if (animateTime == 1) {
                    $("#slideshow-scroller").animate({ opacity: 0 }, 500).animate({ scrollLeft: scrollAmount }, animateTime).animate({ opacity: 1 }, 500);
            }
            else
            {
                if (currentSlide == 1) {
                    $("#slideshow-scroller").animate({ scrollLeft: scrollAmount }, 1);
                }
                else {
                    $("#slideshow-scroller").animate({ scrollLeft: scrollAmount }, animateTime);
                }
            }
                $(".slideshowjump").each(function (i) {
                    if (i == currentSlide) {
                        $(this).addClass("slideshowjumphover");
                    }
                    else
                        $(this).removeClass("slideshowjumphover");//.css("background-color", "blue");
                    
                });
            
            if (currentSlide == totalSlides)
                    $(".slideshowjump").eq(1).addClass("slideshowjumphover");//.css("background-color", "green");
        //}
        }


    function updateButtons() {
        if (currentSlide < totalSlides) {
            $("#slideshow-next").show();
        } else {
            $("#slideshow-next").hide();
        }
        if (currentSlide > 1) {
            $("#slideshow-previous").show();
        } else {
            $("#slideshow-previous").hide();
        }
    }
    function alertme() {
        //alert(currentSlide + ' ' + totalSlides);
    }

    function goto(i) {
        currentSlide = i;
        updateContentHolder(1);
    }

