﻿var ModalBox = {
    Settings: {
        Sessao: '',
        Title: '',
        Description: '',
        YesNo: false,
        CustomModal: '',
        Callback: null,
        CallbackArguments: null,
        Width: 440,
        Height: 'auto',
        Error: false
    },

    Confirm: function (settings) {
        settings.YesNo = true;
        settings.Error = true;
        modal(settings);
    },

    Show: function (settings) {
        modal(settings);
    },

    Close: function () {
        $('#ModalBox_Container').remove();
    }
}


function modal(settings) {
    var options = $.extend({}, ModalBox.Settings, settings);
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    var modal = '';

    if (options.YesNo) {
        modal = ModalTemplate('yesno');
    }
    else {
        modal = ModalTemplate('show');
    }

    $('body').append(modal);

    $('#ModalBox .title').html(options.Title);

    if (options.Description != '')
        $('#ModalBox .description').html(options.Description);
    else
        $('#ModalBox .description').remove();    

    openModal(options.Width, options.Height);

    if (options.Error && !options.YesNo) {
        $('.ModalBox_window').addClass("error")
        $('#ModalBox .sessao').html('Erro');
    }
    else if (!options.YesNo) {
        $('.ModalBox_window').addClass("success")
        $('#ModalBox .sessao').html('Sucesso');
    }
    else {
        $('#ModalBox .sessao').html('confirme');
        $('.ModalBox_window').addClass("confirm")
    }
    Cufon.replace('.gillSans20Light', { fontWeight: 'light', fontSize: '16px', color: '#666', letterSpacing: '1px' });

    RegisterAlfterScript(options.Callback);
    $('html').animate({ scrollTop: 0 }, "slow");
    if (options.YesNo) {
        ExecuteCallBack(options.Callback, options.CallbackArguments);
    }

};

var openModal = function (w, h) {
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    var id = $('#ModalBox');

    $('#ModalBox_Mask').css({ 'width': maskWidth, 'height': maskHeight });
    $('#ModalBox_Mask').show();

    var winH = $(window).height();
    var winW = $(window).width();


    $(id).css({
        'width': w,
        'height': h
    });

    $(id).css('top', winH / 2 - $(id).height() / 2);
    $(id).css('left', winW / 2 - $(id).width() / 2);

    $(id).show();
}

var ExecuteCallBack = function (call, arg) {

    $('a[name=yes]').click(function (e) {
        e.preventDefault();
        if (call)
            if (arg) {
                call(arg);
                ModalBox.Close();
            }
            else {
                call();
                ModalBox.Close();
            }
        else
            alert('callback indefinido');
    });

    $('a[name=no]').click(function (e) {
        e.preventDefault();
        ModalBox.Close();
        
    });
}

var RegisterAlfterScript = function (args) {

    $('.ModalBox_window .ModalBox_close').click(function (e) {
        e.preventDefault();
        $('#ModalBox_Container').remove();
        if (args) {
            args();
        }
    });

    $('#ModalBox_Mask').click(function () {
        $('#ModalBox_Container').remove();
        if (args) {
            args();
        }
    });

    $('a[name=no]').click(function (e) {
        e.preventDefault();
        ModalBox.Close();
        if (args) {
            args();
        }
    });
}

var ModalTemplate = function (type) {
    if (type == 'yesno') {
        return '<div id="ModalBox_Container" class=\"mod_box  ModalBox_Container"\><div id=\"ModalBox\" class=\"ModalBox_window\" >' +
                '<div class=\"clearfix\">' +
                '<h3 class=\"gillSans20Light sessao left\"></h3>' +
                '<a href="#"  name=\"no\" class=\"ModalBox_close right bt_fechar_modal rpl\">fechar</a>' +
                '</div>' +
                '<div class=\"arrow_cinza_520\">&nbsp;</div>' +
                '<h4>' +
                '<span class=\"ico_modal ico_esclamacao rpl\">&nbsp;</span>' +
                '<span class=\"title\"></span>' +
                '</h4>' +
                '<p class=\"description\"> </p>' +
                 '<fieldset><a  href=\"#\" name=\"no\" class=\"bt_modal_nao rpl\" >no</a>' +
                '<a  href=\"#\" name=\"yes\" class=\"bt_modal_sim rpl\" >yes</a></fieldset>' +
                    '</div><div id="ModalBox_Mask"></div></div>';
    }
    if (type == 'show') {
        return '<div id="ModalBox_Container" class=\"mod_box  ModalBox_Container"\>' +
                    '<div id=\"ModalBox\" class=\"ModalBox_window\" >' +
                        '<div class=\"clearfix\">' +
                            '<h3 class=\"gillSans20Light sessao left\"></h3>' +
                            '<a href="#"  name=\"no\" class=\"ModalBox_close right bt_fechar_modal rpl\">fechar</a>' +
                        '</div>' +
                        '<div class=\"arrow_cinza_520\">&nbsp;</div>' +
                        '<h4>' +
                            '<span class=\"ico_modal ico_esclamacao rpl\">&nbsp;</span>' +
                            '<span class=\"title\"></span>' +
                        '</h4>' +
                        '<p class=\"description\"> </p>' +
                        '<fieldset>' +
                            '<a  href=\"#\" name=\"no\" class=\"ModalBox_close bt_fechar rpl\" >yes</a>' +
                        '</fieldset>' +
                    '</div>' +
                    '<div id="ModalBox_Mask"></div>' +
                '</div>';
    }
    return null;
}

