﻿var originalWidth = [];
var cancelExpand = [];

jQuery(document).ready(function () {
    // Destroys page in Firefox when leaving so that -Back- works properly.
    window.onunload = function () { };

    jQuery("input[type=button]").button();
    jQuery("input[type=submit]").button();

    jQuery("div#page-content div.right a img").hover(
        function () { expandImage(jQuery(this)); },
        function () { restoreImage(jQuery(this)); });


    jQuery("a").click(function (e) {
        if (e.which && e.which == 1) {
            if (!jQuery(this).hasClass("no-redirect") && jQuery(this).attr("href") != "#" && jQuery(this).attr("target") != "_blank") {
                fadeOutPage();
                showWorking();
            }
        }
    });

    fadeInPage();
    setSidebarHeight();
});

jQuery(window).load(function () {
    jQuery("div#page-content div.right a img").each(function (idx, elem) {
        originalWidth[elem.id] = jQuery(elem).width();
    });
});

function setSidebarHeight() {
    var centerHeight = jQuery("div#page-content div.center").height();
    jQuery("div#page-content div.left").height(centerHeight);
    jQuery("div#page-content div.right").height(centerHeight);
}


function startSidebarTimer() {
    setTimeout(startSidebarTimer, 500);
    setSidebarHeight();
}

function expandImage(image) {
    var currentWidth = image.width();
    if (currentWidth - 10 >= originalWidth[image.attr("id")]) {
        return;
    }
    if (cancelExpand[image.attr("id")]) {
        return;
    }
    image.width(image.width() + 1);
    setTimeout(function () { expandImage(image); }, 10);
}

function restoreImage(image) {
    cancelExpand[image.attr("id")] = true;
    var currentWidth = image.width();
    if (currentWidth <= originalWidth[image.attr("id")]) {
        cancelExpand[image.attr("id")] = false;
        return;
    }
    image.width(image.width() - 1);
    setTimeout(function () { restoreImage(image); }, 10);
}

function fadeOutPage() {
    jQuery("div#page-wrapper").fadeOut(300);
}

function fadeInPage() {
    jQuery("div#page-wrapper").fadeIn(600);
}

function showWorking() {
    jQuery("#progressBackgroundFilter").show();
    jQuery("#processMessage").show();
}

function hideWorking() {
    jQuery("#progressBackgroundFilter").hide();
    jQuery("#processMessage").hide();
}

function modal(message, title) {
    jQuery("div#dialog-modal p").html(message);
    jQuery("div#dialog-modal").attr("title", title);
    jQuery("div#dialog-modal").dialog({ modal: true, title: title, show: "fadeIn", hide: "fadeOut" });
}

function setTableSorter(jQueryTable) {
    jQueryTable.tablesorter();
    jQueryTable.bind("sortEnd", function () { afterSorted(jQueryTable); });
}

function afterSorted(jQueryTable) {
    jQueryTable.find("tbody tr").removeClass("row-odd");
    jQueryTable.find("tbody tr").removeClass("row-even");
    jQueryTable.find("tbody tr:even").addClass("row-odd");
    jQueryTable.find("tbody tr:odd").addClass("row-even");
}
