/**
 * @fileoverview
 * This script sets up the flykit environment.
 * @author <a href="http://purl.org/net/aliman">Alistair Miles</a>
 * @version $Revision:538 $ on $Date: 2008-08-14 10:36:17 +0100 (Thu, 14 Aug 2008) $ by $Author: aliman $
 * TODO license terms
 */


if (typeof flykit == "undefined" || !flykit) {
    /**
     * @class
	 * The flykit global namespace. If flykit is already defined, the
     * existing flykit will not be overwritten so that defined
     * namespaces are preserved.
     */
	function flykit() {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist.
 * <pre>
 * flykit.namespace("property.package");
 * flykit.namespace("flykit.property.package");
 * </pre>
 * Either of the above would create flykit.property, then
 * flykit.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * flykit.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */
flykit.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=flykit;

        // flykit is implied, so it is ignored if it is included
        for (j=(d[0] == "flykit") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

flykit.log = function(message, channel, context) {
    if (typeof channel == "undefined") {
        channel = "info";
    }
    if (typeof context != "undefined") {
        message = context + " :: " +message;
    }
    YAHOO.log(message,channel);
};

flykit.info = function(message, context) {
    var channel = "info";
    flykit.log(message, channel, context);
};

flykit.debug = function(message, context) {
    var channel = "debug";
    flykit.log(message, channel, context);
};

flykit.err = function(message, context) {
    var channel = "error";
    flykit.log(message, channel, context);
};

flykit.chain = function( glue, chained ) {
    return function(o) {
    	flykit.debug("in chained function, calling glue");
        var p = glue(o);
        flykit.debug("in chained function, calling next in chain");
        chained(p);
    }
};

/**
 * @class
 * Encapsulates an unexpected exception.
 * @constructor
 * @param {String} methodName the name of the method or function in which the cause exception was caught
 * @param {Object} nested
 */
flykit.UnexpectedException = function( methodName, nested ) {
    this.name = "flykit.UnexpectedException";
    this.nested = nested;
    this.message = methodName+" :: unexpected error";
    for (var p in nested) {
        if (p != "message") {
            this.message += "\n      "+p+": "+nested[p];
        }
    }
    if (nested.message) {
        this.message += "\n..."+nested.message;
    }
    flykit.debug(this.message)
};


