jQuery.fx.interval = 33;

var $duration = "fast";
var $prevhash = "#index";

function animate_bg ($object) {
    $(".bg").animate({ left: "+" + $object.position().left + "px" }, "slow");
    $("#bg").animate({ width: $object.outerWidth() + "px" }, "slow");
}
function go (event, $object) {
    if (event && event.button != 0) return;
    if (event) event.preventDefault();

    var $m = $(".main");
    var $t = $object ? $object : $(this);
    animate_bg($t);
    var $n = $t.attr('href').match(/(\w+)\.html$/)[1];
    if (!$object || $n != "index") location = '#' + $n;
    $prevhash = '#' + $n;
    $m.animate({ opacity: 0 }, $duration, function() {
        $("*").addClass("wait");
        $m.load($t.attr('href') + " #content", "fancy", function () {
            $m.animate({ opacity: 1 }, $duration);
            $("*").removeClass("wait");
        });
    });
    this.blur();
}
function check_hash () {
    var $hash = location.hash;
    if (!$hash) $hash = '#index';
    if ($hash != $prevhash) {
        $t = $hash.match(/\w+/);
        go(null, $("nav a[href*='" + $t + "']"));
        $prevhash = $hash;
    }
}

$(function () {
    document.title = "TNX";
    animate_bg($("nav #m-index a"));
    $("nav a").click(go);
    setInterval(check_hash, 333);
});

