/***

MochiKit.Base 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide("MochiKit.Base");
}
if (typeof(MochiKit) == 'undefined') {
    MochiKit = {};
}
if (typeof(MochiKit.Base) == 'undefined') {
    MochiKit.Base = {};
}
if (typeof(MochiKit.__export__) == "undefined") {
    MochiKit.__export__ = (MochiKit.__compat__  ||
        (typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
    );
}

MochiKit.Base.VERSION = "1.4";
MochiKit.Base.NAME = "MochiKit.Base";
/** @id MochiKit.Base.update */
MochiKit.Base.update = function (self, obj/*, ... */) {
    if (self === null) {
        self = {};
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        if (typeof(o) != 'undefined' && o !== null) {
            for (var k in o) {
                self[k] = o[k];
            }
        }
    }
    return self;
};

MochiKit.Base.update(MochiKit.Base, {
    __repr__: function () {
        return "[" + this.NAME + " " + this.VERSION + "]";
    },

    toString: function () {
        return this.__repr__();
    },

    /** @id MochiKit.Base.camelize */
    camelize: function (selector) {
        /* from dojo.style.toCamelCase */
        var arr = selector.split('-');
        var cc = arr[0];
        for (var i = 1; i < arr.length; i++) {
            cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
        }
        return cc;
    },

    /** @id MochiKit.Base.counter */
    counter: function (n/* = 1 */) {
        if (arguments.length === 0) {
            n = 1;
        }
        return function () {
            return n++;
        };
    },

    /** @id MochiKit.Base.clone */
    clone: function (obj) {
        var me = arguments.callee;
        if (arguments.length == 1) {
            me.prototype = obj;
            return new me();
        }
    },

    _flattenArray: function (res, lst) {
        for (var i = 0; i < lst.length; i++) {
            var o = lst[i];
            if (o instanceof Array) {
                arguments.callee(res, o);
            } else {
                res.push(o);
            }
        }
        return res;
    },

    /** @id MochiKit.Base.flattenArray */
    flattenArray: function (lst) {
        return MochiKit.Base._flattenArray([], lst);
    },

    /** @id MochiKit.Base.flattenArguments */
    flattenArguments: function (lst/* ...*/) {
        var res = [];
        var m = MochiKit.Base;
        var args = m.extend(null, arguments);
        while (args.length) {
            var o = args.shift();
            if (o && typeof(o) == "object" && typeof(o.length) == "number") {
                for (var i = o.length - 1; i >= 0; i--) {
                    args.unshift(o[i]);
                }
            } else {
                res.push(o);
            }
        }
        return res;
    },

    /** @id MochiKit.Base.extend */
    extend: function (self, obj, /* optional */skip) {
        // Extend an array with an array-like object starting
        // from the skip index
        if (!skip) {
            skip = 0;
        }
        if (obj) {
            // allow iterable fall-through, but skip the full isArrayLike
            // check for speed, this is called often.
            var l = obj.length;
            if (typeof(l) != 'number' /* !isArrayLike(obj) */) {
                if (typeof(MochiKit.Iter) != "undefined") {
                    obj = MochiKit.Iter.list(obj);
                    l = obj.length;
                } else {
                    throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
                }
            }
            if (!self) {
                self = [];
            }
            for (var i = skip; i < l; i++) {
                self.push(obj[i]);
            }
        }
        // This mutates, but it's convenient to return because
        // it's often used like a constructor when turning some
        // ghetto array-like to a real array
        return self;
    },


    /** @id MochiKit.Base.updatetree */
    updatetree: function (self, obj/*, ...*/) {
        if (self === null) {
            self = {};
        }
        for (var i = 1; i < arguments.length; i++) {
            var o = arguments[i];
            if (typeof(o) != 'undefined' && o !== null) {
                for (var k in o) {
                    var v = o[k];
                    if (typeof(self[k]) == 'object' && typeof(v) == 'object') {
                        arguments.callee(self[k], v);
                    } else {
                        self[k] = v;
                    }
                }
            }
        }
        return self;
    },

    /** @id MochiKit.Base.setdefault */
    setdefault: function (self, obj/*, ...*/) {
        if (self === null) {
            self = {};
        }
        for (var i = 1; i < arguments.length; i++) {
            var o = arguments[i];
            for (var k in o) {
                if (!(k in self)) {
                    self[k] = o[k];
                }
            }
        }
        return self;
    },

    /** @id MochiKit.Base.keys */
    keys: function (obj) {
        var rval = [];
        for (var prop in obj) {
            rval.push(prop);
        }
        return rval;
    },

    /** @id MochiKit.Base.values */
    values: function (obj) {
        var rval = [];
        for (var prop in obj) {
            rval.push(obj[prop]);
        }
        return rval;
    },

     /** @id MochiKit.Base.items */
    items: function (obj) {
        var rval = [];
        var e;
        for (var prop in obj) {
            var v;
            try {
                v = obj[prop];
            } catch (e) {
                continue;
            }
            rval.push([prop, v]);
        }
        return rval;
    },


    _newNamedError: function (module, name, func) {
        func.prototype = new MochiKit.Base.NamedError(module.NAME + "." + name);
        module[name] = func;
    },


    /** @id MochiKit.Base.operator */
    operator: {
        // unary logic operators
        /** @id MochiKit.Base.truth */
        truth: function (a) { return !!a; },
        /** @id MochiKit.Base.lognot */
        lognot: function (a) { return !a; },
        /** @id MochiKit.Base.identity */
        identity: function (a) { return a; },

        // bitwise unary operators
        /** @id MochiKit.Base.not */
        not: function (a) { return ~a; },
        /** @id MochiKit.Base.neg */
        neg: function (a) { return -a; },

        // binary operators
        /** @id MochiKit.Base.add */
        add: function (a, b) { return a + b; },
        /** @id MochiKit.Base.sub */
        sub: function (a, b) { return a - b; },
        /** @id MochiKit.Base.div */
        div: function (a, b) { return a / b; },
        /** @id MochiKit.Base.mod */
        mod: function (a, b) { return a % b; },
        /** @id MochiKit.Base.mul */
        mul: function (a, b) { return a * b; },

        // bitwise binary operators
        /** @id MochiKit.Base.and */
        and: function (a, b) { return a & b; },
        /** @id MochiKit.Base.or */
        or: function (a, b) { return a | b; },
        /** @id MochiKit.Base.xor */
        xor: function (a, b) { return a ^ b; },
        /** @id MochiKit.Base.lshift */
        lshift: function (a, b) { return a << b; },
        /** @id MochiKit.Base.rshift */
        rshift: function (a, b) { return a >> b; },
        /** @id MochiKit.Base.zrshift */
        zrshift: function (a, b) { return a >>> b; },

        // near-worthless built-in comparators
        /** @id MochiKit.Base.eq */
        eq: function (a, b) { return a == b; },
        /** @id MochiKit.Base.ne */
        ne: function (a, b) { return a != b; },
        /** @id MochiKit.Base.gt */
        gt: function (a, b) { return a > b; },
        /** @id MochiKit.Base.ge */
        ge: function (a, b) { return a >= b; },
        /** @id MochiKit.Base.lt */
        lt: function (a, b) { return a < b; },
        /** @id MochiKit.Base.le */
        le: function (a, b) { return a <= b; },

        // strict built-in comparators
        seq: function (a, b) { return a === b; },
        sne: function (a, b) { return a !== b; },

        // compare comparators
        /** @id MochiKit.Base.ceq */
        ceq: function (a, b) { return MochiKit.Base.compare(a, b) === 0; },
        /** @id MochiKit.Base.cne */
        cne: function (a, b) { return MochiKit.Base.compare(a, b) !== 0; },
        /** @id MochiKit.Base.cgt */
        cgt: function (a, b) { return MochiKit.Base.compare(a, b) == 1; },
        /** @id MochiKit.Base.cge */
        cge: function (a, b) { return MochiKit.Base.compare(a, b) != -1; },
        /** @id MochiKit.Base.clt */
        clt: function (a, b) { return MochiKit.Base.compare(a, b) == -1; },
        /** @id MochiKit.Base.cle */
        cle: function (a, b) { return MochiKit.Base.compare(a, b) != 1; },

        // binary logical operators
        /** @id MochiKit.Base.logand */
        logand: function (a, b) { return a && b; },
        /** @id MochiKit.Base.logor */
        logor: function (a, b) { return a || b; },
        /** @id MochiKit.Base.contains */
        contains: function (a, b) { return b in a; }
    },

    /** @id MochiKit.Base.forwardCall */
    forwardCall: function (func) {
        return function () {
            return this[func].apply(this, arguments);
        };
    },

    /** @id MochiKit.Base.itemgetter */
    itemgetter: function (func) {
        return function (arg) {
            return arg[func];
        };
    },

    /** @id MochiKit.Base.typeMatcher */
    typeMatcher: function (/* typ */) {
        var types = {};
        for (var i = 0; i < arguments.length; i++) {
            var typ = arguments[i];
            types[typ] = typ;
        }
        return function () {
            for (var i = 0; i < arguments.length; i++) {
                if (!(typeof(arguments[i]) in types)) {
                    return false;
                }
            }
            return true;
        };
    },

    /** @id MochiKit.Base.isNull */
    isNull: function (/* ... */) {
        for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] !== null) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.Base.isUndefinedOrNull */
    isUndefinedOrNull: function (/* ... */) {
        for (var i = 0; i < arguments.length; i++) {
            var o = arguments[i];
            if (!(typeof(o) == 'undefined' || o === null)) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.Base.isEmpty */
    isEmpty: function (obj) {
        return !MochiKit.Base.isNotEmpty.apply(this, arguments);
    },

    /** @id MochiKit.Base.isNotEmpty */
    isNotEmpty: function (obj) {
        for (var i = 0; i < arguments.length; i++) {
            var o = arguments[i];
            if (!(o && o.length)) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.Base.isArrayLike */
    isArrayLike: function () {
        for (var i = 0; i < arguments.length; i++) {
            var o = arguments[i];
            var typ = typeof(o);
            if (
                (typ != 'object' && !(typ == 'function' && typeof(o.item) == 'function')) ||
                o === null ||
                typeof(o.length) != 'number' ||
                o.nodeType === 3
            ) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.Base.isDateLike */
    isDateLike: function () {
        for (var i = 0; i < arguments.length; i++) {
            var o = arguments[i];
            if (typeof(o) != "object" || o === null
                    || typeof(o.getTime) != 'function') {
                return false;
            }
        }
        return true;
    },


    /** @id MochiKit.Base.xmap */
    xmap: function (fn/*, obj... */) {
        if (fn === null) {
            return MochiKit.Base.extend(null, arguments, 1);
        }
        var rval = [];
        for (var i = 1; i < arguments.length; i++) {
            rval.push(fn(arguments[i]));
        }
        return rval;
    },

    /** @id MochiKit.Base.map */
    map: function (fn, lst/*, lst... */) {
        var m = MochiKit.Base;
        var itr = MochiKit.Iter;
        var isArrayLike = m.isArrayLike;
        if (arguments.length <= 2) {
            // allow an iterable to be passed
            if (!isArrayLike(lst)) {
                if (itr) {
                    // fast path for map(null, iterable)
                    lst = itr.list(lst);
                    if (fn === null) {
                        return lst;
                    }
                } else {
                    throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
                }
            }
            // fast path for map(null, lst)
            if (fn === null) {
                return m.extend(null, lst);
            }
            // disabled fast path for map(fn, lst)
            /*
            if (false && typeof(Array.prototype.map) == 'function') {
                // Mozilla fast-path
                return Array.prototype.map.call(lst, fn);
            }
            */
            var rval = [];
            for (var i = 0; i < lst.length; i++) {
                rval.push(fn(lst[i]));
            }
            return rval;
        } else {
            // default for map(null, ...) is zip(...)
            if (fn === null) {
                fn = Array;
            }
            var length = null;
            for (i = 1; i < arguments.length; i++) {
                // allow iterables to be passed
                if (!isArrayLike(arguments[i])) {
                    if (itr) {
                        return itr.list(itr.imap.apply(null, arguments));
                    } else {
                        throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
                    }
                }
                // find the minimum length
                var l = arguments[i].length;
                if (length === null || length > l) {
                    length = l;
                }
            }
            rval = [];
            for (i = 0; i < length; i++) {
                var args = [];
                for (var j = 1; j < arguments.length; j++) {
                    args.push(arguments[j][i]);
                }
                rval.push(fn.apply(this, args));
            }
            return rval;
        }
    },

    /** @id MochiKit.Base.xfilter */
    xfilter: function (fn/*, obj... */) {
        var rval = [];
        if (fn === null) {
            fn = MochiKit.Base.operator.truth;
        }
        for (var i = 1; i < arguments.length; i++) {
            var o = arguments[i];
            if (fn(o)) {
                rval.push(o);
            }
        }
        return rval;
    },

    /** @id MochiKit.Base.filter */
    filter: function (fn, lst, self) {
        var rval = [];
        // allow an iterable to be passed
        var m = MochiKit.Base;
        if (!m.isArrayLike(lst)) {
            if (MochiKit.Iter) {
                lst = MochiKit.Iter.list(lst);
            } else {
                throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
            }
        }
        if (fn === null) {
            fn = m.operator.truth;
        }
        if (typeof(Array.prototype.filter) == 'function') {
            // Mozilla fast-path
            return Array.prototype.filter.call(lst, fn, self);
        } else if (typeof(self) == 'undefined' || self === null) {
            for (var i = 0; i < lst.length; i++) {
                var o = lst[i];
                if (fn(o)) {
                    rval.push(o);
                }
            }
        } else {
            for (i = 0; i < lst.length; i++) {
                o = lst[i];
                if (fn.call(self, o)) {
                    rval.push(o);
                }
            }
        }
        return rval;
    },


    _wrapDumbFunction: function (func) {
        return function () {
            // fast path!
            switch (arguments.length) {
                case 0: return func();
                case 1: return func(arguments[0]);
                case 2: return func(arguments[0], arguments[1]);
                case 3: return func(arguments[0], arguments[1], arguments[2]);
            }
            var args = [];
            for (var i = 0; i < arguments.length; i++) {
                args.push("arguments[" + i + "]");
            }
            return eval("(func(" + args.join(",") + "))");
        };
    },

    /** @id MochiKit.Base.methodcaller */
    methodcaller: function (func/*, args... */) {
        var args = MochiKit.Base.extend(null, arguments, 1);
        if (typeof(func) == "function") {
            return function (obj) {
                return func.apply(obj, args);
            };
        } else {
            return function (obj) {
                return obj[func].apply(obj, args);
            };
        }
    },

    /** @id MochiKit.Base.method */
    method: function (self, func) {
        var m = MochiKit.Base;
        return m.bind.apply(this, m.extend([func, self], arguments, 2));
    },

    /** @id MochiKit.Base.compose */
    compose: function (f1, f2/*, f3, ... fN */) {
        var fnlist = [];
        var m = MochiKit.Base;
        if (arguments.length === 0) {
            throw new TypeError("compose() requires at least one argument");
        }
        for (var i = 0; i < arguments.length; i++) {
            var fn = arguments[i];
            if (typeof(fn) != "function") {
                throw new TypeError(m.repr(fn) + " is not a function");
            }
            fnlist.push(fn);
        }
        return function () {
            var args = arguments;
            for (var i = fnlist.length - 1; i >= 0; i--) {
                args = [fnlist[i].apply(this, args)];
            }
            return args[0];
        };
    },

    /** @id MochiKit.Base.bind */
    bind: function (func, self/* args... */) {
        if (typeof(func) == "string") {
            func = self[func];
        }
        var im_func = func.im_func;
        var im_preargs = func.im_preargs;
        var im_self = func.im_self;
        var m = MochiKit.Base;
        if (typeof(func) == "function" && typeof(func.apply) == "undefined") {
            // this is for cases where JavaScript sucks ass and gives you a
            // really dumb built-in function like alert() that doesn't have
            // an apply
            func = m._wrapDumbFunction(func);
        }
        if (typeof(im_func) != 'function') {
            im_func = func;
        }
        if (typeof(self) != 'undefined') {
            im_self = self;
        }
        if (typeof(im_preargs) == 'undefined') {
            im_preargs = [];
        } else  {
            im_preargs = im_preargs.slice();
        }
        m.extend(im_preargs, arguments, 2);
        var newfunc = function () {
            var args = arguments;
            var me = arguments.callee;
            if (me.im_preargs.length > 0) {
                args = m.concat(me.im_preargs, args);
            }
            var self = me.im_self;
            if (!self) {
                self = this;
            }
            return me.im_func.apply(self, args);
        };
        newfunc.im_self = im_self;
        newfunc.im_func = im_func;
        newfunc.im_preargs = im_preargs;
        return newfunc;
    },

    /** @id MochiKit.Base.bindMethods */
    bindMethods: function (self) {
        var bind = MochiKit.Base.bind;
        for (var k in self) {
            var func = self[k];
            if (typeof(func) == 'function') {
                self[k] = bind(func, self);
            }
        }
    },

    /** @id MochiKit.Base.registerComparator */
    registerComparator: function (name, check, comparator, /* optional */ override) {
        MochiKit.Base.comparatorRegistry.register(name, check, comparator, override);
    },

    _primitives: {'boolean': true, 'string': true, 'number': true},

    /** @id MochiKit.Base.compare */
    compare: function (a, b) {
        if (a == b) {
            return 0;
        }
        var aIsNull = (typeof(a) == 'undefined' || a === null);
        var bIsNull = (typeof(b) == 'undefined' || b === null);
        if (aIsNull && bIsNull) {
            return 0;
        } else if (aIsNull) {
            return -1;
        } else if (bIsNull) {
            return 1;
        }
        var m = MochiKit.Base;
        // bool, number, string have meaningful comparisons
        var prim = m._primitives;
        if (!(typeof(a) in prim && typeof(b) in prim)) {
            try {
                return m.comparatorRegistry.match(a, b);
            } catch (e) {
                if (e != m.NotFound) {
                    throw e;
                }
            }
        }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        }
        // These types can't be compared
        var repr = m.repr;
        throw new TypeError(repr(a) + " and " + repr(b) + " can not be compared");
    },

    /** @id MochiKit.Base.compareDateLike */
    compareDateLike: function (a, b) {
        return MochiKit.Base.compare(a.getTime(), b.getTime());
    },

    /** @id MochiKit.Base.compareArrayLike */
    compareArrayLike: function (a, b) {
        var compare = MochiKit.Base.compare;
        var count = a.length;
        var rval = 0;
        if (count > b.length) {
            rval = 1;
            count = b.length;
        } else if (count < b.length) {
            rval = -1;
        }
        for (var i = 0; i < count; i++) {
            var cmp = compare(a[i], b[i]);
            if (cmp) {
                return cmp;
            }
        }
        return rval;
    },

    /** @id MochiKit.Base.registerRepr */
    registerRepr: function (name, check, wrap, /* optional */override) {
        MochiKit.Base.reprRegistry.register(name, check, wrap, override);
    },

    /** @id MochiKit.Base.repr */
    repr: function (o) {
        if (typeof(o) == "undefined") {
            return "undefined";
        } else if (o === null) {
            return "null";
        }
        try {
            if (typeof(o.__repr__) == 'function') {
                return o.__repr__();
            } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
                return o.repr();
            }
            return MochiKit.Base.reprRegistry.match(o);
        } catch (e) {
            if (typeof(o.NAME) == 'string' && (
                    o.toString == Function.prototype.toString ||
                    o.toString == Object.prototype.toString
                )) {
                return o.NAME;
            }
        }
        try {
            var ostring = (o + "");
        } catch (e) {
            return "[" + typeof(o) + "]";
        }
        if (typeof(o) == "function") {
            o = ostring.replace(/^\s+/, "");
            var idx = o.indexOf("{");
            if (idx != -1) {
                o = o.substr(0, idx) + "{...}";
            }
        }
        return ostring;
    },

    /** @id MochiKit.Base.reprArrayLike */
    reprArrayLike: function (o) {
        var m = MochiKit.Base;
        return "[" + m.map(m.repr, o).join(", ") + "]";
    },

    /** @id MochiKit.Base.reprString */
    reprString: function (o) {
        return ('"' + o.replace(/(["\\])/g, '\\$1') + '"'
            ).replace(/[\f]/g, "\\f"
            ).replace(/[\b]/g, "\\b"
            ).replace(/[\n]/g, "\\n"
            ).replace(/[\t]/g, "\\t"
            ).replace(/[\r]/g, "\\r");
    },

    /** @id MochiKit.Base.reprNumber */
    reprNumber: function (o) {
        return o + "";
    },

    /** @id MochiKit.Base.registerJSON */
    registerJSON: function (name, check, wrap, /* optional */override) {
        MochiKit.Base.jsonRegistry.register(name, check, wrap, override);
    },


    /** @id MochiKit.Base.evalJSON */
    evalJSON: function () {
        return eval("(" + arguments[0] + ")");
    },

    /** @id MochiKit.Base.serializeJSON */
    serializeJSON: function (o) {
        var objtype = typeof(o);
        if (objtype == "number" || objtype == "boolean") {
            return o + "";
        } else if (o === null) {
            return "null";
        }
        var m = MochiKit.Base;
        var reprString = m.reprString;
        if (objtype == "string") {
            return reprString(o);
        }
        // recurse
        var me = arguments.callee;
        // short-circuit for objects that support "json" serialization
        // if they return "self" then just pass-through...
        var newObj;
        if (typeof(o.__json__) == "function") {
            newObj = o.__json__();
            if (o !== newObj) {
                return me(newObj);
            }
        }
        if (typeof(o.json) == "function") {
            newObj = o.json();
            if (o !== newObj) {
                return me(newObj);
            }
        }
        // array
        if (objtype != "function" && typeof(o.length) == "number") {
            var res = [];
            for (var i = 0; i < o.length; i++) {
                var val = me(o[i]);
                if (typeof(val) != "string") {
                    val = "undefined";
                }
                res.push(val);
            }
            return "[" + res.join(", ") + "]";
        }
        // look in the registry
        try {
            newObj = m.jsonRegistry.match(o);
            if (o !== newObj) {
                return me(newObj);
            }
        } catch (e) {
            if (e != m.NotFound) {
                // something really bad happened
                throw e;
            }
        }
        // undefined is outside of the spec
        if (objtype == "undefined") {
            throw new TypeError("undefined can not be serialized as JSON");
        }
        // it's a function with no adapter, bad
        if (objtype == "function") {
            return null;
        }
        // generic object code path
        res = [];
        for (var k in o) {
            var useKey;
            if (typeof(k) == "number") {
                useKey = '"' + k + '"';
            } else if (typeof(k) == "string") {
                useKey = reprString(k);
            } else {
                // skip non-string or number keys
                continue;
            }
            val = me(o[k]);
            if (typeof(val) != "string") {
                // skip non-serializable values
                continue;
            }
            res.push(useKey + ":" + val);
        }
        return "{" + res.join(", ") + "}";
    },


    /** @id MochiKit.Base.objEqual */
    objEqual: function (a, b) {
        return (MochiKit.Base.compare(a, b) === 0);
    },

    /** @id MochiKit.Base.arrayEqual */
    arrayEqual: function (self, arr) {
        if (self.length != arr.length) {
            return false;
        }
        return (MochiKit.Base.compare(self, arr) === 0);
    },

    /** @id MochiKit.Base.concat */
    concat: function (/* lst... */) {
        var rval = [];
        var extend = MochiKit.Base.extend;
        for (var i = 0; i < arguments.length; i++) {
            extend(rval, arguments[i]);
        }
        return rval;
    },

    /** @id MochiKit.Base.keyComparator */
    keyComparator: function (key/* ... */) {
        // fast-path for single key comparisons
        var m = MochiKit.Base;
        var compare = m.compare;
        if (arguments.length == 1) {
            return function (a, b) {
                return compare(a[key], b[key]);
            };
        }
        var compareKeys = m.extend(null, arguments);
        return function (a, b) {
            var rval = 0;
            // keep comparing until something is inequal or we run out of
            // keys to compare
            for (var i = 0; (rval === 0) && (i < compareKeys.length); i++) {
                var key = compareKeys[i];
                rval = compare(a[key], b[key]);
            }
            return rval;
        };
    },

    /** @id MochiKit.Base.reverseKeyComparator */
    reverseKeyComparator: function (key) {
        var comparator = MochiKit.Base.keyComparator.apply(this, arguments);
        return function (a, b) {
            return comparator(b, a);
        };
    },

    /** @id MochiKit.Base.partial */
    partial: function (func) {
        var m = MochiKit.Base;
        return m.bind.apply(this, m.extend([func, undefined], arguments, 1));
    },

    /** @id MochiKit.Base.listMinMax */
    listMinMax: function (which, lst) {
        if (lst.length === 0) {
            return null;
        }
        var cur = lst[0];
        var compare = MochiKit.Base.compare;
        for (var i = 1; i < lst.length; i++) {
            var o = lst[i];
            if (compare(o, cur) == which) {
                cur = o;
            }
        }
        return cur;
    },

    /** @id MochiKit.Base.objMax */
    objMax: function (/* obj... */) {
        return MochiKit.Base.listMinMax(1, arguments);
    },

    /** @id MochiKit.Base.objMin */
    objMin: function (/* obj... */) {
        return MochiKit.Base.listMinMax(-1, arguments);
    },

    /** @id MochiKit.Base.findIdentical */
    findIdentical: function (lst, value, start/* = 0 */, /* optional */end) {
        if (typeof(end) == "undefined" || end === null) {
            end = lst.length;
        }
        if (typeof(start) == "undefined" || start === null) {
            start = 0;
        }
        for (var i = start; i < end; i++) {
            if (lst[i] === value) {
                return i;
            }
        }
        return -1;
    },

    /** @id MochiKit.Base.mean */
    mean: function(/* lst... */) {
        /* http://www.nist.gov/dads/HTML/mean.html */
        var sum = 0;

        var m = MochiKit.Base;
        var args = m.extend(null, arguments);
        var count = args.length;

        while (args.length) {
            var o = args.shift();
            if (o && typeof(o) == "object" && typeof(o.length) == "number") {
                count += o.length - 1;
                for (var i = o.length - 1; i >= 0; i--) {
                    sum += o[i];
                }
            } else {
                sum += o;
            }
        }

        if (count <= 0) {
            throw new TypeError('mean() requires at least one argument');
        }

        return sum/count;
    },

    /** @id MochiKit.Base.median */
    median: function(/* lst... */) {
        /* http://www.nist.gov/dads/HTML/median.html */
        var data = MochiKit.Base.flattenArguments(arguments);
        if (data.length === 0) {
            throw new TypeError('median() requires at least one argument');
        }
        data.sort(compare);
        if (data.length % 2 == 0) {
            var upper = data.length / 2;
            return (data[upper] + data[upper - 1]) / 2;
        } else {
            return data[(data.length - 1) / 2];
        }
    },

    /** @id MochiKit.Base.findValue */
    findValue: function (lst, value, start/* = 0 */, /* optional */end) {
        if (typeof(end) == "undefined" || end === null) {
            end = lst.length;
        }
        if (typeof(start) == "undefined" || start === null) {
            start = 0;
        }
        var cmp = MochiKit.Base.compare;
        for (var i = start; i < end; i++) {
            if (cmp(lst[i], value) === 0) {
                return i;
            }
        }
        return -1;
    },

    /** @id MochiKit.Base.nodeWalk */
    nodeWalk: function (node, visitor) {
        var nodes = [node];
        var extend = MochiKit.Base.extend;
        while (nodes.length) {
            var res = visitor(nodes.shift());
            if (res) {
                extend(nodes, res);
            }
        }
    },


    /** @id MochiKit.Base.nameFunctions */
    nameFunctions: function (namespace) {
        var base = namespace.NAME;
        if (typeof(base) == 'undefined') {
            base = '';
        } else {
            base = base + '.';
        }
        for (var name in namespace) {
            var o = namespace[name];
            if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
                try {
                    o.NAME = base + name;
                } catch (e) {
                    // pass
                }
            }
        }
    },


    /** @id MochiKit.Base.queryString */
    queryString: function (names, values) {
        // check to see if names is a string or a DOM element, and if
        // MochiKit.DOM is available.  If so, drop it like it's a form
        // Ugliest conditional in MochiKit?  Probably!
        if (typeof(MochiKit.DOM) != "undefined" && arguments.length == 1
            && (typeof(names) == "string" || (
                typeof(names.nodeType) != "undefined" && names.nodeType > 0
            ))
        ) {
            var kv = MochiKit.DOM.formContents(names);
            names = kv[0];
            values = kv[1];
        } else if (arguments.length == 1) {
            // Allow the return value of formContents to be passed directly
            if (typeof(names.length) == "number" && names.length == 2) {
                return arguments.callee(names[0], names[1]);
            }
            var o = names;
            names = [];
            values = [];
            for (var k in o) {
                var v = o[k];
                if (typeof(v) == "function") {
                    continue;
                } else if (typeof(v) != "string" &&
                        typeof(v.length) == "number") {
                    for (var i = 0; i < v.length; i++) {
                        names.push(k);
                        values.push(v[i]);
                    }
                } else {
                    names.push(k);
                    values.push(v);
                }
            }
        }
        var rval = [];
        var len = Math.min(names.length, values.length);
        var urlEncode = MochiKit.Base.urlEncode;
        for (var i = 0; i < len; i++) {
            v = values[i];
            if (typeof(v) != 'undefined' && v !== null) {
                rval.push(urlEncode(names[i]) + "=" + urlEncode(v));
            }
        }
        return rval.join("&");
    },


    /** @id MochiKit.Base.parseQueryString */
    parseQueryString: function (encodedString, useArrays) {
        // strip a leading '?' from the encoded string
        var qstr = (encodedString.charAt(0) == "?")
            ? encodedString.substring(1)
            : encodedString;
        var pairs = qstr.replace(/\+/g, "%20").split(/(\&amp\;|\&\#38\;|\&#x26;|\&)/);
        var o = {};
        var decode;
        if (typeof(decodeURIComponent) != "undefined") {
            decode = decodeURIComponent;
        } else {
            decode = unescape;
        }
        if (useArrays) {
            for (var i = 0; i < pairs.length; i++) {
                var pair = pairs[i].split("=");
                if (pair.length !== 2) {
                    continue;
                }
                var name = decode(pair[0]);
                var arr = o[name];
                if (!(arr instanceof Array)) {
                    arr = [];
                    o[name] = arr;
                }
                arr.push(decode(pair[1]));
            }
        } else {
            for (i = 0; i < pairs.length; i++) {
                pair = pairs[i].split("=");
                if (pair.length !== 2) {
                    continue;
                }
                o[decode(pair[0])] = decode(pair[1]);
            }
        }
        return o;
    }
});

/** @id MochiKit.Base.AdapterRegistry */
MochiKit.Base.AdapterRegistry = function () {
    this.pairs = [];
};

MochiKit.Base.AdapterRegistry.prototype = {
    /** @id MochiKit.Base.AdapterRegistry.prototype.register */
    register: function (name, check, wrap, /* optional */ override) {
        if (override) {
            this.pairs.unshift([name, check, wrap]);
        } else {
            this.pairs.push([name, check, wrap]);
        }
    },

    /** @id MochiKit.Base.AdapterRegistry.prototype.match */
    match: function (/* ... */) {
        for (var i = 0; i < this.pairs.length; i++) {
            var pair = this.pairs[i];
            if (pair[1].apply(this, arguments)) {
                return pair[2].apply(this, arguments);
            }
        }
        throw MochiKit.Base.NotFound;
    },

    /** @id MochiKit.Base.AdapterRegistry.prototype.unregister */
    unregister: function (name) {
        for (var i = 0; i < this.pairs.length; i++) {
            var pair = this.pairs[i];
            if (pair[0] == name) {
                this.pairs.splice(i, 1);
                return true;
            }
        }
        return false;
    }
};


MochiKit.Base.EXPORT = [
    "flattenArray",
    "noop",
    "camelize",
    "counter",
    "clone",
    "extend",
    "update",
    "updatetree",
    "setdefault",
    "keys",
    "values",
    "items",
    "NamedError",
    "operator",
    "forwardCall",
    "itemgetter",
    "typeMatcher",
    "isCallable",
    "isUndefined",
    "isUndefinedOrNull",
    "isNull",
    "isEmpty",
    "isNotEmpty",
    "isArrayLike",
    "isDateLike",
    "xmap",
    "map",
    "xfilter",
    "filter",
    "methodcaller",
    "compose",
    "bind",
    "bindMethods",
    "NotFound",
    "AdapterRegistry",
    "registerComparator",
    "compare",
    "registerRepr",
    "repr",
    "objEqual",
    "arrayEqual",
    "concat",
    "keyComparator",
    "reverseKeyComparator",
    "partial",
    "merge",
    "listMinMax",
    "listMax",
    "listMin",
    "objMax",
    "objMin",
    "nodeWalk",
    "zip",
    "urlEncode",
    "queryString",
    "serializeJSON",
    "registerJSON",
    "evalJSON",
    "parseQueryString",
    "findValue",
    "findIdentical",
    "flattenArguments",
    "method",
    "average",
    "mean",
    "median"
];

MochiKit.Base.EXPORT_OK = [
    "nameFunctions",
    "comparatorRegistry",
    "reprRegistry",
    "jsonRegistry",
    "compareDateLike",
    "compareArrayLike",
    "reprArrayLike",
    "reprString",
    "reprNumber"
];

MochiKit.Base._exportSymbols = function (globals, module) {
    if (!MochiKit.__export__) {
        return;
    }
    var all = module.EXPORT_TAGS[":all"];
    for (var i = 0; i < all.length; i++) {
        globals[all[i]] = module[all[i]];
    }
};

MochiKit.Base.__new__ = function () {
    // A singleton raised when no suitable adapter is found
    var m = this;

    // convenience
    /** @id MochiKit.Base.noop */
    m.noop = m.operator.identity;

    // Backwards compat
    m.forward = m.forwardCall;
    m.find = m.findValue;

    if (typeof(encodeURIComponent) != "undefined") {
        /** @id MochiKit.Base.urlEncode */
        m.urlEncode = function (unencoded) {
            return encodeURIComponent(unencoded).replace(/\'/g, '%27');
        };
    } else {
        m.urlEncode = function (unencoded) {
            return escape(unencoded
                ).replace(/\+/g, '%2B'
                ).replace(/\"/g,'%22'
                ).rval.replace(/\'/g, '%27');
        };
    }

    /** @id MochiKit.Base.NamedError */
    m.NamedError = function (name) {
        this.message = name;
        this.name = name;
    };
    m.NamedError.prototype = new Error();
    m.update(m.NamedError.prototype, {
        repr: function () {
            if (this.message && this.message != this.name) {
                return this.name + "(" + m.repr(this.message) + ")";
            } else {
                return this.name + "()";
            }
        },
        toString: m.forwardCall("repr")
    });

    /** @id MochiKit.Base.NotFound */
    m.NotFound = new m.NamedError("MochiKit.Base.NotFound");


    /** @id MochiKit.Base.listMax */
    m.listMax = m.partial(m.listMinMax, 1);
    /** @id MochiKit.Base.listMin */
    m.listMin = m.partial(m.listMinMax, -1);

    /** @id MochiKit.Base.isCallable */
    m.isCallable = m.typeMatcher('function');
    /** @id MochiKit.Base.isUndefined */
    m.isUndefined = m.typeMatcher('undefined');

    /** @id MochiKit.Base.merge */
    m.merge = m.partial(m.update, null);
    /** @id MochiKit.Base.zip */
    m.zip = m.partial(m.map, null);

    /** @id MochiKit.Base.average */
    m.average = m.mean;

    /** @id MochiKit.Base.comparatorRegistry */
    m.comparatorRegistry = new m.AdapterRegistry();
    m.registerComparator("dateLike", m.isDateLike, m.compareDateLike);
    m.registerComparator("arrayLike", m.isArrayLike, m.compareArrayLike);

    /** @id MochiKit.Base.reprRegistry */
    m.reprRegistry = new m.AdapterRegistry();
    m.registerRepr("arrayLike", m.isArrayLike, m.reprArrayLike);
    m.registerRepr("string", m.typeMatcher("string"), m.reprString);
    m.registerRepr("numbers", m.typeMatcher("number", "boolean"), m.reprNumber);

    /** @id MochiKit.Base.jsonRegistry */
    m.jsonRegistry = new m.AdapterRegistry();

    var all = m.concat(m.EXPORT, m.EXPORT_OK);
    m.EXPORT_TAGS = {
        ":common": m.concat(m.EXPORT_OK),
        ":all": all
    };

    m.nameFunctions(this);

};

MochiKit.Base.__new__();

//
// XXX: Internet Explorer blows
//
if (MochiKit.__export__) {
    compare = MochiKit.Base.compare;
    compose = MochiKit.Base.compose;
    serializeJSON = MochiKit.Base.serializeJSON;
}

MochiKit.Base._exportSymbols(this, MochiKit.Base);

/***

MochiKit.DOM 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide("MochiKit.DOM");
    dojo.require("MochiKit.Base");
}
if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.DOM depends on MochiKit.Base!";
}

if (typeof(MochiKit.DOM) == 'undefined') {
    MochiKit.DOM = {};
}

MochiKit.DOM.NAME = "MochiKit.DOM";
MochiKit.DOM.VERSION = "1.4";
MochiKit.DOM.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};
MochiKit.DOM.toString = function () {
    return this.__repr__();
};

MochiKit.DOM.EXPORT = [
    "removeEmptyTextNodes",
    "formContents",
    "currentWindow",
    "currentDocument",
    "withWindow",
    "withDocument",
    "registerDOMConverter",
    "coerceToDOM",
    "createDOM",
    "createDOMFunc",
    "isChildNode",
    "getNodeAttribute",
    "removeNodeAttribute",
    "setNodeAttribute",
    "updateNodeAttributes",
    "appendChildNodes",
    "insertSiblingNodesAfter",
    "insertSiblingNodesBefore",
    "replaceChildNodes",
    "removeElement",
    "swapDOM",
    "BUTTON",
    "TT",
    "PRE",
    "H1",
    "H2",
    "H3",
    "BR",
    "CANVAS",
    "HR",
    "LABEL",
    "TEXTAREA",
    "FORM",
    "STRONG",
    "SELECT",
    "OPTION",
    "OPTGROUP",
    "LEGEND",
    "FIELDSET",
    "P",
    "UL",
    "OL",
    "LI",
    "TD",
    "TR",
    "THEAD",
    "TBODY",
    "TFOOT",
    "TABLE",
    "TH",
    "INPUT",
    "SPAN",
    "A",
    "DIV",
    "IMG",
    "getElement",
    "$",
    "getElementsByTagAndClassName",
    "addToCallStack",
    "addLoadEvent",
    "focusOnLoad",
    "setElementClass",
    "toggleElementClass",
    "addElementClass",
    "removeElementClass",
    "swapElementClass",
    "hasElementClass",
    "escapeHTML",
    "toHTML",
    "emitHTML",
    "scrapeText",
    "isParent",
    "getFirstParentByTagAndClassName",
    "makeClipping",
    "undoClipping",
    "makePositioned",
    "undoPositioned",
    "getFirstElementByTagAndClassName"
];

MochiKit.DOM.EXPORT_OK = [
    "domConverters"
];

MochiKit.DOM.DEPRECATED = [
    ['computedStyle', 'MochiKit.Style.getStyle', '1.4'],
    /** @id MochiKit.DOM.elementDimensions  */
    ['elementDimensions', 'MochiKit.Style.getElementDimensions', '1.4'],
    /** @id MochiKit.DOM.elementPosition  */
    ['elementPosition', 'MochiKit.Style.getElementPosition', '1.4'],
    ['hideElement', 'MochiKit.Style.hideElement', '1.4'],
    /** @id MochiKit.DOM.setElementDimensions */
    ['setElementDimensions', 'MochiKit.Style.setElementDimensions', '1.4'],
    /** @id MochiKit.DOM.setElementPosition */
    ['setElementPosition', 'MochiKit.Style.setElementPosition', '1.4'],
    ['setDisplayForElement', 'MochiKit.Style.setDisplayForElement', '1.4'],
    /** @id MochiKit.DOM.setOpacity */
    ['setOpacity', 'MochiKit.Style.setOpacity', '1.4'],
    ['showElement', 'MochiKit.Style.showElement', '1.4'],
    /** @id MochiKit.DOM.Coordinates */
    ['Coordinates', 'MochiKit.Style.Coordinates', '1.4'], // FIXME: broken
    /** @id MochiKit.DOM.Dimensions */
    ['Dimensions', 'MochiKit.Style.Dimensions', '1.4'] // FIXME: broken
];

/** @id MochiKit.DOM.getViewportDimensions */
MochiKit.DOM.getViewportDimensions = new Function('' +
    'if (!MochiKit["Style"]) {' +
    '    throw new Error("This function has been deprecated and depends on MochiKit.Style.");' +
    '}' +
    'return MochiKit.Style.getViewportDimensions.apply(this, arguments);');

MochiKit.Base.update(MochiKit.DOM, {

    /** @id MochiKit.DOM.currentWindow */
    currentWindow: function () {
        return MochiKit.DOM._window;
    },

    /** @id MochiKit.DOM.currentDocument */
    currentDocument: function () {
        return MochiKit.DOM._document;
    },

    /** @id MochiKit.DOM.withWindow */
    withWindow: function (win, func) {
        var self = MochiKit.DOM;
        var oldDoc = self._document;
        var oldWin = self._win;
        var rval;
        try {
            self._window = win;
            self._document = win.document;
            rval = func();
        } catch (e) {
            self._window = oldWin;
            self._document = oldDoc;
            throw e;
        }
        self._window = oldWin;
        self._document = oldDoc;
        return rval;
    },

    /** @id MochiKit.DOM.formContents  */
    formContents: function (elem/* = document.body */) {
        var names = [];
        var values = [];
        var m = MochiKit.Base;
        var self = MochiKit.DOM;
        if (typeof(elem) == "undefined" || elem === null) {
            elem = self._document.body;
        } else {
            elem = self.getElement(elem);
        }
        m.nodeWalk(elem, function (elem) {
            var name = elem.name;
            if (m.isNotEmpty(name)) {
                var tagName = elem.tagName.toUpperCase();
                if (tagName === "INPUT"
                    && (elem.type == "radio" || elem.type == "checkbox")
                    && !elem.checked
                ) {
                    return null;
                }
                if (tagName === "SELECT") {
                    if (elem.type == "select-one") {
                        if (elem.selectedIndex >= 0) {
                            var opt = elem.options[elem.selectedIndex];
                            var v = opt.value;
                            if (!v) {
                                var h = opt.outerHTML;
                                // internet explorer sure does suck.
                                if (h && !h.match(/^[^>]+\svalue\s*=/i)) {
                                    v = opt.text;
                                }
                            }
                            names.push(name);
                            values.push(v);
                            return null;
                        }
                        // no form elements?
                        names.push(name);
                        values.push("");
                        return null;
                    } else {
                        var opts = elem.options;
                        if (!opts.length) {
                            names.push(name);
                            values.push("");
                            return null;
                        }
                        for (var i = 0; i < opts.length; i++) {
                            var opt = opts[i];
                            if (!opt.selected) {
                                continue;
                            }
                            var v = opt.value;
                            if (!v) {
                                var h = opt.outerHTML;
                                // internet explorer sure does suck.
                                if (h && !h.match(/^[^>]+\svalue\s*=/i)) {
                                    v = opt.text;
                                }
                            }
                            names.push(name);
                            values.push(v);
                        }
                        return null;
                    }
                }
                if (tagName === "FORM" || tagName === "P" || tagName === "SPAN"
                    || tagName === "DIV"
                ) {
                    return elem.childNodes;
                }
                names.push(name);
                values.push(elem.value || '');
                return null;
            }
            return elem.childNodes;
        });
        return [names, values];
    },

    /** @id MochiKit.DOM.withDocument */
    withDocument: function (doc, func) {
        var self = MochiKit.DOM;
        var oldDoc = self._document;
        var rval;
        try {
            self._document = doc;
            rval = func();
        } catch (e) {
            self._document = oldDoc;
            throw e;
        }
        self._document = oldDoc;
        return rval;
    },

    /** @id MochiKit.DOM.registerDOMConverter */
    registerDOMConverter: function (name, check, wrap, /* optional */override) {
        MochiKit.DOM.domConverters.register(name, check, wrap, override);
    },

    /** @id MochiKit.DOM.coerceToDOM */
    coerceToDOM: function (node, ctx) {
        var m = MochiKit.Base;
        var im = MochiKit.Iter;
        var self = MochiKit.DOM;
        if (im) {
            var iter = im.iter;
            var repeat = im.repeat;
            var map = m.map;
        }
        var domConverters = self.domConverters;
        var coerceToDOM = arguments.callee;
        var NotFound = m.NotFound;
        while (true) {
            if (typeof(node) == 'undefined' || node === null) {
                return null;
            }
            if (typeof(node.nodeType) != 'undefined' && node.nodeType > 0) {
                return node;
            }
            if (typeof(node) == 'number' || typeof(node) == 'boolean') {
                node = node.toString();
                // FALL THROUGH
            }
            if (typeof(node) == 'string') {
                return self._document.createTextNode(node);
            }
            if (typeof(node.__dom__) == 'function') {
                node = node.__dom__(ctx);
                continue;
            }
            if (typeof(node.dom) == 'function') {
                node = node.dom(ctx);
                continue;
            }
            if (typeof(node) == 'function') {
                node = node.apply(ctx, [ctx]);
                continue;
            }

            if (im) {
                // iterable
                var iterNodes = null;
                try {
                    iterNodes = iter(node);
                } catch (e) {
                    // pass
                }
                if (iterNodes) {
                    return map(coerceToDOM, iterNodes, repeat(ctx));
                }
            }

            // adapter
            try {
                node = domConverters.match(node, ctx);
                continue;
            } catch (e) {
                if (e != NotFound) {
                    throw e;
                }
            }

            // fallback
            return self._document.createTextNode(node.toString());
        }
        // mozilla warnings aren't too bright
        return undefined;
    },

    /** @id MochiKit.DOM.isChildNode */
    isChildNode: function (node, maybeparent) {
        var self = MochiKit.DOM;
        if (typeof(node) == "string") {
            node = self.getElement(node);
        }
        if (typeof(maybeparent) == "string") {
            maybeparent = self.getElement(maybeparent);
        }
        if (node === maybeparent) {
            return true;
        }
        while (node && node.tagName.toUpperCase() != "BODY") {
            node = node.parentNode;
            if (node === maybeparent) {
                return true;
            }
        }
        return false;
    },

    /** @id MochiKit.DOM.setNodeAttribute */
    setNodeAttribute: function (node, attr, value) {
        var o = {};
        o[attr] = value;
        try {
            return MochiKit.DOM.updateNodeAttributes(node, o);
        } catch (e) {
            // pass
        }
        return null;
    },

    /** @id MochiKit.DOM.getNodeAttribute */
    getNodeAttribute: function (node, attr) {
        var self = MochiKit.DOM;
        var rename = self.attributeArray.renames[attr];
        node = self.getElement(node);
        try {
            if (rename) {
                return node[rename];
            }
            return node.getAttribute(attr);
        } catch (e) {
            // pass
        }
        return null;
    },

    /** @id MochiKit.DOM.removeNodeAttribute */
    removeNodeAttribute: function (node, attr) {
        var self = MochiKit.DOM;
        var rename = self.attributeArray.renames[attr];
        node = self.getElement(node);
        try {
            if (rename) {
                return node[rename];
            }
            return node.removeAttribute(attr);
        } catch (e) {
            // pass
        }
        return null;
    },

    /** @id MochiKit.DOM.updateNodeAttributes */
    updateNodeAttributes: function (node, attrs) {
        var elem = node;
        var self = MochiKit.DOM;
        if (typeof(node) == 'string') {
            elem = self.getElement(node);
        }
        if (attrs) {
            var updatetree = MochiKit.Base.updatetree;
            if (self.attributeArray.compliant) {
                // not IE, good.
                for (var k in attrs) {
                    var v = attrs[k];
                    if (typeof(v) == 'object' && typeof(elem[k]) == 'object') {
                        if (k == "style" && MochiKit.Style) {
                            MochiKit.Style.setStyle(elem, v);
                        } else {
                            updatetree(elem[k], v);
                        }
                    } else if (k.substring(0, 2) == "on") {
                        if (typeof(v) == "string") {
                            v = new Function(v);
                        }
                        elem[k] = v;
                    } else {
                        elem.setAttribute(k, v);
                    }
                }
            } else {
                // IE is insane in the membrane
                var renames = self.attributeArray.renames;
                for (k in attrs) {
                    v = attrs[k];
                    var renamed = renames[k];
                    if (k == "style" && typeof(v) == "string") {
                        elem.style.cssText = v;
                    } else if (typeof(renamed) == "string") {
                        elem[renamed] = v;
                    } else if (typeof(elem[k]) == 'object'
                            && typeof(v) == 'object') {
                        if (k == "style" && MochiKit.Style) {
                            MochiKit.Style.setStyle(elem, v);
                        } else {
                            updatetree(elem[k], v);
                        }
                    } else if (k.substring(0, 2) == "on") {
                        if (typeof(v) == "string") {
                            v = new Function(v);
                        }
                        elem[k] = v;
                    } else {
                        elem.setAttribute(k, v);
                    }
                }
            }
        }
        return elem;
    },

    /** @id MochiKit.DOM.appendChildNodes */
    appendChildNodes: function (node/*, nodes...*/) {
        var elem = node;
        var self = MochiKit.DOM;
        if (typeof(node) == 'string') {
            elem = self.getElement(node);
        }
        var nodeStack = [
            self.coerceToDOM(
                MochiKit.Base.extend(null, arguments, 1),
                elem
            )
        ];
        var concat = MochiKit.Base.concat;
        while (nodeStack.length) {
            var n = nodeStack.shift();
            if (typeof(n) == 'undefined' || n === null) {
                // pass
            } else if (typeof(n.nodeType) == 'number') {
                elem.appendChild(n);
            } else {
                nodeStack = concat(n, nodeStack);
            }
        }
        return elem;
    },


    /** @id MochiKit.DOM.insertSiblingNodesBefore */
    insertSiblingNodesBefore: function (node/*, nodes...*/) {
        var elem = node;
        var self = MochiKit.DOM;
        if (typeof(node) == 'string') {
            elem = self.getElement(node);
        }
        var nodeStack = [
            self.coerceToDOM(
                MochiKit.Base.extend(null, arguments, 1),
                elem
            )
        ];
        var parentnode = elem.parentNode;
        var concat = MochiKit.Base.concat;
        while (nodeStack.length) {
            var n = nodeStack.shift();
            if (typeof(n) == 'undefined' || n === null) {
                // pass
            } else if (typeof(n.nodeType) == 'number') {
                parentnode.insertBefore(n, elem);
            } else {
                nodeStack = concat(n, nodeStack);
            }
        }
        return parentnode;
    },

    /** @id MochiKit.DOM.insertSiblingNodesAfter */
    insertSiblingNodesAfter: function (node/*, nodes...*/) {
        var elem = node;
        var self = MochiKit.DOM;

        if (typeof(node) == 'string') {
            elem = self.getElement(node);
        }
        var nodeStack = [
            self.coerceToDOM(
                MochiKit.Base.extend(null, arguments, 1),
                elem
            )
        ];

        if (elem.nextSibling) {
            return self.insertSiblingNodesBefore(elem.nextSibling, nodeStack);
        }
        else {
            return self.appendChildNodes(elem.parentNode, nodeStack);
        }
    },

    /** @id MochiKit.DOM.replaceChildNodes */
    replaceChildNodes: function (node/*, nodes...*/) {
        var elem = node;
        var self = MochiKit.DOM;
        if (typeof(node) == 'string') {
            elem = self.getElement(node);
            arguments[0] = elem;
        }
        var child;
        while ((child = elem.firstChild)) {
            elem.removeChild(child);
        }
        if (arguments.length < 2) {
            return elem;
        } else {
            return self.appendChildNodes.apply(this, arguments);
        }
    },

    /** @id MochiKit.DOM.createDOM */
    createDOM: function (name, attrs/*, nodes... */) {
        var elem;
        var self = MochiKit.DOM;
        var m = MochiKit.Base;
        if (typeof(attrs) == "string" || typeof(attrs) == "number") {
            var args = m.extend([name, null], arguments, 1);
            return arguments.callee.apply(this, args);
        }
        if (typeof(name) == 'string') {
            // Internet Explorer is dumb
            var xhtml = self._xhtml;
            if (attrs && !self.attributeArray.compliant) {
                // http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/name_2.asp
                var contents = "";
                if ('name' in attrs) {
                    contents += ' name="' + self.escapeHTML(attrs.name) + '"';
                }
                if (name == 'input' && 'type' in attrs) {
                    contents += ' type="' + self.escapeHTML(attrs.type) + '"';
                }
                if (contents) {
                    name = "<" + name + contents + ">";
                    xhtml = false;
                }
            }
            var d = self._document;
            if (xhtml && d === document) {
                elem = d.createElementNS("http://www.w3.org/1999/xhtml", name);
            } else {
                elem = d.createElement(name);
            }
        } else {
            elem = name;
        }
        if (attrs) {
            self.updateNodeAttributes(elem, attrs);
        }
        if (arguments.length <= 2) {
            return elem;
        } else {
            var args = m.extend([elem], arguments, 2);
            return self.appendChildNodes.apply(this, args);
        }
    },

    /** @id MochiKit.DOM.createDOMFunc */
    createDOMFunc: function (/* tag, attrs, *nodes */) {
        var m = MochiKit.Base;
        return m.partial.apply(
            this,
            m.extend([MochiKit.DOM.createDOM], arguments)
        );
    },

    /** @id MochiKit.DOM.removeElement */
    removeElement: function (elem) {
        var e = MochiKit.DOM.getElement(elem);
        e.parentNode.removeChild(e);
        return e;
    },

    /** @id MochiKit.DOM.swapDOM */
    swapDOM: function (dest, src) {
        var self = MochiKit.DOM;
        dest = self.getElement(dest);
        var parent = dest.parentNode;
        if (src) {
            src = self.getElement(src);
            parent.replaceChild(src, dest);
        } else {
            parent.removeChild(dest);
        }
        return src;
    },

    /** @id MochiKit.DOM.getElement */
    getElement: function (id) {
        var self = MochiKit.DOM;
        if (arguments.length == 1) {
            return ((typeof(id) == "string") ?
                self._document.getElementById(id) : id);
        } else {
            return MochiKit.Base.map(self.getElement, arguments);
        }
    },

    /** @id MochiKit.DOM.getElementsByTagAndClassName */
    getElementsByTagAndClassName: function (tagName, className,
            /* optional */parent) {
        var self = MochiKit.DOM;
        if (typeof(tagName) == 'undefined' || tagName === null) {
            tagName = '*';
        }
        if (typeof(parent) == 'undefined' || parent === null) {
            parent = self._document;
        }
        parent = self.getElement(parent);
        var children = (parent.getElementsByTagName(tagName)
            || self._document.all);
        if (typeof(className) == 'undefined' || className === null) {
            return MochiKit.Base.extend(null, children);
        }

        var elements = [];
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            var cls = child.className;
            if (!cls) {
                continue;
            }
            var classNames = cls.split(' ');
            for (var j = 0; j < classNames.length; j++) {
                if (classNames[j] == className) {
                    elements.push(child);
                    break;
                }
            }
        }

        return elements;
    },

    _newCallStack: function (path, once) {
        var rval = function () {
            var callStack = arguments.callee.callStack;
            for (var i = 0; i < callStack.length; i++) {
                if (callStack[i].apply(this, arguments) === false) {
                    break;
                }
            }
            if (once) {
                try {
                    this[path] = null;
                } catch (e) {
                    // pass
                }
            }
        };
        rval.callStack = [];
        return rval;
    },

    /** @id MochiKit.DOM.addToCallStack */
    addToCallStack: function (target, path, func, once) {
        var self = MochiKit.DOM;
        var existing = target[path];
        var regfunc = existing;
        if (!(typeof(existing) == 'function'
                && typeof(existing.callStack) == "object"
                && existing.callStack !== null)) {
            regfunc = self._newCallStack(path, once);
            if (typeof(existing) == 'function') {
                regfunc.callStack.push(existing);
            }
            target[path] = regfunc;
        }
        regfunc.callStack.push(func);
    },

    /** @id MochiKit.DOM.addLoadEvent */
    addLoadEvent: function (func) {
        var self = MochiKit.DOM;
        self.addToCallStack(self._window, "onload", func, true);

    },

    /** @id MochiKit.DOM.focusOnLoad */
    focusOnLoad: function (element) {
        var self = MochiKit.DOM;
        self.addLoadEvent(function () {
            element = self.getElement(element);
            if (element) {
                element.focus();
            }
        });
    },

    /** @id MochiKit.DOM.setElementClass */
    setElementClass: function (element, className) {
        var self = MochiKit.DOM;
        var obj = self.getElement(element);
        if (self.attributeArray.compliant) {
            obj.setAttribute("class", className);
        } else {
            obj.setAttribute("className", className);
        }
    },

    /** @id MochiKit.DOM.toggleElementClass */
    toggleElementClass: function (className/*, element... */) {
        var self = MochiKit.DOM;
        for (var i = 1; i < arguments.length; i++) {
            var obj = self.getElement(arguments[i]);
            if (!self.addElementClass(obj, className)) {
                self.removeElementClass(obj, className);
            }
        }
    },

    /** @id MochiKit.DOM.addElementClass */
    addElementClass: function (element, className) {
        var self = MochiKit.DOM;
        var obj = self.getElement(element);
        var cls = obj.className;
        // trivial case, no className yet
        if (cls == undefined || cls.length === 0) {
            self.setElementClass(obj, className);
            return true;
        }
        // the other trivial case, already set as the only class
        if (cls == className) {
            return false;
        }
        var classes = cls.split(" ");
        for (var i = 0; i < classes.length; i++) {
            // already present
            if (classes[i] == className) {
                return false;
            }
        }
        // append class
        self.setElementClass(obj, cls + " " + className);
        return true;
    },

    /** @id MochiKit.DOM.removeElementClass */
    removeElementClass: function (element, className) {
        var self = MochiKit.DOM;
        var obj = self.getElement(element);
        var cls = obj.className;
        // trivial case, no className yet
        if (cls == undefined || cls.length === 0) {
            return false;
        }
        // other trivial case, set only to className
        if (cls == className) {
            self.setElementClass(obj, "");
            return true;
        }
        var classes = cls.split(" ");
        for (var i = 0; i < classes.length; i++) {
            // already present
            if (classes[i] == className) {
                // only check sane case where the class is used once
                classes.splice(i, 1);
                self.setElementClass(obj, classes.join(" "));
                return true;
            }
        }
        // not found
        return false;
    },

    /** @id MochiKit.DOM.swapElementClass */
    swapElementClass: function (element, fromClass, toClass) {
        var obj = MochiKit.DOM.getElement(element);
        var res = MochiKit.DOM.removeElementClass(obj, fromClass);
        if (res) {
            MochiKit.DOM.addElementClass(obj, toClass);
        }
        return res;
    },

    /** @id MochiKit.DOM.hasElementClass */
    hasElementClass: function (element, className/*...*/) {
        var obj = MochiKit.DOM.getElement(element);
        var cls = obj.className;
        if (!cls) {
            return false;
        }
        var classes = cls.split(" ");
        for (var i = 1; i < arguments.length; i++) {
            var good = false;
            for (var j = 0; j < classes.length; j++) {
                if (classes[j] == arguments[i]) {
                    good = true;
                    break;
                }
            }
            if (!good) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.DOM.escapeHTML */
    escapeHTML: function (s) {
        return s.replace(/&/g, "&amp;"
            ).replace(/"/g, "&quot;"
            ).replace(/</g, "&lt;"
            ).replace(/>/g, "&gt;");
    },

    /** @id MochiKit.DOM.toHTML */
    toHTML: function (dom) {
        return MochiKit.DOM.emitHTML(dom).join("");
    },

    /** @id MochiKit.DOM.emitHTML */
    emitHTML: function (dom, /* optional */lst) {
        if (typeof(lst) == 'undefined' || lst === null) {
            lst = [];
        }
        // queue is the call stack, we're doing this non-recursively
        var queue = [dom];
        var self = MochiKit.DOM;
        var escapeHTML = self.escapeHTML;
        var attributeArray = self.attributeArray;
        while (queue.length) {
            dom = queue.pop();
            if (typeof(dom) == 'string') {
                lst.push(dom);
            } else if (dom.nodeType == 1) {
                // we're not using higher order stuff here
                // because safari has heisenbugs.. argh.
                //
                // I think it might have something to do with
                // garbage collection and function calls.
                lst.push('<' + dom.tagName.toLowerCase());
                var attributes = [];
                var domAttr = attributeArray(dom);
                for (var i = 0; i < domAttr.length; i++) {
                    var a = domAttr[i];
                    attributes.push([
                        " ",
                        a.name,
                        '="',
                        escapeHTML(a.value),
                        '"'
                    ]);
                }
                attributes.sort();
                for (i = 0; i < attributes.length; i++) {
                    var attrs = attributes[i];
                    for (var j = 0; j < attrs.length; j++) {
                        lst.push(attrs[j]);
                    }
                }
                if (dom.hasChildNodes()) {
                    lst.push(">");
                    // queue is the FILO call stack, so we put the close tag
                    // on first
                    queue.push("</" + dom.tagName.toLowerCase() + ">");
                    var cnodes = dom.childNodes;
                    for (i = cnodes.length - 1; i >= 0; i--) {
                        queue.push(cnodes[i]);
                    }
                } else {
                    lst.push('/>');
                }
            } else if (dom.nodeType == 3) {
                lst.push(escapeHTML(dom.nodeValue));
            }
        }
        return lst;
    },

    /** @id MochiKit.DOM.scrapeText */
    scrapeText: function (node, /* optional */asArray) {
        var rval = [];
        (function (node) {
            var cn = node.childNodes;
            if (cn) {
                for (var i = 0; i < cn.length; i++) {
                    arguments.callee.call(this, cn[i]);
                }
            }
            var nodeValue = node.nodeValue;
            if (typeof(nodeValue) == 'string') {
                rval.push(nodeValue);
            }
        })(MochiKit.DOM.getElement(node));
        if (asArray) {
            return rval;
        } else {
            return rval.join("");
        }
    },

    /** @id MochiKit.DOM.removeEmptyTextNodes */
    removeEmptyTextNodes: function (element) {
        element = MochiKit.DOM.getElement(element);
        for (var i = 0; i < element.childNodes.length; i++) {
            var node = element.childNodes[i];
            if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
                node.parentNode.removeChild(node);
            }
        }
    },

    /** @id MochiKit.DOM.makeClipping */
    makeClipping: function (element) {
        element = MochiKit.DOM.getElement(element);
        var oldOverflow = element.style.overflow;
        if ((MochiKit.Style.getStyle(element, 'overflow') || 'visible') != 'hidden') {
            element.style.overflow = 'hidden';
        }
        return oldOverflow;
    },

    /** @id MochiKit.DOM.undoClipping */
    undoClipping: function (element, overflow) {
        element = MochiKit.DOM.getElement(element);
        if (!overflow) {
            return;
        }
        element.style.overflow = overflow;
    },

    /** @id MochiKit.DOM.makePositioned */
    makePositioned: function (element) {
        element = MochiKit.DOM.getElement(element);
        var pos = MochiKit.Style.getStyle(element, 'position');
        if (pos == 'static' || !pos) {
            element.style.position = 'relative';
            // Opera returns the offset relative to the positioning context,
            // when an element is position relative but top and left have
            // not been defined
            if (/Opera/.test(navigator.userAgent)) {
                element.style.top = 0;
                element.style.left = 0;
            }
        }
    },

    /** @id MochiKit.DOM.undoPositioned */
    undoPositioned: function (element) {
        element = MochiKit.DOM.getElement(element);
        if (element.style.position == 'relative') {
            element.style.position = element.style.top = element.style.left = element.style.bottom = element.style.right = '';
        }
    },

    /** @id MochiKit.DOM.getFirstElementByTagAndClassName */
    getFirstElementByTagAndClassName: function (tagName, className,
            /* optional */parent) {
        var self = MochiKit.DOM;
        if (typeof(tagName) == 'undefined' || tagName === null) {
            tagName = '*';
        }
        if (typeof(parent) == 'undefined' || parent === null) {
            parent = self._document;
        }
        parent = self.getElement(parent);
        var children = (parent.getElementsByTagName(tagName)
            || self._document.all);
        if (typeof(className) == 'undefined' || className === null) {
            return children[0];
        }

        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            var classNames = child.className.split(' ');
            for (var j = 0; j < classNames.length; j++) {
                if (classNames[j] == className) {
                    return child;
                }
            }
        }
    },

    /** @id MochiKit.DOM.getFirstParentByTagAndClassName */
    getFirstParentByTagAndClassName: function (elem, tagName, className) {
        var self = MochiKit.DOM;
        elem = self.getElement(elem);
        if (typeof(tagName) == 'undefined' || tagName === null) {
            tagName = '*';
        } else {
            tagName = tagName.toUpperCase();
        }
        if (typeof(className) == 'undefined' || className === null) {
            className = null;
        }

        var classList = '';
        var curTagName = '';
        while (elem && elem.tagName) {
            elem = elem.parentNode;
            if (tagName == '*' && className === null) {
                return elem;
            }
            classList = elem.className.split(' ');
            curTagName = elem.tagName.toUpperCase();
            if (className === null && tagName == curTagName) {
                return elem;
            } else if (className !== null) {
                for (var i = 0; i < classList.length; i++) {
                    if (tagName == '*' && classList[i] == className) {
                        return elem;
                    } else if (tagName == curTagName && classList[i] == className) {
                        return elem;
                    }
                }
            }
        }
        return elem;
    },

    /** @id MochiKit.DOM.isParent */
    isParent: function (child, element) {
        if (!child.parentNode || child == element) {
            return false;
        }

        if (child.parentNode == element) {
            return true;
        }

        return MochiKit.DOM.isParent(child.parentNode, element);
    },

    __new__: function (win) {

        var m = MochiKit.Base;
        if (typeof(document) != "undefined") {
            this._document = document;
            var kXULNSURI = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
            this._xhtml = (document.documentElement &&
                document.createElementNS &&
                document.documentElement.namespaceURI === kXULNSURI);
        } else if (MochiKit.MockDOM) {
            this._document = MochiKit.MockDOM.document;
        }
        this._window = win;

        this.domConverters = new m.AdapterRegistry();

        var __tmpElement = this._document.createElement("span");
        var attributeArray;
        if (__tmpElement && __tmpElement.attributes &&
                __tmpElement.attributes.length > 0) {
            // for braindead browsers (IE) that insert extra junk
            var filter = m.filter;
            attributeArray = function (node) {
                return filter(attributeArray.ignoreAttrFilter, node.attributes);
            };
            attributeArray.ignoreAttr = {};
            var attrs = __tmpElement.attributes;
            var ignoreAttr = attributeArray.ignoreAttr;
            for (var i = 0; i < attrs.length; i++) {
                var a = attrs[i];
                ignoreAttr[a.name] = a.value;
            }
            attributeArray.ignoreAttrFilter = function (a) {
                return (attributeArray.ignoreAttr[a.name] != a.value);
            };
            attributeArray.compliant = false;
            attributeArray.renames = {
                "class": "className",
                "checked": "defaultChecked",
                "usemap": "useMap",
                "for": "htmlFor",
                "readonly": "readOnly",
                "colspan": "colSpan",
                "bgcolor": "bgColor"
            };
        } else {
            attributeArray = function (node) {
                /***

                    Return an array of attributes for a given node,
                    filtering out attributes that don't belong for
                    that are inserted by "Certain Browsers".

                ***/
                return node.attributes;
            };
            attributeArray.compliant = true;
            attributeArray.renames = {};
        }
        this.attributeArray = attributeArray;

        // FIXME: this really belongs in Base, and could probably be cleaner
        var _deprecated = function(fromModule, arr) {
            var modules = arr[1].split('.');
            var str = '';
            var obj = {};

            str += 'if (!MochiKit.' + modules[1] + ') { throw new Error("';
            str += 'This function has been deprecated and depends on MochiKit.';
            str += modules[1] + '.");}';
            str += 'return MochiKit.' + modules[1] + '.' + arr[0];
            str += '.apply(this, arguments);';

            obj[modules[2]] = new Function(str);
            MochiKit.Base.update(MochiKit[fromModule], obj);
        }
        for (var i; i < MochiKit.DOM.DEPRECATED.length; i++) {
            _deprecated('DOM', MochiKit.DOM.DEPRECATED[i]);
        }

        // shorthand for createDOM syntax
        var createDOMFunc = this.createDOMFunc;
        /** @id MochiKit.DOM.UL */
        this.UL = createDOMFunc("ul");
        /** @id MochiKit.DOM.OL */
        this.OL = createDOMFunc("ol");
        /** @id MochiKit.DOM.LI */
        this.LI = createDOMFunc("li");
        /** @id MochiKit.DOM.TD */
        this.TD = createDOMFunc("td");
        /** @id MochiKit.DOM.TR */
        this.TR = createDOMFunc("tr");
        /** @id MochiKit.DOM.TBODY */
        this.TBODY = createDOMFunc("tbody");
        /** @id MochiKit.DOM.THEAD */
        this.THEAD = createDOMFunc("thead");
        /** @id MochiKit.DOM.TFOOT */
        this.TFOOT = createDOMFunc("tfoot");
        /** @id MochiKit.DOM.TABLE */
        this.TABLE = createDOMFunc("table");
        /** @id MochiKit.DOM.TH */
        this.TH = createDOMFunc("th");
        /** @id MochiKit.DOM.INPUT */
        this.INPUT = createDOMFunc("input");
        /** @id MochiKit.DOM.SPAN */
        this.SPAN = createDOMFunc("span");
        /** @id MochiKit.DOM.A */
        this.A = createDOMFunc("a");
        /** @id MochiKit.DOM.DIV */
        this.DIV = createDOMFunc("div");
        /** @id MochiKit.DOM.IMG */
        this.IMG = createDOMFunc("img");
        /** @id MochiKit.DOM.BUTTON */
        this.BUTTON = createDOMFunc("button");
        /** @id MochiKit.DOM.TT */
        this.TT = createDOMFunc("tt");
        /** @id MochiKit.DOM.PRE */
        this.PRE = createDOMFunc("pre");
        /** @id MochiKit.DOM.H1 */
        this.H1 = createDOMFunc("h1");
        /** @id MochiKit.DOM.H2 */
        this.H2 = createDOMFunc("h2");
        /** @id MochiKit.DOM.H3 */
        this.H3 = createDOMFunc("h3");
        /** @id MochiKit.DOM.BR */
        this.BR = createDOMFunc("br");
        /** @id MochiKit.DOM.HR */
        this.HR = createDOMFunc("hr");
        /** @id MochiKit.DOM.LABEL */
        this.LABEL = createDOMFunc("label");
        /** @id MochiKit.DOM.TEXTAREA */
        this.TEXTAREA = createDOMFunc("textarea");
        /** @id MochiKit.DOM.FORM */
        this.FORM = createDOMFunc("form");
        /** @id MochiKit.DOM.P */
        this.P = createDOMFunc("p");
        /** @id MochiKit.DOM.SELECT */
        this.SELECT = createDOMFunc("select");
        /** @id MochiKit.DOM.OPTION */
        this.OPTION = createDOMFunc("option");
        /** @id MochiKit.DOM.OPTGROUP */
        this.OPTGROUP = createDOMFunc("optgroup");
        /** @id MochiKit.DOM.LEGEND */
        this.LEGEND = createDOMFunc("legend");
        /** @id MochiKit.DOM.FIELDSET */
        this.FIELDSET = createDOMFunc("fieldset");
        /** @id MochiKit.DOM.STRONG */
        this.STRONG = createDOMFunc("strong");
        /** @id MochiKit.DOM.CANVAS */
        this.CANVAS = createDOMFunc("canvas");

        /** @id MochiKit.DOM.$ */
        this.$ = this.getElement;

        this.EXPORT_TAGS = {
            ":common": this.EXPORT,
            ":all": m.concat(this.EXPORT, this.EXPORT_OK)
        };

        m.nameFunctions(this);

    }
});


MochiKit.DOM.__new__(((typeof(window) == "undefined") ? this : window));

//
// XXX: Internet Explorer blows
//
if (MochiKit.__export__) {
    withWindow = MochiKit.DOM.withWindow;
    withDocument = MochiKit.DOM.withDocument;
}

MochiKit.Base._exportSymbols(this, MochiKit.DOM);

/***

MochiKit.Style 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005-2006 Bob Ippolito, Beau Hartshorne.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Style');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
}
if (typeof(JSAN) != 'undefined') {
    JSAN.use('MochiKit.Base', []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Style depends on MochiKit.Base!';
}

try {
    if (typeof(MochiKit.DOM) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Style depends on MochiKit.DOM!';
}


if (typeof(MochiKit.Style) == 'undefined') {
    MochiKit.Style = {};
}

MochiKit.Style.NAME = 'MochiKit.Style';
MochiKit.Style.VERSION = '1.4';
MochiKit.Style.__repr__ = function () {
    return '[' + this.NAME + ' ' + this.VERSION + ']';
};
MochiKit.Style.toString = function () {
    return this.__repr__();
};

MochiKit.Style.EXPORT_OK = [];

MochiKit.Style.EXPORT = [
    'setStyle',
    'setOpacity',
    'getStyle',
    'getElementDimensions',
    'elementDimensions', // deprecated
    'setElementDimensions',
    'getElementPosition',
    'elementPosition', // deprecated
    'setElementPosition',
    'setDisplayForElement',
    'hideElement',
    'showElement',
    'getViewportDimensions',
    'getViewportPosition',
    'Dimensions',
    'Coordinates'
];


/*

    Dimensions

*/
/** @id MochiKit.Style.Dimensions */
MochiKit.Style.Dimensions = function (w, h) {
    this.w = w;
    this.h = h;
};

MochiKit.Style.Dimensions.prototype.__repr__ = function () {
    var repr = MochiKit.Base.repr;
    return '{w: '  + repr(this.w) + ', h: ' + repr(this.h) + '}';
};

MochiKit.Style.Dimensions.prototype.toString = function () {
    return this.__repr__();
};


/*

    Coordinates

*/
/** @id MochiKit.Style.Coordinates */
MochiKit.Style.Coordinates = function (x, y) {
    this.x = x;
    this.y = y;
};

MochiKit.Style.Coordinates.prototype.__repr__ = function () {
    var repr = MochiKit.Base.repr;
    return '{x: '  + repr(this.x) + ', y: ' + repr(this.y) + '}';
};

MochiKit.Style.Coordinates.prototype.toString = function () {
    return this.__repr__();
};


MochiKit.Base.update(MochiKit.Style, {

    /** @id MochiKit.Style.getStyle */
    getStyle: function (elem, cssProperty) {
        var dom = MochiKit.DOM;
        var d = dom._document;

        elem = dom.getElement(elem);
        cssProperty = MochiKit.Base.camelize(cssProperty);

        if (!elem || elem == d) {
            return undefined;
        }
        if (cssProperty == 'opacity' && elem.filters) {
            var opacity = (MochiKit.Style.getStyle(elem, 'filter') || '').match(/alpha\(opacity=(.*)\)/);
            if (opacity && opacity[1]) {
                return parseFloat(opacity[1]) / 100;
            }
            return 1.0;
        }
        var value = elem.style ? elem.style[cssProperty] : null;
        if (!value) {
            if (d.defaultView && d.defaultView.getComputedStyle) {
                var css = d.defaultView.getComputedStyle(elem, null);
                cssProperty = cssProperty.replace(/([A-Z])/g, '-$1'
                    ).toLowerCase(); // from dojo.style.toSelectorCase
                value = css ? css.getPropertyValue(cssProperty) : null;
            } else if (elem.currentStyle) {
                value = elem.currentStyle[cssProperty];
            }
        }
        if (cssProperty == 'opacity') {
            value = parseFloat(value);
        }

        if (/Opera/.test(navigator.userAgent) && (MochiKit.Base.find(['left', 'top', 'right', 'bottom'], cssProperty) != -1)) {
            if (MochiKit.Style.getStyle(elem, 'position') == 'static') {
                value = 'auto';
            }
        }

        return value == 'auto' ? null : value;
    },

    /** @id MochiKit.Style.setStyle */
    setStyle: function (elem, style) {
        elem = MochiKit.DOM.getElement(elem);
        for (name in style) {
            if (name == 'opacity') {
                MochiKit.Style.setOpacity(elem, style[name]);
            } else {
                elem.style[MochiKit.Base.camelize(name)] = style[name];
            }
        }
    },

    /** @id MochiKit.Style.setOpacity */
    setOpacity: function (elem, o) {
        elem = MochiKit.DOM.getElement(elem);
        var self = MochiKit.Style;
        if (o == 1) {
            var toSet = /Gecko/.test(navigator.userAgent) && !(/Konqueror|Safari|KHTML/.test(navigator.userAgent));
            elem.style["opacity"] = toSet ? 0.999999 : 1.0;
            if (/MSIE/.test(navigator.userAgent)) {
                elem.style['filter'] =
                    self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '');
            }
        } else {
            if (o < 0.00001) {
                o = 0;
            }
            elem.style["opacity"] = o;
            if (/MSIE/.test(navigator.userAgent)) {
                elem.style['filter'] =
                    self.getStyle(elem, 'filter').replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + o * 100 + ')';
            }
        }
    },

    /*

        getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0.
        Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved.
        License: BSD, http://developer.yahoo.net/yui/license.txt

    */

    /** @id MochiKit.Style.getElementPosition */
    getElementPosition: function (elem, /* optional */relativeTo) {
        var self = MochiKit.Style;
        var dom = MochiKit.DOM;
        elem = dom.getElement(elem);

        if (!elem ||
            (!(elem.x && elem.y) &&
            (!elem.parentNode === null ||
            self.getStyle(elem, 'display') == 'none'))) {
            return undefined;
        }

        var c = new self.Coordinates(0, 0);
        var box = null;
        var parent = null;

        var d = MochiKit.DOM._document;
        var de = d.documentElement;
        var b = d.body;

        if (!elem.parentNode && elem.x && elem.y) {
            /* it's just a MochiKit.Style.Coordinates object */
            c.x += elem.x || 0;
            c.y += elem.y || 0;
        } else if (elem.getBoundingClientRect) { // IE shortcut
            /*

                The IE shortcut can be off by two. We fix it. See:
                http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp

                This is similar to the method used in
                MochiKit.Signal.Event.mouse().

            */
            box = elem.getBoundingClientRect();

            c.x += box.left +
                (de.scrollLeft || b.scrollLeft) -
                (de.clientLeft || 0);

            c.y += box.top +
                (de.scrollTop || b.scrollTop) -
                (de.clientTop || 0);

        } else if (elem.offsetParent) {
            c.x += elem.offsetLeft;
            c.y += elem.offsetTop;
            parent = elem.offsetParent;

            if (parent != elem) {
                while (parent) {
                    c.x += parent.offsetLeft;
                    c.y += parent.offsetTop;
                    parent = parent.offsetParent;
                }
            }

            /*

                Opera < 9 and old Safari (absolute) incorrectly account for
                body offsetTop and offsetLeft.

            */
            var ua = navigator.userAgent.toLowerCase();
            if ((typeof(opera) != 'undefined' &&
                parseFloat(opera.version()) < 9) ||
                (ua.indexOf('safari') != -1 &&
                self.getStyle(elem, 'position') == 'absolute')) {

                c.x -= b.offsetLeft;
                c.y -= b.offsetTop;

            }
        }

        if (typeof(relativeTo) != 'undefined') {
            relativeTo = arguments.callee(relativeTo);
            if (relativeTo) {
                c.x -= (relativeTo.x || 0);
                c.y -= (relativeTo.y || 0);
            }
        }

        if (elem.parentNode) {
            parent = elem.parentNode;
        } else {
            parent = null;
        }

        while (parent) {
            var tagName = parent.tagName.toUpperCase();
            if (tagName === 'BODY' || tagName === 'HTML') {
                break;
            }
            var disp = self.getStyle(parent, 'display');
            // Handle strange Opera bug for some display
            if (disp != 'inline' && disp != 'table-row') {
                c.x -= parent.scrollLeft;
                c.y -= parent.scrollTop;
            }
            if (parent.parentNode) {
                parent = parent.parentNode;
            } else {
                parent = null;
            }
        }

        return c;
    },

    /** @id MochiKit.Style.setElementPosition */
    setElementPosition: function (elem, newPos/* optional */, units) {
        elem = MochiKit.DOM.getElement(elem);
        if (typeof(units) == 'undefined') {
            units = 'px';
        }
        var newStyle = {};
        var isUndefNull = MochiKit.Base.isUndefinedOrNull;
        if (!isUndefNull(newPos.x)) {
            newStyle['left'] = newPos.x + units;
        }
        if (!isUndefNull(newPos.y)) {
            newStyle['top'] = newPos.y + units;
        }
        MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
    },

    /** @id MochiKit.Style.getElementDimensions */
    getElementDimensions: function (elem) {
        var self = MochiKit.Style;
        var dom = MochiKit.DOM;
        if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') {
            return new self.Dimensions(elem.w || 0, elem.h || 0);
        }
        elem = dom.getElement(elem);
        if (!elem) {
            return undefined;
        }
        var disp = self.getStyle(elem, 'display');
        // display can be empty/undefined on WebKit/KHTML
        if (disp != 'none' && disp !== '' && typeof(disp) != 'undefined') {
            return new self.Dimensions(elem.offsetWidth || 0,
                elem.offsetHeight || 0);
        }
        var s = elem.style;
        var originalVisibility = s.visibility;
        var originalPosition = s.position;
        s.visibility = 'hidden';
        s.position = 'absolute';
        s.display = '';
        var originalWidth = elem.offsetWidth;
        var originalHeight = elem.offsetHeight;
        s.display = 'none';
        s.position = originalPosition;
        s.visibility = originalVisibility;
        return new self.Dimensions(originalWidth, originalHeight);
    },

    /** @id MochiKit.Style.setElementDimensions */
    setElementDimensions: function (elem, newSize/* optional */, units) {
        elem = MochiKit.DOM.getElement(elem);
        if (typeof(units) == 'undefined') {
            units = 'px';
        }
        var newStyle = {};
        var isUndefNull = MochiKit.Base.isUndefinedOrNull;
        if (!isUndefNull(newSize.w)) {
            newStyle['width'] = newSize.w + units;
        }
        if (!isUndefNull(newSize.h)) {
            newStyle['height'] = newSize.h + units;
        }
        MochiKit.DOM.updateNodeAttributes(elem, {'style': newStyle});
    },

    /** @id MochiKit.Style.setDisplayForElement */
    setDisplayForElement: function (display, element/*, ...*/) {
        var elements = MochiKit.Base.extend(null, arguments, 1);
        var getElement = MochiKit.DOM.getElement;
        for (var i = 0; i < elements.length; i++) {
            element = getElement(elements[i]);
            if (element) {
                element.style.display = display;
            }
        }
    },

    /** @id MochiKit.Style.getViewportDimensions */
    getViewportDimensions: function () {
        var d = new MochiKit.Style.Dimensions();

        var w = MochiKit.DOM._window;
        var b = MochiKit.DOM._document.body;

        if (w.innerWidth) {
            d.w = w.innerWidth;
            d.h = w.innerHeight;
        } else if (b.parentElement.clientWidth) {
            d.w = b.parentElement.clientWidth;
            d.h = b.parentElement.clientHeight;
        } else if (b && b.clientWidth) {
            d.w = b.clientWidth;
            d.h = b.clientHeight;
        }
        return d;
    },

    /** @id MochiKit.Style.getViewportPosition */
    getViewportPosition: function () {
        var c = new MochiKit.Style.Coordinates(0, 0);
        var d = MochiKit.DOM._document;
        var de = d.documentElement;
        var db = d.body;
        if (de && (de.scrollTop || de.scrollLeft)) {
            c.x = de.scrollLeft;
            c.y = de.scrollTop;
        } else if (db) {
            c.x = db.scrollLeft;
            c.y = db.scrollTop;
        }
        return c;
    },

    __new__: function () {
        var m = MochiKit.Base;

        this.elementPosition = this.getElementPosition;
        this.elementDimensions = this.getElementDimensions;

        this.hideElement = m.partial(this.setDisplayForElement, 'none');
        this.showElement = m.partial(this.setDisplayForElement, 'block');

        this.EXPORT_TAGS = {
            ':common': this.EXPORT,
            ':all': m.concat(this.EXPORT, this.EXPORT_OK)
        };

        m.nameFunctions(this);
    }
});

MochiKit.Style.__new__();
MochiKit.Base._exportSymbols(this, MochiKit.Style);

/***

MochiKit.Position 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005-2006 Bob Ippolito and others.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Position');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
    dojo.require('MochiKit.Style');
}
if (typeof(JSAN) != 'undefined') {
    JSAN.use('MochiKit.Base', []);
    JSAN.use('MochiKit.DOM', []);
    JSAN.use('MochiKit.Style', []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined' ||
        typeof(MochiKit.Style) == 'undefined' ||
        typeof(MochiKit.DOM) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Style depends on MochiKit.Base, MochiKit.DOM, and MochiKit.Style!';
}

if (typeof(MochiKit.Position) == 'undefined') {
    MochiKit.Position = {};
}

MochiKit.Position.NAME = 'MochiKit.Position';
MochiKit.Position.VERSION = '1.4';
MochiKit.Position.__repr__ = function () {
    return '[' + this.NAME + ' ' + this.VERSION + ']';
};
MochiKit.Position.toString = function () {
    return this.__repr__();
};

MochiKit.Position.EXPORT_OK = [];

MochiKit.Position.EXPORT = [
];


MochiKit.Base.update(MochiKit.Position, {
    // set to true if needed, warning: firefox performance problems
    // NOT neeeded for page scrolling, only if draggable contained in
    // scrollable elements
    includeScrollOffsets: false,

    /** @id MochiKit.Position.prepare */
    prepare: function () {
        var deltaX =  window.pageXOffset
                   || document.documentElement.scrollLeft
                   || document.body.scrollLeft
                   || 0;
        var deltaY =  window.pageYOffset
                   || document.documentElement.scrollTop
                   || document.body.scrollTop
                   || 0;
        this.windowOffset = new MochiKit.Style.Coordinates(deltaX, deltaY);
    },

    /** @id MochiKit.Position.cumulativeOffset */
    cumulativeOffset: function (element) {
        var valueT = 0;
        var valueL = 0;
        do {
            valueT += element.offsetTop  || 0;
            valueL += element.offsetLeft || 0;
            element = element.offsetParent;
        } while (element);
        return new MochiKit.Style.Coordinates(valueL, valueT);
    },

    /** @id MochiKit.Position.realOffset */
    realOffset: function (element) {
        var valueT = 0;
        var valueL = 0;
        do {
            valueT += element.scrollTop  || 0;
            valueL += element.scrollLeft || 0;
            element = element.parentNode;
        } while (element);
        return new MochiKit.Style.Coordinates(valueL, valueT);
    },

    /** @id MochiKit.Position.within */
    within: function (element, x, y) {
        if (this.includeScrollOffsets) {
            return this.withinIncludingScrolloffsets(element, x, y);
        }
        this.xcomp = x;
        this.ycomp = y;
        this.offset = this.cumulativeOffset(element);
        if (element.style.position == "fixed") {
            this.offset.x += this.windowOffset.x;
            this.offset.y += this.windowOffset.y;
        }

        return (y >= this.offset.y &&
                y <  this.offset.y + element.offsetHeight &&
                x >= this.offset.x &&
                x <  this.offset.x + element.offsetWidth);
    },

    /** @id MochiKit.Position.withinIncludingScrolloffsets */
    withinIncludingScrolloffsets: function (element, x, y) {
        var offsetcache = this.realOffset(element);

        this.xcomp = x + offsetcache.x - this.windowOffset.x;
        this.ycomp = y + offsetcache.y - this.windowOffset.y;
        this.offset = this.cumulativeOffset(element);

        return (this.ycomp >= this.offset.y &&
                this.ycomp <  this.offset.y + element.offsetHeight &&
                this.xcomp >= this.offset.x &&
                this.xcomp <  this.offset.x + element.offsetWidth);
    },

    // within must be called directly before
    /** @id MochiKit.Position.overlap */
    overlap: function (mode, element) {
        if (!mode) {
            return 0;
        }
        if (mode == 'vertical') {
          return ((this.offset.y + element.offsetHeight) - this.ycomp) /
                 element.offsetHeight;
        }
        if (mode == 'horizontal') {
          return ((this.offset.x + element.offsetWidth) - this.xcomp) /
                 element.offsetWidth;
        }
    },

    /** @id MochiKit.Position.absolutize */
    absolutize: function (element) {
        element = MochiKit.DOM.getElement(element);
        if (element.style.position == 'absolute') {
            return;
        }
        MochiKit.Position.prepare();

        var offsets = MochiKit.Position.positionedOffset(element);
        var width = element.clientWidth;
        var height = element.clientHeight;

        var oldStyle = {
            'position': element.style.position,
            'left': offsets.x - parseFloat(element.style.left  || 0),
            'top': offsets.y - parseFloat(element.style.top || 0),
            'width': element.style.width,
            'height': element.style.height
        };

        element.style.position = 'absolute';
        element.style.top = offsets.y + 'px';
        element.style.left = offsets.x + 'px';
        element.style.width = width + 'px';
        element.style.height = height + 'px';

        return oldStyle;
    },

    /** @id MochiKit.Position.positionedOffset */
    positionedOffset: function (element) {
        var valueT = 0, valueL = 0;
        do {
            valueT += element.offsetTop  || 0;
            valueL += element.offsetLeft || 0;
            element = element.offsetParent;
            if (element) {
                p = MochiKit.Style.getStyle(element, 'position');
                if (p == 'relative' || p == 'absolute') {
                    break;
                }
            }
        } while (element);
        return new MochiKit.Style.Coordinates(valueL, valueT);
    },

    /** @id MochiKit.Position.relativize */
    relativize: function (element, oldPos) {
        element = MochiKit.DOM.getElement(element);
        if (element.style.position == 'relative') {
            return;
        }
        MochiKit.Position.prepare();

        var top = parseFloat(element.style.top || 0) -
                  (oldPos['top'] || 0);
        var left = parseFloat(element.style.left || 0) -
                   (oldPos['left'] || 0);

        element.style.position = oldPos['position'];
        element.style.top = top + 'px';
        element.style.left = left + 'px';
        element.style.width = oldPos['width'];
        element.style.height = oldPos['height'];
    },

    /** @id MochiKit.Position.clone */
    clone: function (source, target) {
        source = MochiKit.DOM.getElement(source);
        target = MochiKit.DOM.getElement(target);
        target.style.position = 'absolute';
        var offsets = this.cumulativeOffset(source);
        target.style.top = offsets.y + 'px';
        target.style.left = offsets.x + 'px';
        target.style.width = source.offsetWidth + 'px';
        target.style.height = source.offsetHeight + 'px';
    },

    /** @id MochiKit.Position.page */
    page: function (forElement) {
        var valueT = 0;
        var valueL = 0;

        var element = forElement;
        do {
            valueT += element.offsetTop  || 0;
            valueL += element.offsetLeft || 0;

            // Safari fix
            if (element.offsetParent == document.body && MochiKit.Style.getStyle(element, 'position') == 'absolute') {
                break;
            }
        } while (element = element.offsetParent);

        element = forElement;
        do {
            valueT -= element.scrollTop  || 0;
            valueL -= element.scrollLeft || 0;
        } while (element = element.parentNode);

        return new MochiKit.Style.Coordinates(valueL, valueT);
    }
});

/***

MochiKit.Signal 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2006 Jonathan Gardner, Beau Hartshorne, Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Signal');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
    dojo.require('MochiKit.Style');
}
if (typeof(JSAN) != 'undefined') {
    JSAN.use('MochiKit.Base', []);
    JSAN.use('MochiKit.DOM', []);
    JSAN.use('MochiKit.Style', []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Signal depends on MochiKit.Base!';
}

try {
    if (typeof(MochiKit.DOM) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Signal depends on MochiKit.DOM!';
}

try {
    if (typeof(MochiKit.Style) == 'undefined') {
        throw '';
    }
} catch (e) {
    throw 'MochiKit.Signal depends on MochiKit.Style!';
}

if (typeof(MochiKit.Signal) == 'undefined') {
    MochiKit.Signal = {};
}

MochiKit.Signal.NAME = 'MochiKit.Signal';
MochiKit.Signal.VERSION = '1.4';

MochiKit.Signal._observers = [];

/** @id MochiKit.Signal.Event */
MochiKit.Signal.Event = function (src, e) {
    this._event = e || window.event;
    this._src = src;
};

MochiKit.Base.update(MochiKit.Signal.Event.prototype, {

    __repr__: function () {
        var repr = MochiKit.Base.repr;
        var str = '{event(): ' + repr(this.event()) +
            ', src(): ' + repr(this.src()) +
            ', type(): ' + repr(this.type()) +
            ', target(): ' + repr(this.target());

        if (this.type() && 
            this.type().indexOf('key') === 0 ||
            this.type().indexOf('mouse') === 0 ||
            this.type().indexOf('click') != -1 ||
            this.type() == 'contextmenu') {
            str += ', modifier(): ' + '{alt: ' + repr(this.modifier().alt) +
            ', ctrl: ' + repr(this.modifier().ctrl) +
            ', meta: ' + repr(this.modifier().meta) +
            ', shift: ' + repr(this.modifier().shift) +
            ', any: ' + repr(this.modifier().any) + '}';
        }

        if (this.type() && this.type().indexOf('key') === 0) {
            str += ', key(): {code: ' + repr(this.key().code) +
                ', string: ' + repr(this.key().string) + '}';
        }

        if (this.type() && (
            this.type().indexOf('mouse') === 0 ||
            this.type().indexOf('click') != -1 ||
            this.type() == 'contextmenu')) {

            str += ', mouse(): {page: ' + repr(this.mouse().page) +
                ', client: ' + repr(this.mouse().client);

            if (this.type() != 'mousemove') {
                str += ', button: {left: ' + repr(this.mouse().button.left) +
                    ', middle: ' + repr(this.mouse().button.middle) +
                    ', right: ' + repr(this.mouse().button.right) + '}}';
            } else {
                str += '}';
            }
        }
        if (this.type() == 'mouseover' || this.type() == 'mouseout') {
            str += ', relatedTarget(): ' + repr(this.relatedTarget());
        }
        str += '}';
        return str;
    },

     /** @id MochiKit.Signal.Event.prototype.toString */
    toString: function () {
        return this.__repr__();
    },

    /** @id MochiKit.Signal.Event.prototype.src */
    src: function () {
        return this._src;
    },

    /** @id MochiKit.Signal.Event.prototype.event  */
    event: function () {
        return this._event;
    },

    /** @id MochiKit.Signal.Event.prototype.type */
    type: function () {
        return this._event.type || undefined;
    },

    /** @id MochiKit.Signal.Event.prototype.target */
    target: function () {
        return this._event.target || this._event.srcElement;
    },

    _relatedTarget: null,
    /** @id MochiKit.Signal.Event.prototype.relatedTarget */
    relatedTarget: function () {
        if (this._relatedTarget !== null) {
            return this._relatedTarget;
        }

        var elem = null;
        if (this.type() == 'mouseover') {
            elem = (this._event.relatedTarget ||
                this._event.fromElement);
        } else if (this.type() == 'mouseout') {
            elem = (this._event.relatedTarget ||
                this._event.toElement);
        }
        if (elem !== null) {
            this._relatedTarget = elem;
            return elem;
        }

        return undefined;
    },

    _modifier: null,
    /** @id MochiKit.Signal.Event.prototype.modifier */
    modifier: function () {
        if (this._modifier !== null) {
            return this._modifier;
        }
        var m = {};
        m.alt = this._event.altKey;
        m.ctrl = this._event.ctrlKey;
        m.meta = this._event.metaKey || false; // IE and Opera punt here
        m.shift = this._event.shiftKey;
        m.any = m.alt || m.ctrl || m.shift || m.meta;
        this._modifier = m;
        return m;
    },

    _key: null,
    /** @id MochiKit.Signal.Event.prototype.key */
    key: function () {
        if (this._key !== null) {
            return this._key;
        }
        var k = {};
        if (this.type() && this.type().indexOf('key') === 0) {

            /*

                If you're looking for a special key, look for it in keydown or
                keyup, but never keypress. If you're looking for a Unicode
                chracter, look for it with keypress, but never keyup or
                keydown.

                Notes:

                FF key event behavior:
                key     event   charCode    keyCode
                DOWN    ku,kd   0           40
                DOWN    kp      0           40
                ESC     ku,kd   0           27
                ESC     kp      0           27
                a       ku,kd   0           65
                a       kp      97          0
                shift+a ku,kd   0           65
                shift+a kp      65          0
                1       ku,kd   0           49
                1       kp      49          0
                shift+1 ku,kd   0           0
                shift+1 kp      33          0

                IE key event behavior:
                (IE doesn't fire keypress events for special keys.)
                key     event   keyCode
                DOWN    ku,kd   40
                DOWN    kp      undefined
                ESC     ku,kd   27
                ESC     kp      27
                a       ku,kd   65
                a       kp      97
                shift+a ku,kd   65
                shift+a kp      65
                1       ku,kd   49
                1       kp      49
                shift+1 ku,kd   49
                shift+1 kp      33

                Safari key event behavior:
                (Safari sets charCode and keyCode to something crazy for
                special keys.)
                key     event   charCode    keyCode
                DOWN    ku,kd   63233       40
                DOWN    kp      63233       63233
                ESC     ku,kd   27          27
                ESC     kp      27          27
                a       ku,kd   97          65
                a       kp      97          97
                shift+a ku,kd   65          65
                shift+a kp      65          65
                1       ku,kd   49          49
                1       kp      49          49
                shift+1 ku,kd   33          49
                shift+1 kp      33          33

            */

            /* look for special keys here */
            if (this.type() == 'keydown' || this.type() == 'keyup') {
                k.code = this._event.keyCode;
                k.string = (MochiKit.Signal._specialKeys[k.code] ||
                    'KEY_UNKNOWN');
                this._key = k;
                return k;

            /* look for characters here */
            } else if (this.type() == 'keypress') {

                /*

                    Special key behavior:

                    IE: does not fire keypress events for special keys
                    FF: sets charCode to 0, and sets the correct keyCode
                    Safari: sets keyCode and charCode to something stupid

                */

                k.code = 0;
                k.string = '';

                if (typeof(this._event.charCode) != 'undefined' &&
                    this._event.charCode !== 0 &&
                    !MochiKit.Signal._specialMacKeys[this._event.charCode]) {
                    k.code = this._event.charCode;
                    k.string = String.fromCharCode(k.code);
                } else if (this._event.keyCode &&
                    typeof(this._event.charCode) == 'undefined') { // IE
                    k.code = this._event.keyCode;
                    k.string = String.fromCharCode(k.code);
                }

                this._key = k;
                return k;
            }
        }
        return undefined;
    },

    _mouse: null,
    /** @id MochiKit.Signal.Event.prototype.mouse */
    mouse: function () {
        if (this._mouse !== null) {
            return this._mouse;
        }

        var m = {};
        var e = this._event;

        if (this.type() && (
            this.type().indexOf('mouse') === 0 ||
            this.type().indexOf('click') != -1 ||
            this.type() == 'contextmenu')) {

            m.client = new MochiKit.Style.Coordinates(0, 0);
            if (e.clientX || e.clientY) {
                m.client.x = (!e.clientX || e.clientX < 0) ? 0 : e.clientX;
                m.client.y = (!e.clientY || e.clientY < 0) ? 0 : e.clientY;
            }

            m.page = new MochiKit.Style.Coordinates(0, 0);
            if (e.pageX || e.pageY) {
                m.page.x = (!e.pageX || e.pageX < 0) ? 0 : e.pageX;
                m.page.y = (!e.pageY || e.pageY < 0) ? 0 : e.pageY;
            } else {
                /*

                    The IE shortcut can be off by two. We fix it. See:
                    http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp

                    This is similar to the method used in
                    MochiKit.Style.getElementPosition().

                */
                var de = MochiKit.DOM._document.documentElement;
                var b = MochiKit.DOM._document.body;

                m.page.x = e.clientX +
                    (de.scrollLeft || b.scrollLeft) -
                    (de.clientLeft || 0);

                m.page.y = e.clientY +
                    (de.scrollTop || b.scrollTop) -
                    (de.clientTop || 0);

            }
            if (this.type() != 'mousemove') {
                m.button = {};
                m.button.left = false;
                m.button.right = false;
                m.button.middle = false;

                /* we could check e.button, but which is more consistent */
                if (e.which) {
                    m.button.left = (e.which == 1);
                    m.button.middle = (e.which == 2);
                    m.button.right = (e.which == 3);

                    /*

                        Mac browsers and right click:

                            - Safari doesn't fire any click events on a right
                              click:
                              http://bugs.webkit.org/show_bug.cgi?id=6595

                            - Firefox fires the event, and sets ctrlKey = true

                            - Opera fires the event, and sets metaKey = true

                        oncontextmenu is fired on right clicks between
                        browsers and across platforms.

                    */

                } else {
                    m.button.left = !!(e.button & 1);
                    m.button.right = !!(e.button & 2);
                    m.button.middle = !!(e.button & 4);
                }
            }
            this._mouse = m;
            return m;
        }
        return undefined;
    },

    /** @id MochiKit.Signal.Event.prototype.stop */
    stop: function () {
        this.stopPropagation();
        this.preventDefault();
    },

    /** @id MochiKit.Signal.Event.prototype.stopPropagation */
    stopPropagation: function () {
        if (this._event.stopPropagation) {
            this._event.stopPropagation();
        } else {
            this._event.cancelBubble = true;
        }
    },

    /** @id MochiKit.Signal.Event.prototype.preventDefault */
    preventDefault: function () {
        if (this._event.preventDefault) {
            this._event.preventDefault();
        } else if (this._confirmUnload === null) {
            this._event.returnValue = false;
        }
    },

    _confirmUnload: null,

    /** @id MochiKit.Signal.Event.prototype.confirmUnload */
    confirmUnload: function (msg) {
        if (this.type() == 'beforeunload') {
            this._confirmUnload = msg;
            this._event.returnValue = msg;
        }
    }
});

/* Safari sets keyCode to these special values onkeypress. */
MochiKit.Signal._specialMacKeys = {
    3: 'KEY_ENTER',
    63289: 'KEY_NUM_PAD_CLEAR',
    63276: 'KEY_PAGE_UP',
    63277: 'KEY_PAGE_DOWN',
    63275: 'KEY_END',
    63273: 'KEY_HOME',
    63234: 'KEY_ARROW_LEFT',
    63232: 'KEY_ARROW_UP',
    63235: 'KEY_ARROW_RIGHT',
    63233: 'KEY_ARROW_DOWN',
    63302: 'KEY_INSERT',
    63272: 'KEY_DELETE'
};

/* for KEY_F1 - KEY_F12 */
(function () {
    var _specialMacKeys = MochiKit.Signal._specialMacKeys;
    for (i = 63236; i <= 63242; i++) {
        // no F0
        _specialMacKeys[i] = 'KEY_F' + (i - 63236 + 1);
    }
})();

/* Standard keyboard key codes. */
MochiKit.Signal._specialKeys = {
    8: 'KEY_BACKSPACE',
    9: 'KEY_TAB',
    12: 'KEY_NUM_PAD_CLEAR', // weird, for Safari and Mac FF only
    13: 'KEY_ENTER',
    16: 'KEY_SHIFT',
    17: 'KEY_CTRL',
    18: 'KEY_ALT',
    19: 'KEY_PAUSE',
    20: 'KEY_CAPS_LOCK',
    27: 'KEY_ESCAPE',
    32: 'KEY_SPACEBAR',
    33: 'KEY_PAGE_UP',
    34: 'KEY_PAGE_DOWN',
    35: 'KEY_END',
    36: 'KEY_HOME',
    37: 'KEY_ARROW_LEFT',
    38: 'KEY_ARROW_UP',
    39: 'KEY_ARROW_RIGHT',
    40: 'KEY_ARROW_DOWN',
    44: 'KEY_PRINT_SCREEN',
    45: 'KEY_INSERT',
    46: 'KEY_DELETE',
    59: 'KEY_SEMICOLON', // weird, for Safari and IE only
    91: 'KEY_WINDOWS_LEFT',
    92: 'KEY_WINDOWS_RIGHT',
    93: 'KEY_SELECT',
    106: 'KEY_NUM_PAD_ASTERISK',
    107: 'KEY_NUM_PAD_PLUS_SIGN',
    109: 'KEY_NUM_PAD_HYPHEN-MINUS',
    110: 'KEY_NUM_PAD_FULL_STOP',
    111: 'KEY_NUM_PAD_SOLIDUS',
    144: 'KEY_NUM_LOCK',
    145: 'KEY_SCROLL_LOCK',
    186: 'KEY_SEMICOLON',
    187: 'KEY_EQUALS_SIGN',
    188: 'KEY_COMMA',
    189: 'KEY_HYPHEN-MINUS',
    190: 'KEY_FULL_STOP',
    191: 'KEY_SOLIDUS',
    192: 'KEY_GRAVE_ACCENT',
    219: 'KEY_LEFT_SQUARE_BRACKET',
    220: 'KEY_REVERSE_SOLIDUS',
    221: 'KEY_RIGHT_SQUARE_BRACKET',
    222: 'KEY_APOSTROPHE'
    // undefined: 'KEY_UNKNOWN'
};

(function () {
    /* for KEY_0 - KEY_9 */
    var _specialKeys = MochiKit.Signal._specialKeys;
    for (var i = 48; i <= 57; i++) {
        _specialKeys[i] = 'KEY_' + (i - 48);
    }

    /* for KEY_A - KEY_Z */
    for (i = 65; i <= 90; i++) {
        _specialKeys[i] = 'KEY_' + String.fromCharCode(i);
    }

    /* for KEY_NUM_PAD_0 - KEY_NUM_PAD_9 */
    for (i = 96; i <= 105; i++) {
        _specialKeys[i] = 'KEY_NUM_PAD_' + (i - 96);
    }

    /* for KEY_F1 - KEY_F12 */
    for (i = 112; i <= 123; i++) {
        // no F0
        _specialKeys[i] = 'KEY_F' + (i - 112 + 1);
    }
})();

MochiKit.Base.update(MochiKit.Signal, {

    __repr__: function () {
        return '[' + this.NAME + ' ' + this.VERSION + ']';
    },

    toString: function () {
        return this.__repr__();
    },

    _unloadCache: function () {
        var self = MochiKit.Signal;
        var observers = self._observers;

        for (var i = 0; i < observers.length; i++) {
            self._disconnect(observers[i]);
        }

        delete self._observers;

        try {
            window.onload = undefined;
        } catch(e) {
            // pass
        }

        try {
            window.onunload = undefined;
        } catch(e) {
            // pass
        }
    },

    _listener: function (src, func, obj, isDOM) {
        var self = MochiKit.Signal;
        var E = self.Event;
        if (!isDOM) {
            return MochiKit.Base.bind(func, obj);
        }
        obj = obj || src;
        if (typeof(func) == "string") {
            return function (nativeEvent) {
                obj[func].apply(obj, [new E(src, nativeEvent)]);
            };
        } else {
            return function (nativeEvent) {
                func.apply(obj, [new E(src, nativeEvent)]);
            };
        }
    },

    _browserAlreadyHasMouseEnterAndLeave: function () {
        return /MSIE/.test(navigator.userAgent);
    },

    _mouseEnterListener: function (src, sig, func, obj) {
        var E = MochiKit.Signal.Event;
        return function (nativeEvent) {
            var e = new E(src, nativeEvent);
            try {
                e.relatedTarget().nodeName;
            } catch (err) {
                /* probably hit a permission denied error; possibly one of
                 * firefox's screwy anonymous DIVs inside an input element.
                 * Allow this event to propogate up.
                 */
                return;
            }
            e.stop();
            if (MochiKit.DOM.isChildNode(e.relatedTarget(), src)) {
                /* We've moved between our node and a child. Ignore. */
                return;
            }
            e.type = function () { return sig; };
            if (typeof(func) == "string") {
                return obj[func].apply(obj, [e]);
            } else {
                return func.apply(obj, [e]);
            }
        };
    },

    _getDestPair: function (objOrFunc, funcOrStr) {
        var obj = null;
        var func = null;
        if (typeof(funcOrStr) != 'undefined') {
            obj = objOrFunc;
            func = funcOrStr;
            if (typeof(funcOrStr) == 'string') {
                if (typeof(objOrFunc[funcOrStr]) != "function") {
                    throw new Error("'funcOrStr' must be a function on 'objOrFunc'");
                }
            } else if (typeof(funcOrStr) != 'function') {
                throw new Error("'funcOrStr' must be a function or string");
            }
        } else if (typeof(objOrFunc) != "function") {
            throw new Error("'objOrFunc' must be a function if 'funcOrStr' is not given");
        } else {
            func = objOrFunc;
        }
        return [obj, func];

    },

    /** @id MochiKit.Signal.connect */
    connect: function (src, sig, objOrFunc/* optional */, funcOrStr) {
        src = MochiKit.DOM.getElement(src);
        var self = MochiKit.Signal;

        if (typeof(sig) != 'string') {
            throw new Error("'sig' must be a string");
        }

        var destPair = self._getDestPair(objOrFunc, funcOrStr);
        var obj = destPair[0];
        var func = destPair[1];
        if (typeof(obj) == 'undefined' || obj === null) {
            obj = src;
        }

        var isDOM = !!(src.addEventListener || src.attachEvent);
        if (isDOM && (sig === "onmouseenter" || sig === "onmouseleave")
                  && !self._browserAlreadyHasMouseEnterAndLeave()) {
            var listener = self._mouseEnterListener(src, sig.substr(2), func, obj);
            if (sig === "onmouseenter") {
                sig = "onmouseover";
            } else {
                sig = "onmouseout";
            }
        } else {
            var listener = self._listener(src, func, obj, isDOM);
        }

        if (src.addEventListener) {
            src.addEventListener(sig.substr(2), listener, false);
        } else if (src.attachEvent) {
            src.attachEvent(sig, listener); // useCapture unsupported
        }

        var ident = [src, sig, listener, isDOM, objOrFunc, funcOrStr, true];
        self._observers.push(ident);


        if (!isDOM && typeof(src.__connect__) == 'function') {
            var args = MochiKit.Base.extend([ident], arguments, 1);
            src.__connect__.apply(src, args);
        }


        return ident;
    },

    _disconnect: function (ident) {
        // already disconnected
        if (!ident[6]) { return; }
        ident[6] = false;
        // check isDOM
        if (!ident[3]) { return; }
        var src = ident[0];
        var sig = ident[1];
        var listener = ident[2];
        if (src.removeEventListener) {
            src.removeEventListener(sig.substr(2), listener, false);
        } else if (src.detachEvent) {
            src.detachEvent(sig, listener); // useCapture unsupported
        } else {
            throw new Error("'src' must be a DOM element");
        }
    },

     /** @id MochiKit.Signal.disconnect */
    disconnect: function (ident) {
        var self = MochiKit.Signal;
        var observers = self._observers;
        var m = MochiKit.Base;
        if (arguments.length > 1) {
            // compatibility API
            var src = MochiKit.DOM.getElement(arguments[0]);
            var sig = arguments[1];
            var obj = arguments[2];
            var func = arguments[3];
            for (var i = observers.length - 1; i >= 0; i--) {
                var o = observers[i];
                if (o[0] === src && o[1] === sig && o[4] === obj && o[5] === func) {
                    self._disconnect(o);
                    if (!self._lock) {
                        observers.splice(i, 1);
                    } else {
                        self._dirty = true;
                    }
                    return true;
                }
            }
        } else {
            var idx = m.findIdentical(observers, ident);
            if (idx >= 0) {
                self._disconnect(ident);
                if (!self._lock) {
                    observers.splice(idx, 1);
                } else {
                    self._dirty = true;
                }
                return true;
            }
        }
        return false;
    },

    /** @id MochiKit.Signal.disconnectAllTo */
    disconnectAllTo: function (objOrFunc, /* optional */funcOrStr) {
        var self = MochiKit.Signal;
        var observers = self._observers;
        var disconnect = self._disconnect;
        var locked = self._lock;
        var dirty = self._dirty;
        if (typeof(funcOrStr) === 'undefined') {
            funcOrStr = null;
        }
        for (var i = observers.length - 1; i >= 0; i--) {
            var ident = observers[i];
            if (ident[4] === objOrFunc &&
                    (funcOrStr === null || ident[5] === funcOrStr)) {
                disconnect(ident);
                if (locked) {
                    dirty = true;
                } else {
                    observers.splice(i, 1);
                }
            }
        }
        self._dirty = dirty;
    },

    /** @id MochiKit.Signal.disconnectAll */
    disconnectAll: function (src/* optional */, sig) {
        src = MochiKit.DOM.getElement(src);
        var m = MochiKit.Base;
        var signals = m.flattenArguments(m.extend(null, arguments, 1));
        var self = MochiKit.Signal;
        var disconnect = self._disconnect;
        var observers = self._observers;
        var i, ident;
        var locked = self._lock;
        var dirty = self._dirty;
        if (signals.length === 0) {
            // disconnect all
            for (i = observers.length - 1; i >= 0; i--) {
                ident = observers[i];
                if (ident[0] === src) {
                    disconnect(ident);
                    if (!locked) {
                        observers.splice(i, 1);
                    } else {
                        dirty = true;
                    }
                }
            }
        } else {
            var sigs = {};
            for (i = 0; i < signals.length; i++) {
                sigs[signals[i]] = true;
            }
            for (i = observers.length - 1; i >= 0; i--) {
                ident = observers[i];
                if (ident[0] === src && ident[1] in sigs) {
                    disconnect(ident);
                    if (!locked) {
                        observers.splice(i, 1);
                    } else {
                        dirty = true;
                    }
                }
            }
        }
        self._dirty = dirty;
    },

    /** @id MochiKit.Signal.signal */
    signal: function (src, sig) {
        var self = MochiKit.Signal;
        var observers = self._observers;
        src = MochiKit.DOM.getElement(src);
        var args = MochiKit.Base.extend(null, arguments, 2);
        var errors = [];
        self._lock = true;
        for (var i = 0; i < observers.length; i++) {
            var ident = observers[i];
            if (ident[0] === src && ident[1] === sig) {
                try {
                    ident[2].apply(src, args);
                } catch (e) {
                    errors.push(e);
                }
            }
        }
        self._lock = false;
        if (self._dirty) {
            self._dirty = false;
            for (var i = observers.length - 1; i >= 0; i--) {
                if (!observers[i][6]) {
                    observers.splice(i, 1);
                }
            }
        }
        if (errors.length == 1) {
            throw errors[0];
        } else if (errors.length > 1) {
            var e = new Error("Multiple errors thrown in handling 'sig', see errors property");
            e.errors = errors;
            throw e;
        }
    }

});

MochiKit.Signal.EXPORT_OK = [];

MochiKit.Signal.EXPORT = [
    'connect',
    'disconnect',
    'signal',
    'disconnectAll',
    'disconnectAllTo'
];

MochiKit.Signal.__new__ = function (win) {
    var m = MochiKit.Base;
    this._document = document;
    this._window = win;
    this._lock = false;
    this._dirty = false;

    try {
        this.connect(window, 'onunload', this._unloadCache);
    } catch (e) {
        // pass: might not be a browser
    }

    this.EXPORT_TAGS = {
        ':common': this.EXPORT,
        ':all': m.concat(this.EXPORT, this.EXPORT_OK)
    };

    m.nameFunctions(this);
};

MochiKit.Signal.__new__(this);

//
// XXX: Internet Explorer blows
//
if (MochiKit.__export__) {
    connect = MochiKit.Signal.connect;
    disconnect = MochiKit.Signal.disconnect;
    disconnectAll = MochiKit.Signal.disconnectAll;
    signal = MochiKit.Signal.signal;
}

MochiKit.Base._exportSymbols(this, MochiKit.Signal);

/***

MochiKit.Logging 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Logging');
    dojo.require('MochiKit.Base');
}

if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Logging depends on MochiKit.Base!";
}

if (typeof(MochiKit.Logging) == 'undefined') {
    MochiKit.Logging = {};
}

MochiKit.Logging.NAME = "MochiKit.Logging";
MochiKit.Logging.VERSION = "1.4";
MochiKit.Logging.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};

MochiKit.Logging.toString = function () {
    return this.__repr__();
};


MochiKit.Logging.EXPORT = [
    "LogLevel",
    "LogMessage",
    "Logger",
    "alertListener",
    "logger",
    "log",
    "logError",
    "logDebug",
    "logFatal",
    "logWarning"
];


MochiKit.Logging.EXPORT_OK = [
    "logLevelAtLeast",
    "isLogMessage",
    "compareLogMessage"
];


/** @id MochiKit.Logging.LogMessage */
MochiKit.Logging.LogMessage = function (num, level, info) {
    this.num = num;
    this.level = level;
    this.info = info;
    this.timestamp = new Date();
};

MochiKit.Logging.LogMessage.prototype = {
     /** @id MochiKit.Logging.LogMessage.prototype.repr */
    repr: function () {
        var m = MochiKit.Base;
        return 'LogMessage(' +
            m.map(
                m.repr,
                [this.num, this.level, this.info]
            ).join(', ') + ')';
    },
    /** @id MochiKit.Logging.LogMessage.prototype.toString */
    toString: MochiKit.Base.forwardCall("repr")
};

MochiKit.Base.update(MochiKit.Logging, {
    /** @id MochiKit.Logging.logLevelAtLeast */
    logLevelAtLeast: function (minLevel) {
        var self = MochiKit.Logging;
        if (typeof(minLevel) == 'string') {
            minLevel = self.LogLevel[minLevel];
        }
        return function (msg) {
            var msgLevel = msg.level;
            if (typeof(msgLevel) == 'string') {
                msgLevel = self.LogLevel[msgLevel];
            }
            return msgLevel >= minLevel;
        };
    },

    /** @id MochiKit.Logging.isLogMessage */
    isLogMessage: function (/* ... */) {
        var LogMessage = MochiKit.Logging.LogMessage;
        for (var i = 0; i < arguments.length; i++) {
            if (!(arguments[i] instanceof LogMessage)) {
                return false;
            }
        }
        return true;
    },

    /** @id MochiKit.Logging.compareLogMessage */
    compareLogMessage: function (a, b) {
        return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]);
    },

    /** @id MochiKit.Logging.alertListener */
    alertListener: function (msg) {
        alert(
            "num: " + msg.num +
            "\nlevel: " +  msg.level +
            "\ninfo: " + msg.info.join(" ")
        );
    }

});

/** @id MochiKit.Logging.Logger */
MochiKit.Logging.Logger = function (/* optional */maxSize) {
    this.counter = 0;
    if (typeof(maxSize) == 'undefined' || maxSize === null) {
        maxSize = -1;
    }
    this.maxSize = maxSize;
    this._messages = [];
    this.listeners = {};
    this.useNativeConsole = false;
};

MochiKit.Logging.Logger.prototype = {
    /** @id MochiKit.Logging.Logger.prototype.clear */
    clear: function () {
        this._messages.splice(0, this._messages.length);
    },

    /** @id MochiKit.Logging.Logger.prototype.logToConsole */
    logToConsole: function (msg) {
        if (typeof(window) != "undefined" && window.console
                && window.console.log) {
            // Safari and FireBug 0.4
            // Percent replacement is a workaround for cute Safari crashing bug
            window.console.log(msg.replace(/%/g, '\uFF05'));
        } else if (typeof(opera) != "undefined" && opera.postError) {
            // Opera
            opera.postError(msg);
        } else if (typeof(printfire) == "function") {
            // FireBug 0.3 and earlier
            printfire(msg);
        } else if (typeof(Debug) != "undefined" && Debug.writeln) {
            // IE Web Development Helper (?)
            // http://www.nikhilk.net/Entry.aspx?id=93
            Debug.writeln(msg);
        } else if (typeof(debug) != "undefined" && debug.trace) {
            // Atlas framework (?)
            // http://www.nikhilk.net/Entry.aspx?id=93
            debug.trace(msg);
        }
    },

    /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */
    dispatchListeners: function (msg) {
        for (var k in this.listeners) {
            var pair = this.listeners[k];
            if (pair.ident != k || (pair[0] && !pair[0](msg))) {
                continue;
            }
            pair[1](msg);
        }
    },

    /** @id MochiKit.Logging.Logger.prototype.addListener */
    addListener: function (ident, filter, listener) {
        if (typeof(filter) == 'string') {
            filter = MochiKit.Logging.logLevelAtLeast(filter);
        }
        var entry = [filter, listener];
        entry.ident = ident;
        this.listeners[ident] = entry;
    },

    /** @id MochiKit.Logging.Logger.prototype.removeListener */
    removeListener: function (ident) {
        delete this.listeners[ident];
    },

    /** @id MochiKit.Logging.Logger.prototype.baseLog */
    baseLog: function (level, message/*, ...*/) {
        var msg = new MochiKit.Logging.LogMessage(
            this.counter,
            level,
            MochiKit.Base.extend(null, arguments, 1)
        );
        this._messages.push(msg);
        this.dispatchListeners(msg);
        if (this.useNativeConsole) {
            this.logToConsole(msg.level + ": " + msg.info.join(" "));
        }
        this.counter += 1;
        while (this.maxSize >= 0 && this._messages.length > this.maxSize) {
            this._messages.shift();
        }
    },

    /** @id MochiKit.Logging.Logger.prototype.getMessages */
    getMessages: function (howMany) {
        var firstMsg = 0;
        if (!(typeof(howMany) == 'undefined' || howMany === null)) {
            firstMsg = Math.max(0, this._messages.length - howMany);
        }
        return this._messages.slice(firstMsg);
    },

    /** @id MochiKit.Logging.Logger.prototype.getMessageText */
    getMessageText: function (howMany) {
        if (typeof(howMany) == 'undefined' || howMany === null) {
            howMany = 30;
        }
        var messages = this.getMessages(howMany);
        if (messages.length) {
            var lst = map(function (m) {
                return '\n  [' + m.num + '] ' + m.level + ': ' + m.info.join(' ');
            }, messages);
            lst.unshift('LAST ' + messages.length + ' MESSAGES:');
            return lst.join('');
        }
        return '';
    },

    /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */
    debuggingBookmarklet: function (inline) {
        if (typeof(MochiKit.LoggingPane) == "undefined") {
            alert(this.getMessageText());
        } else {
            MochiKit.LoggingPane.createLoggingPane(inline || false);
        }
    }
};

MochiKit.Logging.__new__ = function () {
    this.LogLevel = {
        ERROR: 40,
        FATAL: 50,
        WARNING: 30,
        INFO: 20,
        DEBUG: 10
    };

    var m = MochiKit.Base;
    m.registerComparator("LogMessage",
        this.isLogMessage,
        this.compareLogMessage
    );

    var partial = m.partial;

    var Logger = this.Logger;
    var baseLog = Logger.prototype.baseLog;
    m.update(this.Logger.prototype, {
        debug: partial(baseLog, 'DEBUG'),
        log: partial(baseLog, 'INFO'),
        error: partial(baseLog, 'ERROR'),
        fatal: partial(baseLog, 'FATAL'),
        warning: partial(baseLog, 'WARNING')
    });

    // indirectly find logger so it can be replaced
    var self = this;
    var connectLog = function (name) {
        return function () {
            self.logger[name].apply(self.logger, arguments);
        };
    };

    /** @id MochiKit.Logging.log */
    this.log = connectLog('log');
    /** @id MochiKit.Logging.logError */
    this.logError = connectLog('error');
    /** @id MochiKit.Logging.logDebug */
    this.logDebug = connectLog('debug');
    /** @id MochiKit.Logging.logFatal */
    this.logFatal = connectLog('fatal');
    /** @id MochiKit.Logging.logWarning */
    this.logWarning = connectLog('warning');
    this.logger = new Logger();
    this.logger.useNativeConsole = true;

    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
    };

    m.nameFunctions(this);

};

if (typeof(printfire) == "undefined" &&
        typeof(document) != "undefined" && document.createEvent &&
        typeof(dispatchEvent) != "undefined") {
    // FireBug really should be less lame about this global function
    printfire  = function () {
        printfire.args = arguments;
        var ev = document.createEvent("Events");
        ev.initEvent("printfire", false, true);
        dispatchEvent(ev);
    };
}

MochiKit.Logging.__new__();

MochiKit.Base._exportSymbols(this, MochiKit.Logging);

/***

MochiKit.Iter 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Iter');
    dojo.require('MochiKit.Base');
}

if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Iter depends on MochiKit.Base!";
}

if (typeof(MochiKit.Iter) == 'undefined') {
    MochiKit.Iter = {};
}

MochiKit.Iter.NAME = "MochiKit.Iter";
MochiKit.Iter.VERSION = "1.4";
MochiKit.Base.update(MochiKit.Iter, {
    __repr__: function () {
        return "[" + this.NAME + " " + this.VERSION + "]";
    },
    toString: function () {
        return this.__repr__();
    },

    /** @id MochiKit.Iter.registerIteratorFactory  */
    registerIteratorFactory: function (name, check, iterfactory, /* optional */ override) {
        MochiKit.Iter.iteratorRegistry.register(name, check, iterfactory, override);
    },

    /** @id MochiKit.Iter.iter */
    iter: function (iterable, /* optional */ sentinel) {
        var self = MochiKit.Iter;
        if (arguments.length == 2) {
            return self.takewhile(
                function (a) { return a != sentinel; },
                iterable
            );
        }
        if (typeof(iterable.next) == 'function') {
            return iterable;
        } else if (typeof(iterable.iter) == 'function') {
            return iterable.iter();
        /*
        }  else if (typeof(iterable.__iterator__) == 'function') {
            //
            // XXX: We can't support JavaScript 1.7 __iterator__ directly
            //      because of Object.prototype.__iterator__
            //
            return iterable.__iterator__();
        */
        }

        try {
            return self.iteratorRegistry.match(iterable);
        } catch (e) {
            var m = MochiKit.Base;
            if (e == m.NotFound) {
                e = new TypeError(typeof(iterable) + ": " + m.repr(iterable) + " is not iterable");
            }
            throw e;
        }
    },

    /** @id MochiKit.Iter.count */
    count: function (n) {
        if (!n) {
            n = 0;
        }
        var m = MochiKit.Base;
        return {
            repr: function () { return "count(" + n + ")"; },
            toString: m.forwardCall("repr"),
            next: m.counter(n)
        };
    },

    /** @id MochiKit.Iter.cycle */
    cycle: function (p) {
        var self = MochiKit.Iter;
        var m = MochiKit.Base;
        var lst = [];
        var iterator = self.iter(p);
        return {
            repr: function () { return "cycle(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                try {
                    var rval = iterator.next();
                    lst.push(rval);
                    return rval;
                } catch (e) {
                    if (e != self.StopIteration) {
                        throw e;
                    }
                    if (lst.length === 0) {
                        this.next = function () {
                            throw self.StopIteration;
                        };
                    } else {
                        var i = -1;
                        this.next = function () {
                            i = (i + 1) % lst.length;
                            return lst[i];
                        };
                    }
                    return this.next();
                }
            }
        };
    },

    /** @id MochiKit.Iter.repeat */
    repeat: function (elem, /* optional */n) {
        var m = MochiKit.Base;
        if (typeof(n) == 'undefined') {
            return {
                repr: function () {
                    return "repeat(" + m.repr(elem) + ")";
                },
                toString: m.forwardCall("repr"),
                next: function () {
                    return elem;
                }
            };
        }
        return {
            repr: function () {
                return "repeat(" + m.repr(elem) + ", " + n + ")";
            },
            toString: m.forwardCall("repr"),
            next: function () {
                if (n <= 0) {
                    throw MochiKit.Iter.StopIteration;
                }
                n -= 1;
                return elem;
            }
        };
    },

    /** @id MochiKit.Iter.next */
    next: function (iterator) {
        return iterator.next();
    },

    /** @id MochiKit.Iter.izip */
    izip: function (p, q/*, ...*/) {
        var m = MochiKit.Base;
        var self = MochiKit.Iter;
        var next = self.next;
        var iterables = m.map(self.iter, arguments);
        return {
            repr: function () { return "izip(...)"; },
            toString: m.forwardCall("repr"),
            next: function () { return m.map(next, iterables); }
        };
    },

    /** @id MochiKit.Iter.ifilter */
    ifilter: function (pred, seq) {
        var m = MochiKit.Base;
        seq = MochiKit.Iter.iter(seq);
        if (pred === null) {
            pred = m.operator.truth;
        }
        return {
            repr: function () { return "ifilter(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                while (true) {
                    var rval = seq.next();
                    if (pred(rval)) {
                        return rval;
                    }
                }
                // mozilla warnings aren't too bright
                return undefined;
            }
        };
    },

    /** @id MochiKit.Iter.ifilterfalse */
    ifilterfalse: function (pred, seq) {
        var m = MochiKit.Base;
        seq = MochiKit.Iter.iter(seq);
        if (pred === null) {
            pred = m.operator.truth;
        }
        return {
            repr: function () { return "ifilterfalse(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                while (true) {
                    var rval = seq.next();
                    if (!pred(rval)) {
                        return rval;
                    }
                }
                // mozilla warnings aren't too bright
                return undefined;
            }
        };
    },

    /** @id MochiKit.Iter.islice */
    islice: function (seq/*, [start,] stop[, step] */) {
        var self = MochiKit.Iter;
        var m = MochiKit.Base;
        seq = self.iter(seq);
        var start = 0;
        var stop = 0;
        var step = 1;
        var i = -1;
        if (arguments.length == 2) {
            stop = arguments[1];
        } else if (arguments.length == 3) {
            start = arguments[1];
            stop = arguments[2];
        } else {
            start = arguments[1];
            stop = arguments[2];
            step = arguments[3];
        }
        return {
            repr: function () {
                return "islice(" + ["...", start, stop, step].join(", ") + ")";
            },
            toString: m.forwardCall("repr"),
            next: function () {
                var rval;
                while (i < start) {
                    rval = seq.next();
                    i++;
                }
                if (start >= stop) {
                    throw self.StopIteration;
                }
                start += step;
                return rval;
            }
        };
    },

    /** @id MochiKit.Iter.imap */
    imap: function (fun, p, q/*, ...*/) {
        var m = MochiKit.Base;
        var self = MochiKit.Iter;
        var iterables = m.map(self.iter, m.extend(null, arguments, 1));
        var map = m.map;
        var next = self.next;
        return {
            repr: function () { return "imap(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                return fun.apply(this, map(next, iterables));
            }
        };
    },

    /** @id MochiKit.Iter.applymap */
    applymap: function (fun, seq, self) {
        seq = MochiKit.Iter.iter(seq);
        var m = MochiKit.Base;
        return {
            repr: function () { return "applymap(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                return fun.apply(self, seq.next());
            }
        };
    },

    /** @id MochiKit.Iter.chain */
    chain: function (p, q/*, ...*/) {
        // dumb fast path
        var self = MochiKit.Iter;
        var m = MochiKit.Base;
        if (arguments.length == 1) {
            return self.iter(arguments[0]);
        }
        var argiter = m.map(self.iter, arguments);
        return {
            repr: function () { return "chain(...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                while (argiter.length > 1) {
                    try {
                        return argiter[0].next();
                    } catch (e) {
                        if (e != self.StopIteration) {
                            throw e;
                        }
                        argiter.shift();
                    }
                }
                if (argiter.length == 1) {
                    // optimize last element
                    var arg = argiter.shift();
                    this.next = m.bind("next", arg);
                    return this.next();
                }
                throw self.StopIteration;
            }
        };
    },

    /** @id MochiKit.Iter.takewhile */
    takewhile: function (pred, seq) {
        var self = MochiKit.Iter;
        seq = self.iter(seq);
        return {
            repr: function () { return "takewhile(...)"; },
            toString: MochiKit.Base.forwardCall("repr"),
            next: function () {
                var rval = seq.next();
                if (!pred(rval)) {
                    this.next = function () {
                        throw self.StopIteration;
                    };
                    this.next();
                }
                return rval;
            }
        };
    },

    /** @id MochiKit.Iter.dropwhile */
    dropwhile: function (pred, seq) {
        seq = MochiKit.Iter.iter(seq);
        var m = MochiKit.Base;
        var bind = m.bind;
        return {
            "repr": function () { return "dropwhile(...)"; },
            "toString": m.forwardCall("repr"),
            "next": function () {
                while (true) {
                    var rval = seq.next();
                    if (!pred(rval)) {
                        break;
                    }
                }
                this.next = bind("next", seq);
                return rval;
            }
        };
    },

    _tee: function (ident, sync, iterable) {
        sync.pos[ident] = -1;
        var m = MochiKit.Base;
        var listMin = m.listMin;
        return {
            repr: function () { return "tee(" + ident + ", ...)"; },
            toString: m.forwardCall("repr"),
            next: function () {
                var rval;
                var i = sync.pos[ident];

                if (i == sync.max) {
                    rval = iterable.next();
                    sync.deque.push(rval);
                    sync.max += 1;
                    sync.pos[ident] += 1;
                } else {
                    rval = sync.deque[i - sync.min];
                    sync.pos[ident] += 1;
                    if (i == sync.min && listMin(sync.pos) != sync.min) {
                        sync.min += 1;
                        sync.deque.shift();
                    }
                }
                return rval;
            }
        };
    },

    /** @id MochiKit.Iter.tee */
    tee: function (iterable, n/* = 2 */) {
        var rval = [];
        var sync = {
            "pos": [],
            "deque": [],
            "max": -1,
            "min": -1
        };
        if (arguments.length == 1 || typeof(n) == "undefined" || n === null) {
            n = 2;
        }
        var self = MochiKit.Iter;
        iterable = self.iter(iterable);
        var _tee = self._tee;
        for (var i = 0; i < n; i++) {
            rval.push(_tee(i, sync, iterable));
        }
        return rval;
    },

    /** @id MochiKit.Iter.list */
    list: function (iterable) {
        // Fast-path for Array and Array-like
        var m = MochiKit.Base;
        if (typeof(iterable.slice) == 'function') {
            return iterable.slice();
        } else if (m.isArrayLike(iterable)) {
            return m.concat(iterable);
        }

        var self = MochiKit.Iter;
        iterable = self.iter(iterable);
        var rval = [];
        try {
            while (true) {
                rval.push(iterable.next());
            }
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
            return rval;
        }
        // mozilla warnings aren't too bright
        return undefined;
    },


    /** @id MochiKit.Iter.reduce */
    reduce: function (fn, iterable, /* optional */initial) {
        var i = 0;
        var x = initial;
        var self = MochiKit.Iter;
        iterable = self.iter(iterable);
        if (arguments.length < 3) {
            try {
                x = iterable.next();
            } catch (e) {
                if (e == self.StopIteration) {
                    e = new TypeError("reduce() of empty sequence with no initial value");
                }
                throw e;
            }
            i++;
        }
        try {
            while (true) {
                x = fn(x, iterable.next());
            }
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
        }
        return x;
    },

    /** @id MochiKit.Iter.range */
    range: function (/* [start,] stop[, step] */) {
        var start = 0;
        var stop = 0;
        var step = 1;
        if (arguments.length == 1) {
            stop = arguments[0];
        } else if (arguments.length == 2) {
            start = arguments[0];
            stop = arguments[1];
        } else if (arguments.length == 3) {
            start = arguments[0];
            stop = arguments[1];
            step = arguments[2];
        } else {
            throw new TypeError("range() takes 1, 2, or 3 arguments!");
        }
        if (step === 0) {
            throw new TypeError("range() step must not be 0");
        }
        return {
            next: function () {
                if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
                    throw MochiKit.Iter.StopIteration;
                }
                var rval = start;
                start += step;
                return rval;
            },
            repr: function () {
                return "range(" + [start, stop, step].join(", ") + ")";
            },
            toString: MochiKit.Base.forwardCall("repr")
        };
    },

    /** @id MochiKit.Iter.sum */
    sum: function (iterable, start/* = 0 */) {
        if (typeof(start) == "undefined" || start === null) {
            start = 0;
        }
        var x = start;
        var self = MochiKit.Iter;
        iterable = self.iter(iterable);
        try {
            while (true) {
                x += iterable.next();
            }
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
        }
        return x;
    },

    /** @id MochiKit.Iter.exhaust */
    exhaust: function (iterable) {
        var self = MochiKit.Iter;
        iterable = self.iter(iterable);
        try {
            while (true) {
                iterable.next();
            }
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
        }
    },

    /** @id MochiKit.Iter.forEach */
    forEach: function (iterable, func, /* optional */self) {
        var m = MochiKit.Base;
        if (arguments.length > 2) {
            func = m.bind(func, self);
        }
        // fast path for array
        if (m.isArrayLike(iterable)) {
            try {
                for (var i = 0; i < iterable.length; i++) {
                    func(iterable[i]);
                }
            } catch (e) {
                if (e != MochiKit.Iter.StopIteration) {
                    throw e;
                }
            }
        } else {
            self = MochiKit.Iter;
            self.exhaust(self.imap(func, iterable));
        }
    },

    /** @id MochiKit.Iter.every */
    every: function (iterable, func) {
        var self = MochiKit.Iter;
        try {
            self.ifilterfalse(func, iterable).next();
            return false;
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
            return true;
        }
    },

    /** @id MochiKit.Iter.sorted */
    sorted: function (iterable, /* optional */cmp) {
        var rval = MochiKit.Iter.list(iterable);
        if (arguments.length == 1) {
            cmp = MochiKit.Base.compare;
        }
        rval.sort(cmp);
        return rval;
    },

    /** @id MochiKit.Iter.reversed */
    reversed: function (iterable) {
        var rval = MochiKit.Iter.list(iterable);
        rval.reverse();
        return rval;
    },

    /** @id MochiKit.Iter.some */
    some: function (iterable, func) {
        var self = MochiKit.Iter;
        try {
            self.ifilter(func, iterable).next();
            return true;
        } catch (e) {
            if (e != self.StopIteration) {
                throw e;
            }
            return false;
        }
    },

    /** @id MochiKit.Iter.iextend */
    iextend: function (lst, iterable) {
        if (MochiKit.Base.isArrayLike(iterable)) {
            // fast-path for array-like
            for (var i = 0; i < iterable.length; i++) {
                lst.push(iterable[i]);
            }
        } else {
            var self = MochiKit.Iter;
            iterable = self.iter(iterable);
            try {
                while (true) {
                    lst.push(iterable.next());
                }
            } catch (e) {
                if (e != self.StopIteration) {
                    throw e;
                }
            }
        }
        return lst;
    },

    /** @id MochiKit.Iter.groupby */
    groupby: function(iterable, /* optional */ keyfunc) {
        var m = MochiKit.Base;
        var self = MochiKit.Iter;
        if (arguments.length < 2) {
            keyfunc = m.operator.identity;
        }
        iterable = self.iter(iterable);

        // shared
        var pk = undefined;
        var k = undefined;
        var v;

        function fetch() {
            v = iterable.next();
            k = keyfunc(v);
        };

        function eat() {
            var ret = v;
            v = undefined;
            return ret;
        };

        var first = true;
        var compare = m.compare;
        return {
            repr: function () { return "groupby(...)"; },
            next: function() {
                // iterator-next

                // iterate until meet next group
                while (compare(k, pk) === 0) {
                    fetch();
                    if (first) {
                        first = false;
                        break;
                    }
                }
                pk = k;
                return [k, {
                    next: function() {
                        // subiterator-next
                        if (v == undefined) { // Is there something to eat?
                            fetch();
                        }
                        if (compare(k, pk) !== 0) {
                            throw self.StopIteration;
                        }
                        return eat();
                    }
                }];
            }
        };
    },

    /** @id MochiKit.Iter.groupby_as_array */
    groupby_as_array: function (iterable, /* optional */ keyfunc) {
        var m = MochiKit.Base;
        var self = MochiKit.Iter;
        if (arguments.length < 2) {
            keyfunc = m.operator.identity;
        }

        iterable = self.iter(iterable);
        var result = [];
        var first = true;
        var prev_key;
        var compare = m.compare;
        while (true) {
            try {
                var value = iterable.next();
                var key = keyfunc(value);
            } catch (e) {
                if (e == self.StopIteration) {
                    break;
                }
                throw e;
            }
            if (first || compare(key, prev_key) !== 0) {
                var values = [];
                result.push([key, values]);
            }
            values.push(value);
            first = false;
            prev_key = key;
        }
        return result;
    },

    /** @id MochiKit.Iter.arrayLikeIter */
    arrayLikeIter: function (iterable) {
        var i = 0;
        return {
            repr: function () { return "arrayLikeIter(...)"; },
            toString: MochiKit.Base.forwardCall("repr"),
            next: function () {
                if (i >= iterable.length) {
                    throw MochiKit.Iter.StopIteration;
                }
                return iterable[i++];
            }
        };
    },

    /** @id MochiKit.Iter.hasIterateNext */
    hasIterateNext: function (iterable) {
        return (iterable && typeof(iterable.iterateNext) == "function");
    },

    /** @id MochiKit.Iter.iterateNextIter */
    iterateNextIter: function (iterable) {
        return {
            repr: function () { return "iterateNextIter(...)"; },
            toString: MochiKit.Base.forwardCall("repr"),
            next: function () {
                var rval = iterable.iterateNext();
                if (rval === null || rval === undefined) {
                    throw MochiKit.Iter.StopIteration;
                }
                return rval;
            }
        };
    }
});


MochiKit.Iter.EXPORT_OK = [
    "iteratorRegistry",
    "arrayLikeIter",
    "hasIterateNext",
    "iterateNextIter",
];

MochiKit.Iter.EXPORT = [
    "StopIteration",
    "registerIteratorFactory",
    "iter",
    "count",
    "cycle",
    "repeat",
    "next",
    "izip",
    "ifilter",
    "ifilterfalse",
    "islice",
    "imap",
    "applymap",
    "chain",
    "takewhile",
    "dropwhile",
    "tee",
    "list",
    "reduce",
    "range",
    "sum",
    "exhaust",
    "forEach",
    "every",
    "sorted",
    "reversed",
    "some",
    "iextend",
    "groupby",
    "groupby_as_array"
];

MochiKit.Iter.__new__ = function () {
    var m = MochiKit.Base;
    // Re-use StopIteration if exists (e.g. SpiderMonkey)
    if (typeof(StopIteration) != "undefined") {
        this.StopIteration = StopIteration;
    } else {
        /** @id MochiKit.Iter.StopIteration */
        this.StopIteration = new m.NamedError("StopIteration");
    }
    this.iteratorRegistry = new m.AdapterRegistry();
    // Register the iterator factory for arrays
    this.registerIteratorFactory(
        "arrayLike",
        m.isArrayLike,
        this.arrayLikeIter
    );

    this.registerIteratorFactory(
        "iterateNext",
        this.hasIterateNext,
        this.iterateNextIter
    );

    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
    };

    m.nameFunctions(this);

};

MochiKit.Iter.__new__();

//
// XXX: Internet Explorer blows
//
if (MochiKit.__export__) {
    reduce = MochiKit.Iter.reduce;
}

MochiKit.Base._exportSymbols(this, MochiKit.Iter);

/***

MochiKit.Color 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito and others.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Color');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
    dojo.require('MochiKit.Style');
}

if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
    JSAN.use("MochiKit.DOM", []);
    JSAN.use("MochiKit.Style", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Color depends on MochiKit.Base";
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Color depends on MochiKit.DOM";
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Color depends on MochiKit.Style";
}

if (typeof(MochiKit.Color) == "undefined") {
    MochiKit.Color = {};
}

MochiKit.Color.NAME = "MochiKit.Color";
MochiKit.Color.VERSION = "1.4";

MochiKit.Color.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};

MochiKit.Color.toString = function () {
    return this.__repr__();
};


/** @id MochiKit.Color.Color */
MochiKit.Color.Color = function (red, green, blue, alpha) {
    if (typeof(alpha) == 'undefined' || alpha === null) {
        alpha = 1.0;
    }
    this.rgb = {
        r: red,
        g: green,
        b: blue,
        a: alpha
    };
};


// Prototype methods

MochiKit.Color.Color.prototype = {

    __class__: MochiKit.Color.Color,

    /** @id MochiKit.Color.Color.prototype.colorWithAlpha */
    colorWithAlpha: function (alpha) {
        var rgb = this.rgb;
        var m = MochiKit.Color;
        return m.Color.fromRGB(rgb.r, rgb.g, rgb.b, alpha);
    },

    /** @id MochiKit.Color.Color.prototype.colorWithHue */
    colorWithHue: function (hue) {
        // get an HSL model, and set the new hue...
        var hsl = this.asHSL();
        hsl.h = hue;
        var m = MochiKit.Color;
        // convert back to RGB...
        return m.Color.fromHSL(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.colorWithSaturation */
    colorWithSaturation: function (saturation) {
        // get an HSL model, and set the new hue...
        var hsl = this.asHSL();
        hsl.s = saturation;
        var m = MochiKit.Color;
        // convert back to RGB...
        return m.Color.fromHSL(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.colorWithLightness */
    colorWithLightness: function (lightness) {
        // get an HSL model, and set the new hue...
        var hsl = this.asHSL();
        hsl.l = lightness;
        var m = MochiKit.Color;
        // convert back to RGB...
        return m.Color.fromHSL(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.darkerColorWithLevel */
    darkerColorWithLevel: function (level) {
        var hsl  = this.asHSL();
        hsl.l = Math.max(hsl.l - level, 0);
        var m = MochiKit.Color;
        return m.Color.fromHSL(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.lighterColorWithLevel */
    lighterColorWithLevel: function (level) {
        var hsl  = this.asHSL();
        hsl.l = Math.min(hsl.l + level, 1);
        var m = MochiKit.Color;
        return m.Color.fromHSL(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.blendedColor */
    blendedColor: function (other, /* optional */ fraction) {
        if (typeof(fraction) == 'undefined' || fraction === null) {
            fraction = 0.5;
        }
        var sf = 1.0 - fraction;
        var s = this.rgb;
        var d = other.rgb;
        var df = fraction;
        return MochiKit.Color.Color.fromRGB(
            (s.r * sf) + (d.r * df),
            (s.g * sf) + (d.g * df),
            (s.b * sf) + (d.b * df),
            (s.a * sf) + (d.a * df)
        );
    },

    /** @id MochiKit.Color.Color.prototype.compareRGB */
    compareRGB: function (other) {
        var a = this.asRGB();
        var b = other.asRGB();
        return MochiKit.Base.compare(
            [a.r, a.g, a.b, a.a],
            [b.r, b.g, b.b, b.a]
        );
    },

    /** @id MochiKit.Color.Color.prototype.isLight */
    isLight: function () {
        return this.asHSL().b > 0.5;
    },

    /** @id MochiKit.Color.Color.prototype.isDark */
    isDark: function () {
        return (!this.isLight());
    },

    /** @id MochiKit.Color.Color.prototype.toHSLString */
    toHSLString: function () {
        var c = this.asHSL();
        var ccc = MochiKit.Color.clampColorComponent;
        var rval = this._hslString;
        if (!rval) {
            var mid = (
                ccc(c.h, 360).toFixed(0)
                + "," + ccc(c.s, 100).toPrecision(4) + "%"
                + "," + ccc(c.l, 100).toPrecision(4) + "%"
            );
            var a = c.a;
            if (a >= 1) {
                a = 1;
                rval = "hsl(" + mid + ")";
            } else {
                if (a <= 0) {
                    a = 0;
                }
                rval = "hsla(" + mid + "," + a + ")";
            }
            this._hslString = rval;
        }
        return rval;
    },

    /** @id MochiKit.Color.Color.prototype.toRGBString */
    toRGBString: function () {
        var c = this.rgb;
        var ccc = MochiKit.Color.clampColorComponent;
        var rval = this._rgbString;
        if (!rval) {
            var mid = (
                ccc(c.r, 255).toFixed(0)
                + "," + ccc(c.g, 255).toFixed(0)
                + "," + ccc(c.b, 255).toFixed(0)
            );
            if (c.a != 1) {
                rval = "rgba(" + mid + "," + c.a + ")";
            } else {
                rval = "rgb(" + mid + ")";
            }
            this._rgbString = rval;
        }
        return rval;
    },

    /** @id MochiKit.Color.Color.prototype.asRGB */
    asRGB: function () {
        return MochiKit.Base.clone(this.rgb);
    },

    /** @id MochiKit.Color.Color.prototype.toHexString */
    toHexString: function () {
        var m = MochiKit.Color;
        var c = this.rgb;
        var ccc = MochiKit.Color.clampColorComponent;
        var rval = this._hexString;
        if (!rval) {
            rval = ("#" +
                m.toColorPart(ccc(c.r, 255)) +
                m.toColorPart(ccc(c.g, 255)) +
                m.toColorPart(ccc(c.b, 255))
            );
            this._hexString = rval;
        }
        return rval;
    },

    /** @id MochiKit.Color.Color.prototype.asHSV */
    asHSV: function () {
        var hsv = this.hsv;
        var c = this.rgb;
        if (typeof(hsv) == 'undefined' || hsv === null) {
            hsv = MochiKit.Color.rgbToHSV(this.rgb);
            this.hsv = hsv;
        }
        return MochiKit.Base.clone(hsv);
    },

    /** @id MochiKit.Color.Color.prototype.asHSL */
    asHSL: function () {
        var hsl = this.hsl;
        var c = this.rgb;
        if (typeof(hsl) == 'undefined' || hsl === null) {
            hsl = MochiKit.Color.rgbToHSL(this.rgb);
            this.hsl = hsl;
        }
        return MochiKit.Base.clone(hsl);
    },

    /** @id MochiKit.Color.Color.prototype.toString */
    toString: function () {
        return this.toRGBString();
    },

    /** @id MochiKit.Color.Color.prototype.repr */
    repr: function () {
        var c = this.rgb;
        var col = [c.r, c.g, c.b, c.a];
        return this.__class__.NAME + "(" + col.join(", ") + ")";
    }

};

// Constructor methods

MochiKit.Base.update(MochiKit.Color.Color, {
    /** @id MochiKit.Color.Color.fromRGB */
    fromRGB: function (red, green, blue, alpha) {
        // designated initializer
        var Color = MochiKit.Color.Color;
        if (arguments.length == 1) {
            var rgb = red;
            red = rgb.r;
            green = rgb.g;
            blue = rgb.b;
            if (typeof(rgb.a) == 'undefined') {
                alpha = undefined;
            } else {
                alpha = rgb.a;
            }
        }
        return new Color(red, green, blue, alpha);
    },

    /** @id MochiKit.Color.Color.fromHSL */
    fromHSL: function (hue, saturation, lightness, alpha) {
        var m = MochiKit.Color;
        return m.Color.fromRGB(m.hslToRGB.apply(m, arguments));
    },

    /** @id MochiKit.Color.Color.fromHSV */
    fromHSV: function (hue, saturation, value, alpha) {
        var m = MochiKit.Color;
        return m.Color.fromRGB(m.hsvToRGB.apply(m, arguments));
    },

    /** @id MochiKit.Color.Color.fromName */
    fromName: function (name) {
        var Color = MochiKit.Color.Color;
        // Opera 9 seems to "quote" named colors(?!)
        if (name.charAt(0) == '"') {
            name = name.substr(1, name.length - 2);
        }
        var htmlColor = Color._namedColors[name.toLowerCase()];
        if (typeof(htmlColor) == 'string') {
            return Color.fromHexString(htmlColor);
        } else if (name == "transparent") {
            return Color.transparentColor();
        }
        return null;
    },

    /** @id MochiKit.Color.Color.fromString */
    fromString: function (colorString) {
        var self = MochiKit.Color.Color;
        var three = colorString.substr(0, 3);
        if (three == "rgb") {
            return self.fromRGBString(colorString);
        } else if (three == "hsl") {
            return self.fromHSLString(colorString);
        } else if (colorString.charAt(0) == "#") {
            return self.fromHexString(colorString);
        }
        return self.fromName(colorString);
    },


    /** @id MochiKit.Color.Color.fromHexString */
    fromHexString: function (hexCode) {
        if (hexCode.charAt(0) == '#') {
            hexCode = hexCode.substring(1);
        }
        var components = [];
        var i, hex;
        if (hexCode.length == 3) {
            for (i = 0; i < 3; i++) {
                hex = hexCode.substr(i, 1);
                components.push(parseInt(hex + hex, 16) / 255.0);
            }
        } else {
            for (i = 0; i < 6; i += 2) {
                hex = hexCode.substr(i, 2);
                components.push(parseInt(hex, 16) / 255.0);
            }
        }
        var Color = MochiKit.Color.Color;
        return Color.fromRGB.apply(Color, components);
    },


    _fromColorString: function (pre, method, scales, colorCode) {
        // parses either HSL or RGB
        if (colorCode.indexOf(pre) === 0) {
            colorCode = colorCode.substring(colorCode.indexOf("(", 3) + 1, colorCode.length - 1);
        }
        var colorChunks = colorCode.split(/\s*,\s*/);
        var colorFloats = [];
        for (var i = 0; i < colorChunks.length; i++) {
            var c = colorChunks[i];
            var val;
            var three = c.substring(c.length - 3);
            if (c.charAt(c.length - 1) == '%') {
                val = 0.01 * parseFloat(c.substring(0, c.length - 1));
            } else if (three == "deg") {
                val = parseFloat(c) / 360.0;
            } else if (three == "rad") {
                val = parseFloat(c) / (Math.PI * 2);
            } else {
                val = scales[i] * parseFloat(c);
            }
            colorFloats.push(val);
        }
        return this[method].apply(this, colorFloats);
    },

    /** @id MochiKit.Color.Color.fromComputedStyle */
    fromComputedStyle: function (elem, style) {
        var d = MochiKit.DOM;
        var cls = MochiKit.Color.Color;
        for (elem = d.getElement(elem); elem; elem = elem.parentNode) {
            var actualColor = MochiKit.Style.getStyle.apply(d, arguments);
            if (!actualColor) {
                continue;
            }
            var color = cls.fromString(actualColor);
            if (!color) {
                break;
            }
            if (color.asRGB().a > 0) {
                return color;
            }
        }
        return null;
    },

    /** @id MochiKit.Color.Color.fromBackground */
    fromBackground: function (elem) {
        var cls = MochiKit.Color.Color;
        return cls.fromComputedStyle(
            elem, "backgroundColor", "background-color") || cls.whiteColor();
    },

    /** @id MochiKit.Color.Color.fromText */
    fromText: function (elem) {
        var cls = MochiKit.Color.Color;
        return cls.fromComputedStyle(
            elem, "color", "color") || cls.blackColor();
    },

    /** @id MochiKit.Color.Color.namedColors */
    namedColors: function () {
        return MochiKit.Base.clone(MochiKit.Color.Color._namedColors);
    }
});


// Module level functions

MochiKit.Base.update(MochiKit.Color, {
    /** @id MochiKit.Color.clampColorComponent */
    clampColorComponent: function (v, scale) {
        v *= scale;
        if (v < 0) {
            return 0;
        } else if (v > scale) {
            return scale;
        } else {
            return v;
        }
    },

    _hslValue: function (n1, n2, hue) {
        if (hue > 6.0) {
            hue -= 6.0;
        } else if (hue < 0.0) {
            hue += 6.0;
        }
        var val;
        if (hue < 1.0) {
            val = n1 + (n2 - n1) * hue;
        } else if (hue < 3.0) {
            val = n2;
        } else if (hue < 4.0) {
            val = n1 + (n2 - n1) * (4.0 - hue);
        } else {
            val = n1;
        }
        return val;
    },

    /** @id MochiKit.Color.hsvToRGB */
    hsvToRGB: function (hue, saturation, value, alpha) {
        if (arguments.length == 1) {
            var hsv = hue;
            hue = hsv.h;
            saturation = hsv.s;
            value = hsv.v;
            alpha = hsv.a;
        }
        var red;
        var green;
        var blue;
        if (saturation === 0) {
            red = 0;
            green = 0;
            blue = 0;
        } else {
            var i = Math.floor(hue * 6);
            var f = (hue * 6) - i;
            var p = value * (1 - saturation);
            var q = value * (1 - (saturation * f));
            var t = value * (1 - (saturation * (1 - f)));
            switch (i) {
                case 1: red = q; green = value; blue = p; break;
                case 2: red = p; green = value; blue = t; break;
                case 3: red = p; green = q; blue = value; break;
                case 4: red = t; green = p; blue = value; break;
                case 5: red = value; green = p; blue = q; break;
                case 6: // fall through
                case 0: red = value; green = t; blue = p; break;
            }
        }
        return {
            r: red,
            g: green,
            b: blue,
            a: alpha
        };
    },

    /** @id MochiKit.Color.hslToRGB */
    hslToRGB: function (hue, saturation, lightness, alpha) {
        if (arguments.length == 1) {
            var hsl = hue;
            hue = hsl.h;
            saturation = hsl.s;
            lightness = hsl.l;
            alpha = hsl.a;
        }
        var red;
        var green;
        var blue;
        if (saturation === 0) {
            red = lightness;
            green = lightness;
            blue = lightness;
        } else {
            var m2;
            if (lightness <= 0.5) {
                m2 = lightness * (1.0 + saturation);
            } else {
                m2 = lightness + saturation - (lightness * saturation);
            }
            var m1 = (2.0 * lightness) - m2;
            var f = MochiKit.Color._hslValue;
            var h6 = hue * 6.0;
            red = f(m1, m2, h6 + 2);
            green = f(m1, m2, h6);
            blue = f(m1, m2, h6 - 2);
        }
        return {
            r: red,
            g: green,
            b: blue,
            a: alpha
        };
    },

    /** @id MochiKit.Color.rgbToHSV */
    rgbToHSV: function (red, green, blue, alpha) {
        if (arguments.length == 1) {
            var rgb = red;
            red = rgb.r;
            green = rgb.g;
            blue = rgb.b;
            alpha = rgb.a;
        }
        var max = Math.max(Math.max(red, green), blue);
        var min = Math.min(Math.min(red, green), blue);
        var hue;
        var saturation;
        var value = max;
        if (min == max) {
            hue = 0;
            saturation = 0;
        } else {
            var delta = (max - min);
            saturation = delta / max;

            if (red == max) {
                hue = (green - blue) / delta;
            } else if (green == max) {
                hue = 2 + ((blue - red) / delta);
            } else {
                hue = 4 + ((red - green) / delta);
            }
            hue /= 6;
            if (hue < 0) {
                hue += 1;
            }
            if (hue > 1) {
                hue -= 1;
            }
        }
        return {
            h: hue,
            s: saturation,
            v: value,
            a: alpha
        };
    },

    /** @id MochiKit.Color.rgbToHSL */
    rgbToHSL: function (red, green, blue, alpha) {
        if (arguments.length == 1) {
            var rgb = red;
            red = rgb.r;
            green = rgb.g;
            blue = rgb.b;
            alpha = rgb.a;
        }
        var max = Math.max(red, Math.max(green, blue));
        var min = Math.min(red, Math.min(green, blue));
        var hue;
        var saturation;
        var lightness = (max + min) / 2.0;
        var delta = max - min;
        if (delta === 0) {
            hue = 0;
            saturation = 0;
        } else {
            if (lightness <= 0.5) {
                saturation = delta / (max + min);
            } else {
                saturation = delta / (2 - max - min);
            }
            if (red == max) {
                hue = (green - blue) / delta;
            } else if (green == max) {
                hue = 2 + ((blue - red) / delta);
            } else {
                hue = 4 + ((red - green) / delta);
            }
            hue /= 6;
            if (hue < 0) {
                hue += 1;
            }
            if (hue > 1) {
                hue -= 1;
            }

        }
        return {
            h: hue,
            s: saturation,
            l: lightness,
            a: alpha
        };
    },

    /** @id MochiKit.Color.toColorPart */
    toColorPart: function (num) {
        num = Math.round(num);
        var digits = num.toString(16);
        if (num < 16) {
            return '0' + digits;
        }
        return digits;
    },

    __new__: function () {
        var m = MochiKit.Base;
        /** @id MochiKit.Color.fromRGBString */
        this.Color.fromRGBString = m.bind(
            this.Color._fromColorString, this.Color, "rgb", "fromRGB",
            [1.0/255.0, 1.0/255.0, 1.0/255.0, 1]
        );
        /** @id MochiKit.Color.fromHSLString */
        this.Color.fromHSLString = m.bind(
            this.Color._fromColorString, this.Color, "hsl", "fromHSL",
            [1.0/360.0, 0.01, 0.01, 1]
        );

        var third = 1.0 / 3.0;
        /** @id MochiKit.Color.colors */
        var colors = {
            // NSColor colors plus transparent
            /** @id MochiKit.Color.blackColor */
            black: [0, 0, 0],
            /** @id MochiKit.Color.blueColor */
            blue: [0, 0, 1],
            /** @id MochiKit.Color.brownColor */
            brown: [0.6, 0.4, 0.2],
            /** @id MochiKit.Color.cyanColor */
            cyan: [0, 1, 1],
            /** @id MochiKit.Color.darkGrayColor */
            darkGray: [third, third, third],
            /** @id MochiKit.Color.grayColor */
            gray: [0.5, 0.5, 0.5],
            /** @id MochiKit.Color.greenColor */
            green: [0, 1, 0],
            /** @id MochiKit.Color.lightGrayColor */
            lightGray: [2 * third, 2 * third, 2 * third],
            /** @id MochiKit.Color.magentaColor */
            magenta: [1, 0, 1],
            /** @id MochiKit.Color.orangeColor */
            orange: [1, 0.5, 0],
            /** @id MochiKit.Color.purpleColor */
            purple: [0.5, 0, 0.5],
            /** @id MochiKit.Color.redColor */
            red: [1, 0, 0],
            /** @id MochiKit.Color.transparentColor */
            transparent: [0, 0, 0, 0],
            /** @id MochiKit.Color.whiteColor */
            white: [1, 1, 1],
            /** @id MochiKit.Color.yellowColor */
            yellow: [1, 1, 0]
        };

        var makeColor = function (name, r, g, b, a) {
            var rval = this.fromRGB(r, g, b, a);
            this[name] = function () { return rval; };
            return rval;
        };

        for (var k in colors) {
            var name = k + "Color";
            var bindArgs = m.concat(
                [makeColor, this.Color, name],
                colors[k]
            );
            this.Color[name] = m.bind.apply(null, bindArgs);
        }

        var isColor = function () {
            for (var i = 0; i < arguments.length; i++) {
                if (!(arguments[i] instanceof Color)) {
                    return false;
                }
            }
            return true;
        };

        var compareColor = function (a, b) {
            return a.compareRGB(b);
        };

        m.nameFunctions(this);

        m.registerComparator(this.Color.NAME, isColor, compareColor);

        this.EXPORT_TAGS = {
            ":common": this.EXPORT,
            ":all": m.concat(this.EXPORT, this.EXPORT_OK)
        };

    }
});

MochiKit.Color.EXPORT = [
    "Color"
];

MochiKit.Color.EXPORT_OK = [
    "clampColorComponent",
    "rgbToHSL",
    "hslToRGB",
    "rgbToHSV",
    "hsvToRGB",
    "toColorPart"
];

MochiKit.Color.__new__();

MochiKit.Base._exportSymbols(this, MochiKit.Color);

// Full table of css3 X11 colors <http://www.w3.org/TR/css3-color/#X11COLORS>

MochiKit.Color.Color._namedColors = {
    aliceblue: "#f0f8ff",
    antiquewhite: "#faebd7",
    aqua: "#00ffff",
    aquamarine: "#7fffd4",
    azure: "#f0ffff",
    beige: "#f5f5dc",
    bisque: "#ffe4c4",
    black: "#000000",
    blanchedalmond: "#ffebcd",
    blue: "#0000ff",
    blueviolet: "#8a2be2",
    brown: "#a52a2a",
    burlywood: "#deb887",
    cadetblue: "#5f9ea0",
    chartreuse: "#7fff00",
    chocolate: "#d2691e",
    coral: "#ff7f50",
    cornflowerblue: "#6495ed",
    cornsilk: "#fff8dc",
    crimson: "#dc143c",
    cyan: "#00ffff",
    darkblue: "#00008b",
    darkcyan: "#008b8b",
    darkgoldenrod: "#b8860b",
    darkgray: "#a9a9a9",
    darkgreen: "#006400",
    darkgrey: "#a9a9a9",
    darkkhaki: "#bdb76b",
    darkmagenta: "#8b008b",
    darkolivegreen: "#556b2f",
    darkorange: "#ff8c00",
    darkorchid: "#9932cc",
    darkred: "#8b0000",
    darksalmon: "#e9967a",
    darkseagreen: "#8fbc8f",
    darkslateblue: "#483d8b",
    darkslategray: "#2f4f4f",
    darkslategrey: "#2f4f4f",
    darkturquoise: "#00ced1",
    darkviolet: "#9400d3",
    deeppink: "#ff1493",
    deepskyblue: "#00bfff",
    dimgray: "#696969",
    dimgrey: "#696969",
    dodgerblue: "#1e90ff",
    firebrick: "#b22222",
    floralwhite: "#fffaf0",
    forestgreen: "#228b22",
    fuchsia: "#ff00ff",
    gainsboro: "#dcdcdc",
    ghostwhite: "#f8f8ff",
    gold: "#ffd700",
    goldenrod: "#daa520",
    gray: "#808080",
    green: "#008000",
    greenyellow: "#adff2f",
    grey: "#808080",
    honeydew: "#f0fff0",
    hotpink: "#ff69b4",
    indianred: "#cd5c5c",
    indigo: "#4b0082",
    ivory: "#fffff0",
    khaki: "#f0e68c",
    lavender: "#e6e6fa",
    lavenderblush: "#fff0f5",
    lawngreen: "#7cfc00",
    lemonchiffon: "#fffacd",
    lightblue: "#add8e6",
    lightcoral: "#f08080",
    lightcyan: "#e0ffff",
    lightgoldenrodyellow: "#fafad2",
    lightgray: "#d3d3d3",
    lightgreen: "#90ee90",
    lightgrey: "#d3d3d3",
    lightpink: "#ffb6c1",
    lightsalmon: "#ffa07a",
    lightseagreen: "#20b2aa",
    lightskyblue: "#87cefa",
    lightslategray: "#778899",
    lightslategrey: "#778899",
    lightsteelblue: "#b0c4de",
    lightyellow: "#ffffe0",
    lime: "#00ff00",
    limegreen: "#32cd32",
    linen: "#faf0e6",
    magenta: "#ff00ff",
    maroon: "#800000",
    mediumaquamarine: "#66cdaa",
    mediumblue: "#0000cd",
    mediumorchid: "#ba55d3",
    mediumpurple: "#9370db",
    mediumseagreen: "#3cb371",
    mediumslateblue: "#7b68ee",
    mediumspringgreen: "#00fa9a",
    mediumturquoise: "#48d1cc",
    mediumvioletred: "#c71585",
    midnightblue: "#191970",
    mintcream: "#f5fffa",
    mistyrose: "#ffe4e1",
    moccasin: "#ffe4b5",
    navajowhite: "#ffdead",
    navy: "#000080",
    oldlace: "#fdf5e6",
    olive: "#808000",
    olivedrab: "#6b8e23",
    orange: "#ffa500",
    orangered: "#ff4500",
    orchid: "#da70d6",
    palegoldenrod: "#eee8aa",
    palegreen: "#98fb98",
    paleturquoise: "#afeeee",
    palevioletred: "#db7093",
    papayawhip: "#ffefd5",
    peachpuff: "#ffdab9",
    peru: "#cd853f",
    pink: "#ffc0cb",
    plum: "#dda0dd",
    powderblue: "#b0e0e6",
    purple: "#800080",
    red: "#ff0000",
    rosybrown: "#bc8f8f",
    royalblue: "#4169e1",
    saddlebrown: "#8b4513",
    salmon: "#fa8072",
    sandybrown: "#f4a460",
    seagreen: "#2e8b57",
    seashell: "#fff5ee",
    sienna: "#a0522d",
    silver: "#c0c0c0",
    skyblue: "#87ceeb",
    slateblue: "#6a5acd",
    slategray: "#708090",
    slategrey: "#708090",
    snow: "#fffafa",
    springgreen: "#00ff7f",
    steelblue: "#4682b4",
    tan: "#d2b48c",
    teal: "#008080",
    thistle: "#d8bfd8",
    tomato: "#ff6347",
    turquoise: "#40e0d0",
    violet: "#ee82ee",
    wheat: "#f5deb3",
    white: "#ffffff",
    whitesmoke: "#f5f5f5",
    yellow: "#ffff00",
    yellowgreen: "#9acd32"
};

/***

MochiKit.Visual 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito and others.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.Visual');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
    dojo.require('MochiKit.Style');
    dojo.require('MochiKit.Color');
    dojo.require('MochiKit.Position');
}

if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
    JSAN.use("MochiKit.DOM", []);
    JSAN.use("MochiKit.Style", []);
    JSAN.use("MochiKit.Color", []);
    JSAN.use("MochiKit.Position", []);
}

try {
    if (typeof(MochiKit.Base) === 'undefined' ||
        typeof(MochiKit.DOM) === 'undefined' ||
        typeof(MochiKit.Style) === 'undefined' ||
        typeof(MochiKit.Position) === 'undefined' ||
        typeof(MochiKit.Color) === 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Visual depends on MochiKit.Base, MochiKit.DOM, MochiKit.Style, MochiKit.Position and MochiKit.Color!";
}

if (typeof(MochiKit.Visual) == "undefined") {
    MochiKit.Visual = {};
}

MochiKit.Visual.NAME = "MochiKit.Visual";
MochiKit.Visual.VERSION = "1.4";

MochiKit.Visual.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};

MochiKit.Visual.toString = function () {
    return this.__repr__();
};

MochiKit.Visual._RoundCorners = function (e, options) {
    e = MochiKit.DOM.getElement(e);
    this._setOptions(options);
    if (this.options.__unstable__wrapElement) {
        e = this._doWrap(e);
    }

    var color = this.options.color;
    var C = MochiKit.Color.Color;
    if (this.options.color === "fromElement") {
        color = C.fromBackground(e);
    } else if (!(color instanceof C)) {
        color = C.fromString(color);
    }
    this.isTransparent = (color.asRGB().a <= 0);

    var bgColor = this.options.bgColor;
    if (this.options.bgColor === "fromParent") {
        bgColor = C.fromBackground(e.offsetParent);
    } else if (!(bgColor instanceof C)) {
        bgColor = C.fromString(bgColor);
    }

    this._roundCornersImpl(e, color, bgColor);
};

MochiKit.Visual._RoundCorners.prototype = {
    _doWrap: function (e) {
        var parent = e.parentNode;
        var doc = MochiKit.DOM.currentDocument();
        if (typeof(doc.defaultView) === "undefined"
            || doc.defaultView === null) {
            return e;
        }
        var style = doc.defaultView.getComputedStyle(e, null);
        if (typeof(style) === "undefined" || style === null) {
            return e;
        }
        var wrapper = MochiKit.DOM.DIV({"style": {
            display: "block",
            // convert padding to margin
            marginTop: style.getPropertyValue("padding-top"),
            marginRight: style.getPropertyValue("padding-right"),
            marginBottom: style.getPropertyValue("padding-bottom"),
            marginLeft: style.getPropertyValue("padding-left"),
            // remove padding so the rounding looks right
            padding: "0px"
            /*
            paddingRight: "0px",
            paddingLeft: "0px"
            */
        }});
        wrapper.innerHTML = e.innerHTML;
        e.innerHTML = "";
        e.appendChild(wrapper);
        return e;
    },

    _roundCornersImpl: function (e, color, bgColor) {
        if (this.options.border) {
            this._renderBorder(e, bgColor);
        }
        if (this._isTopRounded()) {
            this._roundTopCorners(e, color, bgColor);
        }
        if (this._isBottomRounded()) {
            this._roundBottomCorners(e, color, bgColor);
        }
    },

    _renderBorder: function (el, bgColor) {
        var borderValue = "1px solid " + this._borderColor(bgColor);
        var borderL = "border-left: "  + borderValue;
        var borderR = "border-right: " + borderValue;
        var style = "style='" + borderL + ";" + borderR +  "'";
        el.innerHTML = "<div " + style + ">" + el.innerHTML + "</div>";
    },

    _roundTopCorners: function (el, color, bgColor) {
        var corner = this._createCorner(bgColor);
        for (var i = 0; i < this.options.numSlices; i++) {
            corner.appendChild(
                this._createCornerSlice(color, bgColor, i, "top")
            );
        }
        el.style.paddingTop = 0;
        el.insertBefore(corner, el.firstChild);
    },

    _roundBottomCorners: function (el, color, bgColor) {
        var corner = this._createCorner(bgColor);
        for (var i = (this.options.numSlices - 1); i >= 0; i--) {
            corner.appendChild(
                this._createCornerSlice(color, bgColor, i, "bottom")
            );
        }
        el.style.paddingBottom = 0;
        el.appendChild(corner);
    },

    _createCorner: function (bgColor) {
        var dom = MochiKit.DOM;
        return dom.DIV({style: {backgroundColor: bgColor.toString()}});
    },

    _createCornerSlice: function (color, bgColor, n, position) {
        var slice = MochiKit.DOM.SPAN();

        var inStyle = slice.style;
        inStyle.backgroundColor = color.toString();
        inStyle.display = "block";
        inStyle.height = "1px";
        inStyle.overflow = "hidden";
        inStyle.fontSize = "1px";

        var borderColor = this._borderColor(color, bgColor);
        if (this.options.border && n === 0) {
            inStyle.borderTopStyle = "solid";
            inStyle.borderTopWidth = "1px";
            inStyle.borderLeftWidth = "0px";
            inStyle.borderRightWidth = "0px";
            inStyle.borderBottomWidth = "0px";
            // assumes css compliant box model
            inStyle.height = "0px";
            inStyle.borderColor = borderColor.toString();
        } else if (borderColor) {
            inStyle.borderColor = borderColor.toString();
            inStyle.borderStyle = "solid";
            inStyle.borderWidth = "0px 1px";
        }

        if (!this.options.compact && (n == (this.options.numSlices - 1))) {
            inStyle.height = "2px";
        }

        this._setMargin(slice, n, position);
        this._setBorder(slice, n, position);

        return slice;
    },

    _setOptions: function (options) {
        this.options = {
            corners: "all",
            color: "fromElement",
            bgColor: "fromParent",
            blend: true,
            border: false,
            compact: false,
            __unstable__wrapElement: false
        };
        MochiKit.Base.update(this.options, options);

        this.options.numSlices = (this.options.compact ? 2 : 4);
    },

    _whichSideTop: function () {
        var corners = this.options.corners;
        if (this._hasString(corners, "all", "top")) {
            return "";
        }

        var has_tl = (corners.indexOf("tl") != -1);
        var has_tr = (corners.indexOf("tr") != -1);
        if (has_tl && has_tr) {
            return "";
        }
        if (has_tl) {
            return "left";
        }
        if (has_tr) {
            return "right";
        }
        return "";
    },

    _whichSideBottom: function () {
        var corners = this.options.corners;
        if (this._hasString(corners, "all", "bottom")) {
            return "";
        }

        var has_bl = (corners.indexOf('bl') != -1);
        var has_br = (corners.indexOf('br') != -1);
        if (has_bl && has_br) {
            return "";
        }
        if (has_bl) {
            return "left";
        }
        if (has_br) {
            return "right";
        }
        return "";
    },

    _borderColor: function (color, bgColor) {
        if (color == "transparent") {
            return bgColor;
        } else if (this.options.border) {
            return this.options.border;
        } else if (this.options.blend) {
            return bgColor.blendedColor(color);
        }
        return "";
    },


    _setMargin: function (el, n, corners) {
        var marginSize = this._marginSize(n) + "px";
        var whichSide = (
            corners == "top" ? this._whichSideTop() : this._whichSideBottom()
        );
        var style = el.style;

        if (whichSide == "left") {
            style.marginLeft = marginSize;
            style.marginRight = "0px";
        } else if (whichSide == "right") {
            style.marginRight = marginSize;
            style.marginLeft = "0px";
        } else {
            style.marginLeft = marginSize;
            style.marginRight = marginSize;
        }
    },

    _setBorder: function (el, n, corners) {
        var borderSize = this._borderSize(n) + "px";
        var whichSide = (
            corners == "top" ? this._whichSideTop() : this._whichSideBottom()
        );

        var style = el.style;
        if (whichSide == "left") {
            style.borderLeftWidth = borderSize;
            style.borderRightWidth = "0px";
        } else if (whichSide == "right") {
            style.borderRightWidth = borderSize;
            style.borderLeftWidth = "0px";
        } else {
            style.borderLeftWidth = borderSize;
            style.borderRightWidth = borderSize;
        }
    },

    _marginSize: function (n) {
        if (this.isTransparent) {
            return 0;
        }

        var o = this.options;
        if (o.compact && o.blend) {
            var smBlendedMarginSizes = [1, 0];
            return smBlendedMarginSizes[n];
        } else if (o.compact) {
            var compactMarginSizes = [2, 1];
            return compactMarginSizes[n];
        } else if (o.blend) {
            var blendedMarginSizes = [3, 2, 1, 0];
            return blendedMarginSizes[n];
        } else {
            var marginSizes = [5, 3, 2, 1];
            return marginSizes[n];
        }
    },

    _borderSize: function (n) {
        var o = this.options;
        var borderSizes;
        if (o.compact && (o.blend || this.isTransparent)) {
            return 1;
        } else if (o.compact) {
            borderSizes = [1, 0];
        } else if (o.blend) {
            borderSizes = [2, 1, 1, 1];
        } else if (o.border) {
            borderSizes = [0, 2, 0, 0];
        } else if (this.isTransparent) {
            borderSizes = [5, 3, 2, 1];
        } else {
            return 0;
        }
        return borderSizes[n];
    },

    _hasString: function (str) {
        for (var i = 1; i< arguments.length; i++) {
            if (str.indexOf(arguments[i]) != -1) {
                return true;
            }
        }
        return false;
    },

    _isTopRounded: function () {
        return this._hasString(this.options.corners,
            "all", "top", "tl", "tr"
        );
    },

    _isBottomRounded: function () {
        return this._hasString(this.options.corners,
            "all", "bottom", "bl", "br"
        );
    },

    _hasSingleTextChild: function (el) {
        return (el.childNodes.length == 1 && el.childNodes[0].nodeType == 3);
    }
};

/** @id MochiKit.Visual.roundElement */
MochiKit.Visual.roundElement = function (e, options) {
    new MochiKit.Visual._RoundCorners(e, options);
};

/** @id MochiKit.Visual.roundClass */
MochiKit.Visual.roundClass = function (tagName, className, options) {
    var elements = MochiKit.DOM.getElementsByTagAndClassName(
        tagName, className
    );
    for (var i = 0; i < elements.length; i++) {
        MochiKit.Visual.roundElement(elements[i], options);
    }
};

/** @id MochiKit.Visual.tagifyText */
MochiKit.Visual.tagifyText = function (element, /* optional */tagifyStyle) {
    /***

    Change a node text to character in tags.

    @param tagifyStyle: the style to apply to character nodes, default to
    'position: relative'.

    ***/
    tagifyStyle = tagifyStyle || 'position:relative';
    if (/MSIE/.test(navigator.userAgent)) {
        tagifyStyle += ';zoom:1';
    }
    element = MochiKit.DOM.getElement(element);
    var ma = MochiKit.Base.map;
    ma(function (child) {
        if (child.nodeType == 3) {
            ma(function (character) {
                element.insertBefore(
                    MochiKit.DOM.SPAN({style: tagifyStyle},
                        character == ' ' ? String.fromCharCode(160) : character), child);
            }, child.nodeValue.split(''));
            MochiKit.DOM.removeElement(child);
        }
    }, element.childNodes);
};

/** @id MochiKit.Visual.forceRerendering */
MochiKit.Visual.forceRerendering = function (element) {
    try {
        element = MochiKit.DOM.getElement(element);
        var n = document.createTextNode(' ');
        element.appendChild(n);
        element.removeChild(n);
    } catch(e) {
    }
};

/** @id MochiKit.Visual.multiple */
MochiKit.Visual.multiple = function (elements, effect, /* optional */options) {
    /***

    Launch the same effect subsequently on given elements.

    ***/
    options = MochiKit.Base.update({
        speed: 0.1, delay: 0.0
    }, options || {});
    var masterDelay = options.delay;
    var index = 0;
    MochiKit.Base.map(function (innerelement) {
        options.delay = index * options.speed + masterDelay;
        new effect(innerelement, options);
        index += 1;
    }, elements);
};

MochiKit.Visual.PAIRS = {
    'slide': ['slideDown', 'slideUp'],
    'blind': ['blindDown', 'blindUp'],
    'appear': ['appear', 'fade'],
    'size': ['grow', 'shrink']
};

/** @id MochiKit.Visual.toggle */
MochiKit.Visual.toggle = function (element, /* optional */effect, /* optional */options) {
    /***

    Toggle an item between two state depending of its visibility, making
    a effect between these states. Default  effect is 'appear', can be
    'slide' or 'blind'.

    ***/
    element = MochiKit.DOM.getElement(element);
    effect = (effect || 'appear').toLowerCase();
    options = MochiKit.Base.update({
        queue: {position: 'end', scope: (element.id || 'global'), limit: 1}
    }, options || {});
    var v = MochiKit.Visual;
    v[element.style.display != 'none' ?
      v.PAIRS[effect][1] : v.PAIRS[effect][0]](element, options);
};

/***

Transitions: define functions calculating variations depending of a position.

***/

MochiKit.Visual.Transitions = {};

/** @id MochiKit.Visual.Transitions.linear */
MochiKit.Visual.Transitions.linear = function (pos) {
    return pos;
};

/** @id MochiKit.Visual.Transitions.sinoidal */
MochiKit.Visual.Transitions.sinoidal = function (pos) {
    return (-Math.cos(pos*Math.PI)/2) + 0.5;
};

/** @id MochiKit.Visual.Transitions.reverse */
MochiKit.Visual.Transitions.reverse = function (pos) {
    return 1 - pos;
};

/** @id MochiKit.Visual.Transitions.flicker */
MochiKit.Visual.Transitions.flicker = function (pos) {
    return ((-Math.cos(pos*Math.PI)/4) + 0.75) + Math.random()/4;
};

/** @id MochiKit.Visual.Transitions.wobble */
MochiKit.Visual.Transitions.wobble = function (pos) {
    return (-Math.cos(pos*Math.PI*(9*pos))/2) + 0.5;
};

/** @id MochiKit.Visual.Transitions.pulse */
MochiKit.Visual.Transitions.pulse = function (pos) {
    return (Math.floor(pos*10) % 2 === 0 ?
        (pos*10 - Math.floor(pos*10)) : 1 - (pos*10 - Math.floor(pos*10)));
};

/** @id MochiKit.Visual.Transitions.none */
MochiKit.Visual.Transitions.none = function (pos) {
    return 0;
};

/** @id MochiKit.Visual.Transitions.full */
MochiKit.Visual.Transitions.full = function (pos) {
    return 1;
};

/***

Core effects

***/

MochiKit.Visual.ScopedQueue = function () {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls();
    }
    this.__init__();
};

MochiKit.Base.update(MochiKit.Visual.ScopedQueue.prototype, {
    __init__: function () {
        this.effects = [];
        this.interval = null;
    },

    /** @id MochiKit.Visual.ScopedQueue.prototype.add */
    add: function (effect) {
        var timestamp = new Date().getTime();

        var position = (typeof(effect.options.queue) == 'string') ?
            effect.options.queue : effect.options.queue.position;

        var ma = MochiKit.Base.map;
        switch (position) {
            case 'front':
                // move unstarted effects after this effect
                ma(function (e) {
                    if (e.state == 'idle') {
                        e.startOn += effect.finishOn;
                        e.finishOn += effect.finishOn;
                    }
                }, this.effects);
                break;
            case 'end':
                var finish;
                // start effect after last queued effect has finished
                ma(function (e) {
                    var i = e.finishOn;
                    if (i >= (finish || i)) {
                        finish = i;
                    }
                }, this.effects);
                timestamp = finish || timestamp;
                break;
            case 'break':
                ma(function (e) {
                    e.finalize();
                }, this.effects);
                break;
        }

        effect.startOn += timestamp;
        effect.finishOn += timestamp;
        if (!effect.options.queue.limit ||
            this.effects.length < effect.options.queue.limit) {
            this.effects.push(effect);
        }

        if (!this.interval) {
            this.interval = this.startLoop(MochiKit.Base.bind(this.loop, this),
                                        40);
        }
    },

    /** @id MochiKit.Visual.ScopedQueue.prototype.startLoop */
    startLoop: function (func, interval) {
        return setInterval(func, interval);
    },

    /** @id MochiKit.Visual.ScopedQueue.prototype.remove */
    remove: function (effect) {
        this.effects = MochiKit.Base.filter(function (e) {
            return e != effect;
        }, this.effects);
        if (!this.effects.length) {
            this.stopLoop(this.interval);
            this.interval = null;
        }
    },

    /** @id MochiKit.Visual.ScopedQueue.prototype.stopLoop */
    stopLoop: function (interval) {
        clearInterval(interval);
    },

    /** @id MochiKit.Visual.ScopedQueue.prototype.loop */
    loop: function () {
        var timePos = new Date().getTime();
        MochiKit.Base.map(function (effect) {
            effect.loop(timePos);
        }, this.effects);
    }
});

MochiKit.Visual.Queues = {
    instances: {},

    get: function (queueName) {
        if (typeof(queueName) != 'string') {
            return queueName;
        }

        if (!this.instances[queueName]) {
            this.instances[queueName] = new MochiKit.Visual.ScopedQueue();
        }
        return this.instances[queueName];
    }
};

MochiKit.Visual.Queue = MochiKit.Visual.Queues.get('global');

MochiKit.Visual.DefaultOptions = {
    transition: MochiKit.Visual.Transitions.sinoidal,
    duration: 1.0,  // seconds
    fps: 25.0,  // max. 25fps due to MochiKit.Visual.Queue implementation
    sync: false,  // true for combining
    from: 0.0,
    to: 1.0,
    delay: 0.0,
    queue: 'parallel'
};

MochiKit.Visual.Base = function () {};

MochiKit.Visual.Base.prototype = {
    /***

    Basic class for all Effects. Define a looping mechanism called for each step
    of an effect. Don't instantiate it, only subclass it.

    ***/

    __class__ : MochiKit.Visual.Base,

    /** @id MochiKit.Visual.Base.prototype.start */
    start: function (options) {
        var v = MochiKit.Visual;
        this.options = MochiKit.Base.setdefault(options || {},
                                                v.DefaultOptions);
        this.currentFrame = 0;
        this.state = 'idle';
        this.startOn = this.options.delay*1000;
        this.finishOn = this.startOn + (this.options.duration*1000);
        this.event('beforeStart');
        if (!this.options.sync) {
            v.Queues.get(typeof(this.options.queue) == 'string' ?
                'global' : this.options.queue.scope).add(this);
        }
    },

    /** @id MochiKit.Visual.Base.prototype.loop */
    loop: function (timePos) {
        if (timePos >= this.startOn) {
            if (timePos >= this.finishOn) {
                return this.finalize();
            }
            var pos = (timePos - this.startOn) / (this.finishOn - this.startOn);
            var frame =
                Math.round(pos * this.options.fps * this.options.duration);
            if (frame > this.currentFrame) {
                this.render(pos);
                this.currentFrame = frame;
            }
        }
    },

    /** @id MochiKit.Visual.Base.prototype.render */
    render: function (pos) {
        if (this.state == 'idle') {
            this.state = 'running';
            this.event('beforeSetup');
            this.setup();
            this.event('afterSetup');
        }
        if (this.state == 'running') {
            if (this.options.transition) {
                pos = this.options.transition(pos);
            }
            pos *= (this.options.to - this.options.from);
            pos += this.options.from;
            this.event('beforeUpdate');
            this.update(pos);
            this.event('afterUpdate');
        }
    },

    /** @id MochiKit.Visual.Base.prototype.cancel */
    cancel: function () {
        if (!this.options.sync) {
            MochiKit.Visual.Queues.get(typeof(this.options.queue) == 'string' ?
                'global' : this.options.queue.scope).remove(this);
        }
        this.state = 'finished';
    },

    /** @id MochiKit.Visual.Base.prototype.finalize */
    finalize: function () {
        this.render(1.0);
        this.cancel();
        this.event('beforeFinish');
        this.finish();
        this.event('afterFinish');
    },

    setup: function () {
    },

    finish: function () {
    },

    update: function (position) {
    },

    /** @id MochiKit.Visual.Base.prototype.event */
    event: function (eventName) {
        if (this.options[eventName + 'Internal']) {
            this.options[eventName + 'Internal'](this);
        }
        if (this.options[eventName]) {
            this.options[eventName](this);
        }
    },

    /** @id MochiKit.Visual.Base.prototype.repr */
    repr: function () {
        return '[' + this.__class__.NAME + ', options:' +
               MochiKit.Base.repr(this.options) + ']';
    }
};

    /** @id MochiKit.Visual.Parallel */
MochiKit.Visual.Parallel = function (effects, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(effects, options);
    }

    this.__init__(effects, options);
};

MochiKit.Visual.Parallel.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Parallel.prototype, {
    /***

    Run multiple effects at the same time.

    ***/
    __init__: function (effects, options) {
        this.effects = effects || [];
        this.start(options);
    },

    /** @id MochiKit.Visual.Parallel.prototype.update */
    update: function (position) {
        MochiKit.Base.map(function (effect) {
            effect.render(position);
        }, this.effects);
    },

    /** @id MochiKit.Visual.Parallel.prototype.finish */
    finish: function () {
        MochiKit.Base.map(function (effect) {
            effect.finalize();
        }, this.effects);
    }
});

/** @id MochiKit.Visual.Opacity */
MochiKit.Visual.Opacity = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.Visual.Opacity.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Opacity.prototype, {
    /***

    Change the opacity of an element.

    @param options: 'from' and 'to' change the starting and ending opacities.
    Must be between 0.0 and 1.0. Default to current opacity and 1.0.

    ***/
    __init__: function (element, /* optional */options) {
        var b = MochiKit.Base;
        var s = MochiKit.Style;
        this.element = MochiKit.DOM.getElement(element);
        // make this work on IE on elements without 'layout'
        if (this.element.currentStyle &&
            (!this.element.currentStyle.hasLayout)) {
            s.setStyle(this.element, {zoom: 1});
        }
        options = b.update({
            from: s.getStyle(this.element, 'opacity') || 0.0,
            to: 1.0
        }, options || {});
        this.start(options);
    },

    /** @id MochiKit.Visual.Opacity.prototype.update */
    update: function (position) {
        MochiKit.Style.setStyle(this.element, {'opacity': position});
    }
});

/**  @id MochiKit.Visual.Move.prototype */
MochiKit.Visual.Move = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.Visual.Move.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Move.prototype, {
    /***

    Move an element between its current position to a defined position

    @param options: 'x' and 'y' for final positions, default to 0, 0.

    ***/
    __init__: function (element, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element);
        options = MochiKit.Base.update({
            x: 0,
            y: 0,
            mode: 'relative'
        }, options || {});
        this.start(options);
    },

    /** @id MochiKit.Visual.Move.prototype.setup */
    setup: function () {
        // Bug in Opera: Opera returns the 'real' position of a static element
        // or relative element that does not have top/left explicitly set.
        // ==> Always set top and left for position relative elements in your
        // stylesheets (to 0 if you do not need them)
        MochiKit.DOM.makePositioned(this.element);

        var s = this.element.style;
        var originalVisibility = s.visibility;
        var originalDisplay = s.display;
        if (originalDisplay == 'none') {
            s.visibility = 'hidden';
            s.display = '';
        }

        this.originalLeft = parseFloat(MochiKit.Style.getStyle(this.element, 'left') || '0');
        this.originalTop = parseFloat(MochiKit.Style.getStyle(this.element, 'top') || '0');

        if (this.options.mode == 'absolute') {
            // absolute movement, so we need to calc deltaX and deltaY
            this.options.x -= this.originalLeft;
            this.options.y -= this.originalTop;
        }
        if (originalDisplay == 'none') {
            s.visibility = originalVisibility;
            s.display = originalDisplay;
        }
    },

    /** @id MochiKit.Visual.Move.prototype.update */
    update: function (position) {
        MochiKit.Style.setStyle(this.element, {
            left: Math.round(this.options.x * position + this.originalLeft) + 'px',
            top: Math.round(this.options.y * position + this.originalTop) + 'px'
        });
    }
});

/** @id MochiKit.Visual.Scale */
MochiKit.Visual.Scale = function (element, percent, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, percent, options);
    }
    this.__init__(element, percent, options);
};

MochiKit.Visual.Scale.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Scale.prototype, {
    /***

    Change the size of an element.

    @param percent: final_size = percent*original_size

    @param options: several options changing scale behaviour

    ***/
    __init__: function (element, percent, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element);
        options = MochiKit.Base.update({
            scaleX: true,
            scaleY: true,
            scaleContent: true,
            scaleFromCenter: false,
            scaleMode: 'box',  // 'box' or 'contents' or {} with provided values
            scaleFrom: 100.0,
            scaleTo: percent
        }, options || {});
        this.start(options);
    },

    /** @id MochiKit.Visual.Scale.prototype.setup */
    setup: function () {
        this.restoreAfterFinish = this.options.restoreAfterFinish || false;
        this.elementPositioning = MochiKit.Style.getStyle(this.element,
                                                        'position');

        var ma = MochiKit.Base.map;
        var b = MochiKit.Base.bind;
        this.originalStyle = {};
        ma(b(function (k) {
                this.originalStyle[k] = this.element.style[k];
            }, this), ['top', 'left', 'width', 'height', 'fontSize']);

        this.originalTop = this.element.offsetTop;
        this.originalLeft = this.element.offsetLeft;

        var fontSize = MochiKit.Style.getStyle(this.element,
                                             'font-size') || '100%';
        ma(b(function (fontSizeType) {
            if (fontSize.indexOf(fontSizeType) > 0) {
                this.fontSize = parseFloat(fontSize);
                this.fontSizeType = fontSizeType;
            }
        }, this), ['em', 'px', '%']);

        this.factor = (this.options.scaleTo - this.options.scaleFrom)/100;

        if (/^content/.test(this.options.scaleMode)) {
            this.dims = [this.element.scrollHeight, this.element.scrollWidth];
        } else if (this.options.scaleMode == 'box') {
            this.dims = [this.element.offsetHeight, this.element.offsetWidth];
        } else {
            this.dims = [this.options.scaleMode.originalHeight,
                         this.options.scaleMode.originalWidth];
        }
    },

    /** @id MochiKit.Visual.Scale.prototype.update */
    update: function (position) {
        var currentScale = (this.options.scaleFrom/100.0) +
                           (this.factor * position);
        if (this.options.scaleContent && this.fontSize) {
            MochiKit.Style.setStyle(this.element, {
                fontSize: this.fontSize * currentScale + this.fontSizeType
            });
        }
        this.setDimensions(this.dims[0] * currentScale,
                           this.dims[1] * currentScale);
    },

    /** @id MochiKit.Visual.Scale.prototype.finish */
    finish: function () {
        if (this.restoreAfterFinish) {
            MochiKit.Style.setStyle(this.element, this.originalStyle);
        }
    },

    /** @id MochiKit.Visual.Scale.prototype.setDimensions */
    setDimensions: function (height, width) {
        var d = {};
        var r = Math.round;
        if (/MSIE/.test(navigator.userAgent)) {
            r = Math.ceil;
        }
        if (this.options.scaleX) {
            d.width = r(width) + 'px';
        }
        if (this.options.scaleY) {
            d.height = r(height) + 'px';
        }
        if (this.options.scaleFromCenter) {
            var topd = (height - this.dims[0])/2;
            var leftd = (width - this.dims[1])/2;
            if (this.elementPositioning == 'absolute') {
                if (this.options.scaleY) {
                    d.top = this.originalTop - topd + 'px';
                }
                if (this.options.scaleX) {
                    d.left = this.originalLeft - leftd + 'px';
                }
            } else {
                if (this.options.scaleY) {
                    d.top = -topd + 'px';
                }
                if (this.options.scaleX) {
                    d.left = -leftd + 'px';
                }
            }
        }
        MochiKit.Style.setStyle(this.element, d);
    }
});

/** @id MochiKit.Visual.Highlight */
MochiKit.Visual.Highlight = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.Visual.Highlight.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Highlight.prototype, {
    /***

    Highlight an item of the page.

    @param options: 'startcolor' for choosing highlighting color, default
    to '#ffff99'.

    ***/
    __init__: function (element, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element);
        options = MochiKit.Base.update({
            startcolor: '#ffff99'
        }, options || {});
        this.start(options);
    },

    /** @id MochiKit.Visual.Highlight.prototype.setup */
    setup: function () {
        var b = MochiKit.Base;
        var s = MochiKit.Style;
        // Prevent executing on elements not in the layout flow
        if (s.getStyle(this.element, 'display') == 'none') {
            this.cancel();
            return;
        }
        // Disable background image during the effect
        this.oldStyle = {
            backgroundImage: s.getStyle(this.element, 'background-image')
        };
        s.setStyle(this.element, {
            backgroundImage: 'none'
        });

        if (!this.options.endcolor) {
            this.options.endcolor =
                MochiKit.Color.Color.fromBackground(this.element).toHexString();
        }
        if (b.isUndefinedOrNull(this.options.restorecolor)) {
            this.options.restorecolor = s.getStyle(this.element,
                                                   'background-color');
        }
        // init color calculations
        this._base = b.map(b.bind(function (i) {
            return parseInt(
                this.options.startcolor.slice(i*2 + 1, i*2 + 3), 16);
        }, this), [0, 1, 2]);
        this._delta = b.map(b.bind(function (i) {
            return parseInt(this.options.endcolor.slice(i*2 + 1, i*2 + 3), 16)
                - this._base[i];
        }, this), [0, 1, 2]);
    },

    /** @id MochiKit.Visual.Highlight.prototype.update */
    update: function (position) {
        var m = '#';
        MochiKit.Base.map(MochiKit.Base.bind(function (i) {
            m += MochiKit.Color.toColorPart(Math.round(this._base[i] +
                                            this._delta[i]*position));
        }, this), [0, 1, 2]);
        MochiKit.Style.setStyle(this.element, {
            backgroundColor: m
        });
    },

    /** @id MochiKit.Visual.Highlight.prototype.finish */
    finish: function () {
        MochiKit.Style.setStyle(this.element,
            MochiKit.Base.update(this.oldStyle, {
                backgroundColor: this.options.restorecolor
        }));
    }
});

/** @id MochiKit.Visual.ScrollTo */
MochiKit.Visual.ScrollTo = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.Visual.ScrollTo.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.ScrollTo.prototype, {
    /***

    Scroll to an element in the page.

    ***/
    __init__: function (element, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element);
        this.start(options || {});
    },

    /** @id MochiKit.Visual.ScrollTo.prototype.setup */
    setup: function () {
        var p = MochiKit.Position;
        p.prepare();
        var offsets = p.cumulativeOffset(this.element);
        if (this.options.offset) {
            offsets.y += this.options.offset;
        }
        var max;
        if (window.innerHeight) {
            max = window.innerHeight - window.height;
        } else if (document.documentElement &&
                   document.documentElement.clientHeight) {
            max = document.documentElement.clientHeight -
                  document.body.scrollHeight;
        } else if (document.body) {
            max = document.body.clientHeight - document.body.scrollHeight;
        }
        this.scrollStart = p.windowOffset.y;
        this.delta = (offsets.y > max ? max : offsets.y) - this.scrollStart;
    },

    /** @id MochiKit.Visual.ScrollTo.prototype.update */
    update: function (position) {
        var p = MochiKit.Position;
        p.prepare();
        window.scrollTo(p.windowOffset.x, this.scrollStart + (position * this.delta));
    }
});

MochiKit.Visual.CSS_LENGTH = /^(([\+\-]?[0-9\.]+)(em|ex|px|in|cm|mm|pt|pc|\%))|0$/;

MochiKit.Visual.Morph = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.Visual.Morph.prototype = new MochiKit.Visual.Base();

MochiKit.Base.update(MochiKit.Visual.Morph.prototype, {
    /***

    Morph effect: make a transformation from current style to the given style,
    automatically making a transition between the two.

    ***/
    __init__: function (element, /* optional */options) {
        this.element = MochiKit.DOM.getElement(element);
        this.start(options || {});
    },

    /** @id MochiKit.Visual.Morph.prototype.setup */
    setup: function () {
        var b = MochiKit.Base;
        var style = this.options.style;
        this.styleStart = {};
        this.styleEnd = {};
        this.units = {};
        var value, unit;
        for (var s in style) {
            value = style[s];
            s = b.camelize(s);
            if (MochiKit.Visual.CSS_LENGTH.test(value)) {
                var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/);
                value = parseFloat(components[1]);
                unit = (components.length == 3) ? components[2] : null;
                this.styleEnd[s] = value;
                this.units[s] = unit;
                value = MochiKit.Style.getStyle(this.element, s);
                components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/);
                value = parseFloat(components[1]);
                this.styleStart[s] = value;
            } else {
                var c = MochiKit.Color.Color;
                value = c.fromString(value);
                if (value) {
                    this.units[s] = "color";
                    this.styleEnd[s] = value.toHexString();
                    value = MochiKit.Style.getStyle(this.element, s);
                    this.styleStart[s] = c.fromString(value).toHexString();

                    this.styleStart[s] = b.map(b.bind(function (i) {
                        return parseInt(
                            this.styleStart[s].slice(i*2 + 1, i*2 + 3), 16);
                    }, this), [0, 1, 2]);
                    this.styleEnd[s] = b.map(b.bind(function (i) {
                        return parseInt(
                            this.styleEnd[s].slice(i*2 + 1, i*2 + 3), 16);
                    }, this), [0, 1, 2]);
                }
            }
        }
    },

    /** @id MochiKit.Visual.Morph.prototype.update */
    update: function (position) {
        var value;
        for (var s in this.styleStart) {
            if (this.units[s] == "color") {
                var m = '#';
                var start = this.styleStart[s];
                var end = this.styleEnd[s];
                MochiKit.Base.map(MochiKit.Base.bind(function (i) {
                    m += MochiKit.Color.toColorPart(Math.round(start[i] +
                                                    (end[i] - start[i])*position));
                }, this), [0, 1, 2]);
                this.element.style[s] = m;
            } else {
                value = this.styleStart[s] + Math.round((this.styleEnd[s] - this.styleStart[s]) * position * 1000) / 1000 + this.units[s];
                this.element.style[s] = value;
            }
        }
    }
});

/***

Combination effects.

***/

/** @id MochiKit.Visual.fade */
MochiKit.Visual.fade = function (element, /* optional */ options) {
    /***

    Fade a given element: change its opacity and hide it in the end.

    @param options: 'to' and 'from' to change opacity.

    ***/
    var s = MochiKit.Style;
    var oldOpacity = s.getStyle(element, 'opacity');
    options = MochiKit.Base.update({
        from: s.getStyle(element, 'opacity') || 1.0,
        to: 0.0,
        afterFinishInternal: function (effect) {
            if (effect.options.to !== 0) {
                return;
            }
            s.hideElement(effect.element);
            s.setStyle(effect.element, {'opacity': oldOpacity});
        }
    }, options || {});
    return new MochiKit.Visual.Opacity(element, options);
};

/** @id MochiKit.Visual.appear */
MochiKit.Visual.appear = function (element, /* optional */ options) {
    /***

    Make an element appear.

    @param options: 'to' and 'from' to change opacity.

    ***/
    var s = MochiKit.Style;
    var v = MochiKit.Visual;
    options = MochiKit.Base.update({
        from: (s.getStyle(element, 'display') == 'none' ? 0.0 :
               s.getStyle(element, 'opacity') || 0.0),
        to: 1.0,
        // force Safari to render floated elements properly
        afterFinishInternal: function (effect) {
            v.forceRerendering(effect.element);
        },
        beforeSetupInternal: function (effect) {
            s.setStyle(effect.element, {'opacity': effect.options.from});
            s.showElement(effect.element);
        }
    }, options || {});
    return new v.Opacity(element, options);
};

/** @id MochiKit.Visual.puff */
MochiKit.Visual.puff = function (element, /* optional */ options) {
    /***

    'Puff' an element: grow it to double size, fading it and make it hidden.

    ***/
    var s = MochiKit.Style;
    var v = MochiKit.Visual;
    element = MochiKit.DOM.getElement(element);
    var oldStyle = {
        position: s.getStyle(element, 'position'),
        top: element.style.top,
        left: element.style.left,
        width: element.style.width,
        height: element.style.height,
        opacity: s.getStyle(element, 'opacity')
    };
    options = MochiKit.Base.update({
        beforeSetupInternal: function (effect) {
            MochiKit.Position.absolutize(effect.effects[0].element);
        },
        afterFinishInternal: function (effect) {
            s.hideElement(effect.effects[0].element);
            s.setStyle(effect.effects[0].element, oldStyle);
        }
    }, options || {});
    return new v.Parallel(
        [new v.Scale(element, 200,
            {sync: true, scaleFromCenter: true,
             scaleContent: true, restoreAfterFinish: true}),
         new v.Opacity(element, {sync: true, to: 0.0 })],
        options);
};

/** @id MochiKit.Visual.blindUp */
MochiKit.Visual.blindUp = function (element, /* optional */ options) {
    /***

    Blind an element up: change its vertical size to 0.

    ***/
    var d = MochiKit.DOM;
    element = d.getElement(element);
    var elemClip = d.makeClipping(element);
    options = MochiKit.Base.update({
        scaleContent: false,
        scaleX: false,
        restoreAfterFinish: true,
        afterFinishInternal: function (effect) {
            MochiKit.Style.hideElement(effect.element);
            d.undoClipping(effect.element, elemClip);
        }
    }, options || {});

    return new MochiKit.Visual.Scale(element, 0, options);
};

/** @id MochiKit.Visual.blindDown */
MochiKit.Visual.blindDown = function (element, /* optional */ options) {
    /***

    Blind an element down: restore its vertical size.

    ***/
    var d = MochiKit.DOM;
    var s = MochiKit.Style;
    element = d.getElement(element);
    var elementDimensions = s.getElementDimensions(element);
    var elemClip;
    options = MochiKit.Base.update({
        scaleContent: false,
        scaleX: false,
        scaleFrom: 0,
        scaleMode: {originalHeight: elementDimensions.h,
                    originalWidth: elementDimensions.w},
        restoreAfterFinish: true,
        afterSetupInternal: function (effect) {
            elemClip = d.makeClipping(effect.element);
            s.setStyle(effect.element, {height: '0px'});
            s.showElement(effect.element);
        },
        afterFinishInternal: function (effect) {
            d.undoClipping(effect.element, elemClip);
        }
    }, options || {});
    return new MochiKit.Visual.Scale(element, 100, options);
};

/** @id MochiKit.Visual.switchOff */
MochiKit.Visual.switchOff = function (element, /* optional */ options) {
    /***

    Apply a switch-off-like effect.

    ***/
    var d = MochiKit.DOM;
    element = d.getElement(element);
    var oldOpacity = MochiKit.Style.getStyle(element, 'opacity');
    var elemClip;
    options = MochiKit.Base.update({
        duration: 0.3,
        scaleFromCenter: true,
        scaleX: false,
        scaleContent: false,
        restoreAfterFinish: true,
        beforeSetupInternal: function (effect) {
            d.makePositioned(effect.element);
            elemClip = d.makeClipping(effect.element);
        },
        afterFinishInternal: function (effect) {
            MochiKit.Style.hideElement(effect.element);
            d.undoClipping(effect.element, elemClip);
            d.undoPositioned(effect.element);
            MochiKit.Style.setStyle(effect.element, {'opacity': oldOpacity});
        }
    }, options || {});
    var v = MochiKit.Visual;
    return new v.appear(element, {
        duration: 0.4,
        from: 0,
        transition: v.Transitions.flicker,
        afterFinishInternal: function (effect) {
            new v.Scale(effect.element, 1, options);
        }
    });
};

/** @id MochiKit.Visual.dropOut */
MochiKit.Visual.dropOut = function (element, /* optional */ options) {
    /***

    Make an element fall and disappear.

    ***/
    var d = MochiKit.DOM;
    var s = MochiKit.Style;
    element = d.getElement(element);
    var oldStyle = {
        top: s.getStyle(element, 'top'),
        left: s.getStyle(element, 'left'),
        opacity: s.getStyle(element, 'opacity')
    };

    options = MochiKit.Base.update({
        duration: 0.5,
        distance: 100,
        beforeSetupInternal: function (effect) {
            d.makePositioned(effect.effects[0].element);
        },
        afterFinishInternal: function (effect) {
            s.hideElement(effect.effects[0].element);
            d.undoPositioned(effect.effects[0].element);
            s.setStyle(effect.effects[0].element, oldStyle);
        }
    }, options || {});
    var v = MochiKit.Visual;
    return new v.Parallel(
        [new v.Move(element, {x: 0, y: options.distance, sync: true}),
         new v.Opacity(element, {sync: true, to: 0.0})],
        options);
};

/** @id MochiKit.Visual.shake */
MochiKit.Visual.shake = function (element, /* optional */ options) {
    /***

    Move an element from left to right several times.

    ***/
    var d = MochiKit.DOM;
    var v = MochiKit.Visual;
    var s = MochiKit.Style;
    element = d.getElement(element);
    options = MochiKit.Base.update({
        x: -20,
        y: 0,
        duration: 0.05,
        afterFinishInternal: function (effect) {
            d.undoPositioned(effect.element);
            s.setStyle(effect.element, oldStyle);
        }
    }, options || {});
    var oldStyle = {
        top: s.getStyle(element, 'top'),
        left: s.getStyle(element, 'left') };
        return new v.Move(element,
          {x: 20, y: 0, duration: 0.05, afterFinishInternal: function (effect) {
        new v.Move(effect.element,
          {x: -40, y: 0, duration: 0.1, afterFinishInternal: function (effect) {
        new v.Move(effect.element,
           {x: 40, y: 0, duration: 0.1, afterFinishInternal: function (effect) {
        new v.Move(effect.element,
          {x: -40, y: 0, duration: 0.1, afterFinishInternal: function (effect) {
        new v.Move(effect.element,
           {x: 40, y: 0, duration: 0.1, afterFinishInternal: function (effect) {
        new v.Move(effect.element, options
        ) }}) }}) }}) }}) }});
};

/** @id MochiKit.Visual.slideDown */
MochiKit.Visual.slideDown = function (element, /* optional */ options) {
    /***

    Slide an element down.
    It needs to have the content of the element wrapped in a container
    element with fixed height.

    ***/
    var d = MochiKit.DOM;
    var b = MochiKit.Base;
    var s = MochiKit.Style;
    element = d.getElement(element);
    if (!element.firstChild) {
        throw "MochiKit.Visual.slideDown must be used on a element with a child";
    }
    d.removeEmptyTextNodes(element);
    var oldInnerBottom = s.getStyle(element.firstChild, 'bottom') || 0;
    var elementDimensions = s.getElementDimensions(element);
    var elemClip;
    options = b.update({
        scaleContent: false,
        scaleX: false,
        scaleFrom: 0,
        scaleMode: {originalHeight: elementDimensions.h,
                    originalWidth: elementDimensions.w},
        restoreAfterFinish: true,
        afterSetupInternal: function (effect) {
            d.makePositioned(effect.element);
            d.makePositioned(effect.element.firstChild);
            if (/Opera/.test(navigator.userAgent)) {
                s.setStyle(effect.element, {top: ''});
            }
            elemClip = d.makeClipping(effect.element);
            s.setStyle(effect.element, {height: '0px'});
            s.showElement(effect.element);
        },
        afterUpdateInternal: function (effect) {
            s.setStyle(effect.element.firstChild,
               {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'});
        },
        afterFinishInternal: function (effect) {
            d.undoClipping(effect.element, elemClip);
            // IE will crash if child is undoPositioned first
            if (/MSIE/.test(navigator.userAgent)) {
                d.undoPositioned(effect.element);
                d.undoPositioned(effect.element.firstChild);
            } else {
                d.undoPositioned(effect.element.firstChild);
                d.undoPositioned(effect.element);
            }
            s.setStyle(effect.element.firstChild,
                                  {bottom: oldInnerBottom});
        }
    }, options || {});

    return new MochiKit.Visual.Scale(element, 100, options);
};

/** @id MochiKit.Visual.slideUp */
MochiKit.Visual.slideUp = function (element, /* optional */ options) {
    /***

    Slide an element up.
    It needs to have the content of the element wrapped in a container
    element with fixed height.

    ***/
    var d = MochiKit.DOM;
    var b = MochiKit.Base;
    var s = MochiKit.Style;
    element = d.getElement(element);
    if (!element.firstChild) {
        throw "MochiKit.Visual.slideUp must be used on a element with a child";
    }
    d.removeEmptyTextNodes(element);
    var oldInnerBottom = s.getStyle(element.firstChild, 'bottom');
    var elemClip;
    options = b.update({
        scaleContent: false,
        scaleX: false,
        scaleMode: 'box',
        scaleFrom: 100,
        restoreAfterFinish: true,
        beforeStartInternal: function (effect) {
            d.makePositioned(effect.element);
            d.makePositioned(effect.element.firstChild);
            if (/Opera/.test(navigator.userAgent)) {
                s.setStyle(effect.element, {top: ''});
            }
            elemClip = d.makeClipping(effect.element);
            s.showElement(effect.element);
        },
        afterUpdateInternal: function (effect) {
            s.setStyle(effect.element.firstChild,
            {bottom: (effect.dims[0] - effect.element.clientHeight) + 'px'});
        },
        afterFinishInternal: function (effect) {
            s.hideElement(effect.element);
            d.undoClipping(effect.element, elemClip);
            d.undoPositioned(effect.element.firstChild);
            d.undoPositioned(effect.element);
            s.setStyle(effect.element.firstChild, {bottom: oldInnerBottom});
        }
    }, options || {});
    return new MochiKit.Visual.Scale(element, 0, options);
};

// Bug in opera makes the TD containing this element expand for a instance
// after finish
/** @id MochiKit.Visual.squish */
MochiKit.Visual.squish = function (element, /* optional */ options) {
    /***

    Reduce an element and make it disappear.

    ***/
    var d = MochiKit.DOM;
    var b = MochiKit.Base;
    var elemClip;
    options = b.update({
        restoreAfterFinish: true,
        beforeSetupInternal: function (effect) {
            elemClip = d.makeClipping(effect.element);
        },
        afterFinishInternal: function (effect) {
            MochiKit.Style.hideElement(effect.element);
            d.undoClipping(effect.element, elemClip);
        }
    }, options || {});

    return new MochiKit.Visual.Scale(element, /Opera/.test(navigator.userAgent) ? 1 : 0, options);
};

/** @id MochiKit.Visual.grow */
MochiKit.Visual.grow = function (element, /* optional */ options) {
    /***

    Grow an element to its original size. Make it zero-sized before
    if necessary.

    ***/
    var d = MochiKit.DOM;
    var v = MochiKit.Visual;
    var s = MochiKit.Style;
    element = d.getElement(element);
    options = MochiKit.Base.update({
        direction: 'center',
        moveTransition: v.Transitions.sinoidal,
        scaleTransition: v.Transitions.sinoidal,
        opacityTransition: v.Transitions.full
    }, options || {});
    var oldStyle = {
        top: element.style.top,
        left: element.style.left,
        height: element.style.height,
        width: element.style.width,
        opacity: s.getStyle(element, 'opacity')
    };

    var dims = s.getElementDimensions(element);
    var initialMoveX, initialMoveY;
    var moveX, moveY;

    switch (options.direction) {
        case 'top-left':
            initialMoveX = initialMoveY = moveX = moveY = 0;
            break;
        case 'top-right':
            initialMoveX = dims.w;
            initialMoveY = moveY = 0;
            moveX = -dims.w;
            break;
        case 'bottom-left':
            initialMoveX = moveX = 0;
            initialMoveY = dims.h;
            moveY = -dims.h;
            break;
        case 'bottom-right':
            initialMoveX = dims.w;
            initialMoveY = dims.h;
            moveX = -dims.w;
            moveY = -dims.h;
            break;
        case 'center':
            initialMoveX = dims.w / 2;
            initialMoveY = dims.h / 2;
            moveX = -dims.w / 2;
            moveY = -dims.h / 2;
            break;
    }

    var optionsParallel = MochiKit.Base.update({
        beforeSetupInternal: function (effect) {
            s.setStyle(effect.effects[0].element, {height: '0px'});
            s.showElement(effect.effects[0].element);
        },
        afterFinishInternal: function (effect) {
            d.undoClipping(effect.effects[0].element);
            d.undoPositioned(effect.effects[0].element);
            s.setStyle(effect.effects[0].element, oldStyle);
        }
    }, options || {});

    return new v.Move(element, {
        x: initialMoveX,
        y: initialMoveY,
        duration: 0.01,
        beforeSetupInternal: function (effect) {
            s.hideElement(effect.element);
            d.makeClipping(effect.element);
            d.makePositioned(effect.element);
        },
        afterFinishInternal: function (effect) {
            new v.Parallel(
                [new v.Opacity(effect.element, {
                    sync: true, to: 1.0, from: 0.0,
                    transition: options.opacityTransition
                 }),
                 new v.Move(effect.element, {
                     x: moveX, y: moveY, sync: true,
                     transition: options.moveTransition
                 }),
                 new v.Scale(effect.element, 100, {
                        scaleMode: {originalHeight: dims.h,
                                    originalWidth: dims.w},
                        sync: true,
                        scaleFrom: /Opera/.test(navigator.userAgent) ? 1 : 0,
                        transition: options.scaleTransition,
                        restoreAfterFinish: true
                })
                ], optionsParallel
            );
        }
    });
};

/** @id MochiKit.Visual.shrink */
MochiKit.Visual.shrink = function (element, /* optional */ options) {
    /***

    Shrink an element and make it disappear.

    ***/
    var d = MochiKit.DOM;
    var v = MochiKit.Visual;
    var s = MochiKit.Style;
    element = d.getElement(element);
    options = MochiKit.Base.update({
        direction: 'center',
        moveTransition: v.Transitions.sinoidal,
        scaleTransition: v.Transitions.sinoidal,
        opacityTransition: v.Transitions.none
    }, options || {});
    var oldStyle = {
        top: element.style.top,
        left: element.style.left,
        height: element.style.height,
        width: element.style.width,
        opacity: s.getStyle(element, 'opacity')
    };

    var dims = s.getElementDimensions(element);
    var moveX, moveY;

    switch (options.direction) {
        case 'top-left':
            moveX = moveY = 0;
            break;
        case 'top-right':
            moveX = dims.w;
            moveY = 0;
            break;
        case 'bottom-left':
            moveX = 0;
            moveY = dims.h;
            break;
        case 'bottom-right':
            moveX = dims.w;
            moveY = dims.h;
            break;
        case 'center':
            moveX = dims.w / 2;
            moveY = dims.h / 2;
            break;
    }
    var elemClip;

    var optionsParallel = MochiKit.Base.update({
        beforeStartInternal: function (effect) {
            elemClip = d.makePositioned(effect.effects[0].element);
            d.makeClipping(effect.effects[0].element);
        },
        afterFinishInternal: function (effect) {
            s.hideElement(effect.effects[0].element);
            d.undoClipping(effect.effects[0].element, elemClip);
            d.undoPositioned(effect.effects[0].element);
            s.setStyle(effect.effects[0].element, oldStyle);
        }
    }, options || {});

    return new v.Parallel(
        [new v.Opacity(element, {
            sync: true, to: 0.0, from: 1.0,
            transition: options.opacityTransition
         }),
         new v.Scale(element, /Opera/.test(navigator.userAgent) ? 1 : 0, {
             sync: true, transition: options.scaleTransition,
             restoreAfterFinish: true
         }),
         new v.Move(element, {
             x: moveX, y: moveY, sync: true, transition: options.moveTransition
         })
        ], optionsParallel
    );
};

/** @id MochiKit.Visual.pulsate */
MochiKit.Visual.pulsate = function (element, /* optional */ options) {
    /***

    Pulse an element between appear/fade.

    ***/
    var d = MochiKit.DOM;
    var v = MochiKit.Visual;
    var b = MochiKit.Base;
    var oldOpacity = MochiKit.Style.getStyle(element, 'opacity');
    options = b.update({
        duration: 3.0,
        from: 0,
        afterFinishInternal: function (effect) {
            MochiKit.Style.setStyle(effect.element, {'opacity': oldOpacity});
        }
    }, options || {});
    var transition = options.transition || v.Transitions.sinoidal;
    var reverser = b.bind(function (pos) {
        return transition(1 - v.Transitions.pulse(pos));
    }, transition);
    b.bind(reverser, transition);
    return new v.Opacity(element, b.update({
        transition: reverser}, options));
};

/** @id MochiKit.Visual.fold */
MochiKit.Visual.fold = function (element, /* optional */ options) {
    /***

    Fold an element, first vertically, then horizontally.

    ***/
    var d = MochiKit.DOM;
    var v = MochiKit.Visual;
    var s = MochiKit.Style;
    element = d.getElement(element);
    var oldStyle = {
        top: element.style.top,
        left: element.style.left,
        width: element.style.width,
        height: element.style.height
    };
    var elemClip = d.makeClipping(element);
    options = MochiKit.Base.update({
        scaleContent: false,
        scaleX: false,
        afterFinishInternal: function (effect) {
            new v.Scale(element, 1, {
                scaleContent: false,
                scaleY: false,
                afterFinishInternal: function (effect) {
                    s.hideElement(effect.element);
                    d.undoClipping(effect.element, elemClip);
                    s.setStyle(effect.element, oldStyle);
                }
            });
        }
    }, options || {});
    return new v.Scale(element, 5, options);
};


// Compatibility with MochiKit 1.0
MochiKit.Visual.Color = MochiKit.Color.Color;
MochiKit.Visual.getElementsComputedStyle = MochiKit.DOM.computedStyle;

/* end of Rico adaptation */

MochiKit.Visual.__new__ = function () {
    var m = MochiKit.Base;

    m.nameFunctions(this);

    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
    };

};

MochiKit.Visual.EXPORT = [
    "roundElement",
    "roundClass",
    "tagifyText",
    "multiple",
    "toggle",
    "Parallel",
    "Opacity",
    "Move",
    "Scale",
    "Highlight",
    "ScrollTo",
    "Morph",
    "fade",
    "appear",
    "puff",
    "blindUp",
    "blindDown",
    "switchOff",
    "dropOut",
    "shake",
    "slideDown",
    "slideUp",
    "squish",
    "grow",
    "shrink",
    "pulsate",
    "fold"
];

MochiKit.Visual.EXPORT_OK = [
    "Base",
    "PAIRS"
];

MochiKit.Visual.__new__();

MochiKit.Base._exportSymbols(this, MochiKit.Visual);



/***

MochiKit.Async 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide("MochiKit.Async");
    dojo.require("MochiKit.Base");
}
if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.Async depends on MochiKit.Base!";
}

if (typeof(MochiKit.Async) == 'undefined') {
    MochiKit.Async = {};
}

MochiKit.Async.NAME = "MochiKit.Async";
MochiKit.Async.VERSION = "1.4";
MochiKit.Async.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};
MochiKit.Async.toString = function () {
    return this.__repr__();
};

/** @id MochiKit.Async.Deferred */
MochiKit.Async.Deferred = function (/* optional */ canceller) {
    this.chain = [];
    this.id = this._nextId();
    this.fired = -1;
    this.paused = 0;
    this.results = [null, null];
    this.canceller = canceller;
    this.silentlyCancelled = false;
    this.chained = false;
};

MochiKit.Async.Deferred.prototype = {
    /** @id MochiKit.Async.Deferred.prototype.repr */
    repr: function () {
        var state;
        if (this.fired == -1) {
            state = 'unfired';
        } else if (this.fired === 0) {
            state = 'success';
        } else {
            state = 'error';
        }
        return 'Deferred(' + this.id + ', ' + state + ')';
    },

    toString: MochiKit.Base.forwardCall("repr"),

    _nextId: MochiKit.Base.counter(),

    /** @id MochiKit.Async.Deferred.prototype.cancel */
    cancel: function () {
        var self = MochiKit.Async;
        if (this.fired == -1) {
            if (this.canceller) {
                this.canceller(this);
            } else {
                this.silentlyCancelled = true;
            }
            if (this.fired == -1) {
                this.errback(new self.CancelledError(this));
            }
        } else if ((this.fired === 0) && (this.results[0] instanceof self.Deferred)) {
            this.results[0].cancel();
        }
    },

    _resback: function (res) {
        /***

        The primitive that means either callback or errback

        ***/
        this.fired = ((res instanceof Error) ? 1 : 0);
        this.results[this.fired] = res;
        this._fire();
    },

    _check: function () {
        if (this.fired != -1) {
            if (!this.silentlyCancelled) {
                throw new MochiKit.Async.AlreadyCalledError(this);
            }
            this.silentlyCancelled = false;
            return;
        }
    },

    /** @id MochiKit.Async.Deferred.prototype.callback */
    callback: function (res) {
        this._check();
        if (res instanceof MochiKit.Async.Deferred) {
            throw new Error("Deferred instances can only be chained if they are the result of a callback");
        }
        this._resback(res);
    },

    /** @id MochiKit.Async.Deferred.prototype.errback */
    errback: function (res) {
        this._check();
        var self = MochiKit.Async;
        if (res instanceof self.Deferred) {
            throw new Error("Deferred instances can only be chained if they are the result of a callback");
        }
        if (!(res instanceof Error)) {
            res = new self.GenericError(res);
        }
        this._resback(res);
    },

    /** @id MochiKit.Async.Deferred.prototype.addBoth */
    addBoth: function (fn) {
        if (arguments.length > 1) {
            fn = MochiKit.Base.partial.apply(null, arguments);
        }
        return this.addCallbacks(fn, fn);
    },

    /** @id MochiKit.Async.Deferred.prototype.addCallback */
    addCallback: function (fn) {
        if (arguments.length > 1) {
            fn = MochiKit.Base.partial.apply(null, arguments);
        }
        return this.addCallbacks(fn, null);
    },

    /** @id MochiKit.Async.Deferred.prototype.addErrback */
    addErrback: function (fn) {
        if (arguments.length > 1) {
            fn = MochiKit.Base.partial.apply(null, arguments);
        }
        return this.addCallbacks(null, fn);
    },

    /** @id MochiKit.Async.Deferred.prototype.addCallbacks */
    addCallbacks: function (cb, eb) {
        if (this.chained) {
            throw new Error("Chained Deferreds can not be re-used");
        }
        this.chain.push([cb, eb]);
        if (this.fired >= 0) {
            this._fire();
        }
        return this;
    },

    _fire: function () {
        /***

        Used internally to exhaust the callback sequence when a result
        is available.

        ***/
        var chain = this.chain;
        var fired = this.fired;
        var res = this.results[fired];
        var self = this;
        var cb = null;
        while (chain.length > 0 && this.paused === 0) {
            // Array
            var pair = chain.shift();
            var f = pair[fired];
            if (f === null) {
                continue;
            }
            try {
                res = f(res);
                fired = ((res instanceof Error) ? 1 : 0);
                if (res instanceof MochiKit.Async.Deferred) {
                    cb = function (res) {
                        self._resback(res);
                        self.paused--;
                        if ((self.paused === 0) && (self.fired >= 0)) {
                            self._fire();
                        }
                    };
                    this.paused++;
                }
            } catch (err) {
                fired = 1;
                if (!(err instanceof Error)) {
                    err = new MochiKit.Async.GenericError(err);
                }
                res = err;
            }
        }
        this.fired = fired;
        this.results[fired] = res;
        if (cb && this.paused) {
            // this is for "tail recursion" in case the dependent deferred
            // is already fired
            res.addBoth(cb);
            res.chained = true;
        }
    }
};

MochiKit.Base.update(MochiKit.Async, {
    /** @id MochiKit.Async.evalJSONRequest */
    evalJSONRequest: function (/* req */) {
        return eval('(' + arguments[0].responseText + ')');
    },

    /** @id MochiKit.Async.succeed */
    succeed: function (/* optional */result) {
        var d = new MochiKit.Async.Deferred();
        d.callback.apply(d, arguments);
        return d;
    },

    /** @id MochiKit.Async.fail */
    fail: function (/* optional */result) {
        var d = new MochiKit.Async.Deferred();
        d.errback.apply(d, arguments);
        return d;
    },

    /** @id MochiKit.Async.getXMLHttpRequest */
    getXMLHttpRequest: function () {
        var self = arguments.callee;
        if (!self.XMLHttpRequest) {
            var tryThese = [
                function () { return new XMLHttpRequest(); },
                function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
                function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
                function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); },
                function () {
                    throw new MochiKit.Async.BrowserComplianceError("Browser does not support XMLHttpRequest");
                }
            ];
            for (var i = 0; i < tryThese.length; i++) {
                var func = tryThese[i];
                try {
                    self.XMLHttpRequest = func;
                    return func();
                } catch (e) {
                    // pass
                }
            }
        }
        return self.XMLHttpRequest();
    },

    _xhr_onreadystatechange: function (d) {
        // MochiKit.Logging.logDebug('this.readyState', this.readyState);
        var m = MochiKit.Base;
        if (this.readyState == 4) {
            // IE SUCKS
            try {
                this.onreadystatechange = null;
            } catch (e) {
                try {
                    this.onreadystatechange = m.noop;
                } catch (e) {
                }
            }
            var status = null;
            try {
                status = this.status;
                if (!status && m.isNotEmpty(this.responseText)) {
                    // 0 or undefined seems to mean cached or local
                    status = 304;
                }
            } catch (e) {
                // pass
                // MochiKit.Logging.logDebug('error getting status?', repr(items(e)));
            }
            // 200 is OK, 201 is CREATED, 204 is NO CONTENT
            // 304 is NOT MODIFIED, 1223 is apparently a bug in IE
            if (status == 200 || status == 201 || status == 204 ||
                    status == 304 || status == 1223) {
                d.callback(this);
            } else {
                var err = new MochiKit.Async.XMLHttpRequestError(this, "Request failed");
                if (err.number) {
                    // XXX: This seems to happen on page change
                    d.errback(err);
                } else {
                    // XXX: this seems to happen when the server is unreachable
                    d.errback(err);
                }
            }
        }
    },

    _xhr_canceller: function (req) {
        // IE SUCKS
        try {
            req.onreadystatechange = null;
        } catch (e) {
            try {
                req.onreadystatechange = MochiKit.Base.noop;
            } catch (e) {
            }
        }
        req.abort();
    },


    /** @id MochiKit.Async.sendXMLHttpRequest */
    sendXMLHttpRequest: function (req, /* optional */ sendContent) {
        if (typeof(sendContent) == "undefined" || sendContent === null) {
            sendContent = "";
        }

        var m = MochiKit.Base;
        var self = MochiKit.Async;
        var d = new self.Deferred(m.partial(self._xhr_canceller, req));

        try {
            req.onreadystatechange = m.bind(self._xhr_onreadystatechange,
                req, d);
            req.send(sendContent);
        } catch (e) {
            try {
                req.onreadystatechange = null;
            } catch (ignore) {
                // pass
            }
            d.errback(e);
        }

        return d;

    },

    /** @id MochiKit.Async.doXHR */
    doXHR: function (url, opts) {
        var m = MochiKit.Base;
        opts = m.update({
            method: 'GET',
            sendContent: ''
            /*
            queryString: undefined,
            username: undefined,
            password: undefined,
            headers: undefined,
            mimeType: undefined
            */
        }, opts);
        var self = MochiKit.Async;
        var req = self.getXMLHttpRequest();
        if (opts.queryString) {
            var qs = m.queryString(opts.queryString);
            if (qs) {
                url += "?" + qs;
            }
        }
        req.open(opts.method, url, true, opts.username, opts.password);
        if (req.overrideMimeType && opts.mimeType) {
            req.overrideMimeType(opts.mimeType);
        }
        if (opts.headers) {
            var headers = opts.headers;
            if (!m.isArrayLike(headers)) {
                headers = m.items(headers);
            }
            for (var i = 0; i < headers.length; i++) {
                var header = headers[i];
                var name = header[0];
                var value = header[1];
                req.setRequestHeader(name, value);
            }
        }
        return self.sendXMLHttpRequest(req, opts.sendContent);
    },

    _buildURL: function (url/*, ...*/) {
        if (arguments.length > 1) {
            var m = MochiKit.Base;
            var qs = m.queryString.apply(null, m.extend(null, arguments, 1));
            if (qs) {
                return url + "?" + qs;
            }
        }
        return url;
    },

    /** @id MochiKit.Async.doSimpleXMLHttpRequest */
    doSimpleXMLHttpRequest: function (url/*, ...*/) {
        var self = MochiKit.Async;
        url = self._buildURL.apply(self, arguments);
        return self.doXHR(url);
    },

    /** @id MochiKit.Async.loadJSONDoc */
    loadJSONDoc: function (url/*, ...*/) {
        var self = MochiKit.Async;
        url = self._buildURL.apply(self, arguments);
        var d = self.doXHR(url, {
            'mimeType': 'text/plain',
            'headers': [['Accept', 'application/json']]
        });
        d = d.addCallback(self.evalJSONRequest);
        return d;
    },

    /** @id MochiKit.Async.wait */
    wait: function (seconds, /* optional */value) {
        var d = new MochiKit.Async.Deferred();
        var m = MochiKit.Base;
        if (typeof(value) != 'undefined') {
            d.addCallback(function () { return value; });
        }
        var timeout = setTimeout(
            m.bind("callback", d),
            Math.floor(seconds * 1000));
        d.canceller = function () {
            try {
                clearTimeout(timeout);
            } catch (e) {
                // pass
            }
        };
        return d;
    },

    /** @id MochiKit.Async.callLater */
    callLater: function (seconds, func) {
        var m = MochiKit.Base;
        var pfunc = m.partial.apply(m, m.extend(null, arguments, 1));
        return MochiKit.Async.wait(seconds).addCallback(
            function (res) { return pfunc(); }
        );
    }
});


/** @id MochiKit.Async.DeferredLock */
MochiKit.Async.DeferredLock = function () {
    this.waiting = [];
    this.locked = false;
    this.id = this._nextId();
};

MochiKit.Async.DeferredLock.prototype = {
    __class__: MochiKit.Async.DeferredLock,
    /** @id MochiKit.Async.DeferredLock.prototype.acquire */
    acquire: function () {
        var d = new MochiKit.Async.Deferred();
        if (this.locked) {
            this.waiting.push(d);
        } else {
            this.locked = true;
            d.callback(this);
        }
        return d;
    },
    /** @id MochiKit.Async.DeferredLock.prototype.release */
    release: function () {
        if (!this.locked) {
            throw TypeError("Tried to release an unlocked DeferredLock");
        }
        this.locked = false;
        if (this.waiting.length > 0) {
            this.locked = true;
            this.waiting.shift().callback(this);
        }
    },
    _nextId: MochiKit.Base.counter(),
    repr: function () {
        var state;
        if (this.locked) {
            state = 'locked, ' + this.waiting.length + ' waiting';
        } else {
            state = 'unlocked';
        }
        return 'DeferredLock(' + this.id + ', ' + state + ')';
    },
    toString: MochiKit.Base.forwardCall("repr")

};

/** @id MochiKit.Async.DeferredList */
MochiKit.Async.DeferredList = function (list, /* optional */fireOnOneCallback, fireOnOneErrback, consumeErrors, canceller) {

    // call parent constructor
    MochiKit.Async.Deferred.apply(this, [canceller]);

    this.list = list;
    var resultList = [];
    this.resultList = resultList;

    this.finishedCount = 0;
    this.fireOnOneCallback = fireOnOneCallback;
    this.fireOnOneErrback = fireOnOneErrback;
    this.consumeErrors = consumeErrors;

    var cb = MochiKit.Base.bind(this._cbDeferred, this);
    for (var i = 0; i < list.length; i++) {
        var d = list[i];
        resultList.push(undefined);
        d.addCallback(cb, i, true);
        d.addErrback(cb, i, false);
    }

    if (list.length === 0 && !fireOnOneCallback) {
        this.callback(this.resultList);
    }

};

MochiKit.Async.DeferredList.prototype = new MochiKit.Async.Deferred();

MochiKit.Async.DeferredList.prototype._cbDeferred = function (index, succeeded, result) {
    this.resultList[index] = [succeeded, result];
    this.finishedCount += 1;
    if (this.fired == -1) {
        if (succeeded && this.fireOnOneCallback) {
            this.callback([index, result]);
        } else if (!succeeded && this.fireOnOneErrback) {
            this.errback(result);
        } else if (this.finishedCount == this.list.length) {
            this.callback(this.resultList);
        }
    }
    if (!succeeded && this.consumeErrors) {
        result = null;
    }
    return result;
};

/** @id MochiKit.Async.gatherResults */
MochiKit.Async.gatherResults = function (deferredList) {
    var d = new MochiKit.Async.DeferredList(deferredList, false, true, false);
    d.addCallback(function (results) {
        var ret = [];
        for (var i = 0; i < results.length; i++) {
            ret.push(results[i][1]);
        }
        return ret;
    });
    return d;
};

/** @id MochiKit.Async.maybeDeferred */
MochiKit.Async.maybeDeferred = function (func) {
    var self = MochiKit.Async;
    var result;
    try {
        var r = func.apply(null, MochiKit.Base.extend([], arguments, 1));
        if (r instanceof self.Deferred) {
            result = r;
        } else if (r instanceof Error) {
            result = self.fail(r);
        } else {
            result = self.succeed(r);
        }
    } catch (e) {
        result = self.fail(e);
    }
    return result;
};


MochiKit.Async.EXPORT = [
    "AlreadyCalledError",
    "CancelledError",
    "BrowserComplianceError",
    "GenericError",
    "XMLHttpRequestError",
    "Deferred",
    "succeed",
    "fail",
    "getXMLHttpRequest",
    "doSimpleXMLHttpRequest",
    "loadJSONDoc",
    "wait",
    "callLater",
    "sendXMLHttpRequest",
    "DeferredLock",
    "DeferredList",
    "gatherResults",
    "maybeDeferred",
    "doXHR"
];

MochiKit.Async.EXPORT_OK = [
    "evalJSONRequest"
];

MochiKit.Async.__new__ = function () {
    var m = MochiKit.Base;
    var ne = m.partial(m._newNamedError, this);

    ne("AlreadyCalledError",
        /** @id MochiKit.Async.AlreadyCalledError */
        function (deferred) {
            /***

            Raised by the Deferred if callback or errback happens
            after it was already fired.

            ***/
            this.deferred = deferred;
        }
    );

    ne("CancelledError",
        /** @id MochiKit.Async.CancelledError */
        function (deferred) {
            /***

            Raised by the Deferred cancellation mechanism.

            ***/
            this.deferred = deferred;
        }
    );

    ne("BrowserComplianceError",
        /** @id MochiKit.Async.BrowserComplianceError */
        function (msg) {
            /***

            Raised when the JavaScript runtime is not capable of performing
            the given function.  Technically, this should really never be
            raised because a non-conforming JavaScript runtime probably
            isn't going to support exceptions in the first place.

            ***/
            this.message = msg;
        }
    );

    ne("GenericError",
        /** @id MochiKit.Async.GenericError */
        function (msg) {
            this.message = msg;
        }
    );

    ne("XMLHttpRequestError",
        /** @id MochiKit.Async.XMLHttpRequestError */
        function (req, msg) {
            /***

            Raised when an XMLHttpRequest does not complete for any reason.

            ***/
            this.req = req;
            this.message = msg;
            try {
                // Strange but true that this can raise in some cases.
                this.number = req.status;
            } catch (e) {
                // pass
            }
        }
    );


    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
    };

    m.nameFunctions(this);

};

MochiKit.Async.__new__();

MochiKit.Base._exportSymbols(this, MochiKit.Async);

/***
MochiKit.DragAndDrop 1.4

See <http://mochikit.com/> for documentation, downloads, license, etc.

Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
    Mochi-ized By Thomas Herve (_firstname_@nimail.org)

***/

if (typeof(dojo) != 'undefined') {
    dojo.provide('MochiKit.DragAndDrop');
    dojo.require('MochiKit.Base');
    dojo.require('MochiKit.DOM');
    dojo.require('MochiKit.Iter');
    dojo.require('MochiKit.Visual');
    dojo.require('MochiKit.Signal');
}

if (typeof(JSAN) != 'undefined') {
    JSAN.use("MochiKit.Base", []);
    JSAN.use("MochiKit.DOM", []);
    JSAN.use("MochiKit.Visual", []);
    JSAN.use("MochiKit.Iter", []);
    JSAN.use("MochiKit.Signal", []);
}

try {
    if (typeof(MochiKit.Base) == 'undefined' ||
        typeof(MochiKit.DOM) == 'undefined' ||
        typeof(MochiKit.Visual) == 'undefined' ||
        typeof(MochiKit.Signal) == 'undefined' ||
        typeof(MochiKit.Iter) == 'undefined') {
        throw "";
    }
} catch (e) {
    throw "MochiKit.DragAndDrop depends on MochiKit.Base, MochiKit.DOM, MochiKit.Visual, MochiKit.Signal and MochiKit.Iter!";
}

if (typeof(MochiKit.DragAndDrop) == 'undefined') {
    MochiKit.DragAndDrop = {};
}

MochiKit.DragAndDrop.NAME = 'MochiKit.DragAndDrop';
MochiKit.DragAndDrop.VERSION = '1.4';

MochiKit.DragAndDrop.__repr__ = function () {
    return '[' + this.NAME + ' ' + this.VERSION + ']';
};

MochiKit.DragAndDrop.toString = function () {
    return this.__repr__();
};

MochiKit.DragAndDrop.EXPORT = [
    "Droppable",
    "Draggable"
];

MochiKit.DragAndDrop.EXPORT_OK = [
    "Droppables",
    "Draggables"
];

MochiKit.DragAndDrop.Droppables = {
    /***

    Manage all droppables. Shouldn't be used, use the Droppable object instead.

    ***/
    drops: [],

    remove: function (element) {
        this.drops = MochiKit.Base.filter(function (d) {
            return d.element != MochiKit.DOM.getElement(element);
        }, this.drops);
    },

    register: function (drop) {
        this.drops.push(drop);
    },

    unregister: function (drop) {
        this.drops = MochiKit.Base.filter(function (d) {
            return d != drop;
        }, this.drops);
    },

    prepare: function (element) {
        MochiKit.Base.map(function (drop) {
            if (drop.isAccepted(element)) {
                if (drop.options.activeclass) {
                    MochiKit.DOM.addElementClass(drop.element,
                                                 drop.options.activeclass);
                }
                drop.options.onactive(drop.element, element);
            }
        }, this.drops);
    },

    findDeepestChild: function (drops) {
        deepest = drops[0];

        for (i = 1; i < drops.length; ++i) {
            if (MochiKit.DOM.isParent(drops[i].element, deepest.element)) {
                deepest = drops[i];
            }
        }
        return deepest;
    },

    show: function (point, element) {
        if (!this.drops.length) {
            return;
        }
        var affected = [];

        if (this.last_active) {
            this.last_active.deactivate();
        }
        MochiKit.Iter.forEach(this.drops, function (drop) {
            if (drop.isAffected(point, element)) {
                affected.push(drop);
            }
        });
        if (affected.length > 0) {
            drop = this.findDeepestChild(affected);
            MochiKit.Position.within(drop.element, point.page.x, point.page.y);
            drop.options.onhover(element, drop.element,
                MochiKit.Position.overlap(drop.options.overlap, drop.element));
            drop.activate();
        }
    },

    fire: function (event, element) {
        if (!this.last_active) {
            return;
        }
        MochiKit.Position.prepare();

        if (this.last_active.isAffected(event.mouse(), element)) {
            this.last_active.options.ondrop(element,
               this.last_active.element, event);
        }
    },

    reset: function (element) {
        MochiKit.Base.map(function (drop) {
            if (drop.options.activeclass) {
                MochiKit.DOM.removeElementClass(drop.element,
                                                drop.options.activeclass);
            }
            drop.options.ondesactive(drop.element, element);
        }, this.drops);
        if (this.last_active) {
            this.last_active.deactivate();
        }
    }
};

/** @id MochiKit.DragAndDrop.Droppable */
MochiKit.DragAndDrop.Droppable = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.DragAndDrop.Droppable.prototype = {
    /***

    A droppable object. Simple use is to create giving an element:

        new MochiKit.DragAndDrop.Droppable('myelement');

    Generally you'll want to define the 'ondrop' function and maybe the
    'accept' option to filter draggables.

    ***/
    __class__: MochiKit.DragAndDrop.Droppable,

    __init__: function (element, /* optional */options) {
        var d = MochiKit.DOM;
        var b = MochiKit.Base;
        this.element = d.getElement(element);
        this.options = b.update({

            /** @id MochiKit.DragAndDrop.greedy */
            greedy: true,

            /** @id MochiKit.DragAndDrop.hoverclass */
            hoverclass: null,

            /** @id MochiKit.DragAndDrop.activeclass */
            activeclass: null,

            /** @id MochiKit.DragAndDrop.hoverfunc */
            hoverfunc: b.noop,

            /** @id MochiKit.DragAndDrop.accept */
            accept: null,

            /** @id MochiKit.DragAndDrop.onactive */
            onactive: b.noop,

            /** @id MochiKit.DragAndDrop.ondesactive */
            ondesactive: b.noop,

            /** @id MochiKit.DragAndDrop.onhover */
            onhover: b.noop,

            /** @id MochiKit.DragAndDrop.ondrop */
            ondrop: b.noop,

            /** @id MochiKit.DragAndDrop.containment */
            containment: [],
            tree: false
        }, options || {});

        // cache containers
        this.options._containers = [];
        b.map(MochiKit.Base.bind(function (c) {
            this.options._containers.push(d.getElement(c));
        }, this), this.options.containment);

        d.makePositioned(this.element); // fix IE

        MochiKit.DragAndDrop.Droppables.register(this);
    },

    /** @id MochiKit.DragAndDrop.isContained */
    isContained: function (element) {
        if (this.options._containers.length) {
            var containmentNode;
            if (this.options.tree) {
                containmentNode = element.treeNode;
            } else {
                containmentNode = element.parentNode;
            }
            return MochiKit.Iter.some(this.options._containers, function (c) {
                return containmentNode == c;
            });
        } else {
            return true;
        }
    },

    /** @id MochiKit.DragAndDrop.isAccepted */
    isAccepted: function (element) {
        return ((!this.options.accept) || MochiKit.Iter.some(
          this.options.accept, function (c) {
            return MochiKit.DOM.hasElementClass(element, c);
        }));
    },

    /** @id MochiKit.DragAndDrop.isAffected */
    isAffected: function (point, element) {
        return ((this.element != element) &&
                this.isContained(element) &&
                this.isAccepted(element) &&
                MochiKit.Position.within(this.element, point.page.x,
                                                       point.page.y));
    },

    /** @id MochiKit.DragAndDrop.deactivate */
    deactivate: function () {
        /***

        A droppable is deactivate when a draggable has been over it and left.

        ***/
        if (this.options.hoverclass) {
            MochiKit.DOM.removeElementClass(this.element,
                                            this.options.hoverclass);
        }
        this.options.hoverfunc(this.element, false);
        MochiKit.DragAndDrop.Droppables.last_active = null;
    },

    /** @id MochiKit.DragAndDrop.activate */
    activate: function () {
        /***

        A droppable is active when a draggable is over it.

        ***/
        if (this.options.hoverclass) {
            MochiKit.DOM.addElementClass(this.element, this.options.hoverclass);
        }
        this.options.hoverfunc(this.element, true);
        MochiKit.DragAndDrop.Droppables.last_active = this;
    },

    /** @id MochiKit.DragAndDrop.destroy */
    destroy: function () {
        /***

        Delete this droppable.

        ***/
        MochiKit.DragAndDrop.Droppables.unregister(this);
    },

    /** @id MochiKit.DragAndDrop.repr */
    repr: function () {
        return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]";
    }
};

MochiKit.DragAndDrop.Draggables = {
    /***

    Manage draggables elements. Not intended to direct use.

    ***/
    drags: [],

    register: function (draggable) {
        if (this.drags.length === 0) {
            var conn = MochiKit.Signal.connect;
            this.eventMouseUp = conn(document, 'onmouseup', this, this.endDrag);
            this.eventMouseMove = conn(document, 'onmousemove', this,
                                       this.updateDrag);
            this.eventKeypress = conn(document, 'onkeypress', this,
                                      this.keyPress);
        }
        this.drags.push(draggable);
    },

    unregister: function (draggable) {
        this.drags = MochiKit.Base.filter(function (d) {
            return d != draggable;
        }, this.drags);
        if (this.drags.length === 0) {
            var disc = MochiKit.Signal.disconnect;
            disc(this.eventMouseUp);
            disc(this.eventMouseMove);
            disc(this.eventKeypress);
        }
    },

    activate: function (draggable) {
        // allows keypress events if window is not currently focused
        // fails for Safari
        window.focus();
        this.activeDraggable = draggable;
    },

    deactivate: function () {
        this.activeDraggable = null;
    },

    updateDrag: function (event) {
        if (!this.activeDraggable) {
            return;
        }
        var pointer = event.mouse();
        // Mozilla-based browsers fire successive mousemove events with
        // the same coordinates, prevent needless redrawing (moz bug?)
        if (this._lastPointer && (MochiKit.Base.repr(this._lastPointer.page) ==
                                  MochiKit.Base.repr(pointer.page))) {
            return;
        }
        this._lastPointer = pointer;
        this.activeDraggable.updateDrag(event, pointer);
    },

    endDrag: function (event) {
        if (!this.activeDraggable) {
            return;
        }
        this._lastPointer = null;
        this.activeDraggable.endDrag(event);
        this.activeDraggable = null;
    },

    keyPress: function (event) {
        if (this.activeDraggable) {
            this.activeDraggable.keyPress(event);
        }
    },

    notify: function (eventName, draggable, event) {
        MochiKit.Signal.signal(this, eventName, draggable, event);
    }
};

/** @id MochiKit.DragAndDrop.Draggable */
MochiKit.DragAndDrop.Draggable = function (element, options) {
    var cls = arguments.callee;
    if (!(this instanceof cls)) {
        return new cls(element, options);
    }
    this.__init__(element, options);
};

MochiKit.DragAndDrop.Draggable.prototype = {
    /***

    A draggable object. Simple instantiate :

        new MochiKit.DragAndDrop.Draggable('myelement');

    ***/
    __class__ : MochiKit.DragAndDrop.Draggable,

    __init__: function (element, /* optional */options) {
        var v = MochiKit.Visual;
        var b = MochiKit.Base;
        options = b.update({

            /** @id MochiKit.DragAndDrop.handle */
            handle: false,

            /** @id MochiKit.DragAndDrop.starteffect */
            starteffect: function (innerelement) {
                this._savedOpacity = MochiKit.Style.getStyle(innerelement, 'opacity') || 1.0;
                new v.Opacity(innerelement, {duration:0.2, from:this._savedOpacity, to:0.7});
            },
            /** @id MochiKit.DragAndDrop.reverteffect */
            reverteffect: function (innerelement, top_offset, left_offset) {
                var dur = Math.sqrt(Math.abs(top_offset^2) +
                          Math.abs(left_offset^2))*0.02;
                return new v.Move(innerelement,
                            {x: -left_offset, y: -top_offset, duration: dur});
            },

            /** @id MochiKit.DragAndDrop.endeffect */
            endeffect: function (innerelement) {
                new v.Opacity(innerelement, {duration:0.2, from:0.7, to:this._savedOpacity});
            },

            /** @id MochiKit.DragAndDrop.onchange */
            onchange: b.noop,

            /** @id MochiKit.DragAndDrop.zindex */
            zindex: 1000,

            /** @id MochiKit.DragAndDrop.revert */
            revert: false,

            /** @id MochiKit.DragAndDrop.scroll */
            scroll: false,

            /** @id MochiKit.DragAndDrop.scrollSensitivity */
            scrollSensitivity: 20,

            /** @id MochiKit.DragAndDrop.scrollSpeed */
            scrollSpeed: 15,
            // false, or xy or [x, y] or function (x, y){return [x, y];}

            /** @id MochiKit.DragAndDrop.snap */
            snap: false
        }, options || {});

        var d = MochiKit.DOM;
        this.element = d.getElement(element);

        if (options.handle && (typeof(options.handle) == 'string')) {
            this.handle = d.getFirstElementByTagAndClassName(null,
                                       options.handle, this.element);
        }
        if (!this.handle) {
            this.handle = d.getElement(options.handle);
        }
        if (!this.handle) {
            this.handle = this.element;
        }

        if (options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) {
            options.scroll = d.getElement(options.scroll);
            this._isScrollChild = MochiKit.DOM.isChildNode(this.element, options.scroll);
        }

        d.makePositioned(this.element);  // fix IE

        this.delta = this.currentDelta();
        this.options = options;
        this.dragging = false;

        this.eventMouseDown = MochiKit.Signal.connect(this.handle,
                              'onmousedown', this, this.initDrag);
        MochiKit.DragAndDrop.Draggables.register(this);
    },

    /** @id MochiKit.DragAndDrop.destroy */
    destroy: function () {
        MochiKit.Signal.disconnect(this.eventMouseDown);
        MochiKit.DragAndDrop.Draggables.unregister(this);
    },

    /** @id MochiKit.DragAndDrop.currentDelta */
    currentDelta: function () {
        var s = MochiKit.Style.getStyle;
        return [
          parseInt(s(this.element, 'left') || '0'),
          parseInt(s(this.element, 'top') || '0')];
    },

    /** @id MochiKit.DragAndDrop.initDrag */
    initDrag: function (event) {
        if (!event.mouse().button.left) {
            return;
        }
        // abort on form elements, fixes a Firefox issue
        var src = event.target();
        var tagName = (src.tagName || '').toUpperCase();
        if (tagName === 'INPUT' || tagName === 'SELECT' ||
            tagName === 'OPTION' || tagName === 'BUTTON' ||
            tagName === 'TEXTAREA') {
            return;
        }

        if (this._revert) {
            this._revert.cancel();
            this._revert = null;
        }

        var pointer = event.mouse();
        var pos = MochiKit.Position.cumulativeOffset(this.element);
        this.offset = [pointer.page.x - pos.x, pointer.page.y - pos.y];

        MochiKit.DragAndDrop.Draggables.activate(this);
        event.stop();
    },

    /** @id MochiKit.DragAndDrop.startDrag */
    startDrag: function (event) {
        this.dragging = true;
        if (this.options.selectclass) {
            MochiKit.DOM.addElementClass(this.element,
                                         this.options.selectclass);
        }
        if (this.options.zindex) {
            this.originalZ = parseInt(MochiKit.Style.getStyle(this.element,
                                      'z-index') || '0');
            this.element.style.zIndex = this.options.zindex;
        }

        if (this.options.ghosting) {
            this._clone = this.element.cloneNode(true);
            this.ghostPosition = MochiKit.Position.absolutize(this.element);
            this.element.parentNode.insertBefore(this._clone, this.element);
        }

        if (this.options.scroll) {
            if (this.options.scroll == window) {
                var where = this._getWindowScroll(this.options.scroll);
                this.originalScrollLeft = where.left;
                this.originalScrollTop = where.top;
            } else {
                this.originalScrollLeft = this.options.scroll.scrollLeft;
                this.originalScrollTop = this.options.scroll.scrollTop;
            }
        }

        MochiKit.DragAndDrop.Droppables.prepare(this.element);
        MochiKit.DragAndDrop.Draggables.notify('start', this, event);
        if (this.options.starteffect) {
            this.options.starteffect(this.element);
        }
    },

    /** @id MochiKit.DragAndDrop.updateDrag */
    updateDrag: function (event, pointer) {
        if (!this.dragging) {
            this.startDrag(event);
        }
        MochiKit.Position.prepare();
        MochiKit.DragAndDrop.Droppables.show(pointer, this.element);
        MochiKit.DragAndDrop.Draggables.notify('drag', this, event);
        this.draw(pointer);
        this.options.onchange(this);

        if (this.options.scroll) {
            this.stopScrolling();
            var p, q;
            if (this.options.scroll == window) {
                var s = this._getWindowScroll(this.options.scroll);
                p = new MochiKit.Style.Coordinates(s.left, s.top);
                q = new MochiKit.Style.Coordinates(s.left + s.width,
                                                   s.top + s.height);
            } else {
                p = MochiKit.Position.page(this.options.scroll);
                p.x += this.options.scroll.scrollLeft;
                p.y += this.options.scroll.scrollTop;
                p.x += (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0);
                p.y += (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
                q = new MochiKit.Style.Coordinates(p.x + this.options.scroll.offsetWidth,
                                                   p.y + this.options.scroll.offsetHeight);
            }
            var speed = [0, 0];
            if (pointer.page.x > (q.x - this.options.scrollSensitivity)) {
                speed[0] = pointer.page.x - (q.x - this.options.scrollSensitivity);
            } else if (pointer.page.x < (p.x + this.options.scrollSensitivity)) {
                speed[0] = pointer.page.x - (p.x + this.options.scrollSensitivity);
            }
            if (pointer.page.y > (q.y - this.options.scrollSensitivity)) {
                speed[1] = pointer.page.y - (q.y - this.options.scrollSensitivity);
            } else if (pointer.page.y < (p.y + this.options.scrollSensitivity)) {
                speed[1] = pointer.page.y - (p.y + this.options.scrollSensitivity);
            }
            this.startScrolling(speed);
        }

        // fix AppleWebKit rendering
        if (/AppleWebKit'/.test(navigator.appVersion)) {
            window.scrollBy(0, 0);
        }
        event.stop();
    },

    /** @id MochiKit.DragAndDrop.finishDrag */
    finishDrag: function (event, success) {
        var dr = MochiKit.DragAndDrop;
        this.dragging = false;
        if (this.options.selectclass) {
            MochiKit.DOM.removeElementClass(this.element,
                                            this.options.selectclass);
        }

        if (this.options.ghosting) {
            // XXX: from a user point of view, it would be better to remove
            // the node only *after* the MochiKit.Visual.Move end when used
            // with revert.
            MochiKit.Position.relativize(this.element, this.ghostPosition);
            MochiKit.DOM.removeElement(this._clone);
            this._clone = null;
        }

        if (success) {
            dr.Droppables.fire(event, this.element);
        }
        dr.Draggables.notify('end', this, event);

        var revert = this.options.revert;
        if (revert && typeof(revert) == 'function') {
            revert = revert(this.element);
        }

        var d = this.currentDelta();
        if (revert && this.options.reverteffect) {
            this._revert = this.options.reverteffect(this.element,
                d[1] - this.delta[1], d[0] - this.delta[0]);
        } else {
            this.delta = d;
        }

        if (this.options.zindex) {
            this.element.style.zIndex = this.originalZ;
        }

        if (this.options.endeffect) {
            this.options.endeffect(this.element);
        }

        dr.Draggables.deactivate();
        dr.Droppables.reset(this.element);
    },

    /** @id MochiKit.DragAndDrop.keyPress */
    keyPress: function (event) {
        if (event.key().string != "KEY_ESCAPE") {
            return;
        }
        this.finishDrag(event, false);
        event.stop();
    },

    /** @id MochiKit.DragAndDrop.endDrag */
    endDrag: function (event) {
        if (!this.dragging) {
            return;
        }
        this.stopScrolling();
        this.finishDrag(event, true);
        event.stop();
    },

    /** @id MochiKit.DragAndDrop.draw */
    draw: function (point) {
        var pos = MochiKit.Position.cumulativeOffset(this.element);
        var d = this.currentDelta();
        pos.x -= d[0];
        pos.y -= d[1];

        if (this.options.scroll && (this.options.scroll != window && this._isScrollChild)) {
            pos.x -= this.options.scroll.scrollLeft - this.originalScrollLeft;
            pos.y -= this.options.scroll.scrollTop - this.originalScrollTop;
        }

        var p = [point.page.x - pos.x - this.offset[0],
                 point.page.y - pos.y - this.offset[1]];

        if (this.options.snap) {
            if (typeof(this.options.snap) == 'function') {
                p = this.options.snap(p[0], p[1]);
            } else {
                if (this.options.snap instanceof Array) {
                    var i = -1;
                    p = MochiKit.Base.map(MochiKit.Base.bind(function (v) {
                            i += 1;
                            return Math.round(v/this.options.snap[i]) *
                                   this.options.snap[i];
                        }, this), p);
                } else {
                    p = MochiKit.Base.map(MochiKit.Base.bind(function (v) {
                        return Math.round(v/this.options.snap) *
                               this.options.snap;
                        }, this), p);
                }
            }
        }
        var style = this.element.style;
        if ((!this.options.constraint) ||
            (this.options.constraint == 'horizontal')) {
            style.left = p[0] + 'px';
        }
        if ((!this.options.constraint) ||
            (this.options.constraint == 'vertical')) {
            style.top = p[1] + 'px';
        }
        if (style.visibility == 'hidden') {
            style.visibility = '';  // fix gecko rendering
        }
    },

    /** @id MochiKit.DragAndDrop.stopScrolling */
    stopScrolling: function () {
        if (this.scrollInterval) {
            clearInterval(this.scrollInterval);
            this.scrollInterval = null;
            MochiKit.DragAndDrop.Draggables._lastScrollPointer = null;
        }
    },

    /** @id MochiKit.DragAndDrop.startScrolling */
    startScrolling: function (speed) {
        if (!speed[0] && !speed[1]) {
            return;
        }
        this.scrollSpeed = [speed[0] * this.options.scrollSpeed,
                            speed[1] * this.options.scrollSpeed];
        this.lastScrolled = new Date();
        this.scrollInterval = setInterval(MochiKit.Base.bind(this.scroll, this), 10);
    },

    /** @id MochiKit.DragAndDrop.scroll */
    scroll: function () {
        var current = new Date();
        var delta = current - this.lastScrolled;
        this.lastScrolled = current;

        if (this.options.scroll == window) {
            var s = this._getWindowScroll(this.options.scroll);
            if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
                var dm = delta / 1000;
                this.options.scroll.scrollTo(s.left + dm * this.scrollSpeed[0],
                                             s.top + dm * this.scrollSpeed[1]);
            }
        } else {
            this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
            this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000;
        }

        var d = MochiKit.DragAndDrop;

        MochiKit.Position.prepare();
        d.Droppables.show(d.Draggables._lastPointer, this.element);
        d.Draggables.notify('drag', this);
        if (this._isScrollChild) {
            d.Draggables._lastScrollPointer = d.Draggables._lastScrollPointer || d.Draggables._lastPointer;
            d.Draggables._lastScrollPointer.x += this.scrollSpeed[0] * delta / 1000;
            d.Draggables._lastScrollPointer.y += this.scrollSpeed[1] * delta / 1000;
            if (d.Draggables._lastScrollPointer.x < 0) {
                d.Draggables._lastScrollPointer.x = 0;
            }
            if (d.Draggables._lastScrollPointer.y < 0) {
                d.Draggables._lastScrollPointer.y = 0;
            }
            this.draw(d.Draggables._lastScrollPointer);
        }

        this.options.onchange(this);
    },

    _getWindowScroll: function (win) {
        var vp, w, h;
        MochiKit.DOM.withWindow(win, function () {
            vp = MochiKit.Style.getViewportPosition(win.document);
        });
        if (win.innerWidth) {
            w = win.innerWidth;
            h = win.innerHeight;
        } else if (win.document.documentElement && win.document.documentElement.clientWidth) {
            w = win.document.documentElement.clientWidth;
            h = win.document.documentElement.clientHeight;
        } else {
            w = win.document.body.offsetWidth;
            h = win.document.body.offsetHeight;
        }
        return {top: vp.x, left: vp.y, width: w, height: h};
    },

    /** @id MochiKit.DragAndDrop.repr */
    repr: function () {
        return '[' + this.__class__.NAME + ", options:" + MochiKit.Base.repr(this.options) + "]";
    }
};

MochiKit.DragAndDrop.__new__ = function () {
    MochiKit.Base.nameFunctions(this);

    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
    };
};

MochiKit.DragAndDrop.__new__();

MochiKit.Base._exportSymbols(this, MochiKit.DragAndDrop);



  // ===================================================================
  // Author : Matt Kruse <matt@ajaxtoolbox.com>
  // WWW    : http://www.ajaxtoolbox.com/
  // ===================================================================

  function AjaxRequest() {
  
    var req               = new Object();
    req.timeout           = null;
    req.generateUniqueUrl = true;
    req.url               = window.location.href;
    req.method            = "GET";
    req.async             = true;
    req.username          = null;
    req.password          = null;
    req.parameters        = new Object();
    req.requestIndex      = AjaxRequest.numAjaxRequests++;
    req.responseReceived  = false;
    req.groupName         = null;
    req.queryString       = "";
    req.responseText      = null;
    req.responseXML       = null;
    req.status            = null;
    req.statusText        = null;
    req.aborted           = false;
    req.xmlHttpRequest    = null;
  
    req.onTimeout         = null; 
    req.onLoading         = null;
    req.onLoaded          = null;
    req.onInteractive     = null;
    req.onComplete        = null;
    req.onSuccess         = null;
    req.onError           = null;
    req.onGroupBegin      = null;
    req.onGroupEnd        = null;
  
    req.xmlHttpRequest    = AjaxRequest.getXmlHttpRequest();

    if (req.xmlHttpRequest==null) { return null; } // end if
    
    req.xmlHttpRequest.onreadystatechange = function() {

      if (req==null || req.xmlHttpRequest==null) { return; } // end if
      if (req.xmlHttpRequest.readyState==1) { req.onLoadingInternal(req);     } // end if
      if (req.xmlHttpRequest.readyState==2) { req.onLoadedInternal(req);      } // end if
      if (req.xmlHttpRequest.readyState==3) { req.onInteractiveInternal(req); } // end if
      if (req.xmlHttpRequest.readyState==4) { req.onCompleteInternal(req);    } // end if

    }; // end function()
    
    req.onLoadingInternalHandled     = false;
    req.onLoadedInternalHandled      = false;
    req.onInteractiveInternalHandled = false;
    req.onCompleteInternalHandled    = false;
    req.onLoadingInternal = function() {

      if (req.onLoadingInternalHandled) { return; } // end if

      AjaxRequest.numActiveAjaxRequests++;

      if (AjaxRequest.numActiveAjaxRequests==1 && typeof(window['AjaxRequestBegin'])=="function") {

        AjaxRequestBegin();

      } // end if

      if (req.groupName!=null) {

        if (typeof(AjaxRequest.numActiveAjaxGroupRequests[req.groupName])=="undefined") {

          AjaxRequest.numActiveAjaxGroupRequests[req.groupName] = 0;

        } // end if

        AjaxRequest.numActiveAjaxGroupRequests[req.groupName]++;

        if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==1 && typeof(req.onGroupBegin)=="function") {

          req.onGroupBegin(req.groupName);

        } // end if

      } // end if

      if (typeof(req.onLoading)=="function") {

        req.onLoading(req);

      } // end if

      req.onLoadingInternalHandled = true;

    }; // end function()

    req.onLoadedInternal = function() {

      if (req.onLoadedInternalHandled) { return; } // end if

      if (typeof(req.onLoaded)=="function") {

        req.onLoaded(req);

      } // end if

      req.onLoadedInternalHandled = true;

    }; // end function()

    req.onInteractiveInternal = function() {

      if (req.onInteractiveInternalHandled) { return; } // end if

      if (typeof(req.onInteractive)=="function") {

        req.onInteractive(req);

      } // end if

      req.onInteractiveInternalHandled = true;

    }; // end function()

    req.onCompleteInternal = function() {

      if (req.onCompleteInternalHandled || req.aborted) { return; } // end if

      req.onCompleteInternalHandled = true;

      AjaxRequest.numActiveAjaxRequests--;

      if (AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function") {

        AjaxRequestEnd(req.groupName);

      } // end if

      if (req.groupName!=null) {

        AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;

        if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function") {

          req.onGroupEnd(req.groupName);

        } // end if

      } // end if

      req.responseReceived = true;
      req.status           = req.xmlHttpRequest.status;
      req.statusText       = req.xmlHttpRequest.statusText;
      req.responseText     = req.xmlHttpRequest.responseText;
      req.responseXML      = req.xmlHttpRequest.responseXML;

      if (typeof(req.onComplete)=="function") {

        req.onComplete(req);

      } // end if

      if (req.xmlHttpRequest.status==200 && typeof(req.onSuccess)=="function") {

        req.onSuccess(req);

      } else if (typeof(req.onError)=="function") {

        req.onError(req);

      } // end if

      delete req.xmlHttpRequest['onreadystatechange'];
      req.xmlHttpRequest = null;

    }; // end function()

    req.onTimeoutInternal = function() {

      if (req!=null && req.xmlHttpRequest!=null && !req.onCompleteInternalHandled) {

        req.aborted = true;
        req.xmlHttpRequest.abort();
        AjaxRequest.numActiveAjaxRequests--;

        if (AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function") {

          AjaxRequestEnd(req.groupName);

        } // end if

        if (req.groupName!=null) {

          AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;

          if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function") {

            req.onGroupEnd(req.groupName);

          } // end if

        } // end if

        if (typeof(req.onTimeout)=="function") {

          req.onTimeout(req);

        } // end if

        delete req.xmlHttpRequest['onreadystatechange'];
        req.xmlHttpRequest = null;

      } // end if

    }; // end function()
  
    req.process = function() {

      if (req.xmlHttpRequest!=null) {

        if (req.generateUniqueUrl && req.method=="GET") {

          req.parameters["AjaxRequestUniqueId"] = new Date().getTime() + "" + req.requestIndex;

        } // end if

        var content = null; // For POST requests, to hold query string

        for (var i in req.parameters) {

          if (req.queryString.length>0) { req.queryString += "&"; }  // end if

          req.queryString += encodeURIComponent(i) + "=" + encodeURIComponent(req.parameters[i]);

        } // end for

        if (req.method=="GET") {

          if (req.queryString.length>0) {

            req.url += ((req.url.indexOf("?")>-1)?"&":"?") + req.queryString;

          } // end if

        } // end if

        req.xmlHttpRequest.open(req.method,req.url,req.async,req.username,req.password);

        if (req.method=="POST") {

          if (typeof(req.xmlHttpRequest.setRequestHeader)!="undefined") {

            req.xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

          } // end if

          content = req.queryString;

        } // end if

        if (req.timeout>0) {

          setTimeout(req.onTimeoutInternal,req.timeout);

        } // end if

        req.xmlHttpRequest.send(content);

      } // end if

    }; // end function()
  
    req.handleArguments = function(args) {

      for (var i in args) {

        if (typeof(req[i])=="undefined") {

          req.parameters[i] = args[i];

        } else {

          req[i] = args[i];

        } // end if

      } // end for

    }; // end function()
  
    req.getAllResponseHeaders = function() {

      if (req.xmlHttpRequest!=null) {

        if (req.responseReceived) {

          return req.xmlHttpRequest.getAllResponseHeaders();

        } // end if

        alert("Cannot getAllResponseHeaders because a response has not yet been received");

      } // end if

    }; // end function()

    req.getResponseHeader = function(headerName) {

      if (req.xmlHttpRequest!=null) {

        if (req.responseReceived) {

          return req.xmlHttpRequest.getResponseHeader(headerName);

        } // end if

        alert("Cannot getResponseHeader because a response has not yet been received");

      } // end if

    }; // end function()
  
    return req;

  } // end class AjaxRequest
  
  AjaxRequest.getXmlHttpRequest = function() {

    if (window.XMLHttpRequest) {

      return new XMLHttpRequest();

    } else if (window.ActiveXObject) {

      // Based on http://jibbering.com/2002/4/httprequest.html
      /*@cc_on @*/
      /*@if (@_jscript_version >= 5)
      try {
        return new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
          return null;
        }
      }
      @end @*/

    } else {

      return null;

    } // end if

  }; // end getXmlHttpRequest()
  
  AjaxRequest.isActive = function() {

    return (AjaxRequest.numActiveAjaxRequests>0);

  }; // end isActive()
  
  AjaxRequest.get = function(args) {

    AjaxRequest.doRequest("GET",args);

  }; // end get()
  
  AjaxRequest.post = function(args) {

    AjaxRequest.doRequest("POST",args);

  }; // end post()
  
  AjaxRequest.doRequest = function(method,args) {

    if (typeof(args)!="undefined" && args!=null) {

      var myRequest    = new AjaxRequest();
      myRequest.method = method;
      myRequest.handleArguments(args);
      myRequest.process();

    } // end if

  }; // end doRequest
  
  AjaxRequest.submit = function(theform, args) {

    var myRequest         = new AjaxRequest();

    if (myRequest==null) { return false; } // end if

    var serializedForm    = AjaxRequest.serializeForm(theform);
    myRequest.method      = theform.method.toUpperCase();
    myRequest.url         = theform.action;
    myRequest.handleArguments(args);
    myRequest.queryString = serializedForm;
    myRequest.process();
    return true;

  }; // end submit()
  
  AjaxRequest.serializeForm = function(theform) {

    var els         = theform.elements;
    var len         = els.length;
    var queryString = "";

    this.addField = function(name,value) { 

        if (queryString.length>0) { 

          queryString += "&";

        } // end if

        queryString += encodeURIComponent(name) + "=" + encodeURIComponent(value);

    }; // end function()

    for (var i=0; i<len; i++) {

      var el = els[i];

      if (!el.disabled) {

        switch(el.type) {

          case 'text':
          case 'password':
          case 'hidden':
          case 'textarea': 

            this.addField(el.name,el.value);

            break;

          case 'select-one':

            if (el.selectedIndex>=0) {

              this.addField(el.name,el.options[el.selectedIndex].value);

            } // end if

            break;

          case 'select-multiple':

            for (var j=0; j<el.options.length; j++) {

              if (el.options[j].selected) {

                this.addField(el.name,el.options[j].value);

              } // end if

            } // end for

            break;

          case 'checkbox':
          case 'radio':

            if (el.checked) {

              this.addField(el.name,el.value);

            } // end if

            break;

        } // end switch

      } // end if

    } // end for

    return queryString;

  }; // end serializeForm()
  
  AjaxRequest.numActiveAjaxRequests      = 0;
  AjaxRequest.numActiveAjaxGroupRequests = new Object();
  AjaxRequest.numAjaxRequests            = 0;
/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }

/*
 * Perform a simple self-test to see if the VM is working
 */
function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);

}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

/*
 * Convert an array of little-endian words to a string
 */
function binl2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  return str;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of little-endian words to a base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}

    var misc_vars  = new Object();

    var tabs       = new Object();
    var active_tab = "";

    var detect = navigator.userAgent.toLowerCase();
    var OS,browser,version,total,thestring;

    if (check_it('konqueror')) {
      browser = "Konqueror";
      OS      = "Linux";
    } else if (check_it('safari'))  { browser = "Safari";            }
      else if (check_it('omniweb')) { browser = "OmniWeb";           }
      else if (check_it('opera'))   { browser = "Opera";             }
      else if (check_it('webtv'))   { browser = "WebTV";             }
      else if (check_it('icab'))    { browser = "iCab";              }
      else if (check_it('msie'))    { browser = "Internet Explorer"; }
      else if (!check_it('compatible')) {
            browser = "Netscape Navigator";
            version = detect.charAt(8);
    } else { browser = "An unknown browser"; }

    if (!version) version = detect.charAt(place + thestring.length);

    if (!OS) {
      if (check_it('linux'))    { OS = "Linux";   }
      else if (check_it('x11')) { OS = "Unix";    }
      else if (check_it('mac')) { OS = "Mac"      }
      else if (check_it('win')) { OS = "Windows"  }
      else { OS = "an unknown operating system"; }
    } // end if

    function check_it(string) {

      place     = detect.indexOf(string) + 1;
      thestring = string;
      return place;

    } // end check_it()

    function ie_getElementsByTagName(str) {

      // Map to the all collections
      if (str=="*") {
        return document.all;
      } else {
        return document.all.tags(str);
      }

    } // end ie_getElementsByTagName()

    if (document.all) { document.getElementsByTagName = ie_getElementsByTagName; } // end if

    delete_form_lookup = new Object();
    notified           = new Object();
    selected_menu_item = "";

    function submit_delete_form(mik_md5) {

      if (delete_form_lookup[mik_md5]) {

        if (MochiKit.DOM.getElement('form_'+delete_form_lookup[mik_md5])) {

          MochiKit.DOM.getElement('form_'+delete_form_lookup[mik_md5]).submit();

        } // end if

      } // end if

    } // end submit_delete_form()
    
    function toggle_menu_item(item_idnum,prepend,a,b) {

      if (!a)       { a = "menu_item_parent"; } // end if
      if (!b)       { b = "menu_item_child";  } // end if
      if (!prepend) { prepend = ""; } // end if

      a_parent = prepend+"menu_parent_"+item_idnum;

      if (MochiKit.DOM.getElement(a_parent)) {

        if ((prepend+item_idnum) != selected_menu_item) {

          a_child  = prepend+"menu_child_"+item_idnum;
          a_img    = prepend+"menu_child_img_"+item_idnum;

          if (MochiKit.DOM.getElement(a_parent).className == a+"_off") { MochiKit.DOM.getElement(a_parent).className = a+"_on"; }; // else { MochiKit.DOM.getElement(a_parent).className = a+"_on"; } // end if

          if (MochiKit.DOM.getElement(a_child).className  == b+"_off")  {

            MochiKit.DOM.getElement(a_child).className  = b+"_on";

          } else {

            MochiKit.DOM.getElement(a_child).className  = b+"_off";

            } // end if
            
          if (MochiKit.DOM.getElement(a_child).className  == b+"_off" && MochiKit.DOM.getElement(a_img))  {

            MochiKit.DOM.getElement(a_img).src          = "images/menu_item_on.gif";

          } else if (MochiKit.DOM.getElement(a_img)){

            MochiKit.DOM.getElement(a_img).src          = "images/menu_item_off.gif";

            } // end if            

        } // end if

      } // end if

    } // end toggle_menu_item()

    function set_tab_type(idnum,type) {

      MochiKit.DOM.getElement("tab"+idnum+"_a_img").src = "images/spacer.gif";

      switch (type) {

        case "regular_selected":

          MochiKit.DOM.getElement("tab"+idnum+"_b_img").src   = "images/tab_on_left.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_d_img").src   = "images/tab_on_right.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_c").className = "tab_regular_on_middle";
          break;

        case "regular_unselected":

          MochiKit.DOM.getElement("tab"+idnum+"_b_img").src   = "images/tab_off_left.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_d_img").src   = "images/tab_off_right.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_c").className = "tab_regular_off_middle";
          break;

        case "end_selected":

          MochiKit.DOM.getElement("tab"+idnum+"_b_img").src   = "images/right_tab_on_left.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_d_img").src   = "images/right_tab_on_right.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_c").className = "right_tab_on_middle";
          break;

        case "end_unselected":

          MochiKit.DOM.getElement("tab"+idnum+"_b_img").src   = "images/right_tab_off_left.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_d_img").src   = "images/right_tab_off_right.gif";
          MochiKit.DOM.getElement("tab"+idnum+"_c").className = "right_tab_off_middle";
          break;

      } // end switch

    } // end set_tab_type()

    function load_tab(current_tab,to_tab,form_id) {

      if (current_tab == to_tab) { return false; } else { MochiKit.DOM.getElement(form_id).submit(); return true; } // end if

      if (active_tab  == "tab" ) {

        MochiKit.DOM.getElement(form_id).submit();
        return true;

      } // end if

      if (current_tab != to_tab) {

        export_form_values('form_0');

        if (tabs[to_tab]["json"]) {

          restore_form_values(to_tab,'form_0');

          for (x=0;x<MochiKit.DOM.getElement('form_0').elements.length;x++) {

            tid  = MochiKit.DOM.getElement('form_0').elements[x].id;
            tv   = MochiKit.DOM.getElement('form_0').elements[x].value;
            tdiv = (tid.substr(0,(tid.length-6))+"div");

            if (tv == "Show / Edit Note") { toggle_object(tdiv,"off"); } else if (tv == "Hide Note") { toggle_object(tdiv,"on"); } // end if

          } // end for

          tindex = current_tab.substr(3);

          tab_class = MochiKit.DOM.getElement(current_tab+"_c").className;
          if (tab_class == "tab_regular_on_middle")  { set_tab_type(tindex,"regular_unselected"); } // end if
          if (tab_class == "right_tab_on_middle")    { set_tab_type(tindex,"end_unselected"); } // end if

          tindex = to_tab.substr(3);

          tab_class = MochiKit.DOM.getElement(to_tab+"_c").className;
          if (tab_class == "tab_regular_off_middle") { set_tab_type(tindex,"regular_selected"); } // end if
          if (tab_class == "right_tab_off_middle")   { set_tab_type(tindex,"end_selected"); } // end if

          active_tab = to_tab;

          return false;

        } else {

          var tabs_field = document.createElement("input");
          tabs_field.setAttribute("name","current_tabs");
          tabs_field.setAttribute("id","current_tabs");
          tabs_field.setAttribute("type","hidden");
          tabs_field.setAttribute("value", encodeBase64(MochiKit.Base.serializeJSON(tabs)));
          MochiKit.DOM.getElement(form_id).appendChild(tabs_field);

          MochiKit.DOM.getElement(form_id).submit();
          return true;

        } // end if

      } // end if

    } // end load_tab
    
    function htmlentities(text) {

      var v = text;
      var s = "";
      var c;

      for (var i=0;i<v.length;i++) {

        c  = v.charAt(i);
        c  = c.charCodeAt(0);
        c  = '&#'+ c + ';'
        s += c;

      } // end for
      
      return s;

    } // end htmlentities()
    
    function auto_save(form,refresh_speed) {
    
      window.status = "Auto-saving form...";

      AjaxRequest.submit(form,{

        'url'         : MochiKit.DOM.getElement(form).action,
        'onSuccess'   : function(req){ 
                          window.status = "Done";
                        }
      });        

      init_auto_save(form,refresh_speed);
    
    } // end auto_save()
    
    function init_auto_save(form,refresh_speed) {
    
      if (!refresh_speed) { refresh_speed = 300000; } // end if
    
      window.setTimeout("auto_save('"+form+"',"+refresh_speed+")",refresh_speed);

    } // end init_auto_save()
    
    function export_form_values(form) {

      output = new Array();
      style  = new Array();

      for (x=0;x<MochiKit.DOM.getElement(form).elements.length;x++) {

        tstyle             = new Object();

        output[x]          = new Object();
        output[x]["id"]    = MochiKit.DOM.getElement(form).elements[x].id;
        output[x]["name"]  = MochiKit.DOM.getElement(form).elements[x].name;
        output[x]["value"] = MochiKit.DOM.getElement(form).elements[x].value;

      } // end for

      tabs[active_tab]         = new Object();
      tabs[active_tab]["link"] = document.URL;
      tabs[active_tab]["json"] = MochiKit.Base.serializeJSON(output);

    } // end export_form_values()

    function restore_form_values(from_tab,to_form) {

      output  = new Array();
      output  = MochiKit.Base.evalJSON(tabs[from_tab]["json"]);
      theform = MochiKit.DOM.getElement(to_form);

      for (var x in output) {

        theform.elements[x].id    = output[x]["id"];
        theform.elements[x].name  = output[x]["name"];
        theform.elements[x].value = output[x]["value"];

      } // end for

    } // end restore_form_values()

    function print_r(theObj) {

      if (theObj) {

        var output = "";

        if ((theObj.constructor==Array)||(theObj.constructor==Object)) {

          output += "<ul>";

          for (var p in theObj) {

            if ((theObj[p].constructor==Array)||(theObj[p].constructor==Object)) {

              output += "<li>["+p+"] => "+(typeof theObj)+"</li>";
              output += "<ul>";
              output += print_r(theObj[p]);
              output += "</ul>";

            } else {

              output += "<li>["+p+"] => "+theObj[p]+"</li>";

            } // end if

          } // end for

          output += "</ul>";

        } // end if

        return output;
        
      } // end if

    } // end print_r
    
    function list_add_item(e) {
    
      if (!confirm('Are you sure you want to add this item?')) { return; } // end if

      list          = new Object();
      
      window.status = "Adding item to list...";

      if (!e) { window.status = "done"; return false; } // end if

      if (is_string(e)) {

        var target = MochiKit.DOM.getElement(e);

      } // end if

      if (e) {

        if (!target) {
        
          var target        = detectTarget(e);

        } // end if

        list["id"]          = target.id.substring(0,(target.id.length-13))+target.id.substring((target.id.length-2),target.id.length);
        list["list_name"]   = list_name;
        list["name"]        = MochiKit.DOM.getElement(list["id"]).name.substring(0,MochiKit.DOM.getElement(list["id"]).name.length-2);
        list["options"]     = new Object();
        
        rrv_length = 0;

        for (var chi = list["id"].length; chi > 0; chi--) {

          if (list["id"].substring(chi, chi-1) == "_") { chi = 0; } // end if

          rrv_length++;

        } // end for
        
        if (MochiKit.DOM.getElement(list["name"]+"_text"+(list["id"].substring(list["id"].length-rrv_length,list["id"].length))).value) {

          select_add_option(list["id"],null,MochiKit.DOM.getElement(list["name"]+"_text"+(list["id"].substring(list["id"].length-rrv_length,list["id"].length))).value);
        
          for (x=0;x<MochiKit.DOM.getElement(list["id"]).options.length;x++) {
        
            list["options"][x]          = new Object();
            list["options"][x]["value"] = MochiKit.DOM.getElement(list["id"]).options[x].value;
            list["options"][x]["text"]  = MochiKit.DOM.getElement(list["id"]).options[x].text;

          } // end for

          var to_post         = list;

          AjaxRequest.post({
  
            'url'         : 'ajax/list.php',
            'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_post))),
            'onSuccess'   : function(req){ 
                              eval(req.responseText);
                              window.status = "Done";
                            }
          });        
          
        } // end if

      } // end if

    } // end list_add_item()
    
    function list_rename_item(e) {

      if (!confirm('Are you sure you want to rename this item with the text currently typed into the "Add This Item" text box?')) { return; } // end if

      list          = new Object();

      window.status = "Renaming item in list...";

      if (!e) { window.status = "done"; return false; } // end if

      if (is_string(e)) {

        var target = MochiKit.DOM.getElement(e);

      } // end if

      if (e) {

        if (!target) {

          var target        = detectTarget(e);

        } // end if

        list["id"]          = target.id.substring(0,(target.id.length-16))+target.id.substring((target.id.length-2),target.id.length);
        list["list_name"]   = list_name;
        list["name"]        = MochiKit.DOM.getElement(list["id"]).name.substring(0,MochiKit.DOM.getElement(list["id"]).name.length-2);
        list["options"]     = new Object();

        index               = MochiKit.DOM.getElement(list["id"]).selectedIndex;

        rrv_length = 0;

        for (var chi = list["id"].length; chi > 0; chi--) {

          if (list["id"].substring(chi, chi-1) == "_") { chi = 0; } // end if

          rrv_length++;

        } // end for
        
        if (MochiKit.DOM.getElement(list["name"]+"_text"+(list["id"].substring(list["id"].length-rrv_length,list["id"].length))).value) {

          MochiKit.DOM.getElement(list["id"]).options[index].text  = MochiKit.DOM.getElement(list["name"]+"_text"+(list["id"].substring(list["id"].length-rrv_length,list["id"].length))).value;
          MochiKit.DOM.getElement(list["name"]+"_text"+(list["id"].substring(list["id"].length-rrv_length,list["id"].length))).value = "";

          for (x=0;x<MochiKit.DOM.getElement(list["id"]).options.length;x++) {

            list["options"][x]          = new Object();
            list["options"][x]["value"] = MochiKit.DOM.getElement(list["id"]).options[x].value;
            list["options"][x]["text"]  = MochiKit.DOM.getElement(list["id"]).options[x].text;

          } // end for

          var to_post         = list;

          AjaxRequest.post({
  
            'url'         : 'ajax/list.php',
            'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_post))),
            'onSuccess'   : function(req){ 
                              eval(req.responseText);
                              window.status = "Done";
                            }
          });        

        } // end if

      } // end if

    } // end list_rename_item()
    
    function list_delete_item(e) {

      if (confirm("Are you sure you want to delete this item?")) {

        list = new Object();

        window.status                       = "Deleting item from list...";

        if (!e) { window.status = "done"; return false; } // end if

        if (is_string(e)) {

          var target = MochiKit.DOM.getElement(e);

        } // end if

        if (e) {

          if (!target) {

            var target        = detectTarget(e);

          } // end if

          list["id"]          = target.id.substring(0,(target.id.length-16))+target.id.substring((target.id.length-2),target.id.length);
          list["list_name"]   = list_name;
          list["name"]        = MochiKit.DOM.getElement(list["id"]).name;
          list["options"]     = new Object();

          index               = MochiKit.DOM.getElement(list["id"]).selectedIndex;

          MochiKit.DOM.getElement(list["id"]).options[index] = null;

          for (x=0;x<MochiKit.DOM.getElement(list["id"]).options.length;x++) {

            list["options"][x]          = new Object();
            list["options"][x]["value"] = MochiKit.DOM.getElement(list["id"]).options[x].value;
            list["options"][x]["text"]  = MochiKit.DOM.getElement(list["id"]).options[x].text;

          } // end for

          var to_post         = list;

          AjaxRequest.post({
  
            'url'         : 'ajax/list.php',
            'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_post))),
            'onSuccess'   : function(req){ 
                              eval(req.responseText);
                              window.status = "Done";
                            }
          });        

        } // end if

      } // end if

    } // end list_delete_item()
    
    function update_date_dropdowns(e) {

      date_array = new Object();

      window.status                       = "Updating date dropdown...";
      
      if (!e) { window.status = "done"; return false; } // end if

      if (is_string(e)) {

        var target = MochiKit.DOM.getElement(e);

      } // end if
      
      if (e) {

        if (!target) {
          var target        = detectTarget(e);
        } // end if

        date_array["id"]    = target.id;
        temp                = date_array["id"].split("_");
        date_array["rrv"]   = temp[temp.length-1];
        date_array["oid"]   = temp[temp.length-2];
        temp                = date_array["id"].substr(0,(date_array["id"].length-temp[temp.length-1].length-temp[temp.length-2].length)-2);
        date_array["id"]    = temp;
        date_array["month"] = MochiKit.DOM.getElement(date_array["id"]+"_month_"+date_array["rrv"]).options[MochiKit.DOM.getElement(date_array["id"]+"_month_"+date_array["rrv"]).selectedIndex].text;
        date_array["year"]  = MochiKit.DOM.getElement(date_array["id"]+"_year_"+date_array["rrv"]).options[MochiKit.DOM.getElement(date_array["id"]+"_year_"+date_array["rrv"]).selectedIndex].text;

        var to_post         = date_array;
        
        AjaxRequest.post({

          'url'         : 'ajax/date.php',
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_post))),
          'onSuccess'   : function(req){ 
                            return_response = MochiKit.Base.evalJSON(decodeBase64(urldecode(req.responseText)));
                            update_days(return_response);
                            MochiKit.Signal.signal(target,"update_calendar");
                          }
        });        

      } // end if

    } // end update_date_dropdowns()

    function update_days(return_array) {

      id                 = return_array["id"]+"_day_"+return_array["rrv"];
      var obj            = MochiKit.DOM.getElement(id);
      var sel_id         = obj.selectedIndex;
      obj.options.length = 0;

      for (var x=0;x<return_array["days"];x++) {

        obj.options[x] = new Option(x+1,x);

      } // end for

      if (obj.options.length > sel_id) {

        obj.selectedIndex = sel_id;

      } else {

        obj.selectedIndex = obj.options.length-1;

      } // end if

      window.status     = "Done";

    } // end update_days()

    function update_json_variables(e) {

      window.status                       = "Validating form fields...";

      if (e) {

        var target                        = detectTarget(e);

        var rrv_run                       = target.id.split("_");
        rrv_run                           = rrv_run[rrv_run.length-1];

      } // end if

      notified[target.id] = "";

      for (var i in variables["form_"+rrv_run]) {
      
        if (!MochiKit.DOM.getElement(i+"_"+rrv_run)) {

          alert("update_json_variables() failed for element ["+i+"_"+rrv_run+"]");
          
        } else {

          variables["form_"+rrv_run][i] = MochiKit.DOM.getElement(i+"_"+rrv_run).value;

        } // end if

      } // end for

      validate_array["form_"+rrv_run]["variables"]         = variables["form_"+rrv_run];
      validate_array["form_"+rrv_run]["require_variables"] = require_variables["form_"+rrv_run];
      validate_array["form_"+rrv_run]["submit_buttons"]    = submit_buttons["form_"+rrv_run];
      validate_array["form_"+rrv_run]["rrv_run"]           = rrv_run;

      validate(validate_array["form_"+rrv_run]);

    } // end update_json_variables()
    
    function notify_validation(return_response) {

      notify_text        = "";
      num_false          = 0;

      var rrv_run        = return_response["rrv_run"];
      var submit_buttons = return_response["submit_buttons"];
      return_response    = return_response["array"];

      for (var key in return_response) {

        for (var rule in return_response[key]) {

          if (is_object(return_response[key][rule])) {

            if (return_response[key][rule][0]==false) {

              if (MochiKit.DOM.getElement(key+"_"+rrv_run)) {

                MochiKit.DOM.getElement(key+"_"+rrv_run).style.backgroundColor = "#FF0000";
                num_false++;
  
                if ((!notified[key+"_"+rrv_run])||(notified[key+"_"+rrv_run] == "")) {
  
                  notified[key+"_"+rrv_run] = "notified";
                  notify_text = notify_text + "Variable \""+key+"_"+rrv_run+"\" does not meet the requirements of constraint \""+rule+"\".\n";
                  
                } // end if

              } // end if

            } else if (return_response[key][rule][0]==true) { //true

              if (MochiKit.DOM.getElement(key+"_"+rrv_run)) {

                MochiKit.DOM.getElement(key+"_"+rrv_run).style.backgroundColor = "";
                notified[key+"_"+rrv_run]                                      = null;

              } // end if

            } // end if

          } // end if

        } // end for

      } // end for

      if (num_false > 0) {
      
        for (var button in submit_buttons) {

          if (MochiKit.DOM.getElement(submit_buttons[button]+"_"+rrv_run)) {

            MochiKit.DOM.getElement(submit_buttons[button]+"_"+rrv_run).disabled = true;

          } // end if

        } // end for

        if (!is_empty(notify_text)) {

          alert(notify_text);

        } // end if

      } else {
      
        for (var button in submit_buttons) {

          if (MochiKit.DOM.getElement(submit_buttons[button]+"_"+rrv_run)) {

            MochiKit.DOM.getElement(submit_buttons[button]+"_"+rrv_run).disabled = false;

          } // end if

        } // end for

      } // end if

      window.status = "Done";

    } // end notify_validation()

    function validate(input_array) {

      AjaxRequest.post({

        'url'         : 'ajax/validate.php',
        'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(input_array))),
        'onSuccess'   : function(req){ 
                          return_response = MochiKit.Base.evalJSON(decodeBase64(urldecode(req.responseText)));
                          notify_validation(return_response);
                        }
      });        

    } // end validate()
    
    function checkRegExpress(obj,submit) {

      if (! obj.value.match(/\d\d-\d\d\d-\d\d$/)) {

        if (obj.value.match(/\d\d-\d\d\d-\d\dP$/)) {

          obj.style.backgroundColor = '';
          submit.disabled=false;
          return true;

        } // end if

        obj.style.backgroundColor = '#FF0000';
        alert("Internal IDs are in the form:\n\txx-xxx-xx");
        submit.disabled=true;
        return false;

      } else {

        obj.style.backgroundColor = '';
        submit.disabled=false;
        return true;

      } // end if

    } // end checkRegExpress()

    function set_date(when,monthfield,dayfield,yearfield) {

      if (when == "today") {
  
        var today = new Date();
        var month = today.getMonth();
        var day   = today.getDate()-1;
        var year  = today.getYear();
    
        if (year > 99) { year = 2000 + (year - 100); }
        else { year = 1900 + year; } // end if
    
        monthfield[month].selected = true;
        dayfield[day].selected     = true;

        for(i=0;i<yearfield.length;i++) {

          if (yearfield[i].text == year) {

            yearfield[i].selected   = true;

          } // end if

        } // end for

      } // end if

    } // end set_date()

    function note_onoff(objtxt,rrv_run) {

      obj    = MochiKit.DOM.getElement(objtxt+'_notediv');
      button = MochiKit.DOM.getElement(objtxt+'_notebutton');
      texta  = MochiKit.DOM.getElement(objtxt+'_note'+'_'+rrv_run);
  
      if (obj.style.display == "none") {
  
        obj.style.display = "";
        button.value = "Delete Note";

      } else {
  
        if (confirm('Are you sure you want to delete this note?') == true) {
  
          obj.style.display = "none";
          button.value = "Add Note";
          texta.value = "";
      
        } // end if

      } // end if

    } // end comment_onoff()
    
    function toggle_image(theobj,a,b) {

      theobj = MochiKit.DOM.getElement(theobj);

      if (theobj.src == a.src) { theobj.src = b.src; } else if (theobj.src == b.src) { theobj.src = a.src; } // end if

    } // end toggle_image()

    function toggle_object(obj,specific) {

      if (!is_object(obj)) {

        if (MochiKit.DOM.getElement(obj)) {

          obj = MochiKit.DOM.getElement(obj);

        } // end if
        
      } // end if
      
      if (!specific) {
      
        if (obj.style.display == "none") {

          obj.style.display = "";

        } else {

          obj.style.display = "none";

        } // end if

      } else {
      
        if (specific == "on") {

          obj.style.display = "";

        } else if (specific == "off") {
          
          obj.style.display = "none";
            
        } // end if
          
      } // end if

    } // end toggle_object()

    function detectTarget(e) {

      var targ;

      if (!e) var e=window.event;

      if (e.target) { targ=e.target; }
      else if (e.srcElement) { targ=e.srcElement; }

      if (targ.nodeType==3) { targ=targ.parentNode; }

      return targ;

    } // end detectTarget()

    /********************************************************
     * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
     * CodeHouse(TM) is a registered trademark.
     *
     * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
     * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
     * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
     *
     * CodeHouse.com JavaScript Library Module: Register Event Class
     *
     * You can obtain this script at http://www.codehouse.com
     ********************************************************/
    function CJL_RegisterEvent(elemName, elem, type, listener, useCapture, noAutoStart) {

      var proto = arguments.callee.prototype;

      this.e    = elem;
      this.type = type;
      this.cap  = useCapture;
      this.l    = listener;

      proto.start = function() {

        if (!elem) { alert("CJL_RegisterEvent() failed for element ["+elemName+"]"); return; } // end if

        if (this.e.attachEvent) {

          this.e.attachEvent("on" + this.type, this.l);

        } else if (this.e.addEventListener) {

          this.e.addEventListener(this.type, this.l, this.cap);

        } // end if

      } // end function()

      if (!noAutoStart) {

        this.start(elem, type, listener);

      } // end if

      proto.stop = function() {
      
        if (this.e.detachEvent) {

          this.e.detachEvent("on" + this.type, this.l);

        } else if( this.e.removeEventListener ) {

          this.e.removeEventListener(this.type, this.l, this.cap);

        } // end if

      } // end function()

    } // end CJL_RegisterEvent()

    var END_OF_INPUT = -1;

    var base64Chars = new Array(
        'A','B','C','D','E','F','G','H',
        'I','J','K','L','M','N','O','P',
        'Q','R','S','T','U','V','W','X',
        'Y','Z','a','b','c','d','e','f',
        'g','h','i','j','k','l','m','n',
        'o','p','q','r','s','t','u','v',
        'w','x','y','z','0','1','2','3',
        '4','5','6','7','8','9','+','/'
    );

    var reverseBase64Chars = new Array();

    for (var i=0; i < base64Chars.length; i++){

      reverseBase64Chars[base64Chars[i]] = i;

    } // end for

    var base64Str;
    var base64Count;
    
    function setBase64Str(str) {

      base64Str   = str;
      base64Count = 0;

    } // end setBase64Str()
    
    function readBase64(){

      if (!base64Str) { return END_OF_INPUT; } // end if
      if (base64Count >= base64Str.length) { return END_OF_INPUT; } // end if
      var c = base64Str.charCodeAt(base64Count) & 0xff;
      base64Count++;
      return c;

    } // end readBase64()
    
    function encodeBase64(str){
        setBase64Str(str);
        var result = '';
        var inBuffer = new Array(3);
        var lineCount = 0;
        var done = false;
        while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
            inBuffer[1] = readBase64();
            inBuffer[2] = readBase64();
            result += (base64Chars[ inBuffer[0] >> 2 ]);
            if (inBuffer[1] != END_OF_INPUT){
                result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
                if (inBuffer[2] != END_OF_INPUT){
                    result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                    result += (base64Chars [inBuffer[2] & 0x3F]);
                } else {
                    result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                    result += ('=');
                    done = true;
                }
            } else {
                result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
                result += ('=');
                result += ('=');
                done = true;
            }
    //        lineCount += 4;
    //        if (lineCount >= 76){
    //            result += ('\n');
    //            lineCount = 0;
    //        }
        }
        return result;
    }
    function readReverseBase64(){   
        if (!base64Str) return END_OF_INPUT;
        while (true){      
            if (base64Count >= base64Str.length) return END_OF_INPUT;
            var nextCharacter = base64Str.charAt(base64Count);
            base64Count++;
            if (reverseBase64Chars[nextCharacter]){
                return reverseBase64Chars[nextCharacter];
            }
            if (nextCharacter == 'A') return 0;
        }
        return END_OF_INPUT;
    }
    
    function ntos(n){
        n=n.toString(16);
        if (n.length == 1) n="0"+n;
        n="%"+n;
        return unescape(n);
    }
    
    function decodeBase64(str){
        setBase64Str(str);
        var result = "";
        var inBuffer = new Array(4);
        var done = false;
        while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
            && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
            inBuffer[2] = readReverseBase64();
            inBuffer[3] = readReverseBase64();
            result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
            if (inBuffer[2] != END_OF_INPUT){
                result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
                if (inBuffer[3] != END_OF_INPUT){
                    result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
                } else {
                    done = true;
                }
            } else {
                done = true;
            }
        }
        return result;
    }
    
    function urlencode( val )
    {
            // The Javascript escape and unescape functions do not correspond
            // with what browsers actually do...
            var SAFECHARS = "0123456789" +                                        // Numeric
                                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +        // Alphabetic
                                            "abcdefghijklmnopqrstuvwxyz" +
                                            "-_.!~*'()";                                        // RFC2396 Mark characters
            var HEX = "0123456789ABCDEF";
    
            var plaintext = val;
            var encoded = "";
            for (var i = 0; i < plaintext.length; i++ ) {
                    var ch = plaintext.charAt(i);
                if (ch == " ") {
                        encoded += "+";                                // x-www-urlencoded, rather than %20
                    } else if (SAFECHARS.indexOf(ch) != -1) {
                        encoded += ch;
                    } else {
                        var charCode = ch.charCodeAt(0);
                            if (charCode > 255) {
                                alert( "Unicode Character '" 
                            + ch 
                            + "' cannot be encoded using standard URL encoding.\n" +
                                              "(URL encoding only supports 8-bit characters.)\n" +
                                                      "A space (+) will be substituted." );
                                    encoded += "+";
                            } else {
                                    encoded += "%";
                                    encoded += HEX.charAt((charCode >> 4) & 0xF);
                                    encoded += HEX.charAt(charCode & 0xF);
                            }
                    }
            } // for
    
            return encoded;
    };
    
    function urldecode( val )
    {
       // Replace + with ' '
       // Replace %xx with equivalent character
       // Put [ERROR] in output if %xx is invalid.
       var HEXCHARS = "0123456789ABCDEFabcdef"; 
       var encoded = val;
       var plaintext = "";
       var i = 0;
       while (i < encoded.length) {
           var ch = encoded.charAt(i);
               if (ch == "+") {
                   plaintext += " ";
                       i++;
               } else if (ch == "%") {
                            if (i < (encoded.length-2) 
                                            && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
                                            && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                                    plaintext += unescape( encoded.substr(i,3) );
                                    i += 3;
                            } else {
                                    alert( 'Bad escape combination near ...' + encoded.substr(i) );
                                    plaintext += "%[ERROR]";
                                    i++;
                            }
                    } else {
                       plaintext += ch;
                       i++;
                    }
            } // while
       return plaintext;
    };
    
    function is_alien(a) {
       return is_object(a) && typeof a.constructor != 'function';
    } // end is_alien()
    
    function is_array(a) {
        return is_object(a) && a.constructor == Array;
    } // end is_array()
    
    function is_boolean(a) {
        return typeof a == 'boolean';
    } // end is_boolean()
    
    function is_empty(o) {
        var i, v;
        if (is_object(o)) {
            for (i in o) {
                v = o[i];
                if (is_undefined(v) && is_function(v)) {
                    return false;
                } // end if
            } // end for
        } else if (is_string(o)) {
          if (o.length>0) { return false; } // end if
        } // end if
        return true;
    } // end is_empty()
    
    function is_function(a) {
        return typeof a == 'function';
    } // end is_function()
    
    function is_null(a) {
        return typeof a == 'object' && !a;
    } // end is_null()
    
    function is_number(a) {
        return typeof a == 'number' && isFinite(a);
    } // end is_number()
    
    function is_object(a) {
        return (a && typeof a == 'object') || is_function(a);
    } // end is_object()
    
    function is_string(a) {
        return typeof a == 'string';
    } // end is_string()
    
    function is_undefined(a) {
        return typeof a == 'undefined';
    }  // end is_undefined()
    
    function select_add_option(theobj,tvalue,ttext) {
    
      var temp    = document.createElement('option');
      temp.value  = tvalue;
      temp.text   = ttext;
    
      if (is_string(theobj)) { theobj = MochiKit.DOM.getElement(theobj); } // end if
    
      try       { theobj.add(temp, null); }
      catch(ex) { theobj.add(temp);       }
    
    } // end select_add_option()

    function calendar_add_date(liid,pid,button_elem) {
    
      thevar    = 'calendar'+liid+'_add_date';

      screend   = MochiKit.Style.getViewportDimensions();
      dated     = MochiKit.Style.getElementDimensions(thevar);
    
      to_pos    = new Object;
      to_pos.x  = ((screend.w / 2)-(dated.w / 2));
      to_pos.y  = ((screend.h / 2)-(dated.h / 2));
    
      MochiKit.Style.setElementPosition(thevar, to_pos);

      if (eval('calendar'+liid+'_adding') == true) { 
    
        eval('calendar'+liid+'_adding = false;'); 
        button_elem.style.backgroundImage = 'url(${root}images/edit.gif)';
        button_elem.value = 'Add Date to Calendar';
    
      } else {
    
        eval('calendar'+liid+'_adding = true;'); 
        button_elem.style.backgroundImage = 'url(${root}images/cancel.gif)';
        button_elem.value = 'Cancel';
    
      } // end if
    
      toggle_object(thevar);
    
    } // end calendar_add_date()

    function set_date_dropdowns_date(id,rrv,date) {

        temp        = date.split("-");
 
        year        = eval(temp[0]);
        month       = eval(temp[1]);
        day         = eval(temp[2]);

        year_field  = MochiKit.DOM.getElement((id+'_year_'+rrv));
        month_field = MochiKit.DOM.getElement((id+'_month_'+rrv));
        day_field   = MochiKit.DOM.getElement((id+'_day_'+rrv));

        year_idx    = null;
        month_idx   = null;
        day_idx     = null;

        for (x=0;x<year_field.options.length;x++) {

          if (year_field.options[x].text == year) { year_idx = year_field.options[x].value; } // end if

        } // end for

        for (x=0;x<month_field.options.length;x++) {

          if (month_field.options[x].value == (month-1)) { month_idx = month_field.options[x].value; } // end if

        } // end for

        for (x=0;x<day_field.options.length;x++) {

          if (day_field.options[x].text == day) { day_idx = day_field.options[x].value; } // end if

        } // end for

        if (year_idx)  { year_field.selectedIndex  = year_idx;  } // end if
        if (month_idx) { month_field.selectedIndex = month_idx; } // end if
        if (day_idx)   { day_field.selectedIndex   = day_idx;   } // end if

    } // end set_date_dropdowns_date()
// <script>

// Copyright (C) 2005 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Distribute this code, any part or any modified version of it.
//     Instead, you can link to the homepage of this code:
//     http://www.php-development.ru/javascripts/menu.php.
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as a part of another product provided that
//     its main use is not creating javascript menus.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind, either
// expressed or implied, including, but not limited to, the implied warranties
// of merchantability and fitness for a particular purpose. You expressly
// acknowledge and agree that use of this code is at your own risk.

// If you find my script useful, you can support my site in the following ways:
// 1. Vote for the script at HotScripts.com (you can do it on my site)
// 2. Link to the homepage of this script or to the homepage of my site:
//    http://www.php-development.ru/javascripts/menu.php
//    http://www.php-development.ru/
//    You will get 50% commission on all orders made by your referrals.
//    More information can be found here:
//    http://www.php-development.ru/affiliates.php

// ----- DropDown Control ------------------------------------------------------

// ----- Show Aux -----

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +2 : 0;

  for (; p; p = p.offsetParent)
  {
    if (p.style.position != 'absolute')
    {
      left += p.offsetLeft;
      top  += p.offsetTop;
    }
  }

  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

// ----- Hide Aux -----

function at_hide_aux(parent, child)
{
  document.getElementById(child).style.visibility = "hidden";
}

// ----- Show -----

function at_show_p()
{
  c = document.getElementById(this["at_child" ]);
  at_show_aux(this.id, c.id);
  clearTimeout(c["at_timeout"]);
}

function at_show_c()
{
  p = document.getElementById(this["at_parent"]);
  at_show_aux(p.id, this.id);
  clearTimeout(this["at_timeout"]);
}

// ----- Hide -----

function at_hide_p()
{
  c = document.getElementById(this["at_child" ]);
  c["at_timeout"] = setTimeout("at_hide_aux('"+this.id+"', '"+c.id+"')", 100);
}

function at_hide_c()
{
  p = document.getElementById(this["at_parent"]);
  this["at_timeout"] = setTimeout("at_hide_aux('"+p.id+"', '"+this.id+"')", 100);
}

// ----- Attach -----

function at_attach(parent, child, position) {

  p = document.getElementById(parent);
  c = document.getElementById(child);

  p["at_child"]    = c.id;
  c["at_parent"]   = p.id;
  c["at_position"] = position;

  p.onmouseover = at_show_p;
  p.onmouseout  = at_hide_p;
  c.onmouseover = at_show_c;
  c.onmouseout  = at_hide_c;
  
  at_show_aux(parent,child);
  at_hide_aux(parent,child);

}

// ----- Menu ---------------------------------------------------------

// ----- Build Aux -----

function dhtmlmenu_build_aux(parent, child, position) {

  document.write('<div class="container_menu" id="'+parent+'_child"><div class="menu_over" id="'+parent+'_child_sub">');

  n = 0;
  
  for (var i in child) {
  
    if (typeof child[i] != "object") {

      if (child[i].substring(0,7) == 'base64:') {
    
        document.write('<div class="menu_item" id="'+parent+'_'+n+'">'+decodeBase64(child[i].substring(7,child[i].length))+'</div>');
        continue;

      } else {

        document.write('<div class="menu_item" id="'+parent+'_'+n+'">'+i+'</div>'); // href="'+child[i]+'"

      } // end if
    
    } else {

      document.write('<div class="menu_item" onmouseover="this.className=\'menu_item_hover\';" onmouseout="this.className=\'menu_item\';" id="'+parent+'_'+n+'"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td>'+i+'</td><td width="20px">&#9658;</td></tr></table></div>');
      dhtmlmenu_build_aux(parent+'_'+n, child[i], "x");

    } // end if
    
    n++;

  } // end for

  document.write('</div></div>');

  at_attach(parent, parent+"_child", position);

} // end dhtmlmenu_build_aux()

// ----- Build -----

function dhtmlmenu_build(menu)
{
  for (var i in menu) dhtmlmenu_build_aux(i, menu[i], "y");
}


  // class.form_data.print_content.print_content.js
  // Copyright (c) 2005,2006 Kevin Kaiser / Data Resolutions

  // Basic usage:
  //
  //     <form id="form1">
  //       First Name: <input type="text" value="Kevin" name="first_name" id="first_name_1">
  //     </form>
  //
  //     <script>
  //
  //       var form     = new form_data();
  //       form.form_id = "form1";
  //       form.is_printable();
  //
  //     </script>
  //
  //     <input type="button" value="Click to Render" onclick="form.printer.render();">

// =================================================================================================================================================

  var printable_forms = new Object();

// =================================================================================================================================================

  if (typeof(form_data) == 'undefined') {

    form_data = {};

  } // end if

  form_data.NAME     = "form_data";
  form_data.VERSION  = "0.1";

  form_data.__repr__ = function () {

    return "[" + this.NAME + " " + this.VERSION + "]";

  }; // end __repr__()

  form_data.toString = function () {

    return this.__repr__();

  }; // end toString()

  form_data = function () {

    this.__init__();

  }; // end form_data()

  form_data.prototype = {

    __init__: function () {

      var form_id;
      var printer;

    }, // end __init__()

    form_ids: function () {

      var ids  = [];
      var m    = MochiKit.Base;
      var self = MochiKit.DOM;

      if (typeof(this.form_id) == "undefined" || this.form_id === null) {

        form_elem = self._document;

      } else {

        form_elem = self.getElement(this.form_id);

      } // end if

      m.nodeWalk(form_elem, function (elem) {

        var id = elem.id;

        if (m.isNotEmpty(id)) {

          var tag_name = elem.nodeName.toUpperCase();

          if (tag_name == "SELECT") {

            ids.push(id);
            return null;

          } // end if

          switch (tag_name) {

            case "FORM":
            case "P":
            case "SPAN":
            case "DIV":
            case "TABLE":
            case "TR":
            case "TD":

              return elem.childNodes;
              break;

          } // end switch

          ids.push(id);

          return null;

        } // end if

        return elem.childNodes;

      }); // end nodeWalk

      return [ids];

    }, // end form_ids()

    is_printable: function () {

      this.parent_document = MochiKit.DOM.currentDocument();
      this.printer         = new form_data.print_content();
      this.printer.parent  = this;

    } // end is_printable()

  }; // end class form_data

// =================================================================================================================================================

  if (typeof(form_data.print_content) == 'undefined') {

    form_data.print_content = {};

  } // end if

  form_data.print_content.NAME     = "form_data.print_content";
  form_data.print_content.VERSION  = "0.1";

  form_data.print_content.__repr__ = function () {

    return "[" + this.NAME + " " + this.VERSION + "]";

  }; // end __repr__()

  form_data.print_content.toString = function () {

    return this.__repr__();

  }; // end toString()

  form_data.print_content = function () {

    this.__init__();

  }; // end form_data.print_content()

  form_data.print_content.prototype = {

    __init__: function () {

      var parent = new Object();
      var print_window;
      var destination;

    }, // end __init__()

    render: function () {

      var theobj = this;

      if (document.getElementById(this.parent.form_id)) {

        content = document.getElementById(this.parent.form_id);

        if (content.innerHTML) {

          this.print_window = window.open("","print_window");

          MochiKit.DOM.withWindow(this.print_window, function () {

            var prefix            = "wrapper_";
            var doc               = MochiKit.DOM.currentDocument();
            doc.body.innerHTML    = "<html><head><link rel=stylesheet type=\"text/css\" href=\"http://demo.occrra-central.org/registry/css/default.css\"><script src=\"http://demo.occrra-central.org/registry/js/javascript.php\"></script></head><body>"+content.innerHTML+"</body></html>";

            var oid               = theobj.parent.form_id;
            theobj.parent.form_id = theobj.parent.form_id.substring(prefix.length);
            theobj.destination    = eval("doc.getElementById('"+theobj.parent.form_id+"');");

            theobj.render_form();

            theobj.parent.form_id = oid;

            theobj.print_window.focus();

          });

        } // end if

      } // end if

    }, // end render()

    render_form: function () {

      if (this.destination) {

        var theform     = MochiKit.DOM.getElement(this.parent.form_id);
        var fields      = this.parent.form_ids();
        var replacement = "";

        for (var i in fields[0]) {

          var el = MochiKit.DOM.getElement(fields[0][i]);

          if (el && el.id) {

            if (!el.disabled) {

              parent_el   = this.parent.parent_document.getElementById(el.id);
              replacement = "";

              switch(parent_el.type) {

                case 'text':
                case 'password':
                case 'textarea':

                  replacement           = MochiKit.DOM.createDOM("span",{"id":parent_el.id});
                  replacement.innerHTML = parent_el.value;

                  break;

                case 'select-one':

                  if (parent_el.selectedIndex>=0) {

                    replacement           = MochiKit.DOM.createDOM("span",{"id":parent_el.id});
                    replacement.innerHTML = parent_el.options[parent_el.selectedIndex].text;

                  } // end if

                  break;

                case 'select-multiple':

                  var ms_replacement = "";

                  for (var j=0; j<parent_el.options.length; j++) {

                    if (parent_el.options[j].selected) {

                      ms_replacement += parent_el.options[j].text+"<br>\n";

                    } // end if

                  } // end for

                  replacement           = MochiKit.DOM.createDOM("div",{"id":parent_el.id});
                  replacement.innerHTML = ms_replacement;

                  break;

                case 'checkbox':

                  if (el.checked) {

                    replacement = MochiKit.DOM.createDOM("img",{"id":parent_el.id,"src":"http://demo.occrra-central.org/registry/images/checkbox_on.gif"});

                  } else {

                    replacement = MochiKit.DOM.createDOM("img",{"id":parent_el.id,"src":"http://demo.occrra-central.org/registry/images/checkbox_off.gif"});

                  } // end if

                  break;

                case 'radio':

                  if (el.checked) {

                    replacement = MochiKit.DOM.createDOM("img",{"id":parent_el.id,"src":"http://demo.occrra-central.org/registry/images/radio_on.gif"});

                  } else {

                    replacement = MochiKit.DOM.createDOM("img",{"id":parent_el.id,"src":"http://demo.occrra-central.org/registry/images/radio_off.gif"});

                  } // end if

                  break;

                case 'button':
                case 'submit':
                case 'hidden':

                  replacement = MochiKit.DOM.createDOM("div",{"id":parent_el.id}," ");

                  break;

                default:

                  replacement = MochiKit.DOM.createDOM("div",{"id":parent_el.id}," ");

                  break;

              } // end switch

              if (replacement) {

                MochiKit.DOM.swapDOM(parent_el.id,replacement);

              } // end if

            } // end if

          } // end if

        } // end for
 
      } // end if

    } // end render_form()

  }; // end class form_data.print_content

// =================================================================================================================================================

  // class.basis_functions.js
  // Copyright (c) 2005,2006, 2010 Ohio Child Care Resource & Referral Association

  if (typeof(Basis) == 'undefined') {

    Basis = {};

  } // end if

  Basis.NAME     = "Basis";
  Basis.VERSION  = "0.1";

  Basis.__repr__ = function () {

    return "[" + this.NAME + " " + this.VERSION + "]";

  }; // end __repr__()

  Basis.misc_vars = new Object();

  if (MochiKit.DOM.currentDocument().images) {

    Basis.misc_vars["ajax_loading"]     = new Image();
    Basis.misc_vars["ajax_loading"].src = "images/ajax_loading.gif";

  } // end if

  MochiKit.Signal.connect(MochiKit.DOM.currentDocument(),"onmousemove",function (e) {

    var pos = e.mouse().client;

    Basis.misc_vars["mouse_x"] = pos.x;
    Basis.misc_vars["mouse_y"] = pos.y;

  });

  Basis.array_flip = function (arr) {

    temp = new Object();

    for (var i in arr) {

      temp[arr[i]] = i;

    } // end for

    return temp;

  }; // end Basis.array_flip()

  Basis.array_size = function (arr) {

    arr_size = 0;

    for (var i in arr) {

      arr_size++;

    } // end for

    return arr_size;

  }; // end Basis.array_size()

  Basis.align_element_to_element = function (element,dest,how,offset,animation,duration) {

    var element = MochiKit.DOM.getElement(element);
    var dest    = MochiKit.DOM.getElement(dest);

    if (element && dest && how) {

      // MochiKit.Style.getElementPosition won't return coordinates if element.style.display = "none";
      //   if that's the case, need to make them completely transparent but display = ""

      // MochiKit.Style.getOpacity doesn't exist, so I need to write Basis.get_opacity and use that in
      // the commented-out sections below

      var e_disp    = element.style.display;
//    var e_opacity = MochiKit.Style.getOpacity(element);
      var d_disp    = dest.style.display;
//    var d_opacity = MochiKit.Style.getOpacity(dest);

      if (e_disp == "none") {

//      MochiKit.Style.setOpacity(element,0);
        element.style.display = "";

      } // end if

      if (d_disp == "none") {

//      MochiKit.Style.setOpacity(dest,0);
        element.style.display = "";

      } // end if

      var e_pos    = MochiKit.Style.getElementPosition(element);
      var e_dim    = MochiKit.Style.getElementDimensions(element);
      var d_pos    = MochiKit.Style.getElementPosition(dest);
      var d_dim    = MochiKit.Style.getElementDimensions(dest);
      var d_anchor = {};

      if (e_pos && e_dim && d_pos && d_dim) {

        switch (how) {

          case "center":

            var e_center = {"x" : e_pos.x + Math.floor(e_dim.w / 2),
                            "y" : e_pos.y + Math.floor(e_dim.h / 2)};

            var d_center = {"x" : d_pos.x + Math.floor(d_dim.w / 2),
                            "y" : d_pos.y + Math.floor(d_dim.h / 2)};

            var d_anchor = {"x" : (d_center.x - (e_center.x - e_pos.x)),
                            "y" : (d_center.y - (e_center.y - e_pos.y))};

            break;

          case "bottom_to_top":

            var d_anchor = {"x" : e_pos.x,
                            "y" : e_pos.y + (d_pos.y - e_pos.y - e_dim.h)};

            break;

          case "bottom_to_bottom":

            var d_anchor = {"x" : e_pos.x,
                            "y" : e_pos.y - ((e_pos.y + e_dim.h) - (d_pos.y + d_dim.h))};

            break;

          case "top_to_bottom":

            var d_anchor = {"x" : e_pos.x,
                            "y" : e_pos.y + (d_pos.y - e_pos.y + d_dim.h)};

            break;

          case "top_to_top":

            var d_anchor = {"x" : e_pos.x,
                            "y" : e_pos.y - (e_pos.y - d_pos.y)};

            break;

          case "left_to_right":

            var d_anchor = {"x" : e_pos.x + (d_pos.x - e_pos.x + e_dim.w),
                            "y" : e_pos.y};

            break;

          case "left_to_left":

            var d_anchor = {"x" : d_pos.x,
                            "y" : e_pos.y};

            break;

          case "right_to_left":

            var d_anchor = {"x" : e_pos.x + (d_pos.x - e_pos.x - e_dim.w),
                            "y" : e_pos.y};

            break;

          case "right_to_right":

            var d_anchor = {"x" : d_pos.x + (d_dim.w - e_dim.w),
                            "y" : e_pos.y};

            break;

        } // end switch

        if (offset && offset.x) {

          d_anchor.x += offset.x;

        } // end if

        if (offset && offset.y) {

          d_anchor.y += offset.y;

        } // end if

        if (!((d_anchor.x == 0) && (d_anchor.y == 0))) {

          element.style.position = "absolute";

          if (!animation) {

            MochiKit.Style.setElementPosition(element, d_anchor);

          } else {

            if (!duration) {

              duration = 0.50;

            } // end if

            MochiKit.Visual.Move(element, {"x":d_anchor.x,"y":d_anchor.y,"duration":duration,"mode":element.style.position});

          } // end if

        } // end if

      } // end if

      element.style.display = e_disp;
//    MochiKit.Style.setOpacity(element,e_opacity);

      dest.style.display    = d_disp;
//    MochiKit.Style.setOpacity(dest,d_opacity);

      return d_anchor;

    } // end if

  }; // end Basis.align_element_to_element()

  Basis.center_element = function (element,animation,duration,ignore_scroll) {

    var obj = MochiKit.DOM.getElement(element);

    if (obj) {

      var screend  = MochiKit.Style.getViewportDimensions();
      var elementd = MochiKit.Style.getElementDimensions(obj);

      var to_pos   = new Object;
      to_pos.x     = ((screend.w / 2)-(elementd.w / 2));
      to_pos.y     = ((screend.h / 2)-(elementd.h / 2));

      if (!ignore_scroll){

        var scroll;
        scroll       = Basis.scroll_coords();

        to_pos.x    += scroll.x;
        to_pos.y    += scroll.y;

      } // end if

      if (to_pos.y < 0) { to_pos.y = 0; } // end if

      if (!animation) {

        MochiKit.Style.setElementPosition(obj, to_pos);

      } else {

        if (!duration) {

          duration = 0.50;

        } // end if

        MochiKit.Visual.Move(obj, {"x":to_pos.x,"y":to_pos.y,"duration":duration,"mode":"absolute"});

      } // end if

    } // end if

  }; // end Basis.center_element()

  Basis.admin = function () {

    this.__init__();

  }; // end gallery()

  Basis.admin.prototype = {

    __init__: function () {

      this.ul_lists  = new Array();
      this.ul_ids    = new Array();
      this.root      = '';
      this.sid       = '';
      this.edit_mode = false;

    }, // end __init__()

    set_page_visibility: function (pid,in_menu) {

      var to_post     = new Object();
      to_post.pid     = pid;
      to_post.in_menu = in_menu;

      window.status = "Auto-saving page rankings...";

      AjaxRequest.post({

        'url'         : this.root+'ajax/admin_save_page_visibility.php?PHPSESSID='+this.sid,
        'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_post))),
        'onSuccess'   : function(req) {

                          window.status   = "Done";

                        }
      });

    }, // end set_page_visibility()

    site_page_order: function () {

      final_order = new Object();

      for (var lindex=0; lindex<this.ul_lists.length; lindex++) {

        final_site = new Array();

        ul = MochiKit.DOM.getElement(this.ul_lists[lindex]);

        if (!ul.childNodes || ul.childNodes.length==0) { return false; }

        for (var itemi=0; itemi<ul.childNodes.length; itemi++) {

          var item = ul.childNodes[itemi];

          if (item.nodeName == "LI") {

            final_site[final_site.length] = item.value;

          } // end if

        } // end for

        final_order[eval(this.ul_ids[lindex])] = final_site;

      } // end for

      window.status = "Auto-saving page rankings...";

      AjaxRequest.post({

        'url'         : this.root+'ajax/admin_save_site_rankings.php?PHPSESSID='+this.sid,
        'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(final_order))),
        'onSuccess'   : function(req) {

                          window.status   = "Done";

                        }
      });

    }, // end site_page_order()

    toggle_edit_mode: function (obj,switcher) {

      obj = MochiKit.DOM.getElement(obj);

      if (obj) {

        if (switcher=="true") {

          this.edit_mode = true;

        } else if (switcher=="false") {

          this.edit_mode = false;

        } // end if

        if (switcher) {

          if (this.edit_mode==true) {

            this.edit_mode = true;
            obj.style.backgroundImage = 'url('+this.root+'images/cancel.gif)';
            obj.value = 'Turn Edit Mode Off';
            change_css('.gfx_but_default','display','');

          } else {

            this.edit_mode = false;
            obj.style.backgroundImage = 'url('+this.root+'images/edit.gif)';
            obj.value = 'Turn Edit Mode On';
            change_css('.gfx_but_default','display','none');

          } // end if

        } else {

          if (this.edit_mode==false) {

            this.edit_mode = true;
            obj.style.backgroundImage = 'url('+this.root+'images/cancel.gif)';
            obj.value = 'Turn Edit Mode Off';
            change_css('.gfx_but_default','display','');

          } else {

            this.edit_mode = false;
            obj.style.backgroundImage = 'url('+this.root+'images/edit.gif)';
            obj.value = 'Turn Edit Mode On';
            change_css('.gfx_but_default','display','none');

          } // end if

        } // end if

      } // end if

    } // end toggle_edit_mode()

  }; // end class Basis.admin

  Basis.ajax_form_post = function () {

    this.__init__();

  }; // end Basis.ajax_form_post()

  Basis.ajax_form_post.instances = [];

  Basis.ajax_form_post.prototype = {

    __init__: function () {

      this.sid;
      this.form_data;
      this.form_element;
      this.origin_element;
      this.status_element;

      this.is_posting = false;

      this.progress_element = "progress_element";

      this.on_success       = function () { return; };

      this.index            = Basis.ajax_form_post.instances.length;
      Basis.ajax_form_post.instances[this.index] = this;

    }, // end __init__()

    compile: function(origin_element) {

      if (!this.is_posting && this.sid && this.form_element && MochiKit.DOM.getElement(this.form_element)) {

        if (origin_element && MochiKit.DOM.getElement(origin_element)) {

          this.origin_element = origin_element;
          MochiKit.DOM.getElement(origin_element).disabled = true;

        } // end if

        var action     = MochiKit.DOM.getElement(this.form_element).action;

        this.form_data = MochiKit.DOM.formContents(this.form_element);

        this.form_data[0][this.form_data[0].length] = "PHPSESSID";
        this.form_data[1][this.form_data[1].length] = this.sid;

        this.form_data[0][this.form_data[0].length] = "exit_after_catch";
        this.form_data[1][this.form_data[1].length] = "true";

        window.status = "Auto-saving...";

        this.create_progress_bar();
        this.is_posting = true;

        AjaxRequest.progress_object = Basis.ajax_form_post.instances[this.index];

        setTimeout(function () {

          AjaxRequest.post({

            'url'         : action,
            'queryString' : MochiKit.Base.queryString(AjaxRequest.progress_object.form_data[0],AjaxRequest.progress_object.form_data[1]),
            'onSuccess'   : function(req) {

                              AjaxRequest.progress_object.on_success();

                              MochiKit.DOM.getElement(AjaxRequest.progress_object.progress_element+"_message").innerHTML = "<b>Success!</b>";
                              MochiKit.DOM.getElement(AjaxRequest.progress_object.progress_element+"_image").innerHTML   = "&nbsp;";

                              setTimeout(function () {

                                           MochiKit.Visual.toggle(AjaxRequest.progress_object.progress_element+"_under","appear",{"duration":0.25});
                                           MochiKit.Visual.toggle(AjaxRequest.progress_object.progress_element,"appear",{"duration":0.25});

                                           if (AjaxRequest.progress_object.origin_element && MochiKit.DOM.getElement(AjaxRequest.progress_object.origin_element)) {

                                             MochiKit.DOM.getElement(AjaxRequest.progress_object.origin_element).disabled = false;

                                           } // end if

                                           if (AjaxRequest.progress_object.forms_container_element && MochiKit.DOM.getElement(AjaxRequest.progress_object.forms_container_element)) {

                                             MochiKit.DOM.getElement(AjaxRequest.progress_object.forms_container_element).innerHTML = "";
                                             MochiKit.DOM.getElement(AjaxRequest.progress_object.forms_container_element).innerHTML = req.responseText;

                                             Basis.execute_javascript(AjaxRequest.progress_object.forms_container_element);

                                           } // end if

                                           window.status = "Done";

                                           AjaxRequest.progress_object.is_posting = false;

                                         },0);

                            }
          });

        },0);

      } // end if

    }, // end compile()

    create_progress_bar: function () {

      var screend = MochiKit.Style.getViewportDimensions();
      var scroll  = Basis.scroll_coords();

// =================

      if (!MochiKit.DOM.getElement(this.progress_element+"_under")) {

        var progress_element_under = new MochiKit.DOM.DIV({"id":this.progress_element+"_under","style":"display:none; background-color: #000000; position: absolute;"},null);

      } else {

        var progress_element_under = MochiKit.DOM.getElement(this.progress_element+"_under");

      } // end if

      document.body.appendChild(progress_element_under);

      MochiKit.DOM.getElement(this.progress_element+"_under").style.height = "75";

      var progressd = MochiKit.Style.getElementDimensions(this.progress_element+"_under");

      to_pos    = new Object;
      to_pos.x  = 0 + scroll.x;
      to_pos.y  = (screend.h/2)-(progressd.h/2) + scroll.y;

      MochiKit.DOM.getElement(this.progress_element+"_under").style.width  = screend.w;

      MochiKit.Style.setElementPosition(this.progress_element+"_under", to_pos);

// =================

      if (!MochiKit.DOM.getElement(this.progress_element)) {

        var progress_element = new MochiKit.DOM.DIV({"id":this.progress_element,"style":"display:none; background-color: #FFFFFF; position: absolute; border-top: 1px solid #000000; border-bottom: 1px solid #000000"},null);

      } else {

        var progress_element = MochiKit.DOM.getElement(this.progress_element);

      } // end if

      document.body.appendChild(progress_element);

      MochiKit.DOM.getElement(this.progress_element).innerHTML    = '<div id="'+this.progress_element+'_message" style="text-align: center; padding: 5px;"><b>Please wait while your document saves...</b></div><div id="'+this.progress_element+'_image" style="text-align: center; padding: 5px;"><img src="images/ajax_loading.gif"></div>';
      MochiKit.DOM.getElement(this.progress_element).style.height = "50";

      var progressd = MochiKit.Style.getElementDimensions(this.progress_element);

      to_pos    = new Object;
      to_pos.x  = 0 + scroll.x;
      to_pos.y  = (screend.h/2)-(progressd.h/2) + scroll.y;

      MochiKit.DOM.getElement(this.progress_element).style.width  = screend.w;

      MochiKit.Style.setElementPosition(this.progress_element, to_pos);

// =================

      MochiKit.Visual.toggle(this.progress_element+"_under","appear",{"duration":0.25,"to":0.50});
      MochiKit.Visual.toggle(this.progress_element,"appear",{"duration":0.25});

    } // end create_progress_bar()

  }; // end class Basis.ajax_form_post

  Basis.execute_javascript = function (element) {

    var element = MochiKit.DOM.getElement(element);

    if (element) {

      var st = element.getElementsByTagName("SCRIPT");
      var string_to_execute;

      for (var i=0;i<st.length; i++) {

        string_to_execute = st[i].innerHTML;

        try {

          eval(string_to_execute.split("<!--").join("").split("-->").join(""));

        } catch(e) {

          MochiKit.Logging.log(e);

        } // end try

      } // end for

    } // end if

  }; // end Basis.execute_javascript()

  Basis.image_rotator = function (element,array) {

    /*

      argument in this format:

      element = id of image to swap with

      array( "URL",
             "URL2",
           );

    */

    var d = MochiKit.DOM.currentDocument();

    if (d.images) {

      if (!d.preloaded_images)           { d.preloaded_images           = new Object(); } // end if
      if (!d.preloaded_images["rotate"]) { d.preloaded_images["rotate"] = new Array();  } // end if

      var index = d.preloaded_images["rotate"].length;
      var obj   = MochiKit.DOM.getElement(element);

      d.preloaded_images["rotate"][index]    = new Array();
      d.preloaded_images["rotate"][index][0] = element;

      if (obj) {

        for (var i in array) {

          var idx = d.preloaded_images["rotate"][index].length;

          d.preloaded_images["rotate"][index][idx]     = new Image();
          d.preloaded_images["rotate"][index][idx].src = array[i];

        } // end for

        obj.rotate = function (index,idx) {

          var element = MochiKit.DOM.getElement(d.preloaded_images["rotate"][index][0]);

          if (element) {

            if (d.preloaded_images["rotate"][index][idx] && d.preloaded_images["rotate"][index][idx].src) {

              element.src = d.preloaded_images["rotate"][index][idx].src;

            } // end if

            var max = d.preloaded_images["rotate"][index].length;

            if (idx == (max-1)) {

              var nidx = 1;

            } else {

              var nidx = idx+1;

            } // end if

            setTimeout(("MochiKit.DOM.getElement('"+element.id+"').rotate("+index+","+nidx+");"),2000);

          } // end if

        } // end rotate()

        if (d.preloaded_images["rotate"][index].length > 1) {

          obj.rotate(index,1);

        } // end if

      } // end if

    } // end if

  }; // end Basis.image_rotator()

  Basis.load_rollover_images = function (array) {

    /*

      argument in this format:

      array( array("id"  => IMG_ID,
                   "off" => URL,
                   "on"  => URL,
                  ),
             array(),
           );

    */

    var d = MochiKit.DOM.currentDocument();

    if (d.images) {

      if (!d.preloaded_images) { d.preloaded_images = new Object(); } // end if

      for (var i in array) {

        if (array[i]["id"] && MochiKit.DOM.getElement(array[i]["id"])) {

          d.preloaded_images[array[i]["id"]]         = new Object();
          d.preloaded_images[array[i]["id"]].off     = new Image();
          d.preloaded_images[array[i]["id"]].off.src = array[i]["off"];
          d.preloaded_images[array[i]["id"]].on      = new Image();
          d.preloaded_images[array[i]["id"]].on.src  = array[i]["on"];

          var obj = MochiKit.DOM.getElement(array[i]["id"]);

          obj.swap = function () {

            if (this.src == d.preloaded_images[this.id].off.src) {

              this.src = d.preloaded_images[this.id].on.src;

            } else {

              this.src = d.preloaded_images[this.id].off.src;

            } // end if

          } // end swap()

          if (!array[i]["noswap"]) {

            obj.onmouseover = function () {

              this.swap();

            };

            obj.onmouseout = function () {

              this.swap();

            };

          } // end if

        } // end if

      } // end for

    } // end if

  }; // end Basis.load_rollover_images()

  Basis.populator = function () {

    this.__init__();

  }; // end gallery()

  Basis.populator.instances = [];

  Basis.populator.prototype = {

    __init__: function () {

      this.dropdown_a                       = '';
      this.dropdown_b                       = new Array;

      this.rrv_run                          = -1;

      this.populate_array                   = new Object;

      this.index                            = Basis.populator.instances.length;
      Basis.populator.instances[this.index] = this;

    }, // end __init__()

    add_dropdown_b: function (element) {

      this.dropdown_b[this.dropdown_b.length] = element;

    }, // end add_dropdown_b()

    populate: function (ddb,to_oidx) {

      var opopulate_array;

      if (typeof this.populate_array == "object") {

        opopulate_array = this.populate_array;

      } else {

        opopulate_array     = MochiKit.Base.evalJSON(decodeBase64(this.populate_array));
        this.populate_array = opopulate_array;

      } // end if

      var i_ddb_idx = false;

      for (var tddb in this.dropdown_b) {

        if (this.dropdown_b[tddb] == ddb) {

          i_ddb_idx = tddb;

        } // end if

      } // end for

      if (this.dropdown_b[tddb]) {

        ddb_idx = this.dropdown_b[i_ddb_idx].substring(0,(this.dropdown_b[i_ddb_idx].length-this.rrv_run.length-1));

        if (this.dropdown_a && this.populate_array) {

          var to             = document.getElementById(this.dropdown_b[i_ddb_idx]);
          var indexes        = {"from" : document.getElementById(this.dropdown_a).options[document.getElementById(this.dropdown_a).selectedIndex].value,
                                "to"   : to.selectedIndex
                               };

          populate_array     = ((opopulate_array[ddb_idx] && opopulate_array[ddb_idx][indexes.from]) ? opopulate_array[ddb_idx][indexes.from] : []);
          to.options.length  = 0;
          x                  = 0;

          for (var i in populate_array) {

            to.options[x] = new Option(populate_array[i],i);
            x++;

          } // end for

          if (to_oidx) {

            to.selectedIndex = to_oidx;

          } // end if

        } // end if

      } // end if

    }, // end populate()

    set_onchange: function (ddb) {

      var tfunc = new Function('Basis.populator.instances['+this.index+'].populate(\''+ddb+'\');');

      MochiKit.Signal.connect(this.dropdown_a,'onchange',tfunc);

    } // end set_onchange()

  }; // end class Basis.populator

  Basis.scroll_coords = function () {

    var scroll_x, scroll_y;

    if (self.pageYOffset) {

      scroll_x = self.pageXOffset;
      scroll_y = self.pageYOffset;

    } else if (document.documentElement && document.documentElement.scrollTop) {

      scroll_x = document.documentElement.scrollLeft;
      scroll_y = document.documentElement.scrollTop;

    } else if (document.body) {

      scroll_x = document.body.scrollLeft;
      scroll_y = document.body.scrollTop;

    } // end if

    var obj = {'x':scroll_x,'y':scroll_y};

    return obj;

  }; // end Basis.scroll_coords()

  Basis.submenu = function () {

    this.__init__();

  }; // end Basis.submenu()

  Basis.submenu.attachments = new Object();

  Basis.submenu.prototype = {

    __init__: function () {

      this.parent;
      this.children = new Array();

      this.menu_title;
      this.menu_link;

    }, // end __init__()

    add_menu_item: function(title,link) {

      var menu_item        = new Basis.submenu();

      menu_item.parent     = this;
      menu_item.menu_title = title;
      menu_item.menu_link  = link;

      this.children[this.children.length] = menu_item;

      return this.children[this.children.length-1];

    }, // end add_menu_item()

    clear_parent_timeouts: function () {

      if (this.id) {

        if (this.id != this.parent_element) {

          var container_id = this.id.split('_');
          container_id     = container_id[container_id.length-1];
          container_id     = this.id.substring(0,(this.id.length-container_id.length-1))+'_container';

        } else {

          var container_id = this.parent_element;

        } // end if

        MochiKit.Logging.log('clearTimeout(MochiKit.DOM.getElement('+container_id+').timeout);\n');
        clearTimeout(MochiKit.DOM.getElement(container_id).timeout);

      } // end if

      if (this.parent && (this.parent.id != this.id)) {

        this.parent.clear_parent_timeouts();

      } else {

        return;

      } // end if

    }, // end clear_parent_timeouts()

    compile: function (x) {

      var output = '';

      if (!x) {

        x = 0;

      } // end if

      if (!this.id && !this.parent && !this.container_id) {

        this.id           = this.parent_element;
        this.container_id = 'submenu_0_container';
        this.parent       = this;

      } // end if

      if (!Basis.submenu.attachments[this.id]) {

        Basis.submenu.attachments[this.id] = new Object();

      } // end if

      output += '<div id="submenu_'+x+'_container" class="menu_container">\n';

      Basis.submenu.attachments[this.id]['onmouseover'] = 'MochiKit.Signal.connect(\''+this.id+'\',\'onmouseover\',function () { MochiKit.Style.showElement(\'submenu_'+x+'_container\'); clearTimeout(MochiKit.DOM.getElement(\'submenu_'+x+'_container\').timeout); });\n';
//    Basis.submenu.attachments[this.id]['onmouseout']  = 'MochiKit.Signal.connect(\''+this.id+'\',\'onmouseout\', function () { MochiKit.DOM.getElement(\'submenu_'+x+'_container\').timeout = setTimeout("MochiKit.Style.hideElement(\'submenu_'+x+'_container\');",1000); });\n';

      if (this.children && (this.children.length>0)) {

        for (var i in this.children) {

          var element                   = 'submenu_'+x;
          this.children[i].id           = 'submenu_'+x+'_'+i;
          this.children[i].container_id = 'submenu_'+x+'_container';

          output += '<div id="'+this.children[i].id+'"><a href="'+this.children[i].menu_link+'">'+this.children[i].menu_title+'</a></div>\n';

          if (this.children[i].children && (this.children[i].children.length>0)) {

            output += this.children[i].compile(x+1);

          } // end if

        } // end for

      } // end if

      output += "</div>\n";

      return output;

    }, // end compile()

    find_node: function (node) {

      var obj = this;

      if (is_array(node)) {

        for (var i in node) {

          if (obj && obj.children && (obj.children.length>0) && obj.children[node[i]]) {

            obj = obj.children[node[i]];

          } // end if

        } // end for

        return obj;

      } // end if

    }, // end find_node()

    find_node_by_id: function (id) {

      var to_return;

      if (this.id == id) {

        return this;

      } else {

        if (this && this.children && (this.children.length>0)) {

          for (var i in this.children) {

            to_return = this.children[i].find_node_by_id(id);

          } // end for

        } // end if

      } // end if

      return to_return;

    } // end find_node_by_id()

  }; // end class Basis.submenu
  
  Basis.toggleElements = function (elements_to_hide, elements_to_show) {
  
    for ( var i in elements_to_hide ) {
    
      var tobj = MochiKit.DOM.getElement(elements_to_hide[i]);

      if (tobj) { 
      
        tobj.style.display = 'none'; 
        
      }; // end if
      
    } // end for
    
    for ( var i in elements_to_show ) {
    
      var tobj = MochiKit.DOM.getElement(elements_to_show[i]);

      if (tobj) { 
      
        tobj.style.display = ''; 
        
      }; // end if
      
    } // end for

  } // end Basis.hideElement()
  
  Basis.hideElements = function (elements_to_hide) {
  
    for ( var i in elements_to_hide ) {
    
      var tobj = MochiKit.DOM.getElement(elements_to_hide[i]);

      if (tobj) { 
      
        tobj.style.display = 'none'; 
        
      }; // end if
      
    } // end for

  } // end Basis.hideElement()
  
  Basis.showElements = function (elements_to_show) {
  
    for ( var i in elements_to_show ) {
    
      var tobj = MochiKit.DOM.getElement(elements_to_show[i]);

      if (tobj) { 
      
        tobj.style.display = ''; 
        
      }; // end if
      
    } // end for

  } // end Basis.hideElement()

  Basis.makePopup = function (id, title, content, disallow_close) {

    // gets screen coordinates
    var scroll_coords = Basis.scroll_coords();

    // blacks out screen at 15%
    var blackout         = DIV({"id" : "blackout" + id, "style" : "vertical-align: middle; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; display: none; background-color: #000000;	filter:alpha(opacity=15);	-moz-opacity:0.15;	-khtml-opacity: 0.15; opacity: 0.15;"});
    
    if (!disallow_close) {
    
      var close_button = "<img style=\"cursor: pointer\" onClick=\"Basis.removePopup('" + id + "')\" src=\"images/icons/cross_grey.png\" />";
    
    } else {
    
      var close_button = "&nbsp;";
    
    } // end if

    // creates popup frame
    var popup          = DIV({"id" : id, "style" : "position: absolute; display: none;"});
    popup.innerHTML    = "<table id=\"popup_table\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td class=\"opdn_popup_top_left\">" + title + "</td><td class=\"opdn_popup_top_right\">" + close_button + "</td></tr><tr><td class=\"opdn_popup_content\" colspan=\"2\">" + MochiKit.Base.evalJSON(decodeBase64(content)) + "</td></tr><tr><td class=\"opdn_popup_bottom\" colspan=\"2\">&nbsp;</td></tr></table>";

    // appends both divs to browser window and centers popup
    document.body.appendChild(blackout);
    document.body.appendChild(popup);
    Basis.center_element(popup,"","");

    //shows blackout
    var blackout = MochiKit.DOM.getElement("blackout" + id);
    blackout.style.display = "";
    //shows popup;
    popup.style.display = "";

  } // end Basis.makePopup();

  Basis.removePopup = function (id) {

    //removes popups
    MochiKit.DOM.removeElement(id);
    MochiKit.DOM.removeElement("blackout" + id);
    
    return false;

  } // end Basis.removePopup();

  Basis.alert = function (content) {
  
    var alert = "<table cellpadding=\"2\" cellpadding=\"0\" style=\"align: center; width: 100%;\"><tr><td align=\"center\">" + content + "</td></tr><tr><td align=\"center\"><button type=\"button\" class=\"sexybutton sexysimple\" onclick=\"Basis.removePopup('" + encodeBase64(MochiKit.Base.serializeJSON(content)) + "');\">OK</button></td></tr></table>";

    Basis.makePopup(encodeBase64(MochiKit.Base.serializeJSON(content)), "Alert", encodeBase64(MochiKit.Base.serializeJSON(alert)));
    
    return false;

  } // end Basis.alert();
  
  Basis.getCookie = function (cookie_name) {

    var cookiename = cookie_name + "=";

    var ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {

        var c = ca[i];

        while (c.charAt(0)==' ') c = c.substring(1,c.length);

        if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);

    }  // end for

    return null;

  } // end Basis.getCookie();

  Basis.setCookie = function ( name, value, expires, path, domain, secure ) {

    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */

    if ( expires ) {

      expires = expires * 1000 * 60 * 60 * 24;

    } // end if

    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );

  } // end Basis.setCookie();

  Basis.countdown2 = function (sec, min, popup_number, original_title) {

    var cookie_data = unescape(Basis.getCookie('occrralogout')).split("+");

    if (cookie_data[0] == "timeout" || cookie_data[0] == "0" || cookie_data[2] == "yes") {

      if (window.location.href.indexOf('?') > 0 ) {

        Basis.setCookie('occrralogout', escape('0+no+yes'));
        var url = window.location.href + '&logout=true';

      } else {

        Basis.setCookie('occrralogout', escape('0+no+yes'));
        var url = window.location.href + '?logout=true';

      };

      window.parent.location = url;

    } // end if

    var countdown_text_container = MochiKit.DOM.getElement("logoff_countdown_" + popup_number);
    
    if (countdown_text_container) {

      sec--;

      if (sec == -01) {

        sec = 59;
        min = min - 1;

      } else {

       min = min;

      } // end if

      if (sec<=9) {

        sec = "0" + sec;

      } // end if

      time = (min<=9 ? "0" + min : min) + " minutes and " + sec + " seconds. ";

      countdown_text_container.innerHTML = time;

      if (window.popup_visible == "no") {

        return false;

      } // end if

      if (sec % 2) {

        document.title = "Notification!";

      } else {

        document.title = "Logoff in " + min + ":" + sec + "!";

      } // end if
      
      SD = setTimeout("Basis.countdown2('"+sec+"', '"+min+"', '"+popup_number+"', '"+original_title+"');", 1000);

      if (min < '00' && sec < '00') {

        clearTimeout(SD);
        document.title = original_title;

      } // end if
      
    } // end if
      
  } // end Basis.countdown2()

  Basis.timeout2 = function (time_before_logout, time_before_popup, session_id, preserve_url, popup_number) {

    var cookie_data = unescape(Basis.getCookie('occrralogout')).split("+");

    if (cookie_data[0] == 0) {

      window.location.href = "" + window.location.href;

    } else {

      var as          = new Basis.ajax_scaffold;
      as.alias        = "get_ts";
      as.sid          = session_id;
      as.url          = "util_get_ts.php";

      var to_post = "hi there";

      as.on_success = function (req) { // this function executes whenever data is returned

        var timestamp = req.responseText;

        var as          = new Basis.ajax_scaffold;
        as.alias        = "check_user_session_activity";
        as.sid          = session_id;
        as.url          = preserve_url;

        var to_post                  = new Object();
        to_post["timestamp"]         = timestamp;
        to_post["time_before_popup"] = time_before_popup;

        if (cookie_data[0] == "timeout") {

          if (window.location.href.indexOf('?') > 0 ) {

            Basis.setCookie('occrralogout', escape('0+no+yes'));
            var url = window.location.href + '&logout=true';

          } else {

          Basis.setCookie('occrralogout', escape('0+no+yes'));
          var url = window.location.href + '?logout=true';

          };

          window.parent.location = url;

        } // end if

        as.on_success = function (req) { // this function executes whenever data is returned

          var response = MochiKit.Base.evalJSON(req.responseText);

          if (response[0] == "set_logout_countdown") {

            var minutes = Math.floor((time_before_logout/60000) % 60);
            var seconds = (time_before_logout/1000) % 60;
            var old_title        = document.title;
            window.popup_visible = "yes";

            Basis.makePopup("logoff_alert_" + popup_number, "Inactivity Notice", encodeBase64(MochiKit.Base.serializeJSON("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"align: center; vertical-align: middle; padding: 5px;\"><tr><td align=\"center\"><span style=\"font-size: 15pt;\">You have been idle for too long.</span><hr><br><span style=\"font-size: 12pt;\"> To continue, please click the button below.</span></td></tr><tr><td align=\"center\" style=\"padding-top: 20px;\"><button type=\"button\" class=\"sexybutton sexysimple sexyblack\" onClick=\"var obj = MochiKit.DOM.getElement('logoff_alert_" + popup_number + "'); if (obj) { clearTimeout(SD); document.title = '" + old_title + "'; window.popup_visible = 'no'; clearTimeout(window.opdn_logout); setTimeout('Basis.timeout2(" + time_before_logout + ", " + time_before_popup + ", \\'" + session_id + "\\', \\'" + preserve_url + "\\', " + (popup_number + 1) +");', " + time_before_popup + "); Basis.setCookie('occrralogout', escape('" + response[1] + "+no+no')); Basis.removePopup('logoff_alert_" + popup_number + "'); }\"><span class=\"time_add\">Keep me logged in!</span></button></td></tr><tr><td align=\"center\"><br><br>You will be logged off in <span id=\"logoff_countdown_" + popup_number + "\">&nbsp;</span></td></tr></table>")), "false");
            //message.innerHTML    = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" valign=\"middle\" align=\"center\"><tr><td align=\"center\"><h2>You have been idle for too long. To stay logged in, please click OK below and save the record you were working on.</h2></td></tr></td></tr><tr><td align=\"center\"><input type=\"button\" value=\"OK\" onClick=\"var obj = MochiKit.DOM.getElement('logoff_alert_" + popup_number + "'); if (obj) { document.title = '" + old_title + "'; fade(obj); window.popup_visible = 'no'; clearTimeout(window.opdn_logout); setTimeout('Basis.timeout2(" + time_before_logout + ", " + time_before_popup + ", \\'" + session_id + "\\', \\'" + preserve_url + "\\', " + (popup_number + 1) +");', " + time_before_popup + "); Basis.setCookie('occrralogout', escape('" + response[1] + "+no+no')); }\"</td></tr><tr><td align=\"center\"><br><br>You will be logged off in <span id=\"logoff_countdown_" + popup_number + "\"></span></td></tr></table>";

            if (cookie_data[1] != "yes") {
 
              Basis.setCookie('occrralogout', escape(cookie_data[0] + "+yes+" + cookie_data[2]));
              Basis.countdown2(seconds,minutes,popup_number,old_title);
              window.opdn_logout = setTimeout(" var cookie_data = unescape(Basis.getCookie('occrralogout')).split('+');  if (cookie_data[1] == 'yes') { if (window.location.href.indexOf('?') > 0 ) { Basis.setCookie('occrralogout', escape('0+no+yes')); var url = window.location.href + '&logout=true'; } else { Basis.setCookie('occrralogout', escape('0+no+yes')); var url = window.location.href + '?logout=true'; }; window.parent.location = url; } else { setTimeout('Basis.timeout2(" + time_before_logout + ", " + time_before_popup + ", \\'" + session_id + "\\', \\'" + preserve_url + "\\', " + (popup_number + 1) +");', " + time_before_popup + "); Basis.setCookie('occrralogout', escape('timeout+no+yes')); } var obj = MochiKit.DOM.getElement('logoff_alert_" + popup_number + "'); if (obj) { fade(obj) } ;", time_before_logout);

            } // end if

          } else {

            if (cookie_data[1] == "yes") {

              var new_time_before_popup = "5000";

            } else {

              var new_time_before_popup = time_before_popup;

            } // end if

            clearTimeout(window.opdn_logout);
            setTimeout("Basis.timeout2(" + time_before_logout + ", " + time_before_popup + ", '" + session_id + "', '" + preserve_url + "', " + (popup_number + 1) +");", new_time_before_popup);

          } // end if

        };

        return as.post(to_post); // this actually sends the request

      };

      return as.post(to_post); // this actually sends the request

    } // end if

  }; // end Basis.timeout2()

  Basis.countDown = function (sec, min, popup_number) {

      sec--;

      if (sec == -01) {

        sec = 59;
        min = min - 1;

      } else {

       min = min;

      } // end if

      if (sec<=9) {

        sec = "0" + sec;

      } // end if

      time = (min<=9 ? "0" + min : min) + " minutes and " + sec + " seconds. ";
      
      var logoff_countdown_popup = MochiKit.DOM.getElement("logoff_countdown_" + popup_number + "");
      
      if (logoff_countdown_popup) {

        logoff_countdown_popup.innerHTML = time;
      
      } // end if

      if (window.popup_visible == "no") {

        return;

      } // end if

      if (sec % 2) {

        document.title = "Notification!";

      } else {

        document.title = "Logoff in " + min + ":" + sec + "!";

      } // end if

      SD = setTimeout("Basis.countDown('"+sec+"', '"+min+"', '"+popup_number+"');", 1000);

      if (min == '00' && sec == '00') {

        clearTimeout(SD);

      } // end if

  } // end Basis.countDown()

  Basis.timeout = function (time_before_logout, time_before_popup, popup_number) {

    var minutes = Math.floor((time_before_logout/60000) % 60);
    var seconds = (time_before_logout/1000) % 60;
    var old_title        = document.title;
    //var message          = DIV({"id" : "logoff_alert_" + popup_number, "style" : "border: 1px solid #0C0C0C; padding: 15px; position: absolute; width: 450px; height: 125px; display: none; background-color: #FFFFFF; filter: alpha(opacity=30); opacity: .3;"});

    window.popup_visible = "yes";

    Basis.makePopup("logoff_alert_" + popup_number, "Inactivity Notice", encodeBase64(MochiKit.Base.serializeJSON("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"align: center; vertical-align: middle; padding: 5px;\"><tr><td align=\"center\"><span style=\"font-size: 15pt;\">You have been idle for too long.</span><hr><br><span style=\"font-size: 12pt;\"> To continue, please click the button below.</span></td></tr><tr><td align=\"center\" style=\"padding-top: 20px;\"><button type=\"button\" class=\"sexybutton sexysimple sexyblack\" onClick=\"var obj = MochiKit.DOM.getElement('logoff_alert_" + popup_number + "'); if (obj) { document.title = '" + old_title + "'; fade(obj); window.popup_visible = 'no'; clearTimeout(window.opdn_logout); setTimeout('Basis.timeout(" + time_before_logout + ", " + time_before_popup + ", " + (popup_number + 1) +");', " + time_before_popup + "); Basis.removePopup('logoff_alert_" + popup_number + "') }\"><span class=\"time_add\">Keep me logged in!</span></button></td></tr><tr><td align=\"center\"><br><br>You will be logged off in <span id=\"logoff_countdown_" + popup_number + "\"></span></td></tr></table>")), "false");

    Basis.countDown(seconds,minutes,popup_number);

    window.opdn_logout = setTimeout("if (window.location.href.indexOf('?') > 0 ) { var url = window.location.href + '&logout=true'; } else { var url = window.location.href + '?logout=true'; }; window.parent.location = url;", time_before_logout);

  }; // end Basis.timeout()

  Basis.mouse_coords = function (e) {

    var posx = 0;
    var posy = 0;

    if (!e) { var e = window.event; } // end if

    if (e.pageX || e.pageY) {

      posx = e.pageX;
      posy = e.pageY;

    } else if (e.clientX || e.clientY) {

      posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;

    } // end if

    return {"x":posx,"y":posy};

  }; // end Basis.mouse_coords()

  Basis.ajax_scaffold = function () {

    this.__init__();

  }; // end Basis.ajax_scaffold()

  Basis.ajax_scaffold.instances = [];

  Basis.ajax_scaffold.prototype = {

    __init__: function () {

      this.alias;
      this.sid;
      this.url;

      this.send_raw   = false;

      this.on_success = function () { return; };

      this.index      = Basis.ajax_scaffold.instances.length;
      Basis.ajax_scaffold.instances[this.index] = this;

    }, // end __init__()

    attach_indicator: function () {

      var obj = MochiKit.DOM.getElement("ajax_scaffold_indicator");

      if (!obj) {

        var obj = new MochiKit.DOM.DIV({"id":"ajax_scaffold_indicator","style":"display:none; width: 16px; height: 16px; position: absolute;"},null);

        obj.innerHTML = "<img id='ajax_scaffold_indicator_image' src=''><script>var obj = MochiKit.DOM.getElement('ajax_scaffold_indicator_image'); if (obj && Basis.misc_vars['ajax_loading'].src) { obj.src = Basis.misc_vars['ajax_loading'].src; }</script>";

        document.body.appendChild(obj);

        Basis.execute_javascript(obj);

        this.position_indicator();

        MochiKit.Signal.connect(MochiKit.DOM.currentDocument(),"onmousemove",this,function () { this.position_indicator(); });

        obj.style.display = "";

      } // end if

    }, // end attach_indicator()

    form_post: function (form_id) {

      var obj = MochiKit.DOM.getElement(form_id);

      if (obj) {

        var action    = MochiKit.DOM.getElement(obj).action;
        var form_data = MochiKit.DOM.formContents(obj);

        if (action && form_data) {

          form_data[0][form_data[0].length] = "exit_after_catch";
          form_data[1][form_data[1].length] = "true";

          var to_post = new Object();

          for (var i in form_data[0]) {

            if (typeof(form_data[0][i]) != "function") {

              to_post[form_data[0][i]] = form_data[1][i];

            } // end if

          } // end for

          this.url = action;

          return this.post(to_post);

        } else {

          log("no action or no form_data");

        } // end if

      } // end if

    }, // end form_post()

    position_indicator: function () {

      var obj    = MochiKit.DOM.getElement("ajax_scaffold_indicator");
      var scroll = Basis.scroll_coords();

      if (obj && Basis.misc_vars["mouse_x"] && Basis.misc_vars["mouse_y"] && scroll && (scroll.x >= 0) && (scroll.y >= 0)) {

        var pos    = {"x":(Basis.misc_vars["mouse_x"] + scroll.x + 15),
                      "y":(Basis.misc_vars["mouse_y"] + scroll.y + 10)};

        MochiKit.Style.setElementPosition(obj,pos);

      } // end if

    }, // end position_indicator()

    post: function (options) {

      if (this.url && this.sid) {

        if (!this.send_raw) {

          var to_send = { "alias"             : this.alias,
                          "ajax_throw_verify" : hex_md5(this.sid)
                        };

          if (options) {

            for (var i in options) {

              to_send[i] = options[i];

            } // end for

          } // end if

        } else {

          var to_send = options;

        } // end if

        if (this.url.indexOf("?") != -1) {

          var base = this.url.substring(0,this.url.indexOf("?"));
          var args = parseQueryString(this.url.substring((this.url.indexOf("?")+1),this.url.length), true);

        } else {

          var base = this.url;
          var args = {};

        } // end if

        args["PHPSESSID"] = this.sid;
        args["alias"]     = this.alias;
        var date          = new Date();
        args["unique"]    = date.getTime();

        var query_string = "";

        for (var i in args) {

          if (typeof(args[i]) != "function") {

            query_string += i+"="+args[i]+"&";

          } // end if

        } // end for

        this.url = base+"?"+query_string;

        this.attach_indicator();

        if (!this.send_raw) {

          var send_content = "array="+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send)));

        } else {

          var temp = "";

          for (var i in to_send) {

            temp += urlencode(i)+"="+urlencode(to_send[i])+"&";

          } // end for

          var send_content = temp;

        } // end if

        this.deferred = MochiKit.Async.doXHR(this.url,{"method"      : "POST",
                                                       "headers"     : {"Content-type":"application/x-www-form-urlencoded"},
                                                       "sendContent" : send_content
                                                      });

        this.deferred.addCallback(bind(this.on_success,this));
        this.deferred.addCallback(bind(this.remove_indicator,this));

        return this.deferred;

      } // end if

      return false;

    }, // end post()

    remove_indicator: function () {

      var obj = MochiKit.DOM.getElement("ajax_scaffold_indicator");

      if (obj) {

        MochiKit.DOM.removeElement(obj);

      } // end if

    } // end remove_indicator()

  }; // end Basis.ajax_scaffold


  // class.bcalendar.js
  // Copyright (c) 2005,2006 Kevin Kaiser / Data Resolutions

  try {

    if (typeof(Basis) == 'undefined') {

      throw "";

    } // end if

  } catch (e) {

    throw "Basis.bcalendar depends on Basis!";

  } // end try
  
  if (typeof(Basis.bcalendar) == 'undefined') {

    Basis.bcalendar = {};

  } // end if
  
  Basis.bcalendar.NAME    = 'Basis.bcalendar';
  Basis.bcalendar.VERSION = '0.1';
  
  Basis.bcalendar.__repr__ = function () {

    return '[' + this.NAME + ' ' + this.VERSION + ']';

  }; // end __repr__()
  
  Basis.bcalendar.toString = function () {

    return this.__repr__();

  }; // end toString()

  Basis.bcalendar = function () {

    this.__init__();

  }; // end gallery()

  Basis.bcalendar.instances = [];

  Basis.bcalendar.prototype = {
  
    __init__: function () {
  
      this.mtend  = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31};
      this.months = {1:"January",2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"};

      this.container_element;
      this.variable_name;
      this.date_element;
      this.append_to_element;

      this.month;
      this.day;
      this.year;

      this.original_month;
      this.original_day;
      this.original_year;

      this.loading_events;

      this.sid;

      this.events             = new Object();

      this.has_events         = false;
      this.show_selected_day  = true;
      this.visible            = false;
      this.linked_to_dropdown = true;
      this.logged_in          = false; // sessions and associate AJAX functions require an actual authenticated PHP session, so while it's trivial
                                       // that a person could edit the HTML to set this.logged_in = true, the accompanying posts to edit or update
                                       // data would fail.
      this.class_prefix       = "bcalendar";
      this.mode               = "calendar"; // enum('calendar','tasklist');
      this.output             = "";

      this.list_id            = 1;

      this.duration           = 0.00;

      this.index                            = Basis.bcalendar.instances.length;
      Basis.bcalendar.instances[this.index] = this;

    }, // end __init__()

    add_event: function (month,day,year) {

      if (month && day && year) {

        if (MochiKit.DOM.getElement("bcalendar_add_event_popup")) {
  
          MochiKit.DOM.removeElement("bcalendar_add_event_popup");
  
        } // end if
  
        if (!MochiKit.DOM.getElement("bcalendar_add_event_popup")) {
  
          var popup  = new MochiKit.DOM.DIV({"id":"bcalendar_add_event_popup","style":"display:none; background-color: #FFFFFF; border: 1px solid #c0c0c0; border-right: 2px outset #c0c0c0; border-bottom: 2px outset #c0c0c0; padding: 5px; position: absolute;"},null);
          document.body.appendChild(popup);
  
        } // end if

        var date_parts           = this.date_parts((year+"-"+month+"-"+day),true);

        var to_send             = new Object();
        to_send["date"]         = date_parts["y"]+"-"+date_parts["m"]+"-"+date_parts["d"];
        to_send["form_request"] = this.list_id;

        window.status = "Fetching form...";

        AjaxRequest.ae_this     = this.index;

        AjaxRequest.month = month;
        AjaxRequest.day   = day;
        AjaxRequest.year  = year;

        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_add_event_form.php?PHPSESSID='+this.sid,
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          'onSuccess'   : function (req) {

                            var element = MochiKit.DOM.getElement("bcalendar_add_event_popup");

                            if (element) {

                              element.innerHTML = "<table><tr><td><div style=\"background-color: #F3F8FF;\"><b>Add Event - "+Basis.bcalendar.instances[AjaxRequest.ae_this].months[eval(AjaxRequest.month)]+" "+eval(AjaxRequest.day)+", "+eval(AjaxRequest.year)+"</b></td><td align=\"right\"><img id=\"bcalendar_add_event_popup_x\" src=\"images/x_off.gif\" style=\"cursor: pointer;\" onclick=\"var element = MochiKit.DOM.getElement('bcalendar_add_event_popup'); element.style.display = (element.style.display == 'none') ? 'inline' : 'none'; MochiKit.DOM.removeElement(element);\"></div></td></tr><tr><td>"+req.responseText+"</td></tr><tr><td align=\"right\"><input type=\"button\" value=\"Save\" onclick=\""+Basis.bcalendar.instances[AjaxRequest.ae_this].variable_name+".add_event_post('"+eval(AjaxRequest.year)+"-"+((eval(AjaxRequest.month)<10) ? ("0"+eval(AjaxRequest.month)) : (eval(AjaxRequest.month)))+"-"+((eval(AjaxRequest.day)<10) ? ("0"+eval(AjaxRequest.day)) : (eval(AjaxRequest.day)))+"');\"></td></tr></table><script>Basis.load_rollover_images({0:{\"id\":\"bcalendar_add_event_popup_x\",\"off\":\"images/x_off.gif\",\"on\":\"images/x_on.gif\"}});</script>";

                              Basis.execute_javascript(element);

                              Basis.center_element(element);

                              element.style.display = (element.style.display == "none") ? "inline" : "none";
                        
                            } // end if
  
                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end add_event()

    add_event_post: function (date) {

      var element = MochiKit.DOM.getElement("bcalendar_add_event_popup");

      if (element) {

        // 1. scrape form values

        var form_data = MochiKit.DOM.formContents("form_999999");

        form_data[0][form_data[0].length] = "PHPSESSID";
        form_data[1][form_data[1].length] = this.sid;

        form_data[0][form_data[0].length] = "date";
        form_data[1][form_data[1].length] = date;

        form_data[0][form_data[0].length] = "form_request";
        form_data[1][form_data[1].length] = this.list_id;

        window.status = "Saving event...";

        // 2. post via AJAX to save to database

        AjaxRequest.aep_this = this.index;
        AjaxRequest.date     = date;

        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_add_event_save.php?PHPSESSID='+this.sid,
          'queryString' : MochiKit.Base.queryString(form_data[0],form_data[1]),
          'onSuccess'   : function (req) {

                            // 3. this.date_render_events(THIS_DATE) to update html

                            MochiKit.DOM.removeElement("bcalendar_add_event_popup");

                            Basis.bcalendar.instances[AjaxRequest.aep_this].compile();
  
                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end add_event_post()

    append_output: function () {

      if (this.container_element) {

        var element = MochiKit.DOM.getElement(this.container_element);

        if (element) {

          var temp = "";
  
          temp += "<div>";
          temp += "  <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"margin-bottom: 10px; border: 1px solid #EBEBEB; background-color: #FFFFFF; padding: 10px;\">";
          temp += "    <tr>";
          temp += "      <td width=\"20%\" align=\"center\"><span style=\"cursor: pointer; font-weight: bold;\" onclick=\""+this.variable_name+".compile();\">Calendar View</span></td>";
          temp += "      <td width=\"20%\" align=\"center\"><span style=\"cursor: pointer; font-weight: bold;\" onclick=\""+this.variable_name+".tasklist_mode();\">Project List View</span></td>";
//        temp += "      <td width=\"20%\" align=\"center\"><b>Summary View</b></td>";
//        temp += "      <td width=\"20%\" align=\"center\"><b>Hide Overdue</b></td>";

          if (this.logged_in) {

            temp += "      <td width=\"20%\" align=\"center\"><span style=\"cursor: pointer; font-weight: bold;\" onclick=\""+this.variable_name+".tasklist_manage_categories();\"><b>Manage Project Categories</b></td>";

          } // end if

          temp += "    </tr>";
          temp += "  </table>";
          temp += "</div>";

          temp = "";

          this.output = temp+this.output;

          element.innerHTML = this.output;

          Basis.execute_javascript(element);

        } // end if

        if (!this.linked_to_dropdown) {

          element.style.display  = "block";
          element.style.position = "static";
          element.style.zIndex   = 0;

        } // end if

      } // end if

    }, // end append_output()

    bcalendar_td_out: function (element) {

      if (MochiKit.DOM.getElement(element)) {

        MochiKit.DOM.getElement(element).className = MochiKit.DOM.getElement(element).oclass;

      } // end if

    }, // end bcalendar_td_over()

    bcalendar_td_over: function (element) {

      if (MochiKit.DOM.getElement(element)) {

        MochiKit.DOM.getElement(element).oclass    = MochiKit.DOM.getElement(element).className;
        MochiKit.DOM.getElement(element).className = this.class_prefix+"_td_over";

      } // end if

    }, // end bcalendar_td_over()

    calendar_mode: function () {

      this.calendar_render();

    }, // end calendar_mode()

    calendar_render: function () {

      if (MochiKit.DOM.getElement("bcalendar_all_events_popup")) {

        MochiKit.DOM.removeElement("bcalendar_all_events_popup");

      } // end if

      var tdate = new Date();

      if (!this.month) { this.month = tdate.getMonth()+1;  } // end if
      if (!this.day)   { this.day   = tdate.getDay();      } // end if
      if (!this.year)  { this.year  = tdate.getFullYear(); } // end if

      if (!this.original_month) { this.original_month = this.month; } // end if
      if (!this.original_day)   { this.original_day   = this.day;   } // end if
      if (!this.original_year)  { this.original_year  = this.year;  } // end if

      if (this.variable_name && (this.date_element || !this.linked_to_dropdown)) {

        if (!MochiKit.DOM.getElement(this.container_element)) {

          if (this.date_element) {

            this.container_element = "calendar_"+this.date_element.name+"_"+this.date_element.rrv+"_"+this.date_element.findex;

          } else {

            this.container_element = "calendar_"+this.variable_name;

          } // end if

        } // end if

        this.day   = 1;

        var grid   = {0:{0:"",1:"",2:"",3:"",4:"",5:"",6:""},
                      1:{0:"",1:"",2:"",3:"",4:"",5:"",6:""},
                      2:{0:"",1:"",2:"",3:"",4:"",5:"",6:""},
                      3:{0:"",1:"",2:"",3:"",4:"",5:"",6:""},
                      4:{0:"",1:"",2:"",3:"",4:"",5:"",6:""},
                      5:{0:"",1:"",2:"",3:"",4:"",5:"",6:""}};

        this.output = "";
  
        if (this.month && this.day && this.year) {
  
          var date_info = this.get_date_info();
  
          var t         = date_info.day_of_week;
          var counter   = 1;
          
          if (date_info) {
  
            for (y=0;y<6;y++) {
  
              for (x=t;x<7;x++) {
  
                if (counter <= date_info.days_in_month) {
  
                  grid[y][x] = counter;
                  counter++;
  
                } // end if
  
              } // end for
  
              t = 0;
  
            } // end for
  
          } // end if
  
          if ((this.month-1) <= 0) {
  
            var pmonth = 12;
            var pyear  = this.year-1;
  
          } else {
  
            var pmonth = this.month-1;
            var pyear  = this.year;
  
          } // end if
  
          if ((this.month+1) >= 13) {
  
            var nmonth = 1;
            var nyear  = this.year+1;
  
          } else {
  
            var nmonth = this.month+1;
            var nyear  = this.year;
  
          } // end if
  
          this.output += "<table id=\"bcalendar_table_"+this.variable_name+"\" class=\""+this.class_prefix+"_table\">\n";
          this.output += "  <tbody>";
          this.output += "    <tr>\n";
          this.output += "      <td id=\"bcalendar_table_"+this.variable_name+"_nav_left\"><a href=\"javascript:if ("+this.variable_name+") {"+this.variable_name+".navigate("+pmonth+","+pyear+"); }\" onmouseover=\"window.status = 'Done'; return true;\"><img src=\"images/prev_on.gif\" border=\"0\"></a></td>\n";
          this.output += "      <td id=\"bcalendar_table_"+this.variable_name+"_month\" class=\""+this.class_prefix+"_month_year\" colspan=\"5\">"+this.months[this.month]+" "+this.year+"</td>\n";
          this.output += "      <td id=\"bcalendar_table_"+this.variable_name+"_nav_right\" align=\"right\"><a href=\"javascript:if ("+this.variable_name+") {"+this.variable_name+".navigate("+nmonth+","+nyear+"); }\" onmouseover=\"window.status = 'Done'; return true;\"><img src=\"images/next_on.gif\" border=\"0\"></a></td>\n";
          this.output += "    </tr>\n";
          this.output += "    <tr class=\""+this.class_prefix+"_tr\">\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Sun</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Mon</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Tue</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Wed</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Thu</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Fri</td>\n";
          this.output += "      <td class=\""+this.class_prefix+"_header_td\">Sat</td>\n";
          this.output += "    </tr>\n";
  
          var rows = 6;

          if (!grid[5][0]) { rows = 5; } // end if

          for (y=0;y<rows;y++) {
  
            this.output += "    <tr class=\""+this.class_prefix+"_tr\">\n";
  
            for (x=0;x<7;x++) {
  
              var today = new Date();
              var td_js = "";
  
              if ((today.getDate()==grid[y][x])&&((today.getMonth()+1)==this.month)&&(today.getFullYear()==this.year)) {
  
                today = "_today";
  
              } else {

                if (!grid[y][x]) {

                  today = "_outside_month";

                } else {
  
                  today = "";

                } // end if
  
              } // end if

              if (this.show_selected_day) {

                if (this.date_element) {
  
                  var month_element = MochiKit.DOM.getElement(this.date_element.name+"_month_"+this.date_element.rrv);
                  var day_element   = MochiKit.DOM.getElement(this.date_element.name+"_day_"+this.date_element.rrv);
                  var year_element  = MochiKit.DOM.getElement(this.date_element.name+"_year_"+this.date_element.rrv);
          
                  if (month_element && day_element && year_element) {
  
                    if ((month_element.options[month_element.selectedIndex].value == this.month) &&
                        (day_element.options[day_element.selectedIndex].text == grid[y][x]) &&
                        (year_element.options[year_element.selectedIndex].text == this.year)) {
  
                      today = "_selected";
  
                    } // end if
  
                  } // end if
  
                } else {
  
                  if ((this.original_month == this.month) && (this.original_year == this.year) && (this.original_day == grid[y][x])) {
  
                    today = "_selected";
  
                  } // end if
  
                } // end if

              } // end if

              if (grid[y][x]) {

                td_js = " onmouseover=\"if ("+this.variable_name+") {"+this.variable_name+".bcalendar_td_over(this); }\" onmouseout=\"if ("+this.variable_name+") {"+this.variable_name+".bcalendar_td_out(this); }\" "; 
               
                if (this.linked_to_dropdown) {

                  td_js += " onclick=\"if ("+this.variable_name+") {"+this.variable_name+".update_date_element('"+this.month+"','"+grid[y][x]+"','"+this.year+"',this); }\" ";

                } // end if

              } // end if

              var add_event = "";

              if (this.logged_in && grid[y][x]) {

                var add_event = "<a href=\"javascript:if ("+this.variable_name+") {"+this.variable_name+".add_event('"+this.month+"','"+grid[y][x]+"','"+this.year+"'); }\" onmouseover=\"window.status = 'Done'; return true;\"><img src=\"images/add_date_small.gif\" border=\"0\" style=\"cursor: pointer;\"></a>";

              } // end if

              this.output += "      <td id=\"bcalendar_table_"+this.variable_name+"_"+grid[y][x]+"\" class=\""+this.class_prefix+"_td"+today+"\" "+td_js+"><table width=\"100%\"><tr><td><span class=\""+this.class_prefix+"_td_date\">"+grid[y][x]+"</span></td><td align=\"right\">"+add_event+"</td></tr></table></td>\n";
  
            } // end for
  
            this.output += "    </tr>\n";
  
          } // end for
  
          this.output += "  </tbody>";
          this.output += "</table>";
  
        } // end if

        if (!MochiKit.DOM.getElement(this.container_element)) {

          var container_element = new MochiKit.DOM.DIV({"id"    : this.container_element,
                                                        "class" : this.class_prefix+"_container"},
                                                        null);

          if (this.date_element) {
 
            var element = MochiKit.DOM.getElement("calendar_"+this.date_element.rrv+"_"+this.date_element.findex+"_placeholder");

          } else if (this.append_to_element && MochiKit.DOM.getElement(this.append_to_element)) {

            var element = MochiKit.DOM.getElement(this.append_to_element);

          } else {

            var element = document.body;

          } // end if

          element.appendChild(container_element);

        } // end if

        this.append_output();

        if (!this.date_element && this.has_events) {

          this.tasklist_get_categories();
          this.date_get_events(this.year+"-"+this.month);

        } // end if

        return this.output;

      } else {

        return false;

      } // end if

    }, // end calendar_render()

    compile: function () {

      switch (this.mode) {

        case "calendar":

          this.calendar_mode();
          break;

        case "tasklist":

          this.tasklist_mode();
          break;

        default:

          this.calendar_mode();
          break;

      } // end switch

    }, // end compile();

    date_parts: function (date,padded) {

      if (date) {

        var date_parts = date.split("-");
        var year       = date_parts[0];
        var month      = date_parts[1];
        var day        = date_parts[2];

        if (year && (year.substring(0,1)=="0"))   { year  = year.substring(1);  } // end if
        if (month && (month.substring(0,1)=="0")) { month = month.substring(1); } // end if
        if (day && (day.substring(0,1)=="0"))     { day   = day.substring(1);   } // end if

        if (year)  { year  = eval(year);  } // end if
        if (month) { month = eval(month); } // end if
        if (day)   { day   = eval(day);   } // end if

        if (!padded) {

	        return {"y":year,
	                "m":month,
	                "d":day};

        } else {

	        return {"y":(year<10)  ? ("0"+year)  : (year),
	                "m":(month<10) ? ("0"+month) : (month),
	                "d":(day<10)   ? ("0"+day)   : (day)};

        } // end if

      } // end if

    }, // end date_parts()

    date_get_events: function (date) {

      if (date && !this.linked_to_dropdown) {

        window.status        = "Fetching calendar events...";

        var to_send          = new Object();
        to_send["date"]      = date;
        to_send["list_id"]   = this.list_id;

        AjaxRequest.dge_this = this.index;

        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_get_events.php?PHPSESSID='+this.sid,
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          'onSuccess'   : function(req) {

                            Basis.bcalendar.instances[AjaxRequest.dge_this].events = MochiKit.Base.evalJSON(req.responseText);

                            var date_parts = Basis.bcalendar.instances[AjaxRequest.dge_this].date_parts(date);
                            var year       = date_parts["y"];
                            var month      = date_parts["m"];
                            var day        = date_parts["d"];

                            if (day == "0") { day = null; } // end if

                            if (year && month && !day) {

                              var date_info = Basis.bcalendar.instances[AjaxRequest.dge_this].get_date_info();

                              for (x=1; x<=date_info["days_in_month"]; x++) {

                                Basis.bcalendar.instances[AjaxRequest.dge_this].date_render_events(year+"-"+month+"-"+x);

                              } // end for

                            } else {

                              Basis.bcalendar.instances[AjaxRequest.dge_this].date_render_events(date);

                            } // end if
  
                            window.status = "Done";
  
                          }
        });

      } // end if     

    }, // end date_get_events()

    date_render_events: function (date,total_show,update_html) {

      var date_parts = this.date_parts(date,true);
      var year       = date_parts["y"];
      var month      = date_parts["m"];
      var day        = date_parts["d"];

      date = year+"-"+month+"-"+day;

      if (this.events[date]) {

        var events = this.events[date];

        if (events) {

          var output     = "";
          var x          = 0;
          var total      = 0;
          var trim       = false;
          var trimmed    = false;

          if (!total_show) { 

            total_show = 4;
            trim       = true;

          } // end if

          if ((year == this.year) && (month == this.month)) {

            for (var i in events) {
  
              if (events[i] && events[i]["event"]) {
  
                if (x<total_show) {

                  var event_title = events[i]["event"];

                  if (trim && (event_title.length > 15)) {

                    event_title = (event_title.substring(0,12)+"...");
                    trimmed     = true;

                  } // end if

                  output += "<div id=\"bcalendar_event_"+events[i]["id"]+"\" class=\""+this.class_prefix+"_event\" style=\"cursor: pointer;\" onmouseover=\"this.className='"+this.class_prefix+"_event_over';\" onmouseout=\"this.className='"+this.class_prefix+"_event';\" onclick=\""+this.variable_name+".event_render_details('"+events[i]["id"]+"');\">"+event_title+"</div>\n";
  
                  x++;

                } // end if

                total++;

              } // end if

            } // end for

            if (output && (total > x)) {

              output += "<div class=\"bcalendar_event\" align=\"right\"><a href=\"javascript:if ("+this.variable_name+") {"+this.variable_name+".date_render_all_events('"+date+"'); }\" onmouseover=\"window.status = 'Done'; return true;\">..."+(total-x)+" more</a></div>";

            } else {

              if (trimmed) {

                output += "<div class=\"bcalendar_event\" align=\"right\"><a href=\"javascript:if ("+this.variable_name+") {"+this.variable_name+".date_render_all_events('"+date+"'); }\" onmouseover=\"window.status = 'Done'; return true;\">...more</a></div>";

              } // end if

            } // end if

            if ((update_html==null) || (update_html==true)) {

              var element = MochiKit.DOM.getElement("bcalendar_table_"+this.variable_name+"_"+eval(day));
  
              if (element) {
  
                element.innerHTML += output;
  
              } // end if

            } // end if

          } // end if

          return output;

        } // end if

      } // end if

    }, // end date_render_events()

    date_render_all_events: function (date) {

      if (MochiKit.DOM.getElement("bcalendar_all_events_popup")) {

        MochiKit.DOM.removeElement("bcalendar_all_events_popup");

      } // end if

      var date_parts = this.date_parts(date);
      var year       = date_parts["y"];
      var month      = date_parts["m"];
      var day        = date_parts["d"];

      var events = this.date_render_events(date,1000,false);

      if (!MochiKit.DOM.getElement("bcalendar_all_events_popup")) {

        var popup  = new MochiKit.DOM.DIV({"id":"bcalendar_all_events_popup","style":"display:none; background-color: #FFFFFF; border: 1px solid #c0c0c0; border-right: 2px outset #c0c0c0; border-bottom: 2px outset #c0c0c0; padding: 5px; position: absolute;"},null);
        document.body.appendChild(popup);

      } // end if

      var element = MochiKit.DOM.getElement("bcalendar_all_events_popup");

      if (element) {

        events = "<table><tr><td><div style=\"background-color: #F3F8FF;\"><b>"+this.months[eval(month)]+" "+eval(day)+", "+eval(year)+"</b></td><td align=\"right\"><img id=\"bcalendar_all_events_popup_x\" src=\"images/x_off.gif\" style=\"cursor: pointer;\" onclick=\"var element = MochiKit.DOM.getElement('bcalendar_all_events_popup'); element.style.display = (element.style.display == 'none') ? 'inline' : 'none'; MochiKit.DOM.removeElement(element);\"></div></td></tr><tr><td>"+events+"</td></tr></table><script>Basis.load_rollover_images({0:{\"id\":\"bcalendar_all_events_popup_x\",\"off\":\"images/x_off.gif\",\"on\":\"images/x_on.gif\"}});</script>";
  
        element.innerHTML = events;
  
        Basis.execute_javascript(element);
        
        var to_pos  = MochiKit.DOM.getElement("bcalendar_table_"+this.variable_name+"_"+eval(day));
  
        if (to_pos) {
  
          to_pos = MochiKit.Style.getElementPosition(to_pos);
  
          MochiKit.Style.setElementPosition(element,to_pos);

          element.style.display = (element.style.display == "none") ? "inline" : "none";
  
        } // end if

      } // end if

    }, // end date_render_all_events()

    delete_event: function (event_id) {

      if (confirm('Are you sure you want to delete this event?')==true) {

        window.status       = "Deleting calendar event...";
  
        var to_send         = new Object();
        to_send["event_id"] = event_id;
  
        AjaxRequest.de_this = this.index;
  
        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_delete_event.php?PHPSESSID='+this.sid,
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          'onSuccess'   : function(req) {
                        
                            if (MochiKit.DOM.getElement("bcalendar_add_event_popup")) {
                      
                              MochiKit.DOM.removeElement("bcalendar_add_event_popup");
                      
                            } // end if
                      
                            if (MochiKit.DOM.getElement("bcalendar_event_detail_popup")) {
                      
                              MochiKit.DOM.removeElement("bcalendar_event_detail_popup");
                      
                            } // end if
                      
                            if (MochiKit.DOM.getElement("bcalendar_all_events_popup")) {
                      
                              MochiKit.DOM.removeElement("bcalendar_all_events_popup");
                      
                            } // end if

                            Basis.bcalendar.instances[AjaxRequest.de_this].compile();
  
                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end delete_event()

    edit_event: function (event_id,date) {

      if (event_id && date) {

        var date_parts       = this.date_parts(date,true);

        var to_send          = new Object();
        to_send["event_id"]  = event_id;
        to_send["date"]      = date_parts["y"]+"-"+date_parts["m"]+"-"+date_parts["d"];

        AjaxRequest.ee_this  = this.index;
        AjaxRequest.event_id = event_id;
        AjaxRequest.day      = date_parts["d"];

        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_edit_event_form.php?PHPSESSID='+this.sid,
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          'onSuccess'   : function (req) {

                            var element = MochiKit.DOM.getElement("bcalendar_event_detail_popup");

                            if (element) {

                              element.innerHTML = "<table><tr><td><div style=\"background-color: #F3F8FF;\"><b>Edit Event</b></td><td align=\"right\"><img id=\"bcalendar_event_detail_popup_x\" src=\"images/x_off.gif\" style=\"cursor: pointer;\" onclick=\"var element = MochiKit.DOM.getElement('bcalendar_event_detail_popup'); element.style.display = (element.style.display == 'none') ? 'inline' : 'none'; MochiKit.DOM.removeElement(element);\"></div></td></tr><tr><td>"+req.responseText+"</td></tr><tr><td align=\"right\"><input type=\"button\" value=\"Save\" onclick=\""+Basis.bcalendar.instances[AjaxRequest.ee_this].variable_name+".save_event_post('"+AjaxRequest.event_id+"');\"></td></tr></table><script>Basis.load_rollover_images({0:{\"id\":\"bcalendar_add_event_popup_x\",\"off\":\"images/x_off.gif\",\"on\":\"images/x_on.gif\"}});</script>";

                              Basis.execute_javascript(element);

                              Basis.center_element(element);
                        
                            } // end if
  
                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end edit_event()

    event_get_details: function (id) {

      if (this.events) {

        for (var date in this.events) {

          for (var i in this.events[date]) {

            if (this.events[date][i]["id"] && this.events[date][i]["id"] == id) {

              return this.events[date][i];

            } // end if

          } // end for

        } // end for

      } // end if

      return false;

    }, // end event_get_details()

    event_render_details: function (id) {

      if (MochiKit.DOM.getElement("bcalendar_event_detail_popup")) {

        MochiKit.DOM.removeElement("bcalendar_event_detail_popup");

      } // end if

      var element = MochiKit.DOM.getElement("bcalendar_event_"+id);

      if (element) {

        if (!MochiKit.DOM.getElement("bcalendar_event_detail_popup")) {
  
          var popup  = new MochiKit.DOM.DIV({"id":"bcalendar_event_detail_popup","style":"display:none; background-color: #FFFFFF; border: 1px solid #c0c0c0; border-right: 2px outset #c0c0c0; border-bottom: 2px outset #c0c0c0; padding: 5px; position: absolute;"},null);
          document.body.appendChild(popup);
  
        } // end if

        var innerHTML = "<table cellpadding=\"5\">";

        var details   = this.event_get_details(id);

        if (details && details["assigned_date"] && (details["assigned_date"] != "0000-00-00")) {

	        var date_parts  = this.date_parts(details["assigned_date"]);
	        var event_year  = date_parts["y"];
	        var event_month = date_parts["m"];
	        var event_day   = date_parts["d"];

        } // end if

        if (details && details["event"]) {

          innerHTML += "<tr><td><b>Name:</b></td><td>"+details["event"]+"</td></tr>\n";

        } // end if

        if (details && details["category_id"] && this.tasklist_categories && this.tasklist_categories[details["category_id"]]) {

          innerHTML += "<tr><td><b>Project:</b></td><td>"+this.tasklist_categories[details["category_id"]]+"</td></tr>\n";

        } // end if

        if (details && details["due_date"] && (details["due_date"] != "0000-00-00")) {

	        var date_parts  = this.date_parts(details["due_date"]);
	        var due_year    = date_parts["y"];
	        var due_month   = date_parts["m"];
	        var due_day     = date_parts["d"];

          innerHTML += "<tr><td><b>Due Date:</b></td><td>"+this.months[due_month].substring(0,3)+" "+due_day+", "+due_year+"</td></tr>\n";

        } // end if

        if (details && details["notes"]) {

          innerHTML += "<tr><td><b>Notes:</b></td><td>"+details["notes"].replace(/\n/g,"<br>")+"</td></tr>\n";

        } // end if

        if (details && details["notes2"]) {

          innerHTML += "<tr><td><b>More notes:</b></td><td>"+details["notes2"].replace(/\n/g,"<br>")+"</td></tr>\n";

        } // end if

        innerHTML += "</table>";

        var delete_button = "";
        var edit_button   = "";

        if (this.logged_in) {

          edit_button   = "<input type=\"button\" value=\"Edit\" onclick=\""+this.variable_name+".edit_event('"+id+"','"+details["assigned_date"]+"');\">";
          delete_button = "<input type=\"button\" value=\"Delete\" onclick=\""+this.variable_name+".delete_event('"+id+"');\">";

        } // end if

        innerHTML  = "<table><tr><td><div style=\"background-color: #F3F8FF;\"><b>Event Detail</b></td><td align=\"right\"><img id=\"bcalendar_event_detail_popup_x\" src=\"images/x_off.gif\" style=\"cursor: pointer;\" onclick=\"var element = MochiKit.DOM.getElement('bcalendar_event_detail_popup'); element.style.display = (element.style.display == 'none') ? 'inline' : 'none'; MochiKit.DOM.removeElement(element);\"></div></td></tr><tr><td>"+innerHTML+"</td></tr></table><script>Basis.load_rollover_images({0:{\"id\":\"bcalendar_event_detail_popup_x\",\"off\":\"images/x_off.gif\",\"on\":\"images/x_on.gif\"}});</script><div align=\"right\">"+edit_button+"&nbsp;"+delete_button+"</div>";

        var element       = MochiKit.DOM.getElement("bcalendar_event_detail_popup");

        if (element) {

          element.innerHTML = innerHTML;
  
          Basis.execute_javascript(element);
  
          var to_pos  = MochiKit.DOM.getElement("bcalendar_event_"+id);
    
          if (to_pos) {
    
            to_pos = MochiKit.Style.getElementPosition(to_pos);
    
            MochiKit.Style.setElementPosition(element,to_pos);
    
            element.style.display = (element.style.display == "none") ? "inline" : "none";
    
          } // end if

        } // end if
        
      } // end if

    }, // end event_render_details()

    format_date_short: function (date_text) {

      if (date_text) {

        if (date_text != "0000-00-00") {
  
	        var date_parts = this.date_parts(date_text);
	        var y          = date_parts["y"];
	        var m          = date_parts["m"];
	        var d          = date_parts["d"];
    
          return this.months[m].substring(0,3)+" "+d+", "+y;
  
        } else {
  
          return "&nbsp;";
  
        } // end if

      } // end if

      return "&nbsp;";

    }, // end format_date_short()

    format_date_long: function (date_text) {

      if (date_text) {
   
        if (date_text != "0000-00-00") {
  
	        var date_parts = this.date_parts(date_text);
	        var y          = date_parts["y"];
	        var m          = date_parts["m"];
	        var d          = date_parts["d"];
    
          return this.months[m]+" "+d+", "+y;
  
        } else {
  
          return "&nbsp;";
  
        } // end if

      } // end if

      return "&nbsp;";

    }, // end format_date_long()

    get_date_info: function () {

      this.month = eval(this.month);
      this.day   = eval(this.day);
      this.year  = eval(this.year);
    
      var mtend = this.mtend[this.month];

      if  ((this.month==2)&&((this.year%4)==0)) {

        mtend++;

      } // end if

      if (this.day < (mtend+1)) {
    
        var c = new Date();
        c.setFullYear(this.year,(this.month-1),this.day);

        return {"day_of_week"   : c.getDay(),
                "days_in_month" : mtend};
    
      } else {
    
        return false;
    
      } // end if
    
    }, // end get_date_info()

    navigate: function (m,y) {

      if (!this.date_element) {

        if (MochiKit.DOM.getElement("bcalendar_add_event_popup")) {
  
          MochiKit.DOM.removeElement("bcalendar_add_event_popup");
  
        } // end if
  
        if (MochiKit.DOM.getElement("bcalendar_event_detail_popup")) {
  
          MochiKit.DOM.removeElement("bcalendar_event_detail_popup");
  
        } // end if
  
        if (MochiKit.DOM.getElement("bcalendar_all_events_popup")) {
  
          MochiKit.DOM.removeElement("bcalendar_all_events_popup");
  
        } // end if

      } // end if

      this.month = m;
      this.year  = y;

      this.compile();

      if (MochiKit.DOM.getElement(this.container_element)) {

        MochiKit.DOM.getElement(this.container_element).innerHTML = this.output;

        Basis.execute_javascript(this.container_element);

      } // end if

      if (this.date_element) {

        var info          = this.get_date_info();
        var days_in_month = info["days_in_month"];

        var element       = MochiKit.DOM.getElement(this.date_element.name+"_day_"+this.date_element.rrv);

        if (element) {

          if (days_in_month != element.options.length) {

            var sindex = element.selectedIndex;

            element.options.length = 0;

            for (var x=0;x<days_in_month;x++) {

              element.options[x] = new Option(x+1,x);

            } // end for

            if (sindex) {

              element.selectedIndex = sindex;

            } // end if

          } // end if

        } // end if

      } // end if

    }, // end navigate()

    save_event_post: function (id) {

      var element = MochiKit.DOM.getElement("bcalendar_event_detail_popup");

      if (element) {

        var form_data = MochiKit.DOM.formContents("form_888888");

        form_data[0][form_data[0].length] = "PHPSESSID";
        form_data[1][form_data[1].length] = this.sid;

        form_data[0][form_data[0].length] = "id";
        form_data[1][form_data[1].length] = id;

        window.status = "Saving event...";

        AjaxRequest.sep_this = this.index;

        AjaxRequest.post({
  
          'url'         : 'ajax/calendar_date_edit_event_save.php?PHPSESSID='+this.sid,
          'queryString' : MochiKit.Base.queryString(form_data[0],form_data[1]),
          'onSuccess'   : function (req) {

                            MochiKit.DOM.removeElement("bcalendar_event_detail_popup");

                            Basis.bcalendar.instances[AjaxRequest.sep_this].compile();
  
                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end save_event_post()

    tasklist_get_categories: function (on_complete) {

      window.status        = "Fetching tasklist events...";

      if (!on_complete) {

        on_complete = function () {};

      } // end if

      this.on_complete     = on_complete;

      var to_send          = new Object();
      to_send["list_id"]   = this.list_id;

      AjaxRequest.tge_this = this.index;

      AjaxRequest.post({

        'url'         : 'ajax/tasklist_get_categories.php?PHPSESSID='+this.sid,
        'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
        'onSuccess'   : function(req) {

                          Basis.bcalendar.instances[AjaxRequest.tge_this].tasklist_categories = MochiKit.Base.evalJSON(req.responseText);

                          var obj = MochiKit.DOM.getElement("category_id_999999");

                          if (obj) {

                            MochiKit.Logging.log("category_id_999999");

                          } // end if

                          var obj = MochiKit.DOM.getElement("category_id_888888");

                          if (obj) {

                            MochiKit.Logging.log("category_id_888888");

                          } // end if

                          Basis.bcalendar.instances[AjaxRequest.tge_this].on_complete();

                          window.status = "Done";

                        }
      });

    }, // end tasklist_get_categories()

    tasklist_get_events: function (on_complete) {

      window.status        = "Fetching tasklist events...";

      if (!on_complete) {

        on_complete = function () {};

      } // end if

      this.on_complete     = on_complete;

      var to_send          = new Object();
      to_send["list_id"]   = this.list_id;

      AjaxRequest.tge_this = this.index;

      AjaxRequest.post({

        'url'         : 'ajax/tasklist_get_events.php?PHPSESSID='+this.sid,
        'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
        'onSuccess'   : function(req) {

                          Basis.bcalendar.instances[AjaxRequest.tge_this].tasklist_events = MochiKit.Base.evalJSON(req.responseText);
                          Basis.bcalendar.instances[AjaxRequest.tge_this].on_complete();

                          window.status = "Done";

                        }
      });

    }, // end tasklist_get_events()

    tasklist_manage_categories: function () {

      var obj = MochiKit.DOM.getElement("bcalendar_tasklist_manage_categories_popup");

      if (obj) {

        MochiKit.DOM.removeElement("bcalendar_tasklist_manage_categories_popup");

      } else {

        window.status        = "Fetching tasklist categories...";
  
        var to_send          = new Object();
        to_send["parent_id"] = "17";
  
        AjaxRequest.post({
  
          "url"         : "ajax/tasklist_manage_categories_form.php?PHPSESSID="+this.sid,
          "queryString" : "array="+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          "onSuccess"   : function (req) {
  
                            var obj = MochiKit.DOM.getElement("bcalendar_tasklist_manage_categories_popup");
  
                            if (!obj) {
                      
                              var popup  = new MochiKit.DOM.DIV({"id":"bcalendar_tasklist_manage_categories_popup","style":"display: none; background-color: #FFFFFF; border: 1px solid #c0c0c0; border-right: 2px outset #c0c0c0; border-bottom: 2px outset #c0c0c0; padding: 5px; position: absolute;"},null);
                              document.body.appendChild(popup);

                            } // end if
  
                            var obj = MochiKit.DOM.getElement("bcalendar_tasklist_manage_categories_popup");
  
                            if (obj) {
  
                              obj.innerHTML = "<table><tr><td><div id=\"bcalendar_tasklist_manage_categories_popup_handle\" style=\"cursor: move; background-color: #F3F8FF;\"><b>Manage Project Categories</b></td><td align=\"right\"><img id=\"bcalendar_tasklist_manage_categories_popup_img\" src=\"images/x_off.gif\" style=\"cursor: pointer;\" onclick=\"var element = MochiKit.DOM.getElement('bcalendar_tasklist_manage_categories_popup'); MochiKit.DOM.removeElement(element);\"></div></td></tr><tr><td>"+req.responseText+"</td></tr></table><script>Basis.load_rollover_images({0:{\"id\":\"bcalendar_tasklist_manage_categories_popup_img\",\"off\":\"images/x_off.gif\",\"on\":\"images/x_on.gif\"}});</script>";
  
                              new MochiKit.DragAndDrop.Draggable(obj.id,{"handle"      : (obj.id+"_handle"),
                                                                         "starteffect" : MochiKit.Base.noop,
                                                                         "endeffect"   : MochiKit.Base.noop
                                                                        });

                              Basis.execute_javascript(obj);
                              Basis.center_element(obj);

                              obj.style.display = (obj.style.display == "none") ? "inline" : "none";

                            } // end if
  
                            window.status = "Done";
  
                          }
        });

      } // end if
      
    }, // end tasklist_manage_categories()

    tasklist_mode: function () {

      this.output = "<img src=\"images/ajax_loading.gif\">";
      this.append_output();

      this.tasklist_get_categories(function () {

       this.tasklist_get_events(function () {

          this.tasklist_render_events();
          this.append_output();

        });

      });
      
    }, // end tasklist_mode()

    tasklist_render_events: function () {

      if (this.tasklist_events && this.tasklist_categories) {

        this.output  = "<style>";
        this.output += "";
        this.output += "  .tasklist_row {";
        this.output += "  ";
        this.output += "    border: 1px solid #EBEBEB;";
        this.output += "    margin-bottom: 3px;";
        this.output += "  ";
        this.output += "  }";
        this.output += "  ";
        this.output += "  .tasklist_row td {";
        this.output += "  ";
        this.output += "    border-top: inherit;";
        this.output += "    border-bottom: inherit;";
        this.output += "    border-left: 0px;";
        this.output += "    border-right: 0px;";
        this.output += "    background-color: #FFFFFF;";
        this.output += "    color: #626262;";
        this.output += "  ";
        this.output += "  }";
        this.output += "  ";
        this.output += "  ";
        this.output += "  .tasklist_row td.left {";
        this.output += "  ";
        this.output += "    border-left: inherit;";
        this.output += "    border-top: inherit;";
        this.output += "    border-bottom: inherit;";
        this.output += "    border-right: 0px;";
        this.output += "  ";
        this.output += "  }";
        this.output += "  ";
        this.output += "  .tasklist_row td.right {";
        this.output += "  ";
        this.output += "    border-right: inherit;";
        this.output += "    border-top: inherit;";
        this.output += "    border-bottom: inherit;";
        this.output += "    border-left: 0px;";
        this.output += "  ";
        this.output += "  }";
        this.output += "";
        this.output += "</style>";

        for (var i in this.tasklist_categories) {

          this.output += "<div style=\"display: inline; font-weight: bold; padding-right: 5px;\">"+this.tasklist_categories[i]+"</div>";

          if (this.logged_in) {

            this.output += "<img src=\"images/add_date_small.gif\">";

          } // end if

          this.output += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
          this.output += "  <tr>";
          this.output += "    <td width=\"18\" style=\"vertical-align: top; padding-top: 5px;\"><img border=\"0\" id=\""+this.variable_name+"_"+i+"_icon\" src=\"images/tree-node-open-end.gif\" style=\"cursor: pointer;\" width=\"18\" height=\"18\" onclick=\"Basis.toggle_visible('"+this.variable_name+"_"+i+"_td'); (Basis.is_visible('"+this.variable_name+"_"+i+"_td')) ? (this.src = 'images/tree-node-open-end.gif') : (this.src = 'images/tree-node-end.gif');\"></td>";
          this.output += "    <td id=\""+this.variable_name+"_"+i+"_td\" style=\"padding: 5px;\">";
          this.output += "      <table border=\"0\" cellpadding=\"5\" width=\"100%\" style=\"border-spacing: 0px 5px;\" class=\"\">";
          this.output += "        <tr class=\"tasklist_row\">";
          this.output += "          <td width=\"100\" class=\"left\"><b>Date</b></td>";
          this.output += "          <td><b>Task Name</b></td>";
          this.output += "          <td width=\"100\"><b>Due</b></td>";
          this.output += "          <td width=\"125\"><b>Assigned To</b></td>";
          this.output += "          <td width=\"125\"><b>Status</b></td>";
          this.output += "          <td width=\"100\" align=\"center\"><b>More Detail</b></td>";
          this.output += "          <td width=\"100\" align=\"center\" class=\"right\"><b>Archive Task</b></td>";
          this.output += "        </tr>";

          for (var j in this.tasklist_events[i]) {

            if (typeof(this.tasklist_events[i][j]) != "function") {

              for (var k in this.tasklist_events[i][j]) {
  
                if (this.tasklist_events[i][j][k]=="") {
  
                  this.tasklist_events[i][j][k] = "&nbsp;";
  
                } // end if
  
              } // end for

              this.output += "        <tr class=\"tasklist_row\">";
              this.output += "          <td width=\"100\" class=\"left\">"+this.format_date_long(this.tasklist_events[i][j]["assigned_date"])+"</td>";
              this.output += "          <td>"+this.tasklist_events[i][j]["event"]+"</td>";
              this.output += "          <td width=\"100\">"+this.format_date_long(this.tasklist_events[i][j]["due_date"])+"</td>";
              this.output += "          <td width=\"125\">"+this.tasklist_events[i][j]["assigned_to"]+"</td>";
              this.output += "          <td width=\"125\">"+this.tasklist_events[i][j]["status"]+"</td>";
              this.output += "          <td width=\"100\" align=\"center\">m.detail</td>";
              this.output += "          <td width=\"100\" align=\"center\" class=\"right\">a.task</td>";
              this.output += "        </tr>";

            } // end if

          } // end for

          this.output += "      </table>";
          this.output += "    </td>";
          this.output += "  </tr>";
          this.output += "</table>";

        } // end for

      } // end if

    }, // end tasklist_render_events()

    toggle: function (button_element) {

      if (MochiKit.DOM.getElement(this.container_element) && MochiKit.DOM.getElement(button_element)) {

        if (!this.visible) {

          this.month = this.original_month;
          this.day   = this.original_day;
          this.year  = this.original_year;

          this.compile();

          var from_pos = MochiKit.Style.getElementPosition(button_element);
          var from_dim = MochiKit.Style.getElementDimensions(button_element);
          var to_dim   = MochiKit.Style.getElementDimensions(this.container_element);

          var to_pos   = new Object;
          to_pos.x     = from_pos.x + from_dim.w + 5;
          to_pos.y     = from_pos.y - to_dim.h;

          if (MochiKit.DOM.isChildNode(button_element,"bcalendar_add_event_popup")) {

            var minus_pos = MochiKit.Style.getElementPosition("bcalendar_add_event_popup");

            to_pos.x -= minus_pos.x;
            to_pos.y -= minus_pos.y;
    
          } else if (MochiKit.DOM.isChildNode(button_element,"bcalendar_event_detail_popup")) {

            var minus_pos = MochiKit.Style.getElementPosition("bcalendar_event_detail_popup");

            to_pos.x -= minus_pos.x;
            to_pos.y -= minus_pos.y;
    
          } // end if

          MochiKit.Style.setElementPosition(this.container_element,to_pos);

        } // end if

        if (!this.visible) { this.visible = true; } else { this.visible = false; } // end if

        MochiKit.DOM.getElement(this.container_element).style.display = (MochiKit.DOM.getElement(this.container_element).style.display == "none") ? "inline" : "none";

      } // end if

    }, // end toggle()

    update_calendar: function () {

      if (this.date_element && this.date_element.name && this.date_element.rrv) {

        var month_element = MochiKit.DOM.getElement(this.date_element.name+"_month_"+this.date_element.rrv);
        var day_element   = MochiKit.DOM.getElement(this.date_element.name+"_day_"+this.date_element.rrv);
        var year_element  = MochiKit.DOM.getElement(this.date_element.name+"_year_"+this.date_element.rrv);

        if (month_element && day_element && year_element) {

          this.month          = month_element.options[month_element.selectedIndex].value;
          this.day            = day_element.options[day_element.selectedIndex].text;
          this.year           = year_element.options[year_element.selectedIndex].text;

          this.original_month = this.month;
          this.original_day   = this.day;
          this.original_year  = this.year;

          this.compile();

        } // end if

      } // end if

    }, // end update_calendar()

    update_date_element: function (month,day,year,button_element) {

      if (this.date_element && this.date_element.name && this.date_element.rrv && month && day && year) {

        var month_element = MochiKit.DOM.getElement(this.date_element.name+"_month_"+this.date_element.rrv);
        var day_element   = MochiKit.DOM.getElement(this.date_element.name+"_day_"+this.date_element.rrv);
        var year_element  = MochiKit.DOM.getElement(this.date_element.name+"_year_"+this.date_element.rrv);

        if (month_element && day_element && year_element) {

          for (var i in month_element.options) {

            if ((month_element.options[i]) && (month_element.options[i].value) && (month_element.options[i].value == month)) {

              month_element.options[i].selected = true;
              this.original_month               = month_element.options[i].value;

            } // end if

          } // end for

          for (var i in day_element.options) {

            if ((day_element.options[i]) && (day_element.options[i].text) && (day_element.options[i].text == day)) {

              day_element.options[i].selected = true;
              this.original_day               = day_element.options[i].text;

            } // end if

          } // end for

          for (var i in year_element.options) {

            if ((year_element.options[i]) && (year_element.options[i].text) && (year_element.options[i].text == year)) {

              year_element.options[i].selected = true;
              this.original_year               = year_element.options[i].text;

            } // end if

          } // end for

          if (button_element && MochiKit.DOM.getElement(button_element)) {

            this.toggle(button_element);

          } // end if

        } // end if

      } // end if

    } // end update_date_element()

  }; // end class Basis.bcalendar
  Basis.tabs = function () {

    this.__init__();

  } // end Basis.tabs()

  Basis.tabs.instances = [];

  Basis.tabs.prototype = {

    __init__: function () {

      var tabs        = new Array();
      var default_url = "";

      this.index                       = Basis.tabs.instances.length;
      Basis.tabs.instances[this.index] = this;

    }, // end __init__()

    delete_tab: function (index) {

      index = index.split("_");
      index = index[1];

      if (index) {

        to_send       = new Object();
        to_send["id"] = index;

        AjaxRequest.tab_object = this;
        AjaxRequest.tab_index  = index;

        AjaxRequest.post({
  
          'url'         : 'ajax/delete_tab.php?PHPSESSID='+this.sid,
          'queryString' : 'array='+urlencode(encodeBase64(MochiKit.Base.serializeJSON(to_send))),
          'onSuccess'   : function(req) {

                            if (req.responseText == "true") {

                              if (MochiKit.DOM.getElement("tab_"+AjaxRequest.tab_index+"_container")) {
  
                                MochiKit.DOM.removeElement("tab_"+AjaxRequest.tab_index+"_container");
  
                              } // end if

                              var nearest_tab = null;
                              var _this       = Basis.tabs.instances[AjaxRequest.tab_object.index];

                              delete _this.tabs[AjaxRequest.tab_index];

                              for (var i in _this.tabs) {

                                if ( (i != AjaxRequest.tab_index) && (_this.tabs[i]) ) {

                                  nearest_tab = i;

                                } // end if

                              } // end for

                              _this.set_tab_types(_this.active_tab);

                            } // end if

                            if (_this) {

                              if (_this.active_tab == AjaxRequest.tab_index) {
  
                                var use_default_url = true;
  
                                for (var i in _this.tabs) {
  
                                  use_default_url = false;
  
                                } // end for
  
                                if (!use_default_url) {
  
                                  _this.load_tab(nearest_tab);
  
                                } else {
    
                                  window.location = _this.default_url;
  
                                } // end if
  
                              } else if (req.responseText == "false") {
  
                              } // end if

                            } // end if

                            window.status = "Done";
  
                          }
        });

      } // end if

    }, // end delete_tab()

    find_hash_index: function (array,hash) {

      var x          = 0;
      var return_val = null;

      for (var i in array) {

        if (i==hash) {

          return_val = x;

        } // end if

        x++;

      } // end for

      return return_val;

    }, // end find_hash_index()

    load_tab: function (index) {

      if (this.tabs[index]) {

        if ((this.tabs[index].type == "get") && (this.tabs[index].url)) {

          window.location = this.tabs[index].url;

        } // end if

      } // end if

    }, // end load_tab()

    set_tab_types: function (current_index) {

      for (var i in this.tabs) {
  
        if (this.tabs[i]) {
  
          if (MochiKit.DOM.getElement("tab_"+i+"_a_img")) {
  
            MochiKit.DOM.getElement("tab_"+i+"_a_img").src = "images/spacer.gif";

          } // end if
  
          if (MochiKit.DOM.getElement("tab_"+i+"_b_img") && MochiKit.DOM.getElement("tab_"+i+"_d_img") && MochiKit.DOM.getElement("tab_"+i+"_c") && MochiKit.DOM.getElement("tab_"+i+"_x")) {

            if (-1 >= (this.tabs.length-1)) {
  
              if (i == current_index) { 
    
                MochiKit.DOM.getElement("tab_"+i+"_b_img").src   = "images/right_tab_on_left.gif";
                MochiKit.DOM.getElement("tab_"+i+"_d_img").src   = "images/right_tab_on_right.gif";
                MochiKit.DOM.getElement("tab_"+i+"_c").className = "right_tab_on_middle";
                MochiKit.DOM.getElement("tab_"+i+"_x").className = "right_tab_on_middle";
  
    
              } else {
    
                MochiKit.DOM.getElement("tab_"+i+"_b_img").src   = "images/right_tab_off_left.gif";
                MochiKit.DOM.getElement("tab_"+i+"_d_img").src   = "images/right_tab_off_right.gif";
                MochiKit.DOM.getElement("tab_"+i+"_c").className = "right_tab_off_middle";
                MochiKit.DOM.getElement("tab_"+i+"_x").className = "right_tab_off_middle";
    
              } // end if
    
            } else {
    
              if (i == current_index) {
    
                MochiKit.DOM.getElement("tab_"+i+"_b_img").src   = "images/tab_on_left.gif";
                MochiKit.DOM.getElement("tab_"+i+"_d_img").src   = "images/tab_on_right.gif";
                MochiKit.DOM.getElement("tab_"+i+"_c").className = "tab_regular_on_middle";
                MochiKit.DOM.getElement("tab_"+i+"_x").className = "tab_regular_on_middle";
    
              } else {
    
                MochiKit.DOM.getElement("tab_"+i+"_b_img").src   = "images/tab_off_left.gif";
                MochiKit.DOM.getElement("tab_"+i+"_d_img").src   = "images/tab_off_right.gif";
                MochiKit.DOM.getElement("tab_"+i+"_c").className = "tab_regular_off_middle";
                MochiKit.DOM.getElement("tab_"+i+"_x").className = "tab_regular_off_middle";
    
              } // end if
    
            } // end if
  
          } // end if

        } // end if
  
      } // end for

    } // end set_tab_types()

  }; // end class Basis.tabs
