var Page={};
Page.FormatHash = function () {
    var hash = "?";
    var len = "";


    for (var q in this.params) {
        if (!isNumeric(q) && this.params[q])
            len = len + "&" + q + "=" + this.params[q];
    }

    if (len) {
        hash = hash + len.substring(1);
    } else if (this.Cid) {
        hash = hash + this.Cid;
    }
    /*
    Commands are a one time execution, they should not stay on the client string
    if (this.cmd) {
    var cmdarr = [];
    for (var i in this.cmd)
    cmdarr.push(i + "/" + this.cmd[i].join("/"));

    hash = hash + cmdarr.join(":");
    }
    */
    if (hash == "?" && !window.location.hash)
        return;
    window.location.hash = hash;
    var action = $("form").attr("action");
    if (!action || action.indexOf("javascript")!=-1)
        return;
    var indofhash = action.indexOf("#");
    if (indofhash == -1)
        action = action + "#" + hash;    
    else
        action = action.substring(0, indofhash)+"#" + hash;

    $("form").attr("action", action)
}
Page.param = function (param, value) {
    
    if(arguments.length==2){
        Page.params[param] = value;
        Page.FormatHash();
    }else if(arguments.length==1){
        return Page.params[param];
    }else{
        return Page.params;
    }
}


Page.evalHash = function (hash) {
    this.params = {};
    if (hash)
        window.location.hash = hash;



    if (window.location.hash != null && window.location.hash != "") {
        if (window.location.hash.indexOf("?") != 1)
            window.location.hash = "#?" + window.location.hash.substring(1);


        this.hash = window.location.hash.substring(2);
        var cmd = this.hash.indexOf("/");
        if (cmd != -1) {

            this.cmd = {};
            //GLOBAL COMMAND
            var cmds = this.hash.substring(cmd + 1).split(":");

            for (var i = 0; i < cmds.length; i++) {

                var thisCmd = cmds[i].split("/");

                this.cmd[thisCmd[i]] = thisCmd.splice(1, 15);

            }
            this.hash = this.hash.substring(0, cmd);
        }




        if (this.hash.indexOf("=") != -1 || this.hash.indexOf("&") != -1) {
            //PARAM SET



            var hash;
            if (cmd != -1)
                hash = this.hash.substring(0, cmd);
            else
                hash = this.hash;



            var queries = hash.split("&");
            for (var key in queries) {
                var set = queries[key].split("=");
                if (set.length > 1) {
                    this.params[set[0]] = set[1];
                    this.params[key] = set[1];
                } else
                    this.params[key] = set[0];
            }
            this.Cid = this.params[0];
        } else {
            //cid == client ID
            this.Cid = this.hash;
            //this.params = null;
        }
    }
    this.FormatHash();
}
Page.ImplyCid = function (id) {

    if (!this.Cid || this.Cid == "") {
        this.Cid = id;
        this.FormatHash();
    }
}
Page.evalHash();

