﻿///<reference path="../TESCO.js" />
/*
	Example:
	
	<a href="/default.aspx?myQuery=hi#test1" id="aLink">a link</a>
	
	var myURI = TESCO.system.URI(document.getElementById("aLink").href);
	console.log(myURI.hash) // 'test1'
	console.log(myURI.protocol) // null
*/

TESCO.$("system").URI = (function() {

    var _uriPattern = /^(?:([^:\/?\#]+):)?(?:\/\/([^\/?\#]*))?([^?\#]*)(?:\?([^\#]*))?(?:\#(.*))?/;

    function _constructor(uri) {
		///	<summary>creates a new uri object with properties for the protocol, domain, path, search and hash</summary>
		///	<param name="uri">string</param>
		///	<returns>instance</returns>
        var _result = uri.match(_uriPattern);
        this.protocol = _result[1] || null;
        this.domain = _result[2] || null;
        this.path = _result[3] || null;
        this.search = _result[4] || null;
        this.hash = _result[5] || null;
        return this;
    }

    _constructor.prototype.NAME = "TESCO.system.URI"

    _constructor.prototype.toString = function() {
		///	<summary>concatenate each object in instance</summary>
		///	<returns>string</returns>
        var _url = this.protocol ? (this.protocol + "://") : "";
        _url += this.domain ? this.domain : "";
        _url += this.path ? this.path : "";
        _url += this.search ? ("?" + this.search) : "";
        _url += this.hash ? ("#" + this.hash) : "";
        return _url;
    }

    return _constructor;
})();