////////////////////// ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19979861-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


////////////////////// NAVIGATION
var xao_current_page = 'about';

$(document).ready(function()
{
  init_menu();
  init_contact_form();
  init_slider();
});


function init_menu()
{
  $('#menu a').removeClass('active');
  $('#menu a:first').removeClass('normal');
  $('#menu a:first').addClass('active');
  
  $('#menu a').click(function()
  {
    switch_page($(this).parent().attr('class'));
    
    return false;
  });
}

function switch_page(page_name)
{
  var container = $('#content-' + page_name);
  
  $('#menu a').removeClass('active');
  $('#menu a').addClass('normal');
  $('#menu li.' + page_name + ' a:first').removeClass('normal');
  $('#menu li.' + page_name + ' a:first').addClass('active');
  
  $('#content-' + xao_current_page).fadeOut(500, function()
  {
    container.fadeIn(500);
  });
  
  xao_current_page = page_name;
  
  if (xao_current_page == 'realisations')
    $('#slider').data('nivoslider').start();
  else
    $('#slider').data('nivoslider').stop();
}

function init_slider()
{
  $('#slider').nivoSlider({
    controlNav: false,
    pauseTime: 8000
  });
  
  $('a.nivo-imageLink').click(function()
  {
    window.open($(this).attr('href'));
    return false;
  });
  
  $('#slider').data('nivoslider').stop();
}

function init_contact_form()
{
  $('#contact-form').submit(function() {
    return submit_contact_form();
  });
  
  $('#content-contact input, textarea').focus(function()
  {
    if ($(this).attr('name') == 'name')
    {
      if ($(this).val() == 'Votre nom, prénom, société...') $(this).val('');
    }
    else if ($(this).attr('name') == 'email')
    {
      if ($(this).val() == 'votre@email.com') $(this).val('');
    }
    else if ($(this).attr('name') == 'message')
    {
      if ($(this).val() == 'Tapez votre message ici...') $(this).val('');
    }
  });
  
  $('#content-contact input, textarea').blur(function()
  {
    if ($(this).attr('name') == 'name')
    {
      if ($(this).val() == '') $(this).val('Votre nom, prénom, société...');
    }
    else if ($(this).attr('name') == 'email')
    {
      if ($(this).val() == '') $(this).val('votre@email.com');
    }
    else if ($(this).attr('name') == 'message')
    {
      if ($(this).val() == '') $(this).val('Tapez votre message ici...');
    }
  });
}

function submit_contact_form()
{
  $('#contact-form-status').html('Envoi en cours...');
  $('#contact-form-status').fadeIn();
  $('#content-contact input, textarea').attr('disabled', 'disabled');
  
  $.post('ajax/mail.php', {
    'name': $('#contact-form-name').val(),
    'email': $('#contact-form-email').val(),
    'message': $('#contact-form-message').val()
    
  },
  function(json)
  {
    $('#content-contact input, textarea').removeAttr('disabled');
    
    if (json.error)
    {
      $('#contact-form-status').fadeOut();
      alert(json.error);
    }
    else
    {
      $('#contact-form-status').html('Message envoyé !');
    }
  }, 'json');
  
  return false;
}
