﻿var zIndexLevel = 0;

function SetaHeaderGrade() {
    var anchor_top = $("#ListContainer").offset().top;

    var clone_table = $("#ListaPrincipal").clone();
    var clone_footer = $("#ListaPrincipal tfoot").clone();

    if (clone_footer.length <= 0) {
        $("#ListFooter").css("display", "none");
    }

    $("#ListaPrincipal tfoot").css("visibility", "hidden");
    $("#ListaPrincipal tfoot tr").css("border-top", "none");

    $("#ListHeader").append(clone_table);
    $("#ListFooter").append(clone_footer);

    var width = $("#ListContainer").innerWidth() - 17;

    $("#ListHeader").css({
        'position': 'absolute',
        'width': width,
        'z-index': '500'
    });

    $("#ListFooter").css({
        'position': 'absolute',
        'width': width,
        'z-index': '500'
    });

    $('#ListContainer').bind("scroll", function () {
        $("#ListHeader").scrollLeft($(this).scrollLeft());
    });

    $(window).bind("resize", function () {
        width = $("#ListContainer").innerWidth() - 17;

        $("#ListFooter").css("width", width);
        $("#ListHeader").css("width", width);
        $("#ListHeader").scrollLeft($('#ListContainer').scrollLeft());
    });
}

function Delete(controllerName, id, returnFunction) {
    parent.jConfirm("Deseja excluir este registro?", "Excluir?", function (confirmed) {
        if (confirmed) {
            $.ajax({
                type: 'POST',
                url: "/" + controllerName + "/Delete?id=" + id,
                success: function () {
                    if (returnFunction != null)
                        returnFunction();
                },
                error: function (resnponseText) {
                    var ret = resnponseText.responseText.match("<title>(.+?)<\/title>");
                    var error = "Ocorreu um erro com sua requisição. Por favor, tente novamente!";

                    if (ret.length >= 0)
                        error = ret[0].replace("<title>", "").replace("</title>", "");

                    parent.jError(error, "Atenção", null);
                }
            });

        }
    });
}

function OpenEdit(title, page, width, height, returnFunction, editName) {
    parent.top.zIndexLevel += 990;
    
    if (editName == undefined)
        editName = "";
    
    if (!$.browser.msie)
        height = height + 30;

    var windowHeight = $(parent.top.document).height();
    var windowWidth = $(parent.top.document).width();
    var maxHeight = (height > windowHeight ? windowHeight - 100 : height);
    var maxWidth = (width > 750 ? 750 : width);
    var top = (windowHeight - maxHeight) / 2;
    var left = (windowWidth - maxWidth) / 2;

    $("BODY", parent.top.document).append(
        '<div id="popup_backPanelEdit_' + editName + '" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index: ' + parent.top.zIndexLevel + '; background-image: url(/Imagens/transparente.png);"></div>' +
        '<div id="divEditContent_' + editName + '" name="divEditContent_"' + editName + ' style="position: absolute; z-index: ' + (parent.top.zIndexLevel + 1) + ';">' +
        '<div style="background: url(/Imagens/window_sup_esq.png) no-repeat left top; float: left; width: 6px; height: 26px;"></div>' +
        '<div style="background: url(/Imagens/window_sup_dir.png) no-repeat right top; width: 6px; height: 26px; float: right;"></div>' +
        '<div style="background: url(/Imagens/window_sup.png) repeat top; height: 26px; margin-left: 6px; margin-right: 6px; line-height: 26px;"><div id="ContentTitle" style="float: left; margin-top: 1px;">' + title + '</div><div id="editClose_' + editName + '" name="editClose_' + editName + '" style="background: url(/Imagens/window_close.png) no-repeat top; float: right; height: 17px; width: 31px; margin-top: 6px; cursor: pointer;"></div></div>' +
        '<div style="background: url(/Imagens/window_esq.png) repeat-Y left; padding-left: 6px; background-color: #dddddd;">' +
        '<div style="background: url(/Imagens/window_dir.png) repeat-Y right;">' +
        '<iframe id="frameEdit_' + editName + '" name="frameEdit_' + editName + '" src="' + page + '" style="position: relative;" frameborder="0" scrolling="no"></iframe>' +
        '</div>' +
        '</div>' +
        '<div style="background: url(/Imagens/window_inf_esq.png) no-repeat left bottom; float: left; width: 6px; height: 6px;"></div>' +
        '<div style="background: url(/Imagens/window_inf_dir.png) no-repeat right bottom; width: 6px; height: 6px; float: right;"></div>' +
        '<div style="background: url(/Imagens/window_inf.png) repeat bottom; height: 6px; margin-left: 6px; margin-right: 6px;"></div>' +
        '</div>'
    );

    $("#divEditContent_" + editName, parent.top.document).css({
        "height": maxHeight,
        "width": maxWidth,
        "top": top,
        "left": left
    });

    $("#frameEdit_" + editName, parent.top.document).css({
        "height": maxHeight - 31,
        "width": maxWidth - 12
    });

    $("#SiteContent", parent.top.document).attr("disabled", "disabled");

    $(window).resize(function () {
        $("#divEditContent_" + editName, parent.top.document).css({
            "height": 0,
            "width": maxWidth,
            "top": top,
            "left": left
        });

        var windowHeight = $(parent.top.document).height();
        var windowWidth = $(parent.top.document).width();
        var maxHeight = (height > windowHeight ? windowHeight - 100 : height);
        var maxWidth = (width > 750 ? 750 : width);
        var top = (windowHeight - maxHeight) / 2;
        var left = (windowWidth - maxWidth) / 2;

        $("#divEditContent_" + editName, parent.top.document).css({
            "height": maxHeight,
            "width": maxWidth,
            "top": top,
            "left": left
        });

        $("#frameEdit_" + editName, parent.top.document).css({
            "height": maxHeight - 31,
            "width": maxWidth - 12
        });
    });

    $("#frameEdit_" + editName, parent.top.document)[0].returnFunction = returnFunction;

    $("#editClose_" + editName, parent.top.document).click(function () {
        $("#SiteContent", parent.top.document).removeAttr("disabled");
        $("#popup_backPanelEdit_" + editName, parent.top.document).remove();
        $("#divEditContent_" + editName, parent.top.document).remove();
        parent.top.zIndexLevel -= 990;
    });
}

function CloseEdit(execReturnFunction, editName) {
    parent.top.zIndexLevel -= 990;
    
    if (editName == undefined)
        editName = "";

    if (execReturnFunction && $("#frameEdit_" + editName, parent.top.document)[0].returnFunction != null)
        $("#frameEdit_" + editName, parent.top.document)[0].returnFunction();

    if (parent.top.zIndexLevel == 0)
        $("#SiteContent", parent.top.document).removeAttr("disabled");

    $("#popup_backPanelEdit_" + editName, parent.top.document).remove();
    $("#divEditContent_" + editName, parent.top.document).remove();
}
