/** *****************************************
 * @class BLOOMIES
 * @module BLOOMIES Base
 * @Pattern Singleton
 * @requires YAHOO, BLOOMIES
 * @static
 ***************************************** */

/**
 * BLOOMIES global namespace object. Only create if it doesn't already exist.
 * This governs the Singleton nature of this base object.
 * @static
 */
if (typeof BLOOMIES == "undefined" || !BLOOMIES) {
    var BLOOMIES = {};

    /** Adapted namespacing code from core YUI YAHOO Singleton. Provides for
     * namespacing with BLOOMIES as the root package.
     *
     * @method namespace
     * @param  {String*} arguments 1-n namespaces to create
     * @return {Object}  A reference to the last namespace object created
     * @static
     */
    BLOOMIES.namespace = function() {
        var a=arguments, o=null, i, j, d;
        for (i=0; i<a.length; i=i+1) {
            d=a[i].split(".");
            o=BLOOMIES;

            // BLOOMIES is implied, so it is ignored if it is included
            for (j=(d[0] == "BLOOMIES") ? 1 : 0; j<d.length; j=j+1) {
                o[d[j]]=o[d[j]] || {};
                o=o[d[j]];
            }
        }
        return o;
    };


    /**
     * Simple logger wrapper for YUI logger. Provide a log message along with
     * a verbosity level and optional source object demarkation. Verbosity-level
     * is checked against the config setting to see if it should be logged.
     *
     * TODO: Simple logger wrapper for YUI logger. Eventually this could be written
     * to pipe log data of a given verbosity level to a web service that could
     *  write the statement to a system log on the server.
     *
     * @method log
     * @param  {String}  msg  The message to log.
     * @param  {String}  verbosity  ["info", "warn", "error", time"]
     * @param  {String}  src  The source of the the message (opt)
     * @static
     */
    BLOOMIES.logcount = 0;
    BLOOMIES.log = function(msg,level,src){
        if (arguments.length==0) msg = "";
        msg = (BLOOMIES.logcount++).toString() + "> " + msg;
        var a = BLOOMIES.config.Base.logVerbosity;
        if (!level) level = "debug";
        for (var i=0; i<a.length; i=i+1) {
            if (level === a[i]){
                YAHOO.log(msg,level,src);
                break;
            }
        }
    }

    BLOOMIES.load = (function(){
        var self = {};
        self.load_queue = [];
        self.loaded = false;
        self.onload = function(method, scope, args) {
            args = typeof args=="object"?args:[args];
            if (this.loaded) method.apply(scope, args);
            else this.load_queue.push([method, scope, args]);
       }
       YAHOO.util.Event.onDOMReady(function(){
            var m, s, a;
            this.loaded = true;
            //alert(this.load_queue.length);
            for (var i=0; i < this.load_queue.length; i++){
                m = this.load_queue[i][0];
                s = this.load_queue[i][1];
                a = this.load_queue[i][2];
                m.apply(s, a);
            }}, self, true);
       return self;
    })();
    BLOOMIES.onload = function(){BLOOMIES.load.onload.apply(BLOOMIES.load, arguments)};
}



/* to help IE act a little more like FF */
Error.prototype.toString = function() { return this.message; };
