if (typeof Udig == "undefined" || !Udig) {
    /**
     * The Udig global namespace object.  If Udig is already defined, the
     * existing Udig object will not be overwritten so that defined
     * namespaces are preserved.
     * @class Udig
     * @static
     */
    var Udig = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 * <pre>
 * Udig.namespace("property.package");
 * Udig.namespace("Udig.property.package");
 * </pre>
 * Either of the above would create Udig.property, then
 * Udig.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>
 * Udig.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
 */
Udig.RegisterNamespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=Udig;

        // Udig is implied, so it is ignored if it is included
        for (j=(d[0] == "Udig") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

//Register The Global Object
Udig.RegisterNamespace("Udig.Global");
Udig.Global = {

    BaseUrl:''

};

//The Following Namespace and Functions Handle CSS Class adding and removing
Udig.RegisterNamespace("Udig.Css");
Udig.Css = {

    AddClassName:function(_obj, _class)
    { 
        if (_obj != null)
        {
            if ( _obj.className ) // if there is a class
            {
                //the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');
                
                // if the new class name may already exist in list
                Udig.Css.RemoveClassName(_obj, _class);

                // add the new class to end of list
                arrList[arrList.length] = _class;

                // add the new class to beginning of list
                //arrList.splice(0, 0, class);

                // assign modified class name attribute
                _obj.className = arrList.join(' ');

            }
            // if there was no class
            else
            {
                // assign modified class name attribute      
                _obj.className = _class;
            }
        }
    },

    RemoveClassName:function(_obj, _class)
    {
        var exists = false;
        if (_obj != null)
        {
            // if there is a class
            if ( _obj.className )
            {
                // the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');

                // get uppercase class for comparison purposes
                var classUpper = _class.toUpperCase();

                // find all instances and remove them
                for ( var i = 0; i < arrList.length; i++ )
                {
                    // if class found
                    if ( arrList[i].toUpperCase() == classUpper )
                    {
                        // remove array item
                        arrList.splice(i, 1);
                        // decrement loop counter as we have adjusted the array's contents
                        i--;
                        exists = true;
                    }
                }
                // assign modified class name attribute
                _obj.className = arrList.join(' ');
            }
        }
        return exists;
    },
    
    HasClassName:function(_obj, _class)
    {
        var ret = false;
        if (_obj != null)
        {
            // if there is a class
            if ( _obj.className )
            {
                // the classes are just a space separated list, so first get the list
                var arrList = _obj.className.split(' ');

                // get uppercase class for comparison purposes
                var classUpper = _class.toUpperCase();

                // find all instances and check them
                for ( var i = 0; i < arrList.length; i++ )
                {
                    // if class found
                    if ( arrList[i].toUpperCase() == classUpper )
                    {
                        ret = true;
                    }
                }
                // assign modified class name attribute
                _obj.className = arrList.join(' ');
            }
        }
        return ret;
    },
    
    SwapClassName:function(_addObj, _removeObj, _class)
    {
        Udig.Css.AddClassName(_addObj, _class);
        Udig.Css.RemoveClassName(_removeObj, _class);
    },
    
    ReplaceClassName:function(_obj, _oldClass, _newClass)
    {
        Udig.Css.RemoveClassName(_obj, _oldClass);
        Udig.Css.AddClassName(_obj, _newClass);
    }
};

/* Udig Sound Namespace is a helper function that plays a background sound of your choice */
Udig.RegisterNamespace("Udig.Sounds");
Udig.Sounds = {
    Play:function(sound, debug)
    {
        var embed = document.getElementById('__SendMailSoundFrame');
        if (embed != null)
        {
            embed.parentNode.removeChild(embed);
        }

        embed = document.createElement('embed');
        embed.setAttribute('id', '__SendMailSoundFrame');
        embed.setAttribute('src', sound);
        embed.setAttribute('loop', 'false');
        embed.setAttribute('volume', '200');
        embed.setAttribute('autostart', 'true');
        if (debug == null || debug == false)
        {
            embed.setAttribute('hidden', 'true');
        }
        document.body.appendChild(embed);
    }
};

/* Udig Script Namespace is a helper function that let you create dynamic javascript tags */
//Udig.RegisterNamespace("Udig.Script");
//Udig.Script = {
//    Insert:function(scriptContent)
//    {
//        var script = document.createElement('script');
//        script.setAttribute('type', 'text/javascript');
//        script.setAttribute('language', 'javascript');
//        script.innerHTML = scriptContent;
//        document.body.appendChild(script);
//    }
//};

Udig.RegisterNamespace("Udig.String");
Udig.String = {
    
    IsNullOrEmpty:function()
    {
        var ret = false;
        for(var i=0;i<arguments.length;i++)
        {
           if (ret == false && (arguments[i]==null || Udig.String.Trim(arguments[i].toString()).length == 0)) { ret = true; }
        }
        return ret;
    },
    
    Format:function(str)
    {
        for(var i=1;i<arguments.length;i++)
        {
            var re = new RegExp('\\{' + (i-1) + '\\}','gm');
            var val = (Udig.String.IsNullOrEmpty(arguments[i])) ? '' : arguments[i];
            str = str.replace(re, val);
        }
        return str;
    },
    
    Trim:function(str)
    {
        var matchSpace = /^\s+(.*?)\s+$/;
        return str.replace(matchSpace, "$1");
    }
};
 
Udig.RegisterNamespace("Udig.Utils");
Udig.Utils = {

    IsValidEmail:function(email)
    {
        var reg = new RegExp('^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$');
        return reg.test(email);
    }

};


Udig.RegisterNamespace("Udig.Browser");
Udig.Browser = {
    
    GetWidth:function(){
        var w;
        if(document.innerWidth){ w=document.innerWidth;
        } else if(document.documentElement.clientWidth){ w=document.documentElement.clientWidth;
        } else if(document.body){ w=document.body.clientWidth; }
        return w;
    },

    GetHeight:function(){
        var h;
        if(document.innerHeight){ h=document.innerHeight;
        } else if(document.documentElement.clientHeight){ h=document.documentElement.clientHeight;
        } else if(document.body){ h=document.body.clientHeight; }
        return h;
    }
};

Udig.RegisterNamespace("Udig.Url");
Udig.Url = {

    SetHash:function(value)
    {
        var url = document.location.href.split("#");
        document.location.href = url[0] + '#' + value;
    },
    
    ClearHash:function()
    {
        var url = document.location.href.split("#");
        document.location.href = url[0] + '#';
    },
    
    RemoveHash:function()
    {
        var url = document.location.href.split("#");
        document.location.href = url[0];
    }

};