/* Minification failed. Returning unminified contents.
(1,18): run-time error CSS1031: Expected selector, found '='
(1,18): run-time error CSS1025: Expected comma or open brace, found '='
(2,18): run-time error CSS1031: Expected selector, found '='
(2,18): run-time error CSS1025: Expected comma or open brace, found '='
(5,1): run-time error CSS1019: Unexpected token, found '$'
(5,2): run-time error CSS1019: Unexpected token, found '('
(5,12): run-time error CSS1031: Expected selector, found '('
(5,12): run-time error CSS1025: Expected comma or open brace, found '('
(279,2): run-time error CSS1019: Unexpected token, found ')'
(281,10): run-time error CSS1031: Expected selector, found 'onAjaxFailure('
(281,10): run-time error CSS1025: Expected comma or open brace, found 'onAjaxFailure('
(299,10): run-time error CSS1031: Expected selector, found 'showMessageBox('
(299,10): run-time error CSS1025: Expected comma or open brace, found 'showMessageBox('
(346,10): run-time error CSS1031: Expected selector, found 'updateSection('
(346,10): run-time error CSS1025: Expected comma or open brace, found 'updateSection('
(371,10): run-time error CSS1031: Expected selector, found 'updateSection2('
(371,10): run-time error CSS1025: Expected comma or open brace, found 'updateSection2('
(396,10): run-time error CSS1031: Expected selector, found 'getParentDialogRef('
(396,10): run-time error CSS1025: Expected comma or open brace, found 'getParentDialogRef('
(404,10): run-time error CSS1031: Expected selector, found 'closeDialog('
(404,10): run-time error CSS1025: Expected comma or open brace, found 'closeDialog('
(412,10): run-time error CSS1031: Expected selector, found 'updateContainer('
(412,10): run-time error CSS1025: Expected comma or open brace, found 'updateContainer('
(423,10): run-time error CSS1031: Expected selector, found 'updateSearchContainer('
(423,10): run-time error CSS1025: Expected comma or open brace, found 'updateSearchContainer('
(431,10): run-time error CSS1031: Expected selector, found 'updateSearchContainer2('
(431,10): run-time error CSS1025: Expected comma or open brace, found 'updateSearchContainer2('
(438,10): run-time error CSS1031: Expected selector, found 'makeAjaxCall('
(438,10): run-time error CSS1025: Expected comma or open brace, found 'makeAjaxCall('
(462,10): run-time error CSS1031: Expected selector, found 'showMessageBox2('
(462,10): run-time error CSS1025: Expected comma or open brace, found 'showMessageBox2('
 */
var arrDialogRef = [];
var curDialogRef = null;

