﻿$(function(){
  // hide/show header
  $('#close-open-top a').bind('click', function() {
    if($('header:visible').length) {
      $('img', this).attr('src', 'images/open.png');
    } else {
      $('img', this).attr('src', 'images/close.png');
    }
    $('header').slideToggle('slow');
    
    return false;
  });

  // tabs
  $('.tab_content').hide();
  $('ul.tabs li:first').addClass('active').show();
  $('.tab_content:first').show();

  $('ul.tabs li').click(function() {
    $('ul.tabs li').removeClass('active');
    $(this).addClass('active');
    $('.tab_content').hide();
    var activeTab = $(this).find('a').attr('href');
    $(activeTab).fadeIn();
    return false;
  });

  // hide/show default text when user focuses on newsletter subscribe field
  var defaultEmailTxt = $('#email-address').val();
  $('#email-address').focus(function() {
        if ($('#email-address').val() == defaultEmailTxt) {
            $('#email-address').val('');
        }
    });
  $('#email-address').blur(function() {
        if ($('#email-address').val() == '') {
            $('#email-address').val(defaultEmailTxt);
        }
  });

  // Lightbox
  $(".gallery a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'dark_rounded',slideshow:4000, autoplay_slideshow: false});
  
  // Tipsy
  $('#social li a img').tipsy({delayIn: 1200, delayOut: 1200, gravity: 's'});

  // init newsletter subscription AJAX handling
  $('#newslettersubmit').click(function() { $('#newsletterform').submit(); return false; });
  $('#newsletterform').ajaxForm({dataType: 'json',
                                 timeout: 2000,
                                 success: newsletterResponse});
    
  // Twitter script config
  if ($('#tweet').length) {
      getTwitters('tweet', {
        id: '',
        count: 3,
        enableLinks: true,
        ignoreReplies: true,
        template: '"%text%" <a class="meta" href="http://twitter.com/%user_screen_name%/status/%id%">%time%</a>'});
  }
  
  
  // init contact form validation and AJAX handling
  if ($("#contactform").length > 0) {
    $("#contactform").validate({ rules: { name: "required",
                                          email: { required: true, email: true },
                                          message: "required"},
                                 messages: { name: "Este campo é necessário.",
                                             email: { required: "Este campo é necessário.",
                                                     email: "Por favor insira um endereço de email válido."},
                                             message: "Este campo é necessário."},
                                 submitHandler: function(form) {  $(form).ajaxSubmit({dataType: 'json', success: contactFormResponse}); }
                              });
  }
});

// handle newsletter subscribe AJAX response
function newsletterResponse(response) {
  if (response.responseStatus == 'err') {
    if (response.responseMsg == 'ajax') {
      alert('Error - this script can only be invoked via an AJAX call.');
    } else if (response.responseMsg == 'fileopen') {
      alert('Error opening $emailsFile. Please refer to documentation for help.');
    } else if (response.responseMsg == 'email') {
      alert('Por favor insira um endereço de email válido.');
    } else if (response.responseMsg == 'duplicate') {
      alert('Você já está inscrito em nossa newsletter.');
    } else if (response.responseMsg == 'filewrite') {
      alert('Error writing to $emailsFile. Please refer to documentation for help.');
    } else {
      alert('Um erro ocorreu. Por favor atualize a página e tente novamente.');
    }
  } else if (response.responseStatus == 'ok') {
    alert('Obrigado por se inscrever em nossa Newsletter!');
  } else {
    alert('Um erro ocorreu. Por favor atualize a página e tente novamente.');
  }
} // newsletterResponse

// handle contact form AJAX response
function contactFormResponse(response) {
  if (response.responseStatus == 'err') {
    if (response.responseMsg == 'ajax') {
      alert('Error - this script can only be invoked via an AJAX call.');
    } else if (response.responseMsg == 'notsent') {
      alert('Nós estamos tendo alguns problemos com o servidor de emails. Por favor atualize a página ou tente novamente mais tarde.');
    } else {
      alert('Um erro ocorreu. Por favor atualize a página e tente novamente.');
    }
  } else if (response.responseStatus == 'ok') {
    alert('Obrigado por entrar em contato conosco! Nós responderemos sua mensagem assim que possível.');
  } else {
    alert('Um erro ocorreu. Por favor atualize a página e tente novamente.');
  }
} // contactFormResponse
