/**
 * Script loader plugin 1.0.0
 *
 * 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($) {

var scripts = [];

$.loadScript = function(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;
            }
        });
    }

};

})(jQuery);
