
var MESSAGES = new Array();
MESSAGES['fr'] = new Array(
	'Veuillez patientez...',
	'Message envoyé !',
	'obligatoire',
	'incorrecte',
	'non confirmée',
	'Candidature enregistrée',
	"Séparez l'adresse email des destinataires par des virgules ou des sauts de ligne."
);
MESSAGES['en'] = new Array(
	'Please wait...',
	'Message sent!',
	'required',
	'incorrect',
	'not confirmed',
	'Candidature registered',
	"Separate recipient email addresses with comas or line breaks."
);

var siteLang = 'fr';
var sitePreviousPage = 'home';
var siteCurPage = 'home';
var listeCurId = 0;

var headerHeight = 0;
var footerHeight = 0;
var contentHeight = 0;
var menusHeight = 9;
var fmenusHeight = 12;

var siteWidth = 0;
var detailsWidth = 0;

var headerRatio = 0.07;
var menusRatio = 0.027522936;
var greyRatio = 0.336734694;

var creditsTimerid = null;
var contactTimerid = null;
var smenuTimerid = null;
var smenuCurrent = '';



function completeDocument(lang, cpage) {
  if (lang) siteLang = lang;
  if (cpage) siteCurPage = cpage;
  var l = '';
  if (lang == 'en') l = '-en';

  $('body').append('<div id="headerbg"></div><div id="footerbg"></div><div id="back"><img src="/images/back'+
	l+'.png" /></div>');
  if (siteCurPage == 'home') homePrepare();
  else if (siteCurPage == 'liste') listePrepare();
  else fichePrepare();

  initSizes();
  $(window).resize(initSizes);
  // main actions
  $('#header h2, #header p').click(menuOver);
  $('#header h2, #header p').hover(pngOver2, menuOut);
  $('#f1').click(footerOver);
  $('#footer p, #footer h3').hover(pngOver2, footerOut);
  $('#sm1 img, #back img').hover(pngOver, pngOut);
  $('div.smenu').hover(smenuStay, smenuHide);
  $('#credits').unbind();
  $('#credits').hover(footerStay, footerHide);
  $('#ficheemail, div.liste2').unbind();
  $('#sm3 p.but').click(sendMes);
  $('#sm4 p.but').click(regCandidature);
  $('#sm1 a').click(gotoListe);
  $('#m0 a').click(gotoHome);
  $('#m10').unbind('click');
  $('#m10').click(gotoFavorites);

  $('div.liste a, div.liste2 p.alpha').live('click', gotoFiche);
  $('div.pages p').live('click', gotoPage);
  if ($.browser.msie)
    $('div.thumbs div').live('mousemove', thumbsNavIE);
  else $('div.thumbs').live('mousemove', thumbsNav);
  $('div.thumbs img').live('click', photoLoad);
  $('div.fdetails p.selection').live('click', selectionAdd);
  $('div.fdetails p.send').live('click', emailShow);
  $('div.fdetails p.print').live('click', printComposit);
  $('#ficheemail p.but').live('click', emailSend);
  $('#back').live('click', backToListe);
  $('div.ldetails p.liste img').live('mouseover', pngOver);
  $('div.ldetails p.liste img').live('mouseout', pngOut);
  $('div.ldetails p.liste').live('click', liste2Show);
  $('div.liste2').live('mouseover', liste2Stay);
  $('div.liste2').live('mouseout', liste2Hide);
}

