/*
 * jQuery.include - A dynamic javascript/css loader plugin for jQuery.
 *
 * Copyright (c) 2007 Dave Stewart <d...@yesterdave.com>, http://www.yesterdave.com
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * $Version: 2007.04.27 +r3
 */
/* hard time figuring out a way to ensure js/css is loaded before proceeding with the next file
 *   - use ajax to get js/css file then inject it in a new script/ style tag
 *
 * hard time trying to figure out why safari wouldn't work with either:
 *   - script.onload
 *   - script.text
 * finally found http://tobielangel.com/2007/2/23/to-eval-or-not-to-eval
 *   - append javscript text to script tag with document.createTextNode
 *
 * keep in mind that no two external files should have the same name, becase path is disregarded
 *
 * Options:
 *   - sync: determines whether or not the files are load synchronously (Default: true)
 *   - onload: the function to run after all the files are loaded (Default: null)
 *   - jsBase: the base directory where javascript files are found (Default: '')
 *   - cssBase: the base directory where css files are found (Default: '')
 *
 */
(function($){
   
    var list = {};
   
    var queue = [];
   
    var running = false;
   
    var add = function(file, o){
        o = $.extend({sync: true, onload: null, jsBase: '', cssBase: ''}, o || {});
        fillList();
        var files = file.split(',');
        var len = files.length;
        $.each(files, function(i, file){
            file = (file.indexOf('.css') > -1 ? o.cssBase : o.jsBase) + file;
            var cb = (i == len - 1) ? o.onload : null;
            if (!o.sync)
                insert(file, cb || function(){}, false);
            else
                queue.push({file: file, onload: cb});
        });
        if (!running)
            next();
    };

    var next = function(){
        if (queue.length > 0) {
            running = true;
            var item = queue.shift();
            var cb = function(){
                if (item.onload)
                    item.onload();
                next();
            };
            insert(item.file, cb, true);
        } else
            running = false;
    };

    var insert = function(file, cb, sync){
        if (!list[file]) {
            list[file] = true;
            if (file.indexOf('.css') > -1)
                createCSS(file, cb, sync);
            else
                createJS(file, cb, sync);
        } else
            cb();
    };

    var createJS = function(file, cb, sync){
        if (!sync) {
            $(document.createElement('script')).attr({type: 'text/javascript', src: file}).appendTo('head');
            cb();
        } else {
            $.get(file, null, function(jsText){
                var s = $(document.createElement('script')).attr('type', 'text/javascript');
                if ($.browser.safari)
                    s.append(document.createTextNode(jsText));
                else
                    s[0].text = jsText;
                s.appendTo('head');
                cb();
            });
        }
    }

    var createCSS = function(file, cb, sync){
        if (!sync) {
            $(document.createElement('link')).attr({type: 'text/css', rel: 'stylesheet', href: file}).appendTo('head');
            cb();
        } else {
            $.get(file, null, function(cssText){
                if ($.browser.msie) {
                    var s = document.createStyleSheet();
                    s.cssText = cssText;
                } else {
                    var s = $(document.createElement('style')).attr('type', 'text/css');
                    try {
                        s.append(document.createTextNode(cssText));
                    } catch (e) {
                        s[0].cssText = cssText;
                    }
                    s.appendTo('head');
                }
                cb();
            });
        }
    };

    var fillList = function(){
        var toAdd = [];
        $("script").each(function(){
            toAdd.push(this.src);
        });
        $("link").each(function(){
            toAdd.push(this.href);
        });
        $.each(toAdd, function(i, file){
            if (file && file.length > 0 && file != '//:')
                list[stripPath(file)] = true;
        });
    };

    var stripPath = function(href){
        return href.substring(href.lastIndexOf('/') + 1);
    };

    $.extend({
        include: add,
        externalResources: list
    });

    $(fillList);
})(jQuery);