
var contentHeightMax = 530;

var fichePhotoWidth = 500;
var thumbsWidth = 70;
var modelNameWidth = 62;

var greyMLRatio = 0.036734694;

var ficheCurId = 0;

var thumbs = new Array();
var photoExpanded = new Array();
var modelVideo = new Array();

var emailTimerid = null;
var emailTimerid2 = null;

var thumbsSpeed = 0;
var thumbsTimerid = null;
var thumbsHeight = 0;


function fichePrepare() {
  ficheCurId = $('div.photo:first').attr('id').substr(5);
  buildFiche(ficheCurId);
  $('#thumbs'+ficheCurId+' img:last').load(function(){
    $('#cache1').fadeOut(400, function(){
      $('#cache1').remove();
    });
  });
}

function ficheInitSizes() {
  if (contentHeight > contentHeightMax)
    contentHeight = contentHeightMax;

  $('h1').css('marginLeft', '-'+Math.ceil(siteWidth / 2 - 30)+'px');
  detailsWidth = Math.ceil(siteWidth * greyRatio);
  $('#content').css({width: siteWidth+'px', height: contentHeight+'px', marginLeft: '-'+Math.ceil(siteWidth / 2)+'px'});
  $('div.fdetails').css({width: detailsWidth+'px', height: contentHeight+'px'});
  $('div.fdetails p').css('marginLeft', Math.ceil(siteWidth * greyMLRatio)+'px');
  $('div.fdetails p.name img').load(function(){
    if ($(this).height() > contentHeight - 16)
      $(this).height(contentHeight - 16);
  });
/*  $('div.fdetails').each(function(){
    if ($('#details p.video').length)
      $('#details p.send').css('bottom', '65px');

  });*/
  $('div.photo').css({left: detailsWidth+'px', height: contentHeight+'px'});
  $('div.photo img').each(function(){
    if ($(this).height() != contentHeight)
      $(this).height(contentHeight);
  });
  $('div.thumbs').css({height: contentHeight+'px', left: (detailsWidth + fichePhotoWidth)+'px'});
  $('#back').css({top: Math.floor(headerHeight+contentHeight+(footerHeight-11)/2)+'px', 
	marginLeft: Math.ceil(0 - siteWidth / 2 + detailsWidth)+'px'});
  // large photo
  if (ficheCurId && photoExpanded[ficheCurId]) {
    $('#fdetails'+ficheCurId).css('width', modelNameWidth+'px');
    $('#photo'+ficheCurId).css('left', modelNameWidth+'px');
  }
  // email form
  var o = $('#fdetails'+ficheCurId+' p:first').offset();
  $('#ficheemail').css({top: (headerHeight + contentHeight)+'px', left: o.left+'px'});
}

function buildFiche(id) {
  photoExpanded[id] = false;
  $('#content').append('<div id="thumbs'+id+'" class="thumbs"><div></div></div>');
  $('#photo'+id).html('<div>'+$('#photo'+id).html()+'</div>');
  // load images
  for (i = 0; i < thumbs[id].length; i++)
    $('#thumbs'+id+' div').append('<img src="'+thumbs[id][i]+'" style="width: '+thumbsWidth+'px;" />');
  $('#ficheemail').live('mouseover', emailStay);
  $('#ficheemail').live('mouseout', emailHide);
}

function ficheLoad() {
  // preload images
  var i = 0;
  thumbsHeight = 0;
  for (; i < thumbs[ficheCurId].length; i++) {
    preloadImages(thumbs[ficheCurId][i].replace('-tn', ''));
    thumbsHeight += $('#thumbs'+ficheCurId+' img:eq('+i+')').height() + 5;
  }
}

function gotoFiche() {
  liste2Cancel();
  var id = $(this).attr('id').substr(2);
  if (siteCurPage == 'fiche')
    return false;
  sitePreviousPage = siteCurPage;
  siteCurPage = 'fiche';
  ficheCurId = id;

  if ($('#photo'+id).length) {
    initSizes();
    $('#fdetails'+id+', #photo'+id+', #thumbs'+id).css('top', contentHeight+'px');
    return gotoFicheTerminate();
  }
  $('body').append('<div id="cache2" class="cache"><div><img src="/images/loading3.gif" /></div></div>');
  $('#cache2 div').css({width: $('#content').css('width'), marginLeft: $('#content').css('marginLeft'), 
	top: (2*headerHeight+contentHeight-38)+'px'});
  $.ajax({
    type: "GET",
    url: '/templates/fiche.php',
    data: 'id='+id+'&lang='+siteLang,
    success: function(data){
	$('#content').append(data);
	buildFiche(id);
	initSizes();
	$('#fdetails'+id+', #photo'+id+', #thumbs'+id).css('top', contentHeight+'px');
	$('#cache2 div').css({top: (2*headerHeight+contentHeight-38)+'px'});
	$('#thumbs'+id+' img:last').load(function(){
	  gotoFicheTerminate();
	});
    }
  });
  return false;
}