// on load of application
$(function () {

    curDialogRef = $("#ListaGeral");

    $(document).on("click", "a.dialog", function (event) {

        event.preventDefault();

        var currentLinkRef = this;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var dialog = $("<div></div>");

        $(dialog)
            .load(url)
            .dialog({
                position: ['center', 10],
                title: title,
                autoOpen: false,
                resizable: false,
                height: 'auto',
                width: '60%',
                minWidth: '400',
                minHeight: '200',
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: true,
                open: function (event, ui) {

                    var me = this;

                    // storing reference
                    arrDialogRef.push({
                        Source: curDialogRef,// parent dialog reference
                        Destination: me, // child or open dialog refernce
                        Element: currentLinkRef, // current link reference
                    });

                    curDialogRef = me;
                },
                close: function (event, ui) {

                    // releasing resource of dialog on close event
                    var me = this;
                    $.each(arrDialogRef, function (i, val) {

                        if (val.Destination == me) {
                            curDialogRef = val.Source;
                            arrDialogRef.splice(i, 1);
                            return false;
                        }
                    });

                    $(this).empty().dialog('destroy').remove();
                }
            });

        $(dialog).dialog('open');
    });

    $(document).on("click", "a.dialog2", function (event) {

        event.preventDefault();

        var currentLinkRef = this;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var dialog = $("<div>");

        $(dialog)
            .load(url)
            .dialog({
                position: ['center', 10],
                title: title,
                autoOpen: false,
                resizable: false,
                height: 'auto',
                width: '99%',
                minWidth: '600',
                minHeight: '400',
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: true,
                open: function (event, ui) {

                    var me = this;

                    // storing reference
                    arrDialogRef.push({
                        Source: curDialogRef,// parent dialog reference
                        Destination: me, // child or open dialog refernce
                        Element: currentLinkRef, // current link reference
                    });

                    curDialogRef = me;
                },
                close: function (event, ui) {

                    // releasing resource of dialog on close event
                    var me = this;
                    $.each(arrDialogRef, function (i, val) {

                        if (val.Destination == me) {
                            curDialogRef = val.Source;
                            arrDialogRef.splice(i, 1);
                            return false;
                        }
                    });

                    $(this).empty().dialog('destroy').remove();
                }
            });

        $(dialog).dialog('open');
    });

    $(document).on("click", "a.mostraImagem", function (event) {

        event.preventDefault();

        var currentLinkRef = this;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var dialog = $("<div></div>");

        $(dialog)
            .load(url)
            .dialog({
                position: ['center', 5],
                title: title,
                height: 'auto',
                width: '90%',
                minWidth: '600',
                minHeight: '400',

                //width: "auto",
                //create: function( event, ui ) {
                //    // Set maxWidth
                //    $(this).css("maxWidth", "660px");
                //},

                show: { effect: 'fade', direction: "up" },
                modal: true,
                draggable: true,
                open: function (event, ui) {

                    var me = this;

                    // storing reference
                    arrDialogRef.push({
                        Source: curDialogRef,// parent dialog reference
                        Destination: me, // child or open dialog refernce
                        Element: currentLinkRef, // current link reference
                    });

                    curDialogRef = me;
                },
                close: function (event, ui) {

                    // releasing resource of dialog on close event
                    var me = this;
                    $.each(arrDialogRef, function (i, val) {

                        if (val.Destination == me) {
                            curDialogRef = val.Source;
                            arrDialogRef.splice(i, 1);
                            return false;
                        }
                    });

                    $(this).empty().dialog('destroy').remove();
                }
            });

        $(dialog).dialog('open');
    });

    $(document).on("click", "a.mostraVideo", function (event) {

        event.preventDefault();

        var currentLinkRef = this;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var dialog = $("<div></div>");

        $(dialog)
            .load(url)
            .dialog({
                position: ['center', 20],
                title: title,
                height: '580',
                width: '924',
                show: { effect: 'fade', direction: "up" },
                modal: true,
                draggable: true,

                open: function (event, ui) {

                    var me = this;

                    // storing reference
                    arrDialogRef.push({
                        Source: curDialogRef,// parent dialog reference
                        Destination: me, // child or open dialog refernce
                        Element: currentLinkRef, // current link reference
                    });

                    curDialogRef = me;
                },
                close: function (event, ui) {

                    // releasing resource of dialog on close event
                    var me = this;
                    $.each(arrDialogRef, function (i, val) {

                        if (val.Destination == me) {
                            curDialogRef = val.Source;
                            arrDialogRef.splice(i, 1);
                            return false;
                        }
                    });

                    $(this).empty().dialog('destroy').remove();
                }
            });

        $(dialog).dialog('open');
    });

    $(document).on("click", "a.delete", function (event) {

        event.preventDefault();
        var url = $(this).attr("href");

        showMessageBox({
            title: "Pergunta",
            content: "Tem certeza que deseja excluir este registro?",
            btn1text: "Sim",
            btn2text: "Não",
            functionText: "makeAjaxCall",
            parameterList: {
                url: url,
                callback: 'updateSection'
            }
        });
    });

    $(document).on("click", "a.confirma", function (event) {

        event.preventDefault();
        var url = $(this).attr("href");

        showMessageBox({
            title: "Pergunta",
            content: "Confirma o processamento?",
            btn1text: "Sim",
            btn2text: "Não",
            functionText: "makeAjaxCall",
            parameterList: {
                url: url,
                callback: 'updateSection'
            }
        });
    });

    $(document).on("click", "a.marcar", function (event) {
        event.preventDefault();
        var url = $(this).attr("href");
        var par = { url: url, callback: 'updateSection' }
        makeAjaxCall(par);
    });

    updateSection(); // update the section of product list on load
});