function initSizes() {
  var h = $(window).height();
  var w = $(window).width();
/*  if ($.browser.msie) {
    h -= ieMargins;
    w -= ieMargins;
  }
  else*/ if ($.browser.safari) {
    h = $(document).height();
    w = $(document).width();
  }
  headerHeight = Math.ceil(600 * headerRatio);
  footerHeight = headerHeight;
  contentHeight = h - headerHeight - footerHeight;
  if ($.browser.msie && (contentHeight > 1400))
    contentHeight = 1400;
  siteWidth = contentHeight * homeRatio;
  // image width too large
  if (contentHeight * homeRatio > w) {
    contentHeight = Math.ceil(w / homeRatio);
    siteWidth = w;
  }
  // menus
  $('#header h2, #header p').each(function(){
    $(this).css({height: headerHeight+'px', lineHeight: headerHeight+'px',
	marginRight: Math.ceil(siteWidth * menusRatio)+'px', marginLeft: Math.ceil(siteWidth * menusRatio)+'px'});
  });
  $('#header img').css({marginTop: Math.ceil((headerHeight - menusHeight) / 2)+'px'});
  $('#header img').load(function(){
    var id = $(this).attr('id').substr(2);
    if ((id == 1) || (id == 2) || (id == 3)) {
      var o = $('#m'+id).offset();
      $('#sm'+id).css({left: (o.left - Math.ceil($('#sm'+id).width() / 2) + Math.ceil($('#m'+id).width() / 2))+'px'});
    }
  });
  $('#footer h3, #footer p').each(function(){
    $(this).css({height: footerHeight+'px', lineHeight: footerHeight+'px',
	marginRight: Math.ceil(siteWidth * menusRatio)+'px', marginLeft: Math.ceil(siteWidth * menusRatio)+'px'});
  });
  $('#footer img').css({marginTop: Math.ceil((headerHeight - fmenusHeight) / 2)+'px'});
  $('#footer #f1 img').load(function(){
    var o = $('#footer #f1').offset();
    $('#credits').css({left: Math.ceil(o.left - ($('#credits').width() / 2) + $('#footer #f1').width() / 2)+'px'});
  });
  // to not perturb fiche email
  $('div.smenu').css({top: headerHeight+'px'});

  if (siteCurPage == 'home') homeInitSizes(w, h);
  else if (siteCurPage == 'liste') listeInitSizes(w, h);
  else ficheInitSizes(w, h);

  // header & footer
  $('#headerbg, #footerbg, #header, #footer').css({width: siteWidth+'px', height: headerHeight+'px', 
	marginLeft: '-'+Math.ceil(siteWidth / 2)+'px'});
  $('#footerbg, #footer').css('top', (headerHeight+contentHeight)+'px');
  $('#content').css({top: headerHeight+'px'});
  // smenus
  var i = 5;
  $('#sm1 h2').each(function(){
    i += $(this).height() + 25;
  });
  $('#sm1 h2:first').css({marginTop: (contentHeight - i)+'px'});
  $('#sm2 img').css({marginTop: (contentHeight - 65 - $('#at2 img').height())+'px'});
  sm4MarginLeft = (Math.ceil(siteWidth / 2) - $('#sm4').width());
  $('#sm4').css({marginLeft: sm4MarginLeft+'px'});
  i = 0;
  $('#credits img').each(function(){
    i += $(this).height();
  });
  $('#credits').css({top: (headerHeight + contentHeight)+'px'});
  $('#credits img:first').css('marginTop', (contentHeight - i - 30)+'px');
}

function commonLoad() {
  $('#header img, #sm1 img').each(function(){
    if ($(this).hasClass('sel'))
      preloadImages($(this).attr('src').replace('-sel', ''));
    else
      preloadImages($(this).attr('src').replace('.png', '-sel.png'));
  });
  preloadImages('/images/back-sel.png', '/images/back-en-sel.png', '/images/send-by-email-sel.png', 
	'/images/video-sel.png', '/images/send-sel.png', '/images/selection-sel.png', '/images/liste-alphabetique-sel.png',
	'/images/liste-alphabetique-en-sel.png', '/images/composite-sel.png', $('#footer h3:first img').attr('src').replace('.png', '-sel.png'));
}

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 pngOver() {
  if ($(this).hasClass('ignore')) return false;
  $(this).attr('src', $(this).attr('src').replace('.png', '-sel.png'));
}

function pngOut() {
  if ($(this).hasClass('ignore')) return false;
  $(this).attr('src', $(this).attr('src').replace('-sel', ''));
}

