//Peter Scott  - peterscott.sa@gmail.com


(function ($)
{
    jQuery.fn.slide = function (options)
    {
        var defaults = {
            "Rotate speed": 6000,
            "Fade speed": 1000
        };

        var o = jQuery.extend(defaults, options);

        return this.each(function ()
        {
            var el = $(this); //Cache the selector for better performance
            var imgCount = $("#container img").length;  //Get the amount of images in the slideshow;
            var currentPosition = imgCount - 1;
            var imageNav = $(".imageNav ul");

            function setup()
            {
                el.children("div.background").each(function (index)
                {
                    $(this).addClass("slide_" + index)
							.css("z-index", index);
                }); 

                ///Setup controls
                for (var i = 0; i < imgCount; i++)
                {
                    ///Closure
                    var m = (function ()
                    {
                        var n = i;
                        return n;
                    })(i);

                    imageNav.append("<li><a class='numbering' rel='" + m + "'>&nbsp;</a></li>");
                };

                //Setup the controls to the default position
                $(".imageNav ul li a[rel='0']").css("background", "url(App_Themes/Default/images/dot_on.png) no-repeat");
            };
            setup();

            function manageControls(currentPosition)
            {
                $(".imageNav ul li a").css("background", "url(App_Themes/Default/images/dot.png) no-repeat");
                $(".imageNav ul li a[rel='" + currentPosition + "']").css("background", "url(App_Themes/Default/images/dot_on.png) no-repeat");
            };

            //Append navigation according to how many pictures there

            var timer = function ()
            {
                var t = setTimeout(function ()
                {
                    if (currentPosition < 0) //Ensure that currentPosition begins from the start
                    {
                        currentPosition = imgCount - 1;
                    };

                    var image = $(".slide_" + currentPosition + " img"); //Cache the selector for better performance
                    var currentSlide = $(".slide_" + currentPosition);
                    var otherSlides = $(".background").not(".slide_" + currentPosition);
                    image.stop();

                    image.fadeTo(o["Fade speed"], 0, function ()
                    {
                        otherSlides.each(function () ///After the fade is complete, iterate over each slide and increment its z index
                        {
                            var orZindex = $(this).css("z-index");
                            $(this).css("z-index", parseInt(orZindex) + 1)
                        });
                        currentSlide.css({  //Ensure the current slide goes to the bottom of the pile
                            "z-index": 0
                        });
                        image.css("opacity", 1); //Reset the opacity of the faded div
                    });
                    currentPosition--;
                    manageControls(currentPosition + 1);
                    clearTimeout(t);
                    timer();
                }, o["Rotate speed"]);
            };
            timer();
        });
    };
})(jQuery);
