
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;
  if ($('#header').length)
    $('body').append('<div id="headerbg"></div><div id="footerbg"></div>');
  $('#sm1').css('display', 'none');
  if (siteCurPage == 'home') homePrepare();
  else if (siteCurPage == 'liste') listePrepare();
  else if (siteCurPage == 'fiche') fichePrepare();
  else standardPrepare();

  initSizes();
  $(window).resize(initSizes);
  // main actions
  $('#m1').click(function(){$('#sm1').css('display', 'block');});
  $('#ficheemail, div.liste2').unbind();
  $('#t1 p.but').click(regCandidature);

  $('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.print').live('click', printComposit);
}

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 (siteCurPage != 'home')
    contentHeight -= 58;
  if ($.browser.msie && (contentHeight > 1400))
    contentHeight = 1400;
  if (contentHeight < 600 - 42 - 100)
    contentHeight = 600 - 42 - 100;
  siteWidth = contentHeight * homeRatio;
  // image width too large
  if (contentHeight * homeRatio > w) {
    contentHeight = Math.ceil(w / homeRatio);
    siteWidth = w;
  }
  // menus
  $('#header h2').each(function(){
    $(this).css({height: headerHeight+'px', lineHeight: headerHeight+'px',
	marginRight: Math.ceil(siteWidth * menusRatio)+'px',
	marginLeft: Math.ceil(siteWidth * menusRatio * 2.4)+'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'});
  });

  // header & footer
  $('#headerbg, #footerbg, #header, #footer').css({width: siteWidth+'px', height: headerHeight+'px', 
	marginLeft: '-'+Math.ceil(siteWidth / 2)+'px'});
  if (siteCurPage != 'home') {
    $('#content').css({top: '100px'});
    $('#footerbg, #footer').css('top', (headerHeight+58+contentHeight)+'px');
  }
  else {
    $('#content').css({top: headerHeight+'px'});
    $('#footerbg, #footer').css('top', (headerHeight+contentHeight)+'px');
  }
  if (siteCurPage == 'home') homeInitSizes(w, h);
  else if (siteCurPage == 'liste') listeInitSizes(w, h);
  else if (siteCurPage == 'fiche') ficheInitSizes(w, h);
  else standardInitSizes(w, h);

  // smenus
  var o = $('#m1').offset();
  $('#sm1').css({left: (o.left - Math.floor((150 - $('#m1').width())/2))+'px'});
}

function commonLoad() {
  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');
}

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 menuOver() {
  if (smenuTimerid) clearTimeout(smenuTimerid);
  var id = $(this).attr('id');
  if ($('#'+id).hasClass('sel'))
    return false;
  smenuShow(id);
}

function menuOut() {
  var id = $(this).attr('id');
  if ($('#'+id).hasClass('sel') && $('#s'+id).length) {
    if (smenuTimerid) clearTimeout(smenuTimerid);
    smenuTimerid = setTimeout('_smenuHide(\''+id+'\')', 700);
    return false;
  }
  $('#'+id).removeClass('sel');
}

function smenuShow(id) {
  if (smenuTimerid) clearTimeout(smenuTimerid);
  $('#header p').removeClass('sel');
  $('#'+id).addClass('sel');
  $('div.smenu').stop();
  $('div.smenu').css('height', '0px');
  $('#sm2').css({width: $('#at1').width()+'px', marginLeft: '0px'});
  if (id == smenuCurrent) return;
  smenuCurrent = id;
  // pas de sous menus
  if (!$('#s'+id).length)
    return false;
  var o = $('#'+id).offset();
  $('#s'+id).css({left: Math.floor(o.left - ($('#s'+id).width() - $('#'+id).width()) / 2)+'px'});
  $('#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() {
  if (smenuTimerid) clearTimeout(smenuTimerid);
}

function smenuHide() {
  var i = $(this).attr('id');
  if (!i) return;
  var id = i.substr(1);
  if (smenuTimerid) clearTimeout(smenuTimerid);
  smenuTimerid = setTimeout('_smenuHide(\''+id+'\')', 1000);
}

function _smenuHide(id) {
  $('#'+id).removeClass('sel');
  $('div.smenu').stop();
  smenuCurrent = '';
  if (id == 'm4') {
    $('#sm4').animate({marginLeft: '+=500px', 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;
  $('#t1 span.mes, #t2 span.mes, #t3 span.mes').remove();
  $('#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.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;
  }
  if ($('#t3 p.mes').length)
    $('#t3 p.mes').html(MESSAGES[siteLang][0]);
  else
    $('#t3').append('<p class="mes">'+MESSAGES[siteLang][0]+'</p>');
  $(this).unbind();
  $('#candform').ajaxSubmit({
    type: "POST",
    url: '/scripts/register-candidature.php',
    success: function(data){
      var r = data.substr(0, 1);
      if (r == '1') {
	$('#t3 p.mes').html(data.substr(1));
	$('#t1 p.but').click(regCandidature);
	return false;
      }
      $('#t1, #t2, #t3').animate({opacity: '0'}, 400, '', function(){
	$('#t1').empty();
	$('#t2, #t3').remove();
	$('#t1').css('opacity', '1');
	$('#t1').html(data.substr(1));
      });
    }
  });
}

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%' });
}

function popupClose() {
  $('#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).hasClass('sel') && (id == 'f1')) {
    if (creditsTimerid) clearTimeout(creditsTimerid);
    creditsTimerid = setTimeout('_footerHide(\''+id+'\')', 700);
    return false;
  }
  $('#'+id).removeClass('sel');
}

function footerShow(id) {
  if (creditsTimerid) clearTimeout(creditsTimerid);
  $('#footer p').removeClass('sel');
  $('#'+id).addClass('sel');
  if (id == 'f0') return false;
  var o = $('#'+id).offset();
  $('#credits').css({left: Math.floor(o.left - ($('#credits').width() - $('#'+id).width()) / 2)+'px'});
  $('#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').removeClass('sel');
  $('#credits').animate({top: (headerHeight + contentHeight)+'px', height: '0px'}, 300);
}


