var MESSAGE_TIMEOUT = 5000;

function genericInits() {

    $("#content .listTable").parent("#content").css("padding", "0 0 0 0");
    $(".listTable").attr("cellSpacing", "0");

    if (jQuery.browser.msie)
    {
        $(".listTable").parent("#rightFrame").css("overflow-x", "hidden");
        $("#leftFrame").css("height", parseInt(document.documentElement.clientHeight) - 125);
        $(".searchForm input[type='text']").css("fontSize", "11px");
        $("#rightFrame").css("overflowX", "hidden");
    }

    extendLinkToTR(".listTable", ".header");

    checkTabsOverflow();

    $.each($(".listTable"), function() {
        if ($("tr td a", this).length == 0)
        {
            $("tr,td", this).css("cursor", "auto");
        }
        $("tr td", this).bind("mouseover", function() {
            $(this).css("whiteSpace", "normal");
        });
        $("tr td", this).bind("mouseout", function() {
            $(this).css("whiteSpace", "noWrap");
        });
    });

    $("a.deleteRed").click(function() {
        return confirm('Confirma?');
    });
}

function handleEscape() {

    if ($("#closeButton").parents("a").length)
    {
        $(document).bind("keydown", function(e)
        {
            if (e.keyCode == 27)
                window.setTimeout('window.location.href = document.getElementById("closeButton").parentNode.href', 50);
            return true;
        }
                );
    }

}


function extendLinkToTR(element) {
    $("tr", $(element)).bind("click", function() {
        if (this.getElementsByTagName("A") && this.getElementsByTagName("A").length)
        {
            document.location.href = this.getElementsByTagName("A")[0].href;
            return false;
        }
        return true;
    });
}

/*
 the failures are given by the MainPage.getJSONError
 it can be an error string or a javascript array of objects with property an description attributes defined
 */
function showFailures(failures) {
    if (failures == null)
        return;

    if (typeof failures != "object" || !(typeof failures[0].description != "undefined"))
    {
        showMessage(failures, "error");
        return;
    }

    var hasf = false;
    var other = "";
    for (var i = 0; failures != undefined && i < failures.length; i++) {
        hasf = true;
        var failure = failures[i];
        var el = $("#" + failure.property);
        if (el != undefined && el.length) {
            showFailureOnInput(el, failure.description);
        } else
            other += failure.description.replace("is required", "é obrigatório") + "<br/>";
    }
    if (other.length > 0)
        showMessage(other, "error");
    else
        if (hasf)
            showMessage("Erros", "error");
}

function showFailureOnInput(el, description) {
    el.addClass("failure");

    var lbl = $("label[for=" + el[0].id + "]");
    if (lbl && lbl.length == 1)
        description = description.replace("\""+el[0].id+"\"", "");

    description = description.replace("is required", "é obrigatório");

    el.after("<span class=\"failureDesc\">" + description + "</span>")

}

/*
 show a message on the top.
 type: warning, info, error
 */
function showMessage(mess, mtype) {
    if (mess != "null" && mess != undefined && mess.length > 0) {
        $("#message").html("<div class=\"" + mtype + "\">" + mess + "</div>");
        $("#message").show();

        window.setTimeout(function() {
            $("#message").slideUp()
        }, MESSAGE_TIMEOUT);
    }
}

/*
 scrolls the tabs bar for overflow situations
 */
function checkTabsOverflow() {
    var tc = $("#tabsContainer");
    if (tc.length == 0) return;

    var w = 0;
    var sel = null;
    $.each($("#tabsBar *"), function() {
        w += $(this).width()
        if (this.className == "tablink-selected")
            sel = $(this);
    })
    $("#tabsBar").width(w); //expand-needed because of the flow...
    $("#tabsContainer").css("overflow-x", "hidden");


    if (tc.width() < w && sel.length > 0) {
        tc[0].scrollLeft = sel[0].offsetLeft - tc[0].offsetWidth / 2;

    }
}
