﻿(function ($) {

    $.fn.slideshowyeni = function (vars) {

        var obj = this;
        var timeout = (vars.timeout != undefined) ? vars.timeout : 4000;
        var lock = false;

        var items = $("#" + obj[0].id + " DIV.slideitems DIV");
        var controller = $("#" + obj[0].id + " DIV.slidecontroler");
        var headerHTML = "<table><tr><td class=\"arrow\"><img src=\"site/slideshow/arrow.png\" /></td><td>";
        var footerHTML = "</td></tr></table>";
        var innerHTML = "";
        for (var i = 1; i <= items.length; i++) {
            innerHTML += "<span><a slide=\"" + i + "\">" + i + "</a></span>";
        }
        if (innerHTML != "") {
            innerHTML = headerHTML + innerHTML + footerHTML;
            controller.html(innerHTML);
        }

        $(".arrow img", controller).click(function () {
            lock = true;
            var $active = $("#" + obj[0].id + " DIV.slideitems DIV.active");
            var $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN.active");

            if ($active.length == 0) $active = $("#" + obj[0].id + " DIV.slideitems DIV:last");
            if ($contactive.length == 0) $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN:last");

            // use this to pull the divs in the order they appear in the markup
            var $next = $active.next().length ? $active.next() : $("#" + obj[0].id + " DIV.slideitems DIV:first");
            var $contnext = $contactive.next().length ? $contactive.next() : $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN:first");

            // uncomment below to pull the divs randomly
            // var $sibs  = $active.siblings();
            // var rndNum = Math.floor(Math.random() * $sibs.length );
            // var $next  = $( $sibs[ rndNum ] );


            $active.addClass('last-active');
            $contactive.addClass('last-active');

            $next.css({ opacity: 0.0 })
            .addClass('active')
            .animate({ opacity: 1.0 }, 1000, function () {
                $active.removeClass('active last-active');
            });

            $contnext.addClass('active');
            $contactive.removeClass('active last-active');

            lock = false;
        });

        $("a", controller).click(function () {
            lock = true;
            var i = $(this).attr("slide");

            var $active = $("#" + obj[0].id + " DIV.slideitems DIV.active");
            var $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN.active");

            if ($active.length == 0) $active = $("#" + obj[0].id + " DIV.slideitems DIV:last");
            if ($contactive.length == 0) $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN:last");

            var $next = $("#" + obj[0].id + " DIV.slideitems DIV");
            var $contnext = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN");

            $active.addClass('last-active');
            $contactive.addClass('last-active');

            $next.each(function (index) {
                if (index == (i - 1)) {
                    $(this).css({ opacity: 0.0 })
                        .addClass('active')
                        .animate({ opacity: 1.0 }, 1000, function () {
                            $active.removeClass('active last-active');
                        });
                }
            })

            $contnext.each(function (index) {
                if (index == (i - 1)) {
                    $(this).addClass('active');
                    $contactive.removeClass('active last-active');
                }
            });
            lock = false;
        });

        var makeSlider = function () {
            if (lock) { setTimeout(makeSlider, timeout); }
            var $active = $("#" + obj[0].id + " DIV.slideitems DIV.active");
            var $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN.active");

            if ($active.length == 0) $active = $("#" + obj[0].id + " DIV.slideitems DIV:last");
            if ($contactive.length == 0) $contactive = $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN:last");

            // use this to pull the divs in the order they appear in the markup
            var $next = $active.next().length ? $active.next() : $("#" + obj[0].id + " DIV.slideitems DIV:first");
            var $contnext = $contactive.next().length ? $contactive.next() : $("#" + obj[0].id + " DIV.slidecontroler TABLE TR TD SPAN:first");

            // uncomment below to pull the divs randomly
            // var $sibs  = $active.siblings();
            // var rndNum = Math.floor(Math.random() * $sibs.length );
            // var $next  = $( $sibs[ rndNum ] );


            $active.addClass('last-active');
            $contactive.addClass('last-active');

            $next.css({ opacity: 0.0 })
            .addClass('active')
            .animate({ opacity: 1.0 }, 1000, function () {
                $active.removeClass('active last-active');
            });

            $contnext.addClass('active');
            $contactive.removeClass('active last-active');

            var $div = $("#" + obj[0].id + " DIV.slideitems DIV");
            if ($div.length > 1) {
                setTimeout(makeSlider, timeout);
            }
        }

        var controllerClick = function () {
        }
        makeSlider();
        //setInterval("makeSlider()", timeout);
    }

})(jQuery);