function gotoFicheTerminate() {
  if (sitePreviousPage == 'home') {
    $('#logo').css('display', 'none');
    $('h1').css('display', 'block');
    $('#content img.home').animate({top: '-'+($('#content img.home').height()+5)+'px'}, 400);
  }
  else if (sitePreviousPage == 'liste') {
    $('#pages'+listeCurId).css('display', 'none');
    $('#ldetails'+listeCurId+', #liste'+listeCurId).animate({top: '-'+($('#liste'+listeCurId).height()+5)+'px'}, 
	400, '', function(){
      if (listeCurId == 1000)
	$('#ldetails1000, #liste1000').remove();
    });
  }
  $('#back').css('display', 'block');
  $('#fdetails'+ficheCurId).animate({top: '0px'}, 400, '', function(){
    $('#cache2').remove();
    ficheLoad();
  });
  $('#photo'+ficheCurId+', #thumbs'+ficheCurId).animate({top: '0px'}, 400);
  return false;
}

function thumbsNav(e) {
  var h = $('#thumbs'+ficheCurId+' div').height();
  if (h <= contentHeight) return;
  var top = -1 * Math.ceil((e.pageY - headerHeight) * (h - contentHeight) / contentHeight);
//  var top = -1 * Math.ceil((e.pageY - 5) * (h - 10 - contentHeight) / contentHeight + 10);
  $('#thumbs'+ficheCurId+' div').css('top', top+'px');
}

function thumbsNavIE(e) {
  var orig = headerHeight + Math.ceil(contentHeight/2);
  if (e.pageY - orig < -150) thumbsSpeed = 15;
  else if (e.pageY - orig < -110) thumbsSpeed = 8;
  else if (e.pageY - orig < -60) thumbsSpeed = 4;
  else if (e.pageY - orig < -35) thumbsSpeed = 1;
  else if (e.pageY - orig < 35) {
    thumbsSpeed = 0;
    return true;
  }
  else if (e.pageY - orig < 60) thumbsSpeed = -1;
  else if (e.pageY - orig < 110) thumbsSpeed = -4;
  else if (e.pageY - orig < 150) thumbsSpeed = -8;
  else thumbsSpeed = -15;
  return _thumbNavMove();
}

function _thumbNavMove() {
  if (!thumbsSpeed) return true;
  var top = $('#thumbs'+ficheCurId+' div').css('top');
  if (!top) return true;
  top = parseInt(top.replace('px', ''));
  if (top+thumbsSpeed > 0) {
    $('#thumbs'+ficheCurId+' div').css('top', '0px');
    return true;
  }
  if (top+thumbsSpeed < -1 * (thumbsHeight - contentHeight + 5)) {
    $('#thumbs'+ficheCurId+' div').css('top', '-'+(thumbsHeight - contentHeight + 5)+'px');
    return true;
  }
  $('#thumbs'+ficheCurId+' div').css('top', (top+thumbsSpeed)+'px');
  if (thumbsTimerid) clearTimeout(thumbsTimerid);
  thumbsTimerid = setTimeout('_thumbNavMove()', 1);
  return true;
}

function thumbsNavStop() {
  thumbsSpeed = 0;
}

function photoLoad() {
  $('#photo'+ficheCurId+' div').append('<img src="'+$(this).attr('src').replace('-tn', '')+'" />');
  if ($('#photo'+ficheCurId+' img:last').height() > contentHeight)
    $('#photo'+ficheCurId+' img:last').height(contentHeight);
  if ($('#photo'+ficheCurId+' img:last').height() < contentHeight)
    $('#photo'+ficheCurId+' img:last').css('marginTop', (contentHeight - $('#photo'+ficheCurId+' img:last').height())+'px');
  // large after thin
  if (!photoExpanded[ficheCurId] && 
	($('#photo'+ficheCurId+' img:last').width() > $('#photo'+ficheCurId+' img:last').height())) {
    $('h1').css('display', 'none');
    $('#fdetails'+ficheCurId+' p').css('visibility', 'hidden');
    $('#fdetails'+ficheCurId+' p.name').css('visibility', 'visible');
    $('#fdetails'+ficheCurId).animate({width: modelNameWidth+'px'}, 600, 'easeOutExpo');
    $('#photo'+ficheCurId).animate({left: modelNameWidth+'px', width: $('#photo'+ficheCurId+' img:last').width()+'px'}, 
	600, 'easeOutExpo', function(){
      _photoLoad();
    });
/* déplacer thumbs
      $('#thumbs'+ficheCurId).animate({left: (i + $('#photo'+ficheCurId+' img:last').width() + 20)+'px'}, 
	600, 'easeOutExpo');*/
    photoExpanded[ficheCurId] = true;
    return false;
  }
  // thin after large
  if (photoExpanded[ficheCurId] && 
	($('#photo'+ficheCurId+' img:last').width() < $('#photo'+ficheCurId+' img:last').height())) {
    $('#fdetails'+ficheCurId).animate({width: detailsWidth+'px'}, 600, 'easeOutExpo');
    $('#photo'+ficheCurId).animate({left: detailsWidth+'px', width: fichePhotoWidth+'px'}, 
	600, 'easeOutExpo', function(){
      $('#fdetails'+ficheCurId+' p').css('visibility', 'visible');
      $('h1').css('display', 'block');
      _photoLoad();
    });
    $('#thumbs'+ficheCurId).animate({left: (detailsWidth + fichePhotoWidth)+'px'}, 600, 'easeOutExpo');
    photoExpanded[ficheCurId] = false;
    return false;
  }
  // large after large
/*  if (photoExpanded[ficheCurId] && 
	($('#photo'+ficheCurId+' img:last').width() > $('#photo'+ficheCurId+' img:last').height()) && 
	($('#photo'+ficheCurId+' img:last').width() > $('#photo'+ficheCurId).width())) {
    $('#photo'+ficheCurId).animate({width: $('#photo'+ficheCurId+' img:last').width()+'px'}, 
	600, 'easeOutExpo', function(){
      _photoLoad();
    });
    // move thumbs ?
    return false;
  }*/
  _photoLoad();
}

