
var siteTid = null;
var smenuTid = new Array();


function commonPrepare() {
  $('.menu').wrapAll('<div id="menus" class="header"/>');
  $('.recherche').wrapAll('<div id="recherche" class="header"/>');
  $('.header').wrapAll('<div id="header" class="page"/>');
  $('.ariane').wrapAll('<div id="ariane" class="page"/>');
  $('.footer').wrapAll('<div id="footer" class="page"/>');
  $('.page').wrapAll('<div id="page"/>');
  $('div.smenu').each(function(){
    var id = $(this).attr('id').substr(5);
    $(this).html('<div>'+$(this).html()+'</div>');
    smenuTid[id] = null;
  });
  $('div.smenu').css('display', 'none');
  commonResize();
  // actions
  $(window).resize(commonResize);
  $('#recherche input').keypress(function(e){
    if (e.which == 13)
      document.searchform.submit();
  });
  $('#rechercher').click(function(){
    document.searchform.submit();
  });
  $('#menus h2').hover(smenuShow, smenuHide);
  $('div.smenu').hover(smenuOver, smenuOut);
}

function commonResize() {
  var w = $(window).width();
  if (w < 991)
    $('#page').css({left: '0px'});
  else
    $('#page').css({left: Math.floor((w - 991) / 2)+'px'});
  var h = $('#page').height()+50;
  if ($(window).height() > h)
    h = $(window).height();
  $('#sbg').css({height: h+'px'});
  resizeDocument(w);
}

function commonPrepared() {
  preloadImages($('body').attr('sbg'));
  if (siteTid) clearTimeout(siteTid);
  siteTid = setTimeout('showSbg()', 1000);
}

function preloadImages() {
  var d = document;
  if(d.images) {
    if(!d.MM_p) d.MM_p = new Array();
    var i,j = d.MM_p.length, a = preloadImages.arguments;
    for(i = 0; i < a.length; i ++)
      if (a[i].indexOf("#") != 0) {
	d.MM_p[j] = new Image;
	d.MM_p[j++].src = a[i];
      }
  }
}

function smenuShow() {
  var id = $(this).attr('id').substr(1);
  if (!$('#smenu'+id).length) return false;
  if (smenuTid[id]) clearTimeout(smenuTid[id]);
  if ($('#smenu'+id).css('left') == '0px') {
    var o = $(this).offset();
    $('#smenu'+id).css('left', (o.left-parseInt($('#page').css('left').replace('px', '')))+'px');
  }
  $('#smenu'+id).stop();
  $('#smenu'+id).css('display', 'block');
  $('#smenu'+id).animate({height: ($('#smenu'+id+' div').height()+15)+'px'}, 400);
}

function smenuHide() {
  var id = $(this).attr('id').substr(1);
  if (!$('#smenu'+id).length) return false;
  if (smenuTid[id]) clearTimeout(smenuTid[id]);
  smenuTid[id] = setTimeout('_smenuHide('+id+')', 100);
}

function _smenuHide(id) {
  $('#smenu'+id).stop();
  $('#m'+id).removeClass('sel');
  $('#smenu'+id).css({display: 'none', height: '0px'});
}

function smenuOver() {
  var id = $(this).attr('id').substr(5);
  if (smenuTid[id]) clearTimeout(smenuTid[id]);
  $('#m'+id).addClass('sel');
}

function smenuOut() {
  var id = $(this).attr('id').substr(5);
  if (smenuTid[id]) clearTimeout(smenuTid[id]);
  smenuTid[id] = setTimeout('_smenuHide('+id+')', 100);
}

function showSbg() {
  var h = $('#page').height()+50;
  if ($(window).height() > h)
    h = $(window).height();
  $('body').append('<div id="sbg"/>');
  $('#sbg').css({height: h+'px', opacity: '0', 
	background: 'transparent url('+$('body').attr('sbg')+') no-repeat top center'});
  $('#sbg').animate({opacity: '1'}, 600);
}

function recoForm() {
  $('#popupbg').css('display', 'block');
  $.ajax({
    type: "GET",
    url: '/templates/recommander.php',
    success: function(data){
      $('body').append(data);
      popupShow();
      $('#cancel').click(popupClose);
      $('#validate').click(recommander);
      return false;
    }
  });
  return false;
}

function recommander() {
  var f = document.myform;
  if (f.nom.value) f.nom.value = f.nom.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.nom.value) {
    ppAlert('Indiquez votre nom');
    return false;
  }
  if (f.email.value) f.email.value = f.email.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.email.value) {
    ppAlert('Indiquez votre email');
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    ppAlert('Email incorrect');
    return false;
  }
  f.email.value = e;
  if (f.destinataires.value) f.destinataires.value = f.destinataires.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.destinataires.value) {
    ppAlert('Indiquez les emails');
    return false;
  }
  f.destinataires.value = f.destinataires.value.replace(/[\n\r\s\t,;]+/g, '\n');
  f.destinataires.value = f.destinataires.value.replace(/\n$/, '');
  var arr = f.destinataires.value.split('\n');
  for (i = 0; i < arr.length; i++) {
    e = emailIsValid(arr[i]);
    if (e == null) {
      ppAlert('Email '+arr[i]+' incorrect');
      return false;
    }
  }
  ppAlert('Veuillez patienter...');
  $.ajax({
    type: "POST",
    url: '/scripts/recommander.php',
    data: 'nom='+f.nom.value+'&email='+f.email.value+'&destinataires='+f.destinataires.value.replace('\n', ',')+
	'&href='+window.location.href+'&title='+document.title+'&message='+f.message.value,
    success: function(data){
      ppAlert('Envoyé !');
      popupClose();
    }
  });
}

function emailIsValid( email ) {
  var e = email.toLowerCase(); 
  e = e.replace( /(^\s+|\s+$)/g, '' );
  if (!e.match(/[a-z0-9\.\-]+@[a-z0-9\.\-]+\.[a-z]{2,4}/))
    return null;
  return e;
}

function ppAlert(mes) {
  $('#popup th.mes').html(mes);
}

function popupShow() {
  $('#popup').css({opacity: '0', display: 'block'});
  var w = 0;
  var h = 0;
  if ($('#popup table.main').length) {
    w = $('#popup table.main').width();
    h = $('#popup table.main').height();
  }
  else {
    w = $('#popup').width();
    h = $('#popup').height();
  }
  if (h > $(window).height())
    $('#popup').css({ width: w+'px', height: h+'px', top: ($(document).scrollTop()+10)+'px', 
	marginLeft: '-'+Math.floor(w/2)+'px', left: '50%' });
  else
    $('#popup').css({ width: w+'px', height: h+'px', marginTop: Math.floor(-h/2 + $(document).scrollTop())+'px', 
	marginLeft: '-'+Math.floor(w/2)+'px', top: '50%', left: '50%' });
  $('#popup').animate({opacity: '1'}, 500);
}

function popupClose() {
  $('#popupbg').animate({opacity: '0'}, 500);
  $('#popup').animate({opacity: '0'}, 500, '', function(){
    $('#popup').remove();
    $('#popupbg').css('display', 'none');
  });
}