function pngOver2() {
  var id = $(this).attr('id');
  if ($('#'+id+' img').hasClass('sel')) return false;
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('.png', '-sel.png'));
}

function menuOver() {
  var id = $(this).attr('id');
//  var id = $(this).attr('id').replace('i', '');
  if ($('#'+id+' img').hasClass('sel')) {
    if (smenuTimerid) clearTimeout(smenuTimerid);
    return false;
  }
//  $('#header h2, #header p').removeClass('sel');
//  $('#'+id+' img').addClass('sel');
//  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('.png', '-sel.png'));
  smenuShow(id);
}

function menuOut() {
  var id = $(this).attr('id').replace('i', '');
  if ($('#'+id+' img').hasClass('sel') && $('#s'+id).length) {
    if (smenuTimerid) clearTimeout(smenuTimerid);
    smenuTimerid = setTimeout('_smenuHide(\''+id+'\')', 700);
    return false;
  }
  $('#'+id+' img').removeClass('sel');
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('-sel.png', '.png'));
}

function smenuShow(id) {
  $('#header img').each(function(){
    $(this).removeClass('sel');
    $(this).attr('src', $(this).attr('src').replace('-sel.png', '.png'));
  });
  $('#'+id+' img').addClass('sel');
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('.png', '-sel.png'));
  $('div.smenu').stop();
  $('div.smenu').css('height', '0px');
  $('#sm4').css({width: '250px', marginLeft: sm4MarginLeft+'px'});
  $('#sm2').css({width: $('#at1').width()+'px', marginLeft: '0px'});
  if (id == smenuCurrent) return;
  smenuCurrent = id;
  // pas de sous menus
  if (!$('#s'+id).length)
    return false;
  if (smenuTimerid) clearTimeout(smenuTimerid);
  $('#s'+id).animate({height: contentHeight+'px'}, 400, '', function(){
    if (id == 'm4')
      $('#sm4').animate({marginLeft: '-=500px', width: '750px'}, 400);
    if (id == 'm2')
      $('#sm2').animate({marginLeft: '-='+$('#at2').width()+'px', width: '+='+$('#at2').width()+'px'}, 400);
  });
}

function smenuStay() {
/*  var i = $(this).attr('id');
  if (!i) return;
  var id = i.substr(2);*/
  if (smenuTimerid) clearTimeout(smenuTimerid);
}

function smenuHide() {
  var i = $(this).attr('id');
  if (!i) return;
  var id = id = i.substr(1);
  if (smenuTimerid) clearTimeout(smenuTimerid);
  smenuTimerid = setTimeout('_smenuHide(\''+id+'\')', 1000);
}

function _smenuHide(id) {
  $('#'+id+' img').removeClass('sel');
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('-sel', ''));
  $('div.smenu').stop();
  smenuCurrent = '';
  if (id == 'm4') {
    $('#sm4').animate({marginLeft: sm4MarginLeft+'px', width: '250px'}, 300, '', function(){
      $('#sm4').animate({height: '0px'}, 300);
    });
    return false;
  }
  if (id == 'm2') {
    $('#sm2').animate({marginLeft: '0px', width: '-='+$('#at2').width()+'px'}, 300, '', function(){
      $('#sm2').animate({height: '0px'}, 300);
    });
    return false;
  }
  $('#s'+id).animate({height: '0px'}, 300);
}

