

$(document).ready(function () {

    var busy = false;
    var slideshow = $("#dss");
    var items = slideshow.children('.dss-item');
    var current = 0;
    var interval = -1;
    var slideSpeed = 600;
    var delay = 10000;
    var isIE = window.navigator.userAgent.indexOf("MSIE") > -1;

    var rightBtn = $('#dss_right').mousedown(function () {

        slide(current + 1, false, true);
        interval = setSlideshow(interval);

    });

    var leftBtn = $('#dss_left').mousedown(function () {

        slide(current - 1, true, true);
        interval = setSlideshow(interval);

    }); /*.hide();*/

    var circ = $('#dss_circ a').mousedown(function () {

        if ($(this).hasClass('sel')) { return; }

        var index = circ.index(this);

        slide(index, current > index, true);
        interval = setSlideshow(interval);

    });

    interval = setSlideshow();

    function setSlideshow(ref) {

        if (ref) {
            clearInterval(ref);
        }

        return setInterval(function () {
            slide(current + 1, false, true);
        }, delay);
    }

    function slide(i, d, l) {

        if (busy) { return; }

        busy = true;

        var last = current;

        current = l ? loopValueDir(i, items.length - 1, d) : blockValue(i, 0, items.length - 1);

        items.eq(last).animate(protectIEcss({ left: d ? 1888 : 0, opacity: 0 }), slideSpeed * 0.8, function () {

            items.eq(last).hide();

        });


        items.eq(current).css(protectIEcss({ left: d ? 0 : 1888, opacity: 0 })).show().animate(protectIEcss({ left: 944, opacity: 1 }), slideSpeed, function () {

            busy = false;

            /*if (current > 0) {*/
            leftBtn.show().animate({ opacity: 1 }, 250);
            /*}

            if (current < items.length - 1) {*/
            rightBtn.show().animate({ opacity: 1 }, 250);
            /*}*/


            circ.removeClass('sel').animate({ opacity: 1 }, 250).eq(current).addClass('sel');

        });


        circ.animate({ opacity: 0 }, 250);
        rightBtn.animate({ opacity: 0 }, 250, function () { rightBtn.hide(); });
        leftBtn.animate({ opacity: 0 }, 250, function () { leftBtn.hide(); });

    }

    function protectIEcss(obj) {

        if (window.navigator.userAgent.indexOf("MSIE") == -1) {
            return obj;
        }

        var ret = {};

        for (var prop in obj) {
            if (prop != "opacity") {
                ret[prop] = obj[prop];
            }
        }

        return ret;

    }

    function loopValueDir(val, max, d) {

        return d ? (val < 0 ? max : val) : (val > max ? 0 : val);

    }

    function loopValue(val, max) {

        return val > max ? 0 : val;

    }

    function blockValue(val, min, max) {

        if (val < min) {
            return min;
        } else if (val > max) {
            return max;
        } else {
            return val;
        }

    }

});
