$(document).ready(function () {
$('#loginForm a.login').click(function (e) {
                e.preventDefault();
                // load the contact form using ajax
                $.get("/check-login.php", function(data){
                        // create a modal dialog with the data
                        $(data).modal({
                                close: false,
                                position: ["15%",],
                                overlayId: 'login-overlay',
                                containerId: 'login-container',
                                onOpen: login.open,
                                onShow: login.show,
                                onClose: login.close
                        });
                });
        });

        // preload images
        var img = ['but_contact.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif'];
        $(img).each(function () {
                var i = new Image();
                i.src = '/images/' + this;
        });
});

var login = {
        message: null,
        open: function (dialog) {
                // add padding to the buttons in firefox/mozilla
                if ($.browser.mozilla) {
                        $('#login-container .login-button').css({
                                'padding-bottom': '2px'
                        });
                }
                // input field font size
                if ($.browser.safari) {
                        $('#login-container .login-input').css({
                                'font-size': '.9em'
                        });
                }

                var title = $('#login-container .login-title').html();
                $('#login-container .login-title').html('Cargando...');
                dialog.overlay.fadeIn(200, function () {
                        dialog.container.fadeIn(200, function () {
                                dialog.data.fadeIn(200, function () {
                                        $('#login-container .login-content').animate({
                                                height: h
                                        }, function () {
                                                $('#login-container .login-title').html(title);
                                                $('#login-container form').fadeIn(200, function () {
                                                        $('#login-container #login-name').focus();
                                                        // fix png's for IE 6
                                                        if ($.browser.msie && $.browser.version < 7) {
                                                                $('#login-container .login-button').each(function () {
                                                                        if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
                                                                                var src = RegExp.$1;
                                                                                $(this).css({
                                                                                        backgroundImage: 'none',
                                                                                        filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'                                                                                });
                                                                        }
                                                                });
                                                        }
                                                });
                                        });
                                });
                        });
                });
        },
       /* show: function (dialog) {
                $('#login-container .login-inicio').click(function (e) {
                        e.preventDefault();
                        $('#contact-container .contact-loading').fadeIn(200, function () {
                                                $.ajax({
                                                        url: 'contact.php',
                                                        data: $('#contact-container form').serialize() + '&action=send',
                                                        type: 'post',
                                                        cache: false,
                                                        dataType: 'html',
                                                        complete: function (xhr) {
                                                                $('#contact-container .contact-loading').fadeOut(200, function () {
                                                                        $('#contact-container .contact-title').html('Gracias!');
                                                                        $('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
                                                                });
                                                        },
                                                        error: contact.error                                                });
                                        });
                        }else {
                                if ($('#contact-container .contact-message:visible').length > 0) {
                                        var msg = $('#contact-container .contact-message div');
                                        msg.fadeOut(200, function () {
                                                msg.empty();
                                                contact.showError();
                                                msg.fadeIn(200);
                                        });
                                }
                                else {
                                        $('#contact-container .contact-message').animate({
                                                height: '30px'
                                        }, contact.showError);
                                }

                        }
                });
        },*/
        close: function (dialog) {
                $('#login-container .login-message').fadeOut();
                $('#login-container .login-title').html('Que tenga buen dia...');
                $('#login-container form').fadeOut(200);
                $('#login-container .login-text').fadeOut(200);
                $('#login-container .login-content').animate({
                        height: 40
                }, function () {
                        dialog.data.fadeOut(200, function () {
                                dialog.container.fadeOut(200, function () {
                                        dialog.overlay.fadeOut(200, function () {
                                                $.modal.close();
                                        });
                                });
                        });
                });
        },
        error: function (xhr) {
                alert(xhr.statusText);
        },
        validate: function () {
                contact.message = '';
                if (!$('#login-container #login-name').val()) {
                        login.message += 'Usuario requerido. ';
                }
                else {
                        return true;
                }
        },
        showError: function () {                $('#login-container .login-message')
                        .html($('<div class="login-error">').append(login.message))
                        .fadeIn(200);
        }
};