function sendMes() {
  var f = document.sendform;
  $('#sm3 p span.mes').remove();
  f.nom.value = f.nom.value.replace(/(^\s+|\s+$)/, '');
  if (!f.nom.value) {
    $('#snom').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.email.value = f.email.value.replace(/(^\s+|\s+$)/, '');
  if (!f.email.value) {
    $('#semail').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    $('#semail').append('<span class="mes"> '+MESSAGES[siteLang][3]+'</span>');
    return false;
  }
  f.email.value = e;
  f.tel.value = f.tel.value.replace(/(^\s+|\s+$)/, '');
  if (!f.tel.value) {
    $('#stel').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.objet.value = f.objet.value.replace(/(^\s+|\s+$)/, '');
  if (!f.objet.value) {
    $('#sobjet').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.message.value = f.message.value.replace(/(^\s+|\s+$)/, '');
  if (!f.message.value) {
    $('#smessage').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  $('#sm3').append('<p class="mes">'+MESSAGES[siteLang][0]+'</p>');
  $('#sendform').ajaxSubmit({
    type: "POST",
    url: '/scripts/send-message.php',
    success: function(data){
      document.sendform.reset();
      $('#sm3 p:last').html(MESSAGES[siteLang][1]);
      if (contactTimerid) clearTimeout(contactTimerid);
      contactTimerid = setTimeout('sendMesClean()', 5000);
    }
  });
}

function sendMesClean() {
  $('#sm3 p:last').remove();
}

function regCandidature() {
  var f = document.candform;
  $('#sm4 p span.mes').remove();
  f.prenom.value = f.prenom.value.replace(/(^\s+|\s+$)/, '');
  if (!f.prenom.value) {
    $('#prenom').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.nom.value = f.nom.value.replace(/(^\s+|\s+$)/, '');
  if (!f.nom.value) {
    $('#nom').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.portable.value = f.portable.value.replace(/(^\s+|\s+$)/, '');
  if (!f.portable.value) {
    $('#portable').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.fixe.value = f.fixe.value.replace(/(^\s+|\s+$)/, '');
  if (!f.fixe.value) {
    $('#fixe').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.email.value = f.email.value.replace(/(^\s+|\s+$)/, '');
  if (!f.email.value) {
    $('#email').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    $('#email').append('<span class="mes"> '+MESSAGES[siteLang][3]+'</span>');
    return false;
  }
  f.email.value = e;
  f.cemail.value = f.cemail.value.replace(/(^\s+|\s+$)/, '');
  if (!f.cemail.value) {
    $('#cemail').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (f.email.value != f.cemail.value) {
    $('#email').append('<span class="mes"> '+MESSAGES[siteLang][4]+'</span>');
    return false;
  }
  f.adresse.value = f.adresse.value.replace(/(^\s+|\s+$)/, '');
  if (!f.adresse.value) {
    $('#adresse').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.cp.value = f.cp.value.replace(/(^\s+|\s+$)/, '');
  if (!f.cp.value) {
    $('#cp').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.ville.value = f.ville.value.replace(/(^\s+|\s+$)/, '');
  if (!f.ville.value) {
    $('#ville').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.pays.value = f.pays.value.replace(/(^\s+|\s+$)/, '');
  if (!f.pays.value) {
    $('#pays').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.bday.selectedIndex | !f.bmonth.selectedIndex | !f.byear.selectedIndex) {
    $('#birth').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!dateIsValid(f.bday.options[f.bday.selectedIndex].value, f.bmonth.options[f.bmonth.selectedIndex].value, 
	f.byear.options[f.byear.selectedIndex].value)) {
    $('#birth').append('<span class="mes"> '+MESSAGES[siteLang][3]+'</span>');
    return false;
  }
  f.blieu.value = f.blieu.value.replace(/(^\s+|\s+$)/, '');
  if (!f.blieu.value) {
    $('#blieu').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.hauteur.selectedIndex) {
    $('#hauteur').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.buste.selectedIndex) {
    $('#buste').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.taille.selectedIndex) {
    $('#taille').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.hanches.selectedIndex) {
    $('#hanches').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.confection.value = f.confection.value.replace(/(^\s+|\s+$)/, '');
  if (!f.confection.value) {
    $('#confection').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.pointure.value = f.pointure.value.replace(/(^\s+|\s+$)/, '');
  if (!f.pointure.value) {
    $('#pointure').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.cheveux.selectedIndex) {
    $('#cheveux').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  if (!f.yeux.selectedIndex) {
    $('#yeux').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  $('#sm4 #t1').append('<p class="mes">'+MESSAGES[siteLang][0]+'</p>');
  $(this).unbind();
  $('#candform').ajaxSubmit({
    type: "POST",
    url: '/scripts/register-candidature.php',
    success: function(data){
      document.candform.reset();
      $('#sm4 p.but').click(regCandidature);
      $('#sm4 #t1 p:last').html(MESSAGES[siteLang][5]);
      if (contactTimerid) clearTimeout(contactTimerid);
      contactTimerid = setTimeout('regCandClean()', 5000);
      candidatureForm(data);
    }
  });
}

function candidatureForm(id) {
  $.ajax({
    type: "GET",
    url: '/templates/candidature-form.php',
    data: 'id='+id+'&lang='+siteLang,
    success: function(data){
      $('body').append(data);
      popupShow();
      $('#popup p.but').click(candidatureComplete);
      return false;
    }
  });
  return false;
}

function candidatureComplete() {
  $(this).unbind();
  $('#myform').ajaxSubmit({
    type: "POST",
    url: '/scripts/complete-candidature.php',
    success: function(data){
      popupClose();
    }
  });
}

function popupShow() {
  $('#popup').css({ display: 'block' });
  w = $('#popup').width();
  h = $('#popup').height();
  $('#popup').css({ width: w+'px', height: h+'px', marginTop: '-'+Math.floor(h/2)+'px', 
	marginLeft: '-'+Math.floor(w/2)+'px', top: '50%', left: '50%' });
  var sid = $("#popup").shadowId();
  if (sid) {
    $('#'+sid).css({ display: 'block' });  
    $('#popup').redrawShadow();
  }
  else $("#popup").dropShadow();
}

function popupClose() {
  $('#popup').removeShadow();
  $('#popup').remove();
  return false;
}

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 dateIsValid(d, m, y) {
  if ((m < 1) || (m > 12))
    return false;
  var n = _monthNbDays( m, y );
  if (d > n)
    return false;
  return true;
}

function _monthNbDays(m, y) {
  if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) ||(m == 10) || (m == 12))
    return 31;
  if ((m == 4) || (m == 6) || (m == 9) || (m == 11))
    return 30;
  if (!y)
    return 28;
  if ((y % 100 == 0) && (y % 400 == 0))
    return 29;
  if (y % 4 == 0)
    return 29;
  return 28;
}

function regCandClean() {
  $('#sm4 #t1 p:last').remove();
}

function footerOver() {
  var id = $(this).attr('id').replace('i', '');
  if ($('#'+id+' img').hasClass('sel')) {
    if (creditsTimerid) clearTimeout(creditsTimerid);
    return false;
  }
  footerShow(id);
}

function footerOut() {
  var id = $(this).attr('id').replace('i', '');
  if ($('#'+id+' img').hasClass('sel') && (id == 'f1')) {
    if (creditsTimerid) clearTimeout(creditsTimerid);
    creditsTimerid = setTimeout('_footerHide(\''+id+'\')', 700);
    return false;
  }
  $('#'+id+' img').removeClass('sel');
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('-sel', ''));
}

function footerShow(id) {
  $('#footer img').each(function(){
    $(this).removeClass('sel');
    $(this).attr('src', $(this).attr('src').replace('-sel', ''));
  });
  $('#'+id+' img').addClass('sel');
  $('#'+id+' img').attr('src', $('#'+id+' img').attr('src').replace('.png', '-sel.png'));
  if (id == 'f0') return false;
  if (creditsTimerid) clearTimeout(creditsTimerid);
  $('#credits').animate({top: headerHeight+'px', height: contentHeight+'px'}, 400);
}

function footerStay() {
  if (creditsTimerid) clearTimeout(creditsTimerid);
}

function footerHide() {
  if (creditsTimerid) clearTimeout(creditsTimerid);
  creditsTimerid = setTimeout('_footerHide()', 700);
}

function _footerHide() {
  $('#f1 img').removeClass('sel');
  $('#f1 img').attr('src', $('#f1 img').attr('src').replace('-sel', ''));
  $('#credits').animate({top: (headerHeight + contentHeight)+'px', height: '0px'}, 300);
}