function _photoLoad() {
  $('#photo'+ficheCurId+' div').animate({top: '-'+$('#photo'+ficheCurId+' img:first').height()+'px'}, 
	600, 'easeOutExpo', function(){
    $('#photo'+ficheCurId+' img:first').remove();
    $('#photo'+ficheCurId+' div').css('top', '0px');
  });
}

function emailShow() {
  if (emailTimerid) clearTimeout(emailTimerid);
  $('#ficheemail').animate({top: headerHeight+'px', height: contentHeight+'px'}, 400);
}

function emailStay() {
  if (emailTimerid) clearTimeout(emailTimerid);
}

function emailHide() {
  if (emailTimerid) clearTimeout(emailTimerid);
  emailTimerid = setTimeout('_emailHide()', 700);
}

function _emailHide() {
  $('#ficheemail').animate({top: (headerHeight + contentHeight)+'px', height: '0px'}, 300);
}

function emailSend() {
  var f = document.emailform;
  f.id.value = ficheCurId;
  $('#ficheemail p span.mes').remove();
  f.nom.value = f.nom.value.replace(/(^\s+|\s+$)/, '');
  if (!f.nom.value) {
    $('#enom').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  f.email.value = f.email.value.replace(/(^\s+|\s+$)/, '');
  if (!f.email.value) {
    $('#eemail').append('<span class="mes"> '+MESSAGES[siteLang][2]+'</span>');
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    $('#eemail').append('<span class="mes"> '+MESSAGES[siteLang][3]+'</span>');
    return false;
  }
  f.email.value = e;

  e = emailCheck(f.destinataires.value);
  if (e == null)
    return false;
  f.destinataires.value = e;

  $('#ficheemail p.mes').html(MESSAGES[siteLang][0]);
  $('#emailform').ajaxSubmit({
    type: "POST",
    url: '/scripts/email-page.php',
    success: function(data){
      f.reset();
      $('#ficheemail p.mes').html(MESSAGES[siteLang][1]);
      if (emailTimerid2) clearTimeout(emailTimerid2);
      emailTimerid2 = setTimeout('emailMesClean()', 5000);
    }
  });
}

function emailCheck(emails) {
  emails = emails.toLowerCase();
  emails = emails.replace( /[\s\t,;]+/g, "\n" );
  emails = emails.replace( /[\n\r]+/g, "\n" );
  emails = emails.replace( /^\n/, "" );
  emails = emails.replace( /\n$/, "" );
  t = emails.split("\n");
  for (i = 0; i < t.length; i ++) {
    if (emailIsValid(t[i]) == null) {
      $('#edest').append('<span class="mes"> '+MESSAGES[siteLang][3]+'</span>');
      return null;
    }
  }
  return emails;
}

function emailMesClean() {
  $('#ficheemail p.mes').html(MESSAGES[siteLang][6]);
}

function selectionAdd() {
  var act = 'add';
  if ($(this).hasClass('del')) act = 'del';
  $(this).html(MESSAGES[siteLang][0]);
  var obj = $(this);
  $.ajax({
    type: "GET",
    url: '/scripts/selection-'+act+'.php',
    data: 'id='+ficheCurId+'&lang='+siteLang,
    success: function(data){
	obj.html(data);
	if (act == 'add') obj.addClass('del');
    }
  });
}
function printComposit() {
  var id = '#photo'+ficheCurId+' img:last';
  if ($(id).width() > $(id).height()) {
    alert('Merci de bien vouloir choisir une autre photo !');
    return false;
  }
  window.location.href = '/print-composit.php?lang='+siteLang+'&id='+ficheCurId+'&photo='+$(id).attr('src');
}

