$(document).ready(function () {
  $('#newsletter').click(function (e) {
    e.preventDefault();

    $.get("newsletter.php", function(data) {
      $(data).modal({
        close: false,
        position: ["15%",],
        overlayId: 'modal-overlay',
        containerId: 'modal',
        onOpen: newsletter.open,
        onClose: newsletter.close
      });
    });
  });

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

var newsletter = {
  message: null,
  open: function (dialog) {
    if ($.browser.mozilla) {
      $('#modal .modal-button').css({
        'padding-bottom': '2px'
      });
    }

    if ($.browser.safari) {
      $('#modal .modal-input').css({
        'font-size': '.9em'
      });
    }

    var h = 290;

    var title = $('#modal .modal-title').html();
    $('#modal .modal-title').html('Carregando...');
    dialog.overlay.fadeIn(200, function () {
      dialog.container.fadeIn(200, function () {
        dialog.data.fadeIn(200, function () {
          $('#modal .modal-content').animate({
            height: h
          }, function () {
            $('#modal .modal-title').html(title);
            $('#modal form').fadeIn(200, function () {
              $('#modal #name').focus();

              if ($.browser.msie && $.browser.version < 7) {
                $('#modal .modal-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")'
                    });
                  }
                });
              }
            });
          });
        });
      });
    });
  },
  close: function (dialog) {
    $('#modal .modal-message').fadeOut();
    $('#modal .modal-title').html('Obrigado!');
    $('#modal form').fadeOut(200);
    $('#modal .modal-content').animate({
      height: 40
    }, function () {
      dialog.data.fadeOut(200, function () {
        dialog.container.fadeOut(200, function () {
          dialog.overlay.fadeOut(200, function () {
            $.modal.close();
          });
        });
      });
    });
  }
};