/**
	Software License Agreement (BSD License)
	Copyright (c) 2009, Yahoo! Inc.
	All rights reserved.
 */
 
/**
 * The CITRIXWS object is the single global object.  It
 * contains utility function for setting up namespaces and inheritance
 */

if (typeof CITRIXWS == "undefined" || !CITRIXWS) {
    /**
     * The CITRIXWS global namespace object.  If CITRIXWS is already defined, the
     * existing CITRIXWS object will not be overwritten so that defined
     * namespaces are preserved.
     * @class CITRIXWS
     * @static
     */
    var CITRIXWS = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 * <pre>
 * CITRIXWS.namespace("property.package");
 * CITRIXWS.namespace("CITRIXWS.property.package");
 * </pre>
 * Either of the above would create CITRIXWS.property, then
 * CITRIXWS.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * CITRIXWS.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */
CITRIXWS.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=CITRIXWS;

        // CITRIXWS is implied, so it is ignored if it is included
        for (j=(d[0] == "CITRIXWS") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

/**
     * Utility to set up the prototype, constructor and superclass properties to
     * support an inheritance strategy that can chain constructors and methods.
     * Static members will not be inherited.
     *
     * @method extend
     * @static
     * @param {Function} subc   the object to modify
     * @param {Function} superc the object to inherit
     * @param {Object} overrides  additional properties/methods to add to the
     *                              subclass prototype.  These will override the
     *                              matching items obtained from the superclass 
     *                              if present.
     */
CITRIXWS.extend = function(subc, superc, overrides) {
        if (!superc||!subc) {
            throw new Error("extend failed, please check that " +
                            "all dependencies are included.");
        }
        var F = function() {}, i;
        F.prototype=superc.prototype;
        subc.prototype=new F();
        subc.prototype.constructor=subc;
        subc.superclass=superc.prototype;
        if (superc.prototype.constructor == OP.constructor) {
            superc.prototype.constructor=superc;
        }
    
        if (overrides) {
            for (i in overrides) {
                if (overrides.hasOwnProperty(i)) {
                    subc.prototype[i]=overrides[i];
                }
            }

            CITRIXWS._IEEnumFix(subc.prototype, overrides);
        }
    };

CITRIXWS._IEEnumFix = (jQuery.browser.msie) ? function(r, s) {
            var i, fname, f;
            for (i=0;i<ADD.length;i=i+1) {

                fname = ADD[i];
                f = s[fname];

                if (L.isFunction(f) && f!=OP[fname]) {
                    r[fname]=f;
                }
            }
    } : function(){}

