/**
 * Created by IntelliJ IDEA.
 * User: Owner
 * Date: Sep 21, 2010
 * Time: 7:47:09 AM
 * To change this template use File | Settings | File Templates.
 */

function URL(){
    this.protocol = location.protocol;
    this.host = location.host;
    this.hostname = location.hostname;
    this.port = location.port;
    this.pathname = location.pathname;
    this.search = new QS();
    this.hash = location.hash;

    URL.prototype.getFullURL = function (){
        var ret = this.protocol + "//" + this.hostname;
        if (this.port){
           ret += ":" + this.port;
        }
        ret += this.pathname;
        if (this.search){
            ret += "?" + this.search
        }
        ret += this.hash;
        return ret;
    }

    URL.prototype.toString = URL.prototype.getFullURL;
    
}

function QS(){
    this.qs = {};
    var s = location.search.replace( /^\?|#.*$/g, '' );
    if( s ) {
        var qsParts = s.split('&');
        var i, nv;
        for (i = 0; i < qsParts.length; i++) {
            nv = qsParts[i].split('=');
            this.qs[nv[0]] = nv[1];
        }
    }
}

QS.prototype.add = function( name, value ) {
    if( arguments.length == 1 && arguments[0].constructor == Object ) {
        this.addMany( arguments[0] );
        return;
    }
    this.qs[name] = value;
}

QS.prototype.addMany = function( newValues ) {
    for( nv in newValues ) {
        this.qs[nv] = newValues[nv];
    }
}

QS.prototype.remove = function( name ) {
    if( arguments.length == 1 && arguments[0].constructor == Array ) {
        this.removeMany( arguments[0] );
        return;
    }
    delete this.qs[name];
}

QS.prototype.removeMany = function( deleteNames ) {
    var i;
    for( i = 0; i < deleteNames.length; i++ ) {
        delete this.qs[deleteNames[i]];
    }
}

QS.prototype.getQueryString = function() {
    var nv, q = [];
    for( nv in this.qs ) {
        q[q.length] = nv+'='+this.qs[nv];
    }
    return q.join( '&' );
}

QS.prototype.toString = QS.prototype.getQueryString;

