module("net.URI")
.declare("net.URI")
.as(/**@lends net.URI.prototype*/{

    /**
     * Creates a new instance of a URI component.
     * @class
     * Utility class that represents a Unique Resource Identifier instance.
     * Provides methods to access different sections of an URI instance and some
     * operations that can be performed to combine it with other URIs.
     * <pre>
     *    foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose
     *    \_/   \_______________/ \_________/ \__/\__________/\_______/ \______________________/ \__/
     *     |           |               |       |        |         |               |                |
     *     |        userinfo          host    port  directory    file           query             anchor
     *     |    \________________________________/\___________________/
     *     |                    |                          |
     *  protocol             authority                    path
     *  (scheme)                                   \_________________________________________________/
     *                                                                        |
     *                                                                     relative
     *  \____________________________________________________________________________________________/
     *                                                  |
     *                                                source
     * </pre>
     * @constructs net.URI
     * @param {string} uri A string contianing a URI to parse.
     * @example
     * var uri = new net.URI("http://www.autodesk.com/path/to/section?q=queryValue#anchor");
     * uri.protocol// "http"
     * uri.authority // "www.autodesk.com"
     * uri.path // "/path/to/section"
     * uri.queryKey // {q: "queryValue"}
     * uri.anchor // "anchor"
     */
    constructor: function (uri){
        jQuery.extend(this, this._parse(uri));
    },
    /**
     * (See the scheme above)
     * @type string
     */
    source:"",
    /**
     * (See the scheme above)
     * @type string
     */
    protocol:"",
    /**
     * (See the scheme above)
     * @type string
     */
    authority:"",
    /**
     * (See the scheme above)
     * @type string
     */
    userInfo:"",
    /**
     * (See the scheme above)
     * @type string
     */
    user:"",
    /**
     * (See the scheme above)
     * @type string
     */
    password:"",
    /**
     * (See the scheme above)
     * @type string
     */
    host:"",
    /**
     * (See the scheme above)
     * @type string
     */
    port:"",
    /**
     * (See the scheme above)
     * @type string
     */
    relative:"",
    /**
     * (See the scheme above)
     * @type string
     */
    path:"",
    /**
     * (See the scheme above)
     * @type string
     */
    directory:"",
    /**
     * (See the scheme above)
     * @type string
     */
    file:"",
    /**
     * (See the scheme above)
     * @type string
     */
    query:"",
    /**
     * A mapped collection of key-values parsed from the <code>query</code>
     * section of the URI.
     * @type object
     */
    queryKey:{},
    /**
     * (See the scheme above)
     * @type string
     */
    anchor:"",

    _parse: function (str) {
        var key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
            parser = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/,
            qparser = /(?:^|&)([^&=]*)=?([^&]*)/g,
            m   = parser.exec(str),
            uri = {},
            i   = 14;

        while (i--) uri[key[i]] = m[i] || undefined;

        if(uri["query"]){
            uri["queryKey"] = {};
            uri["query"].replace(qparser, function ($0, $1, $2) {
                if ($1) uri["queryKey"][$1] = $2;
            });
        }

        return uri;
    },

    /**
     * Combines this uri components with another uri and returns a new Uri instance.
     * This method is useful to, for example, complement uris that define different
     * parts of an url.
     * @param {net.URI} uri The uri instance to combine with.
     * @returns {net.URI} A new <code>net.URI</code> instance result of the
     * combined uris.
     * @example
     * var domainUri = new net.URI("http://autodesk.com");
     * var relativeUri = new net.URI("/path/to/file.html");
     * var fullUri = domainUri.expand(relativeUri);
     *
     * fullUri.toString() // "http://autodesk.com/path/to/file.html"
     */
    expand: function (uri){
        var result = this.clone();
        if (uri.domain){
            jQuery.extend(result, uri);
        }
        else if(uri.path){
            var oldpath = result.path;
            jQuery.extend(result, uri);
            if(oldpath && oldpath !=="/"){
                result.path = oldpath + result.path;
            }
        }
        else if (uri.query){
            if(result.queryKey){
                jQuery.extend(result.queryKey, uri.queryKey);
            }
            else {
                result.queryKey = uri.queryKey;
            }
            result.query = [];
            for (var k in result.queryKey){
                result.query.push(k+"="+result.queryKey[k]);
            }
            result.query = result.query.join("&");
        }

        result.relative = (result.path || "")+ (result.query? "?"+result.query:"") +(result.anchor || "")

        return result;
    },

    /**
     * Indicates if the current uri instance belongs to the current doamin where
     * the application is runnig. It basically checks matches for the
     * <ul>
     * <li>Protocol</li<>
     * <li>Host</li<>
     * <li>Port</li<>
     * </ul>
     * If all of these components match with the current <code>window.location</code>,
     * the result is positive.
     * @returns {boolean} <code>true</code> if the uri is local,
     * <code>false</code> otherwise
     */
    isLocal:function(){
        var local = true;
        local &= !this.protocol || this.protocol == window.location.protocol;
        local &= !this.host || this.host == window.location.host;
        local &= !this.port || this.port == window.location.port;
        return local;
    },

    /**
     * Indicates if the current uri instance does not belong to the current doamin.
     * This call is equivalent to call <code>!uri.isLocal()</code>.
     * @returns {boolean} <code>true</code> if the uri is external,
     * <code>false</code> otherwise.
     */
    isExternal:function(){
        return !this.isLocal();
    },

    /**
     * Creates a new instance of <code>net.URI</code> by duplicating all the
     * data from the current uri object.
     * @returns {net.URI} The duplicate instance of this uri.
     */
    clone: function () {
        var dup = new net.URI("");
        jQuery.extend(dup,this);
        return dup;
    },

    toString: (function(){
        function unparse(construct){
            jQuery.each(construct, function (i, o){
                if(!o){
                    construct.splice(0,construct.length);
                    return false; //break
                }
                else if (jQuery.isArray(o)){
                    construct[i] = unparse(o);
                }
                return true; //continue
            });
            return construct.join("");
        }

        function serializeQueryKey(queryKey) {
            var s = []
            jQuery.each(queryKey, function(key,value){
                s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
            })
            return s.join( "&" )
        }

        return function(){
            var basepath = unparse([[this.protocol,"://"],[this.user,[":",this.password],"@"], this.host,[":",this.port]]);
            var path = unparse([[this.directory],[this.file],["?",serializeQueryKey(this.queryKey)],["#",this.anchor]]);

            return basepath + path;
        }
    })()
},
// Static
{
    fromString: function (str){
        return new net.URI(str);
    },
    fromEncodedString: function (str){
        return new net.URI(decodeURIComponent(str));
    }
});
// SIG // Begin signature block
// SIG // MIIMzQYJKoZIhvcNAQcCoIIMvjCCDLoCAQExDjAMBggq
// SIG // hkiG9w0CBQUAMGYGCisGAQQBgjcCAQSgWDBWMDIGCisG
// SIG // AQQBgjcCAR4wJAIBAQQQEODJBs441BGiowAQS9NQkAIB
// SIG // AAIBAAIBAAIBAAIBADAgMAwGCCqGSIb3DQIFBQAEEMS8
// SIG // 9c9/WGRFIkL+fsrkls6gggoPMIIE/DCCBGWgAwIBAgIQ
// SIG // ZVIm4bIuGOFZDymFrCLnXDANBgkqhkiG9w0BAQUFADBf
// SIG // MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVyaVNpZ24s
// SIG // IEluYy4xNzA1BgNVBAsTLkNsYXNzIDMgUHVibGljIFBy
// SIG // aW1hcnkgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcN
// SIG // MDkwNTIxMDAwMDAwWhcNMTkwNTIwMjM1OTU5WjCBtjEL
// SIG // MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJ
// SIG // bmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3
// SIG // b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0
// SIG // cHM6Ly93d3cudmVyaXNpZ24uY29tL3JwYSAoYykwOTEw
// SIG // MC4GA1UEAxMnVmVyaVNpZ24gQ2xhc3MgMyBDb2RlIFNp
// SIG // Z25pbmcgMjAwOS0yIENBMIIBIjANBgkqhkiG9w0BAQEF
// SIG // AAOCAQ8AMIIBCgKCAQEAvmcdtGCqEElvVhd8Zslehg3V
// SIG // 8ayncYOOi4n4iASJFQa6LYQhleTRnFBM+9IivdrysjU7
// SIG // Ho/DCfv8Ey5av4l8PTslHvbzWHuc9AG1xgq4gM6+J3Rh
// SIG // ZydNauXsgWFYeaPgFxASFSew4U00fytHIES53mYkZorN
// SIG // T7ofxTjIVJDhcvYZZnVquUlozzh5DaowqNssYEie16oU
// SIG // AamD1ziRMDkTlgM6fEBUtq3gLxuD3KgRUj4Cs9cr/SG2
// SIG // p1yjDwupphBQDjQuTafOyV4l1Iy88258KbwBXfwxh1rV
// SIG // jIVnWIgZoL818OoroyHnkPaD5ajtYHhee2CD/VcLXUEN
// SIG // Y1Rg1kMh7wIDAQABo4IB2zCCAdcwEgYDVR0TAQH/BAgw
// SIG // BgEB/wIBADBwBgNVHSAEaTBnMGUGC2CGSAGG+EUBBxcD
// SIG // MFYwKAYIKwYBBQUHAgEWHGh0dHBzOi8vd3d3LnZlcmlz
// SIG // aWduLmNvbS9jcHMwKgYIKwYBBQUHAgIwHhocaHR0cHM6
// SIG // Ly93d3cudmVyaXNpZ24uY29tL3JwYTAOBgNVHQ8BAf8E
// SIG // BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJ
// SIG // aW1hZ2UvZ2lmMCEwHzAHBgUrDgMCGgQUj+XTGoasjY5r
// SIG // w8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVyaXNp
// SIG // Z24uY29tL3ZzbG9nby5naWYwHQYDVR0lBBYwFAYIKwYB
// SIG // BQUHAwIGCCsGAQUFBwMDMDQGCCsGAQUFBwEBBCgwJjAk
// SIG // BggrBgEFBQcwAYYYaHR0cDovL29jc3AudmVyaXNpZ24u
// SIG // Y29tMDEGA1UdHwQqMCgwJqAkoCKGIGh0dHA6Ly9jcmwu
// SIG // dmVyaXNpZ24uY29tL3BjYTMuY3JsMCkGA1UdEQQiMCCk
// SIG // HjAcMRowGAYDVQQDExFDbGFzczNDQTIwNDgtMS01NTAd
// SIG // BgNVHQ4EFgQUl9BrqCZwyKE/lB8ILcQ1m6ShHvIwDQYJ
// SIG // KoZIhvcNAQEFBQADgYEAiwPA3ZTYQaJhabAVqHjHMMaQ
// SIG // PH5C9yS25INzFwR/BBCcoeL6gS/rwMpE53LgULZVECCD
// SIG // bpaS5JpRarQ3MdylLeuMAMcdT+dNMrqF+E6++mdVZfBq
// SIG // vnrKZDgaEBB4RXYx84Z6Aw9gwrNdnfaLZnaCG1nhg+W9
// SIG // SaU4VuXeQXcOWA8wggULMIID86ADAgECAhAjTaBL0kKF
// SIG // PErLmuhhwhm7MA0GCSqGSIb3DQEBBQUAMIG2MQswCQYD
// SIG // VQQGEwJVUzEXMBUGA1UEChMOVmVyaVNpZ24sIEluYy4x
// SIG // HzAdBgNVBAsTFlZlcmlTaWduIFRydXN0IE5ldHdvcmsx
// SIG // OzA5BgNVBAsTMlRlcm1zIG9mIHVzZSBhdCBodHRwczov
// SIG // L3d3dy52ZXJpc2lnbi5jb20vcnBhIChjKTA5MTAwLgYD
// SIG // VQQDEydWZXJpU2lnbiBDbGFzcyAzIENvZGUgU2lnbmlu
// SIG // ZyAyMDA5LTIgQ0EwHhcNMDkwODI3MDAwMDAwWhcNMTIw
// SIG // OTIwMjM1OTU5WjCByDELMAkGA1UEBhMCVVMxEzARBgNV
// SIG // BAgTCkNhbGlmb3JuaWExEzARBgNVBAcTClNhbiBSYWZh
// SIG // ZWwxFjAUBgNVBAoUDUF1dG9kZXNrLCBJbmMxPjA8BgNV
// SIG // BAsTNURpZ2l0YWwgSUQgQ2xhc3MgMyAtIE1pY3Jvc29m
// SIG // dCBTb2Z0d2FyZSBWYWxpZGF0aW9uIHYyMR8wHQYDVQQL
// SIG // FBZEZXNpZ24gU29sdXRpb25zIEdyb3VwMRYwFAYDVQQD
// SIG // FA1BdXRvZGVzaywgSW5jMIGfMA0GCSqGSIb3DQEBAQUA
// SIG // A4GNADCBiQKBgQC1R1YKdvQF2Rj4+knWfj81afUtVyep
// SIG // GF2P4tcYD53qlYmYi9t9Csc6PMWlUZhMGbOceQKYcBjc
// SIG // mWvyghJnoaEQ6796ZcsLD0pMH2R4SpV/SjxiQ280beig
// SIG // NerJS5X+ftOMCs1qSQ1LXlRFEohaewkNWsIp/+f1Y0vE
// SIG // dGzggnEKOQIDAQABo4IBgzCCAX8wCQYDVR0TBAIwADAO
// SIG // BgNVHQ8BAf8EBAMCB4AwRAYDVR0fBD0wOzA5oDegNYYz
// SIG // aHR0cDovL2NzYzMtMjAwOS0yLWNybC52ZXJpc2lnbi5j
// SIG // b20vQ1NDMy0yMDA5LTIuY3JsMEQGA1UdIAQ9MDswOQYL
// SIG // YIZIAYb4RQEHFwMwKjAoBggrBgEFBQcCARYcaHR0cHM6
// SIG // Ly93d3cudmVyaXNpZ24uY29tL3JwYTATBgNVHSUEDDAK
// SIG // BggrBgEFBQcDAzB1BggrBgEFBQcBAQRpMGcwJAYIKwYB
// SIG // BQUHMAGGGGh0dHA6Ly9vY3NwLnZlcmlzaWduLmNvbTA/
// SIG // BggrBgEFBQcwAoYzaHR0cDovL2NzYzMtMjAwOS0yLWFp
// SIG // YS52ZXJpc2lnbi5jb20vQ1NDMy0yMDA5LTIuY2VyMB8G
// SIG // A1UdIwQYMBaAFJfQa6gmcMihP5QfCC3ENZukoR7yMBEG
// SIG // CWCGSAGG+EIBAQQEAwIEEDAWBgorBgEEAYI3AgEbBAgw
// SIG // BgEBAAEB/zANBgkqhkiG9w0BAQUFAAOCAQEAIfxkh8Fd
// SIG // IHK+qtTf+9Eq5fjkk5gMMTQCOa+2aR7mqDzLfIFRGQzr
// SIG // tcc5/izsb/wPLUwdw1cRpnxO8/qLA2Ol+b99FONTOyyc
// SIG // Y8unwviYhdEjuFmqHkyC5MUYroZEjvTObFkkgN98Y48h
// SIG // C+mG2hdlVKXR0zr9r5q/rmmivVWbojvcYKHoW31O5OcM
// SIG // PsrWuOD305Ygmpck/91iopE3UN4tfeWYhzNj1F6Ai4Xa
// SIG // 5KiaMcs3zmoZ+6SePyI2YUgtff6yvy/rk8KVT41KGD2h
// SIG // fC+QrtVSkoDWP/MDtLjbTPCWElC1NWSEKowRI8t2x594
// SIG // Skjnzu/W5Lh97ixircOuM4IvBzGCAigwggIkAgEBMIHL
// SIG // MIG2MQswCQYDVQQGEwJVUzEXMBUGA1UEChMOVmVyaVNp
// SIG // Z24sIEluYy4xHzAdBgNVBAsTFlZlcmlTaWduIFRydXN0
// SIG // IE5ldHdvcmsxOzA5BgNVBAsTMlRlcm1zIG9mIHVzZSBh
// SIG // dCBodHRwczovL3d3dy52ZXJpc2lnbi5jb20vcnBhIChj
// SIG // KTA5MTAwLgYDVQQDEydWZXJpU2lnbiBDbGFzcyAzIENv
// SIG // ZGUgU2lnbmluZyAyMDA5LTIgQ0ECECNNoEvSQoU8Ssua
// SIG // 6GHCGbswDAYIKoZIhvcNAgUFAKCBsDAZBgkqhkiG9w0B
// SIG // CQMxDAYKKwYBBAGCNwIBBDAcBgorBgEEAYI3AgELMQ4w
// SIG // DAYKKwYBBAGCNwIBFTAfBgkqhkiG9w0BCQQxEgQQeIA/
// SIG // uoe5oSYmSnksDdeTwjBUBgorBgEEAYI3AgEMMUYwRKAm
// SIG // gCQAQQB1AHQAbwBkAGUAcwBrACAAQwBvAG0AcABvAG4A
// SIG // ZQBuAHShGoAYaHR0cDovL3d3dy5hdXRvZGVzay5jb20g
// SIG // MA0GCSqGSIb3DQEBAQUABIGAHEdZgVhmeU4zvio08c7G
// SIG // d/S3CH1ovxKUdpnqMzWF8+sln/6m7b4XJX26A3jsv8vx
// SIG // 1+cuyoEsQqZtnHFd38cDJsK7FLXlghEI0p9nV+I4t7mx
// SIG // YfcKQbFiw5RI42W1bDkaUS1d7u0ERJ0KLoyCt+B6tGz7
// SIG // 6iOiuETK9QrY791S0gA=
// SIG // End signature block
