﻿//Make all links in the AJAX panel use hash navigation
$("#AjaxResult a").live("click", function() {
    document.location.href = this.href.replace("?", "#");
    return false;
});

var currentAnchor = "";

$().ready(function() {
    //Set timer to monitor anchors
    setInterval("checkAnchor()", 500);
});

function checkAnchor() {
    //Check if anchor has changed
    if (currentAnchor != document.location.hash) {
        preAjaxLoad("AjaxResult")
        currentAnchor = document.location.hash;
        var queryData = "&" + currentAnchor.substring(1)
        $.get(actionUrl + queryData, onSuccess);
    }
}

function onSuccess(response) {
    postAjaxLoad("AjaxResult", response)
}

function preAjaxLoad(elemId) {
    var h = $("#" + elemId).height();
    $("#" + elemId).fadeTo("fast", 0, function() {
        $("#" + elemId).height(h); //retain height
        $("#" + elemId).fadeTo("fast", 1); //fadein again
        $("#" + elemId).html("<img src='" + loadingImage + "'/>"); //Show message
    });  //fadeout
}

function postAjaxLoad(elemId, result) {
    $("#" + elemId).fadeTo("fast", 0, function() {
        $("#" + elemId).html(result); //show the new results
        $("#" + elemId).removeAttr("style"); //remove the fixed height so it resizes to the new content
        $("#" + elemId).fadeTo("fast", 1); //fade in
    });
}