(function ($) {
    jQuery.fn.SQ1ImageViewer = function (options) {
        var defaults = {
            scrollInterval: 7000,
            slideSpeed: 1500,
            autoscroll: true,
            randomize: false,
            autoresume:false
        };
        var options = $.extend(defaults, options);
        if (options.randomize == true) {
            var arrPics = new Array();
            var i = 0;
            $("ul.pictures").find("li").each(function () {
                arrPics[i] = $(this);
                $(this).remove()
                i++;
            });
            arrPics.sort(randOrd);
            for (var j = 0; j < arrPics.length; j++) {
                $("ul.pictures").append(arrPics[j]);
            }
        }
        $(this).show();
        return this.find("ul").each(function () {
            if ($(this).attr("class") == "counter") {
                var count = $(".counter li").size();
                showImage(0)
                var slideShow;
                if (options.autoscroll == true) {
                    slideShow = setInterval('autoScroll(' + options.slideSpeed + ' )', options.scrollInterval);
                }
                //pause
                $(this).parent().find(".pause").click(function () {
                    clearInterval(slideShow);
                });
                //restart
                $(this).parent().find(".restart").click(function () {
                    slideShow = setInterval('autoScroll(' + options.slideSpeed + ' )', 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.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)
//                    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.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 autoScroll(slideSpeed)
{
    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)
    //$(".debug").html(count + " " + index)
}

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

function showImage(index) {
    $("ul.counter").find("li").each(function () {
        if ($("ul.counter li").index(this) == index) {
            $(this).attr("class", "on")
        }
        else {
            $(this).attr("class", "")
        }
    });
    $("ul.pictures").find("li").each(function () {
        if ($("ul.pictures li").index(this) == index) {
            $(this).fadeIn()
        }
        else {
            $(this).fadeOut()
        }
    });
}



