/* Anchor Manager. as a generic handler */

var anchor = {
    urlPrefix: "",
    parameter: {},
    isHashFree: false,
    isDefault: false,
    defaultTabs: null,
    read: function () {
        anchor.isHashFree = (document.location.toString().indexOf("#") == -1);
        anchor.parameter = {};
        try {
            anchor.urlPrefix = document.location.toString().substr(0, document.location.toString().indexOf(document.location.host) + document.location.host.length);
            anchor.parameter = {};
            var loc = document.location.toString().split("#");
            var pairs = loc[1].split("&");
            for (var i = 0; i < pairs.length; i++) {
                var data = pairs[i].split("=");
                this.parameter[data[0]] = data[1];
            }
        } catch (e) {
        }
    },
    rebuild: function () {
        var temp = [];
        // Use arguments to rebuild the anchor
        if (typeof (arguments[0]) != "undefined" && typeof (arguments[1]) != "undefined") {
            this.parameter[arguments[0]] = arguments[1];
        }
        for (var key in this.parameter) {
            if (typeof (this.parameter[key]) != "undefined") {
                temp.push(key + "=" + this.parameter[key].replace(anchor.urlPrefix, ""));
            }
        }
        console.warn("Anchor.Rebuild: %o", temp);
        var loc = document.location.toString().split("#");
        document.location = loc[0] + "#" + temp.join("&");

        $(".top_quicklink .changeLanguage a").each(function () {
            var href = $(this).attr("href").split("returnUrl")[0];
            var loc = document.location.toString().split("//")[1];
            $(this).attr("href", href + "returnUrl=" + loc.substr(loc.indexOf("/"), 10000));
        })
    }
}
