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

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

  $('#photos').click(function (e) {
    e.preventDefault();

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

  $('#register').click(function (e) {
    e.preventDefault();

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

  $('#good').click(function (e) {
    e.preventDefault();

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

  $('#myspace').click(function (e) {
    e.preventDefault();

    $.get("myspace.html", function(data) {
      $(data).modal({
        close: false,
        position: ["5%",],
        overlayId: 'modal-overlay',
        containerId: 'modal',
        onOpen: myspace.open,
        onShow: myspace.show,
        onClose: myspace.close
      });
    });
  });

  $('#contact').click(function (e) {
    e.preventDefault();

    $.get("contact.html", function(data) {
      $(data).modal({
        close: false,
        position: ["0%",],
        overlayId: 'modal-overlay',
        containerId: 'modal',
        onOpen: contact.open,
        onShow: contact.show,
        onClose: contact.close
      });
    });
  });

  $('#paraty').click(function (e) {
    e.preventDefault();

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

  $('#socorro').click(function (e) {
    e.preventDefault();

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

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

var normal = {
  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 = 360;

    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 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();
          });
        });
      });
    });
  }
 };

var register = {
  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 = 85;

    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 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();
          });
        });
      });
    });
  }
};

var myspace = {
  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 = 280;

    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")'
                    });
                  }
                });
              }
            });
          });
        });
      });
    });
  },
  show: function (dialog) {
    $('#modal .modal-send').click(function (e) {
      e.preventDefault();

      $('#modal .modal-title').html('Enviando...');
      $('#modal form').fadeOut(200);
      $('#modal .modal-content').animate({
          height: '80px'
      }, function () {
        $('#modal .modal-loading').fadeIn(200, function () {
          $.ajax({
            url: 'myspace_action.php',
            data: $('#modal form').serialize() + '&action=send',
            type: 'post',
            cache: false,
            dataType: 'html',
            complete: function (xhr) {
              $('#modal .modal-loading').fadeOut(200, function () {
                $('#modal .modal-title').html('Obrigado!');
                $('#modal .modal-message').html(xhr.responseText).fadeIn(200);
              });
            },
            error: contact.error
          });
        });
      });
    });
  },
  close: function (dialog) {
    $('#modal .modal-message').fadeOut();
    $('#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();
          });
        });
      });
    });
  }
 };

var contact = {
  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 = 390;

    if ($('#subject').length) {
      h += 26;
    }

    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")'
                    });
                  }
                });
              }
            });
          });
        });
      });
    });
  },
  show: function (dialog) {
    $('#modal .modal-send').click(function (e) {
      e.preventDefault();

      $('#modal .modal-title').html('Enviando...');
      $('#modal form').fadeOut(200);
      $('#modal .modal-content').animate({
          height: '80px'
      }, function () {
        $('#modal .modal-loading').fadeIn(200, function () {
          $.ajax({
            url: 'contact_action.php',
            data: $('#modal form').serialize() + '&action=send',
            type: 'post',
            cache: false,
            dataType: 'html',
            complete: function (xhr) {
              $('#modal .modal-loading').fadeOut(200, function () {
                $('#modal .modal-title').html('Obrigado!');
                $('#modal .modal-message').html(xhr.responseText).fadeIn(200);
              });
            },
            error: contact.error
          });
        });
      });
    });
  },
  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();
          });
        });
      });
    });
  }
};
