/**
 * Script loader plugin 1.2.1
 *
 * Copyright (c) 2009 Filatov Dmitry (alpha@zforms.ru)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function($) {

if ($.ScriptLoader)
{
    return;
}

var scripts = [];

function loadScript(url, callback, context) {

        var script = scripts[url] || (scripts[url] = {
                loaded    : false,
                callbacks : []
        });

        if(script.loaded) {
                return callback.apply(context);
        }

        script.callbacks.push({
                fn      : callback,
                context : context
        });

        if(script.callbacks.length == 1) {
                $.ajax({
                        type     : 'GET',
                        url      : url,
                        dataType : 'script',
                        cache    : true,
                        success  : function() {
                                script.loaded = true;
                                $.each(script.callbacks, function() {
                                        this.fn.apply(this.context);
                                });
                                script.callbacks.length = 0;
                        }
                });
        }

}


$.appendUriParams = function (uri, params)
{
    if (!params)
    {
        return uri;
    }

    if (uri.indexOf ("?") == -1)
    {
        uri += "?";
    }
    else
    if (uri[uri.length - 1] != "&")
    {
        uri += "&";
    }

    return uri + $.param (params);
};


$.stopEvent = function (e)
{
    e.stopPropagation ();
    e.preventDefault ();
};


$.log = function ()
{
    if (window.console && $.isFunction (window.console.log))
    {
        window.console.log.apply (window.console, arguments);
    }
    else
    {
        alert ($.makeArray (arguments).join (" "));
    }
};


$.ScriptLoader = function ()
{
    var scriptUris = [];
    var assetUri = $ (":hidden[name = 'x-server-assetUri']").val ();


    this.addScript = function (name)
    {
        scriptUris.push (assetUri + "/script/" + name + ".js");
        return this;
    };


    this.addModuleScript = function (module, name)
    {
        scriptUris.push (assetUri + "module/" + module + "/script/" + name + ".js");
        return this;
    };


    this.load = function (callback, context, options)
    {
        options = options || {};
        options.parallel = false;

        $.requireScript (scriptUris, callback, context, options);
    };
};


$.requireScript = function(url, callback, context, options) {

        if(typeof options === 'undefined' && context && context.hasOwnProperty('parallel')) {
                options = context;
                context = window;
        }

        options = $.extend({ parallel : true }, options);

        if(!$.isArray(url)) {
                return loadScript(url, callback, context);
        }

        var counter = 0;

        // parallel loading
        if(options.parallel) {
                return $.each(url, function() {
                        loadScript(this, function() {
                                if(++counter == url.length) {
                                        callback.apply(context);
                                }
                        });
                });
        }

        // sequential loading
        (function() {
                if(counter == url.length) {
                        return callback.apply(context);
                }
                loadScript(url[counter++], arguments.callee);
        })();

};

$.requireScript.registerLoaded = function(url) {
        $.each($.makeArray(url), function() {
                (scripts[url] || (scripts[url] = {})).loaded = true;
        });
};


$.fn.enter = function (callback)
{
    this.each (function ()
    {
        $ (this).keydown (function (e)
        {
            if (e.keyCode == 13)
            {
                $.stopEvent (e);
                callback.call (this, e);
            }
        });
    });

    return this;
};

})(jQuery);

jQuery (function ($)
{
    if (opener && opener.jQuery)
    {
        opener.jQuery (opener).trigger ("windowready", window);
    }
});