function onAjaxFailure(response) {
    console.log(response);

    var data = $.parseJSON(response.responseJSON);
    var content = "<ul>";
    for (var key in data) {
        content += "<ol>" + data[key] + "</ol>";
    }

    content += "</ul>"

    showMessageBox2({
        title: "Erro",
        content: content,
        btn1text: "Ok"
    });
}

function showMessageBox(params) {

    var btn1css;
    var btn2css;

    if (params.btn1text) {
        btn1css = "showcss";
    } else {
        btn1css = "hidecss";
    }

    if (params.btn2text) {
        btn2css = "showcss";
    } else {
        btn2css = "hidecss";
    }

    $("#lblMessage").html(params.content);

    $("#dialog").dialog({
        resizable: false,
        title: params.title,
        modal: true,
        width: 'auto',
        height: 'auto',
        bgiframe: false,
        hide: { effect: 'scale', duration: 400 },
        buttons: [
            {
                text: params.btn1text,
                "class": btn1css,
                click: function () {
                    if (params.functionText) { window[params.functionText](params.parameterList); } // Call function after clicking on button.
                    $("#dialog").dialog('close');
                }
            },
            {
                text: params.btn2text,
                "class": btn2css,
                click: function () {
                    $("#dialog").dialog('close');
                }
            }
        ]
    });
}

function updateSection() {
    
    var source = null;
    var updateContainerId = "";
    var ref = getParentDialogRef();
    if (ref && ref.Destination) {
        source = ref.Destination;
        updateContainerId = source;
    }
    else if (curDialogRef) {
        source = curDialogRef;
        updateContainerId = $("#gride_geral");
    }

    var url = $(source).find("form").attr("action");

    if (url) {

        $.get(url, function (data) {
            
            $(updateContainerId).html(data);
        });
    }
}

function updateSection2() {

    var source = null;
    var updateContainerId = "";
    var ref = getParentDialogRef();
    if (ref && ref.Destination) {
        source = ref.Destination;
        updateContainerId = source;
    }
    else if (curDialogRef) {
        source = curDialogRef;
        updateContainerId = $("#gride_geral2");
    }

    var url = $(source).find("form").attr("action");

    if (url) {

        $.get(url, function (data) {

            $(updateContainerId).html(data);
        });
    }
}

function getParentDialogRef() {
    if (arrDialogRef && arrDialogRef.length > 0) {
        return arrDialogRef[arrDialogRef.length - 1];
    }

    return null;
}

function closeDialog() {

    var ref = getParentDialogRef();
    if (ref) {
        $(ref.Destination).dialog("close");
    }
}

function updateContainer(response) {
    var ref = getParentDialogRef();

    if (ref) {

        $(ref.Element).parent().find("input[type='hidden']").val(response.Id);
        $(ref.Element).parent().find("input[type='text']").val(response.Name);
        closeDialog();
    }
}

function updateSearchContainer(response) {
    var ref = getParentDialogRef();
    if (ref) {
        $(ref.Destination).html(response);
    }
}


function updateSearchContainer2(response) {
    var ref = getParentDialogRef();
    if (ref) {
        $(ref.Destination).html(response);
    }
}

function makeAjaxCall(params) {

    $.ajax({
        type: 'POST',
        url: params.url,
        data: params.parameters,
        dataType: 'json',
        success: function (response) {
            if (response.success) {
                eval(params.callback + '(response)');
            } else {
                //alert(response.responseText);
                showMessageBox2({
                    title: "Erro",
                    content: response.responseText,
                    btn1text: "Ok"
                })

            }
        }
    })
}


function showMessageBox2(params) {
    var btn1css;
    if (params.btn1text) {
        btn1css = "showcss";
    } else {
        btn1css = "hidecss";
    }
    $("#lblMessage").html(params.content);
    $("#dialog").dialog({
        resizable: false,
        title: params.title,
        modal: false,
        width: 'auto',
        height: 'auto',
        bgiframe: false,
        hide: { effect: 'scale', duration: 100 },
        buttons: [
            {
                text: params.btn1text,
                "class": btn1css,
                click: function () {
                    if (params.functionText) { window[params.functionText](params.parameterList); } // Call function after clicking on button.
                    $("#dialog").dialog('close');
                }
            }
        ]
    });
}




