﻿//MyBox v1.3.4 , Joaquim Adráz

var MyBox = new (function () {

    function MyBox(Options_) {
        var that = this;
        var root = $('#MyBoxWrapper');

        //default options
        var Default = {
            Text: "Insert your text!",
            Action: "Ok", //Ok, YesNo, OkCancel
            State: "Info", //Error, Success, Info, Prompt
            Image: "",
            Size: "Small", //Small, Medium
            onClick: null,
            onLoad: null
        };

        var Options = $.extend(Default, Options_);

        this.CreateBox = function () {
            that.CloseBox(null, false, that.ShowBox);
        }

        this.HTMLFactory = function (Text, Action, State, Image, Size) {
            var HTML = "";

            if (Image.length > 0) {
                HTML += '<div class="Box ' + Action + ' ' + State + ' ' + Size + ' Image grey-white" style="display: none;">';
                HTML += '<div class="Image"><img width="190" img src="' + Image + '" alt="" />\n';
                HTML += '<div class="_block"></div></div>';
            } else {
                HTML += '<div class="Box ' + Action + ' ' + State + ' ' + Size + ' grey-white" style="display: none;">';
            }
            HTML += '<div class="TextWrapper">';
            HTML += '<div class="Text">' + Text + '</div>\n';
            if (State == 'Prompt') {
                HTML += '<textarea></textarea>\n';
            }
            HTML += '<div class="_block"></div>';
            HTML += '</div">';
            HTML += '<div class="Actions">';
            if (Action == "Ok")
                HTML += '<input type="button" class="MyButton Ok Green" value="Ok" />';
            if (Action == "YesNo") {
                HTML += '<div class="MyButton Ok Green">Sim</div>';
                HTML += '<div class="MyButton Cancel Red">Não</div>';
            }
            if (Action == "OkCancel") {
                HTML += '<div class="MyButton Ok Green">Ok</div>';
                HTML += '<div class="MyButton Cancel Red">Cancelar</div>';
            }
            HTML += '</div>';
            HTML += '</div>';

            return HTML;
        }

        this.ShowBox = function () {
            var HTMLFactored = that.HTMLFactory(Options.Text, Options.Action, Options.State, Options.Image, Options.Size);

            $(root).append(HTMLFactored);

            if (navigator.appVersion.indexOf("Chrome/") != -1) {
                $('#MyBoxWrapper .Box').css('line-height', '64px');
            }

            $(root).fadeIn(300);
            $(root).find('.Box').show();

            var BodyTop = document.body.scrollTop;
            var ElementTOp = document.documentElement ? document.documentElement.scrollTop : null;

            $(root).find('.Box').css('margin-top', (BodyTop || ElementTOp) + 100)

            that.Bind();
        }

        this.CloseBox = function (Button, Clicked, Callback) {
            if (Clicked) {
                if ($(Button).hasClass('Ok')) {
                    if (typeof Options.onClick == 'function') {
                        var Text = undefined;
                        if (Options.State == 'Prompt') {
                            Text = $(root).find('textarea').val();
                        }
                        Options.onClick("Ok", Text); //1 = clicked, null = not clicked
                    }
                } else {
                    if (typeof Options.onClick == 'function') Options.onClick('Cancel'); //1 = clicked, null = not clicked
                }

                $('body input').attr('disabled', false);
            }

            $('#MyBoxWrapper').fadeOut(300);

            //close before show simultaneous
            if ($(root).find('.Box').length > 0) {
                $(root).find('.Box').fadeOut(function () {
                    $(this).remove();
                    if (typeof Callback == "function") Callback();
                });
            } else
                Callback();

        }

        this.Bind = function () {
            $(root).find('.MyButton').attr('disabled', false);

            $(root).find('.MyButton.Ok').focus();

            $(root).find('.MyButton').click(function () {
                that.CloseBox(this, true, null);
            });

            if (typeof Options.onLoad == "function") Options.onLoad(Options.Text);

        }

        this.CreateBox();
    }

    //multiple instances
    this.Create = function (Options_) {
        new MyBox(Options_);
    };
})();
