/// <reference name="MicrosoftAjax.js" />
/// <reference path="jquery-1.3.2-vsdoc2.js" />

//EICv3 - Focusnetworks Brasil
//Version: 0.9.0.1

var EICv3 = function() {

    var self = this;
    var zIndex = 10;
    this.showSuccess = function(open, message, closeAction) {

        var container = $('#showSuccess');
        var content, closeButton;

        self.showCurtain(open);

        if (open) {

            content = $('#showSuccessMessage');
            closeButton = $('#showSuccessLink');

            container.show().center();
            content.html(message);

            if (closeAction != undefined) {
                if (typeof (closeAction) == 'string') {
                    closeButton[0].href = closeAction;
                    closeButton[0].onclick = function() { };
                }
                else {
                    closeButton[0].href = '#';
                    closeButton[0].onclick = closeAction;
                }
            }
            else {
                closeButton[0].href = '#';
                closeButton[0].onclick = function() { return self.showSuccess(false); };
            }
        }
        else {
            container.hide();
        }

        return false;
    };
    this.showInformation = function(open, message, closeAction) {

        var container = $('#showInformation');
        var content, closeButton;

        self.showCurtain(open);

        if (open) {

            content = $('#showInformationMessage');
            closeButton = $('#showInformationLink');

            container.show().center();
            content.html(message);

            if (closeAction != undefined) {
                if (typeof (closeAction) == 'string') {
                    closeButton[0].href = closeAction;
                    closeButton[0].onclick = function() { };
                }
                else {
                    closeButton[0].href = '#';
                    closeButton[0].onclick = closeAction;
                }
            }
            else {
                closeButton[0].href = '#';
                closeButton[0].onclick = function() { return self.showSuccess(false); };
            }
        }
        else {
            container.hide();
        }

        return false;
    };
    this.showWarning = function(open, message, closeAction) {

        var container = $('#showWarning');
        var content, closeButton;

        self.showCurtain(open);

        if (open) {

            content = $('#showWarningMessage');
            closeButton = $('#showWarningLink');

            container.show().center();
            content.html(message);

            if (closeAction != undefined) {
                if (typeof (closeAction) == 'string') {
                    closeButton[0].href = closeAction;
                    closeButton[0].onclick = function() { };
                }
                else {
                    closeButton[0].href = '#';
                    closeButton[0].onclick = closeAction;
                }
            }
            else {
                closeButton[0].href = '#';
                closeButton[0].onclick = function() { return self.showSuccess(false); };
            }
        }
        else {
            container.hide();
        }

        return false;
    };
    this.alertDebug = function(message) {
        if (!$('#debugContainer')[0]) {
            $('body').prepend('<div id="debugContainer" style="position:fixed;top:170px;left:5px;z-index:1000;border:1px solid #00f;background:#fff;min-width:200px;min-height:110px;padding:5px;color:#000;"><span>Debug Window</span><a href="javascript:void[0];" onclick="$(this).parent().hide();eic.alertDebugClear();" style="position:absolute;top:2px;right:2px;color:#f00;font-weight:bold;">X</a><div id="debugContent" style="font-size:12px !important;"></div></div>');
        }

        $('#debugContainer').show();
        $('#debugContent').html($('#debugContent').html() + '<br />\n' + message);

        return false;
    };
    this.alertDebugClear = function() {
        if ($('#debugContent')[0]) {
            $('#debugContent').html('');
        }
        return false;
    };

    this.showCurtain = function(open) {

        if (!$('#curtain')[0]) {
            $('body').append('<iframe id="curtain" src="" frameborder="0" scrolling="no" style="display:none;"></iframe>');
        }

        if (open) {
            $('#curtain').css({ position: 'absolute', display: 'block', height: $(document).height(), border: 'none', left: '0px', top: '0px', width: '100%', backgroundColor: '#000', zIndex: zIndex, opacity: 0 }).fadeTo(200, 0.5);
        }
        else {
            $('#curtain').fadeTo(200, 0).hide();
        }

        return false;
    };

    this.OnlyNumbers = function(oEvent) {
        var oRegExp = /[0-9]/;
        var tecla_codigo;
        var tecla_char;

        if (oEvent.keyCode)
            tecla_codigo = oEvent.keyCode;
        else if (oEvent.which)
            tecla_codigo = oEvent.which;

        tecla_char = String.fromCharCode(tecla_codigo);

        //Permite algumas teclas
        if (tecla_codigo == 8 || tecla_codigo == 9 || (tecla_codigo == 35 && oEvent.shiftKey) || (tecla_codigo == 36 && oEvent.shiftKey) || (tecla_codigo == 37 && (oEvent.shiftKey || tecla_char != '%')) || (tecla_codigo == 39 && tecla_char != "'") || (tecla_codigo == 46 && tecla_char != '.'))
            return true;
        //Se for número
        else if (!oRegExp.test(tecla_char))
            return false;
        //Se não for número
        else
            return true;
    }

    // Não é recomendado o uso para toda a página, o modo menos obstrutivo é aplicar aos elementos necessários.
    // A função abaixo faz com que todos os elementos que possuem rel="nocopy", não abram o seu context menu. Ex.: Botão direito do mouse, Salvar imagem como..
    this.blockContextMenu = function() {
        $('[rel*=nocontext]').bind('contextmenu', function(e) {
            return false;
        });
    };




    // A W3C não aceita mais o atributo "target" em links (tag <a>) em doctypes XHTML 1.0 Strict
    // A função abaixo faz com que todos os links que possuem rel="externo" no link, abram em outra janela conforme no exemplo abaixo:
    this.createExternalLinks = function() {
        $('a[rel*=externo]').click(function() {
            window.open(this.href);
            return false;
        });
    }
};

jQuery.fn.center = function() {
    // Always return each...
    return this.each(function() {
        var t = $(this);

        // Set position to other than 'static' so element shrink-wraps and width/height is calculated properly
        t.css({ position: 'fixed' });

        // Why are there no jQuery.fn.outerWidth/Height:s?
        var w = t.width(),
                    h = t.height(),
                    lrPadding = parseInt(t.css('paddingLeft'), 10) + parseInt(t.css('paddingRight'), 10),
                    lrBorder = parseInt(t.css('borderLeftWidth'), 10) + parseInt(t.css('borderRightWidth'), 10),
                    tbPadding = parseInt(t.css('paddingTop'), 10) + parseInt(t.css('paddingBottom'), 10),
                    tbBorder = parseInt(t.css('borderTopWidth'), 10) + parseInt(t.css('borderBottomWidth'), 10),
                    leftMargin = (w + lrPadding + lrBorder) / 2;
        topMargin = (h + tbPadding + tbBorder) / 2;

        t.css({
            position: 'fixed',
            left: '50%',
            top: '50%',
            marginLeft: '-' + leftMargin + 'px',
            marginTop: '-' + topMargin + 'px',
            zIndex: '99'
        });

        if ($.browser.msie && $.browser.version == '6.0') {
            t.css({
                position: 'absolute',
                top: ($(window).height() / 2) - $(this).outerHeight() + $(window).scrollTop(),
                marginTop: 0
            });

            //top: ($(window).height() - $(this).outerHeight()) / 2 + $(window).scrollTop() - 200
        }
    });
};

var eic = new EICv3();


