﻿/// <reference path="jquery-1.4.1.js"/>
function autoScrollSlider() {
    $(".slides", document).each(function () {
        if ($(this).data("autoScroll") == true) {
            var index = $(this).data("current");            
            $(this).toSlide(++index);
        }
    });
    setTimeout("autoScrollSlider()", 10000);
};
$.fn.initSlider = function (index) {
    return this.each(function () {
        var slider = $(this);
        var slides = $("div.slide", slider);        
        $(slider).data("current", index);
        $(slider).data("autoScroll", true);
        $(slides).not(":eq(" + index + ")").hide();
        $(slides).eq(index).show();
        $(slider).setSliderButtons(index);
        $(".scroll").click(function (e) {
            e.preventDefault();
            $(slider).data("autoScroll", false);
            $(slider).toSlide($(this).data("target"));
        });
    });
};
$.fn.setSliderButtons = function (index) {
    return $(this).each(function () {
        var slides = $("div.slide", this);
        if ($(slides).eq(index).hasClass("slide-no-nav")) {
            $(".scroll-l", this).hide();
            $(".scroll-r", this).hide();
        }
        else {
            if (index > 0) {
                $(".scroll-l", this).data("target", index - 1);
                $(".scroll-l", this).show();
            }
            else
                $(".scroll-l", this).hide();
            if (index < slides.length - 1) {
                $(".scroll-r", this).data("target", index + 1);
                $(".scroll-r", this).show();
            }
            else
                $(".scroll-r", this).hide();
        }
    });
};
$.fn.toSlide = function (index) {
    return this.each(function () {
        var slider = $(this);
        var slides = $("div.slide", slider);
        if (index >= slides.length)
            index = 0;
        var current = slider.data("current");
        if (current == index)
            return;
        if (index < 0)
            return;
        if (index >= slides.length)
            return;
        if ($(slides).eq(index).hasClass("slider-nav-stop"))
            $(slider).data("autoScroll", false);
        if ($(":visible", slides).length > 0) {
            $(slides).eq(current).fadeOut(600,
                                        function () {
                                            $(slides).eq(index).fadeIn(1200, function (e) { $(this).trigger("slide_shown", this); });
                                        });
        }
        else
            $(slides).eq(index).fadeIn(1200, function (e) { $(this).trigger("slide_shown", this); });
        $(slider).setSliderButtons(index);
        slider.data("current", index);

    });
}
