﻿/**
* @license 
* jQuery llsl 0.91 - a low load slideshow plugin
* 
* License: GNU LGPL http://www.gnu.de/documents/lgpl-3.0.en.html
* 
* Author: Claus Ehrenberg, ce@aubergemediale.com
* Since: October 2010
* Date: Mon Oct 25 23:34:00 2010 +0000 
*/

(function ($) {

    // The plugin signature - for the sake of clarity, 
    // The plugin MUST ALWAYS be called with 3 parameters
    // The parameter should be null, if not needed.
    $.fn.llsl = function (method, mparams, settings) {

        // the methods
        // you should collect all of your plugin's methods in an object literal
        // and call them by passing the string name of the method to the plugin.
        var methods = {
            play: function (mparams, config) {

                var nextImageUrl = config.basepath + config.imageurls[config.currentindex];

                var currentfrontbuffer = $("." + config.frontbufferclass);
                var currentbackbuffer = $("." + config.backbufferclass);
                var currentfrontbufferimage = $("." + config.frontbufferimageclass);
                var currentbackbufferimage = $("." + config.backbufferimageclass);
                //console.log("play called");

                // if we are just starting, we dont want to fade immediately, so we skip one loop
                if (!config.isInitialized) {
                    config.isInitialized = true;
                    setTimeout(function () { methods.play(mparams, config) }, config.imageduration);
                    return;
                }

                // make front buffer transparent so that backbuffer appears
                currentfrontbuffer.fadeTo(3000, 0, function () {

                    // we must put this in a callback so that prog execution waits till fading is done.

                    // advance the backbuffers z index
                    currentbackbuffer.css("z-index", config.zfront);
                    // decrease frontbuffers z-index
                    currentfrontbuffer.css("z-index", config.zback);

                    // start load new image into frontbuffer in the background
                    currentfrontbufferimage.attr("src", nextImageUrl);
                    //alert(nextImageUrl + " " + index);
                    // and fade it back in in the background
                    currentfrontbuffer.fadeTo(0, 1, function () {

                        // now swap the names

                        currentfrontbuffer.addClass(config.backbufferclass);
                        currentfrontbuffer.removeClass(config.frontbufferclass);
                        currentfrontbufferimage.addClass(config.backbufferimageclass);
                        currentfrontbufferimage.removeClass(config.frontbufferimageclass);

                        currentbackbuffer.addClass(config.frontbufferclass);
                        currentbackbuffer.removeClass(config.backbufferclass);
                        currentbackbufferimage.addClass(config.frontbufferimageclass);
                        currentbackbufferimage.removeClass(config.backbufferimageclass);

                        config.currentindex += 1;
                        if (config.currentindex >= config.imageurls.length) {
                            config.currentindex = 0;
                        }
                        // animation complete.
                        // var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
                        setTimeout(function () { methods.play(mparams, config) }, config.imageduration); // 1: startindex, 3000);
                    });
                });
            },
            stop: function (mparams, config) {
                //console.log("stop called");
                $("div").clearQueue();
                $("." + config.frontbufferclass).stop();
                $("." + config.backbufferclass).stop();
                isInitialized = false;
            },
            m3: function (mparams, config) {
                alert("m3 called");
            },
            m4: function (mparams, config) {
                //console.log("m4 called with argument: " + mparams);
                //console.log("config: version: " + config.version);
            }
        };

        // object literal of the default settings
        var config = {
            "frontbufferclass": "frontbuffer",
            "frontbufferimageclass": "frontbufferimage",
            "backbufferclass": "backbuffer",
            "backbufferimageclass": "backbufferimage",
            "imageduration": 3000,
            "imageurls": ["", "", ""],
            "basepath": "/static/",
            "currentindex": 2,
            "zfront": 3,
            "zback": 2,
            "version": "0.8",
            "isInitialized": false
        }; // jslint: ; needed



        // if settings are passed in, merge them
        // with our default config
        if (settings) {
            $.extend(config, settings);
        } // jslint: ; not needed

        // reuse the settings parameter for config. it will be passed as second parameter to methods
        settings = config;

        // Method calling logic - the methods should do their work and return so that the code below gets executed
        // apply(thisArg, [argsArray])
        // Array.prototype.slice.call(arguments): http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript

        if (method) {
            if (methods[method]) {
                methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); // 1: startindex, args are not correctly passed if != 3
            } else if (typeof method === 'object' || !method) {
                methods.start.apply(this, Array.prototype.slice.call(arguments, 1));
            } else {
                $.error('Method ' + method + ' does not exist on jQuery.llsl');
            }
        } // jslint: ; not needed



        // 'this' is the jQuery object, return for chainability
        // think whether this plugin should work in the each loop or not
        return this.each(function () {
           // console.log("method " + method + " completed - now returning jQuery object(s)!");
        });
    };


    var animRunning = true;
    var animStopped = false;

    function on_animStopped() {
        //createModalWindow();
    }


})(jQuery);
