QuickHit.History = {};
QuickHit.History.enabled = QuickHit.History.enabled || true;

QuickHit.History.hash = QuickHit.History.hash || "";
QuickHit.History.stack = QuickHit.History.stack || {};
QuickHit.History.PageState = function(type, data) {
    this.type = type || "page";
    this.data = data;
};

QuickHit.History.updateLocation = function(newHash, type, data) {
    QuickHit.History.hash = newHash;
    window.location.hash = newHash;
    var pageState = new QuickHit.History.PageState(type, data);
    QuickHit.History.stack[QuickHit.History.hash.replace("#!", "")] = pageState;
};

QuickHit.History.init = function() {
    QuickHit.History.hash = window.location.hash;
    setInterval(function() { if(QuickHit.History.hash != window.location.hash) { QuickHit.History.onLocationChange(); }}, 100);
};

QuickHit.History.onLocationChange = function() {
    QuickHit.History.hash = window.location.hash;
    if (QuickHit.History.stack[QuickHit.History.hash.replace("#!", "")]) {
        var pageState = QuickHit.History.stack[QuickHit.History.hash.replace("#!", "")];
        if (pageState.type == "tab") {
            var tab = $(".tab-" + pageState.data);
            $(".page-tab").removeClass("selected");
            tab.parent().addClass("selected");
            $(".tabbed-content-wrapper").children().hide();
            $(".content-" + pageState.data).show();
        }
    } else {
        var location = window.location;
        var pathname = location.pathname ? location.pathname : "";
        var path = pathname.match(/([\w-]+$)/)[0];
        if ($(".tab-" + path).length) {
            $(".page-tab").removeClass("selected");
            $(".tab-" + path).parent().addClass("selected");
            $(".tabbed-content-wrapper").children().hide();
            $(".content-" + path).show();
        }
    }
};

QuickHit.History.cleanURL = function(url) {
    url = url.replace(/^\//, "");
    url = url.replace(/\//g, "-");
    url = url.replace(/([a-z])([A-Z])/, "$1-$2").toLowerCase();
    return url;
};

$(document).ready(function() {
    QuickHit.History.init();
    $(".ajax-tab").live("click", QuickHit.History.click);
});

QuickHit.History.click = function() {
    if (QuickHit.History.enabled) {
        var link = $(this);
        var name = QuickHit.History.cleanURL(link.attr("href"));
        var arrUrl = link.attr("href").replace(/^\//, "").split("/");
        var parentName = QuickHit.History.cleanURL(arrUrl.splice(0, arrUrl.length - 1).join("/"));
        var type = link.hasClass("ajax-page") ? "page" : "tab";
        var target = $(".content-" + name, "#content-area");
        if (target.length) {
            if (!target.is(":visible")) {
                showContent(name);
            }
        } else {
            getContent(link, type, name);
        }
        return false;
    }
};

function getContent(link, type, name) {
    $("#page").block({message:null});
    $.ajax({
        url: link.attr("href"),
        dataType: "json",
        cache: false,
        success: function(data, status, xhr) {
            $("#content-area").empty();
            $("#content-area").append(data.html + data.js);
            QuickHit.Utility.refreshAds();
            QuickHit.History.updateLocation("#!" + link.attr("href"), type, name);
            $("#page").unblock();
        }
    });
};

function showContent(name) {
    $("#content-area").children().hide();
    $(".content-" + name, "#content-area").show();
    QuickHit.Utility.refreshAds();
};
