﻿(function ($) {
    jQuery.fn.SQ1VerticalViewer = function (options) {
        var defaults = {
            scrollInterval: 15000,
            slideSpeed: 2000,
            autoscroll: true,
            randomize: false,
            autoresume: false,
            itemHeight: 100
        };
        var options = $.extend(defaults, options);
        var $viewer = $(this);
        if (options.randomize == true) {
            var arrPics = new Array();
            var i = 0;
            $viewer.find("ul.image li").each(function () {
                arrPics[i] = $(this);
                $(this).remove()
                i++;
            });
            arrPics.sort(randOrd);
            for (var j = 0; j < arrPics.length; j++) {
                $viewer.find("ul.image").append(arrPics[j]);
            }
        }
        $(this).show();
        $(this).css({ height: ($(".vs-counter li").size() * options.height) + "px" })
        return this.find("ul").each(function () {
            if ($(this).attr("class") == "vs-counter") {
                var count = $(".vs-counter li").size();
                showNext(0, options.itemHeight, options.slideSpeed)
                var slideShow;
                if (options.autoscroll == true) {
                    slideShow = setInterval('autoVerticalScroll(' + options.slideSpeed + ', ' + options.itemHeight + ' )', options.scrollInterval);
                }
                //                //pause
                //                $(this).parent().find(".pause").click(function () {
                //                    clearInterval(slideShow);
                //                });
                //                //restart
                //                $(this).parent().find(".restart").click(function () {
                //                    slideShow = setInterval('autoScroll(' + options.slideSpeed + ', ' + options.itemHeight + ' )', options.scrollInterval);
                //                });
                //                //next
                //                $(this).parent().find(".next").click(function () {
                //                    clearInterval(slideShow);
                //                    var activeButton = $("ul.counter").find("li.on");
                //                    var index = $(".counter li").index(activeButton);
                //                    var count = $(".counter li").size();
                //                    if (index == (count - 1))
                //                        index = -1;
                //                    index = index + 1;
                //                    showImage(index)
                //                    //                    if (options.autoscroll == true && options.autoresume == true) {
                //                    //                        slideShow = setInterval('autoScroll(' + options.slideSpeed + ' )', options.scrollInterval);
                //                    //                    }
                //                    if (options.autoscroll == true && $(".pause").is(":visible") == true) {
                //                        slideShow = setInterval('autoScroll(' + options.slideSpeed + ', ' + options.itemHeight  + ')', options.scrollInterval);
                //                    }
                //                    //$(".debug").html(index)
                //                });
                //                //prev
                //                $(this).parent().find(".prev").click(function () {
                //                    clearInterval(slideShow);
                //                    var activeButton = $("ul.counter").find("li.on");
                //                    var index = $(".counter li").index(activeButton);
                //                    var count = $(".counter li").size();
                //                    if (index == 0)
                //                        index = count;
                //                    index = index - 1;
                //                    showImage(index, options.itemHeight)
                //                    //                    if (options.autoscroll == true && options.autoresume == true) {
                //                    //                        slideShow = setInterval('autoScroll(' + options.slideSpeed + ' )', options.scrollInterval);
                //                    //                    }
                //                    if (options.autoscroll == true && $(".pause").is(":visible") == true) {
                //                        slideShow = setInterval('autoScroll(' + options.slideSpeed + ', ' + options.itemHeight + ' )', options.scrollInterval);
                //                    }
                //                    $(".debug").html(index)
                //                });

                //                //button clicks
                //                $(this).find("li").click(function () {
                //                    //alert("click")
                //                    clearInterval(slideShow);
                //                    $(this).parent().find("li").each(function () {
                //                        $(this).attr("class", "")
                //                    });
                //                    $(this).attr("class", "on")
                //                    var index = $("ul.counter li").index(this);
                //                    $("ul.pictures").find("li").each(function () {
                //                        if ($("ul.pictures li").index(this) == index) {
                //                            $(this).fadeIn()
                //                        }
                //                        else {
                //                            $(this).fadeOut()
                //                        }
                //                    });
                //                    if (options.autoscroll == true) {
                //                        slideShow = setInterval('autoScroll(' + options.slideSpeed + ' )', options.scrollInterval);
                //                    }
                //                });
            }
        });
    };
})(jQuery);
function autoVerticalScroll(slideSpeed, itemHeight) {
    var activeButton = $("ul.vs-counter").find("li.on");
    var index = $("ul.vs-counter li").index(activeButton);
    var count = $("ul.vs-counter li").size();
    if (index == (count - 1))
        index = -1;
    index = index + 1;
    showNext(index, itemHeight, slideSpeed)
    //$(".debug").html(count + " " + index)
}

function randOrd() {
    return (Math.round(Math.random()) - 0.5);
}

function showNext(index, itemHeight, slideSpeed) {
    $("ul.vs-counter").find("li").each(function () {
        if ($("ul.vs-counter li").index(this) == index) {
            $(this).attr("class", "on")
        }
        else {
            $(this).attr("class", "")
        }
    });
    var moveUp = -(itemHeight * index)
    if (index == 0) {
        $("ul.image").css({ 'top': moveUp + 'px' })
    } else {
        $("ul.image").animate({ top: moveUp + 'px' }, slideSpeed)
    }
}



