﻿(function ($) {

    $.fn.slideshow = function (vars) {

        var obj = this;
        var timeout = (vars.timeout != undefined) ? vars.timeout : 4000;
        var direction = (vars.direction != undefined) ? vars.direction : "slide";

        var makeSlider = function () {
            var $active = $("#" + obj[0].id + " DIV.active");

            if ($active.length == 0) $active = $("#" + obj[0].id + " DIV: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: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');

            if (direction == "slide") {
                $next.css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 1000, function () { $active.removeClass('active last-active'); });
            }
            if (direction == "left") {
                var w = $next.css("width");
                $next.addClass('active').css({ left: w, width: '0px' }).animate({ left: '0px', width: w }, 400, function () { $active.removeClass('active last-active'); });
            }
            if (direction == "right") {
                var w = $next.css("width");
                var p = $next.position();
                var l = p.left - w;
                //alert(p.left);
                //$next.addClass('active').css({ width: '0px' }).animate({ width: w }, 400, function () { $active.removeClass('active last-active'); });
                $next.css({ left: -500 }).addClass('active').animate({ left: 0 }, 1000, function () { $active.removeClass('active last-active'); });
            }
            var $div = $("#" + obj[0].id + " DIV");
            if ($div.length > 1) {
                setTimeout(makeSlider, timeout);
            }
        }

        makeSlider();
        //setInterval("makeSlider()", timeout);
    }

})(jQuery);
