function shakeRabbit() {
    if ($('#follow.hidden').length == 0) {
        $("#follow").effect('shake', {}, 'fast', function() {
            setTimeout(shakeRabbit, 5000);
        });
    }
}

var setInputHint = function(id) {
    var el = $(id);
    var label = el.attr('placeholder');
    if (el.val() == '') {
        el.val(label);
    }

    el.bind({
        click: function() {
           if (el.val() == label) {
               el.val('');
           }
        },
        blur: function() {
            if (el.val() == '') {
                el.val(label);
            }
        }
    });
}

$(document).ready(function() {
  setTimeout(shakeRabbit, 5000);

  if (!Modernizr.input.placeholder){
    setInputHint('#newsletter_email');
  }

  if ($('#follow.hidden').length == 0) {

      $('#boxsubscribe').hide();
      $('#follow a').click(function(event) {
        event.preventDefault();
        $('#follow').hide('clip', {}, 'slow', function() {
            $('#follow').addClass('hidden');
            $('#boxsubscribe').show('clip', {}, 'slow');
        });
      });
  }

  var form = $('#boxsubscribe form');
  form.submit(function(event) {
    event.preventDefault();
    $('.error, .success').remove();
    var action = form.attr('action');
    $.ajax({
        url: action,
        dataType: 'json',
        type: 'POST',
        data: {
            newsletter: {
                email: $('#newsletter_email').val()
            }
        },
        success: function(data) {
            if (!data) {
                $('#boxsubscribe').prepend('<div class="error">Une erreur est survenue.<div></div></div>');
            }
            else if (data.status == 500) {
                $('#boxsubscribe').prepend('<div class="error">'+data.result.msg+'<div></div></div>');
            }
            else if (data.status == 200) {
                $('#boxsubscribe').prepend('<div class="success">'+data.result.msg+'<div></div></div>');
            }
        }
    })
  });

    $("#social_networks a").each(function(){
        $(this).attr({ title: ($(this).attr("title"))?$(this).attr("title"):$(this).attr("href")+" (new tab)" });
        $(this).click(function() {window.open($(this).attr('href'));return false;});
    });
});
