
var alertTimerid = null;

var lmenuLen = 45;
var lmenuMaxLen = 148;
var lmenuTops = new Array( 187, 242, 297, 352 );

var contentWidthCpt = 998;
var contentWidth = 782;
var contentHeight = 358;
var photoHeight = 179;
var photoMargin = 14;
var tnHeight = 118;
var footerTop = 215;
var bnavTop = 189;
var balertTop = 549;

var nbIllus = 0;
var nbIllusLoaded = 1;
var curIllus = 1;
var illusTimerid = null;

var files = new Array();

var maxImgWidth = 550;


function completeDocument() {
  preloadImages('/images/next-sel.png', '/images/previous-sel.png', '/images/radio-sel.png', '/images/delete-sel.png');
  $('div.lmenu h4').after('<div class="top"><img src="/images/arrow.png" /></div>');
  if (($('#dpt p').html()) != '&nbsp;') $('#dpt p').after('<div class="bottom"><img src="/images/delete.png" /></div>');
  if (($('#city p').html()) != '&nbsp;') $('#city p').after('<div class="bottom"><img src="/images/delete.png" /></div>');
  if (($('#cat p').html()) != '&nbsp;') $('#cat p').after('<div class="bottom"><img src="/images/delete.png" /></div>');
  if (($('#reportage p').html()) != '&nbsp;') $('#reportage p').after('<div class="bottom"><img src="/images/delete.png" /></div>');
  setLabels();

  // files
  $('#il1').css({zIndex: '100'});
  nbIllus = files.length + 1;
//  $('#il1 img').load(illusSlideStart);

  $('#format a, #format img').click(formatSel);
  $('#dpt').hover(dptShow, dptHide);
  $('#city').hover(villeShow, villeHide);
  $('#cat').hover(catShow, catHide);
  $('#reportage').hover(repShow, repHide);
  $('div.lmenu div.bottom').click(lmenuReset);
  $('div.lmenu h2 a').click(searchSet);
  $('#nav img, #format img, div.lmenu div.bottom img').hover(imgOver, imgOut);
  $('#content a.search, #panier a').click(gotoImage);
//  $('#details img').load(panoramiqueCheck);
  $('div.lmenu div.list').jScrollPane();
//  $(window).load(checkSizes);
}

function setLabels() {
  $('span.labpan').html('panier');
  $('span.labdet').html('détails');
  $('span.labref').html('réf');
}

function checkSizes() {
  initPhotos();
  checkTableSize('order');
  checkTableSize('form');
  checkTextSize();
}

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 formatSel() {
  var id = '';
  if ($(this).attr('id')) {
    id = $(this).attr('id').substr(2);
    if ($(this).attr('id').match(/^t/))
      return typeSel(id);
  }
  else {
    id = $(this).parent().attr('id').substr(1);
    if ($(this).parent().attr('id').match(/^t/))
      return typeSel(id);
  }
  if (document.myform.format.value == id)
    document.myform.format.value = '';
  else
    document.myform.format.value = id;
  document.myform.submit();
  return false;
}

function typeSel(id) {
  if (document.myform.type.value == id)
    document.myform.type.value = '';
  else
    document.myform.type.value = id;
  document.myform.submit();
  return false;
}

function basketAdd(pid) {
  $.post( "/basket-add.php", { pid: pid }, function(data){
    if (data == '-1') {
      myAlert('Cette image est déjà dans votre panier.');
      return false;
    }
    myAlert('Image rajoutée à votre panier.');
    $('#basket').html(data);
    return false;
  });
  return false;
}

function basketDel(pid) {
  if (!confirm('Voulez-vous vraiement retirer cette image de votre panier ?'))
    return false;
  $.post( "/basket-del.php", { pid: pid }, function(data){
    if (data == '-1') {
      myAlert('Impossible d\'effectuer cette opération.');
      return false;
    }
    myAlert('Image retirée de votre panier.');
    $('#photo'+pid).remove();
    $('#basket').html(data);
    return false;
  });
  return false;
}

function myAlert(mes) {
  if (alertTimerid) clearTimeout(alertTimerid);
  $('#alert, #balert').remove();
  var len = mes.length * 8;
  var ml = Math.floor(len / 2) * -1;
  if ($('#city').length)
    ml += 108;
  $('body').append('<div id="alert" style="margin-left: '+ml+'px; width: '+len+'px;">'+mes+'</div><div id="balert" style="margin-left: '+ml+'px; width: '+len+'px;">'+mes+'</div>');
  if ($('#content').height() > contentHeight) {
    h = $('#content').height() - contentHeight;
    $('#balert').css('top', (h + balertTop) + 'px');
  }
  alertTimerid = setTimeout("myAlertClose()", 10000);
}

function myAlertClose() {
  $('#alert, #balert').remove();
}

function _lmenuShow(obj, menus) {
  $('div.lmenu').stop();
  // init
  $('div.lmenu').children('div.top').css('visibility', 'visible');
  obj.children('div.top').css('visibility', 'hidden');
  $('div.lmenu').css('height', lmenuLen+'px');
  $('#city').css('top', lmenuTops[1]+'px');
  $('#cat').css('top', lmenuTops[2]+'px');
  $('#reportage').css('top', lmenuTops[3]+'px');
  obj.animate({height: '+='+lmenuMaxLen+'px'}, 600, 'easeOutCirc');
  if (menus)
    $(menus).animate({top: '+='+lmenuMaxLen+'px'}, 600, 'easeOutCirc');
}

function _lmenuHide(obj) {
  $('div.lmenu').stop();
  obj.animate({height: lmenuLen+'px'}, 200, 'easeOutCirc', function(){
    obj.children('div.top').css('visibility', 'visible');
  });
}

function dptShow() {
  _lmenuShow($(this), '#city, #cat, #reportage');
}

function dptHide() {
  _lmenuHide($(this));
  $('#city').animate({top: lmenuTops[1]+'px'}, 200, 'easeOutCirc');
  $('#cat').animate({top: lmenuTops[2]+'px'}, 200, 'easeOutCirc');
  $('#reportage').animate({top: lmenuTops[3]+'px'}, 200, 'easeOutCirc');
}

function villeShow() {
  _lmenuShow($(this), '#cat, #reportage');
}

function villeHide() {
  _lmenuHide($(this));
  $('#cat').animate({top: lmenuTops[2]+'px'}, 200, 'easeOutCirc');
  $('#reportage').animate({top: lmenuTops[3]+'px'}, 200, 'easeOutCirc');
}

function catShow() {
  _lmenuShow($(this), '#reportage');
}

function catHide() {
  _lmenuHide($(this));
  $('#reportage').animate({top: lmenuTops[3]+'px'}, 200, 'easeOutCirc');
}

function repShow() {
  _lmenuShow($(this));
}

function repHide() {
  _lmenuHide($(this));
}

function lmenuReset() {
  if ($(this).parent().attr('id') == 'reportage') {
    window.location.href = window.location.href.substr(0, window.location.href.lastIndexOf('/'));
    return false;
  }
  document.myform[$(this).parent().attr('id')].value = '';
  document.myform.submit();
  return false;
}

function _searchSet(id) {
  document.myform.start.value = '0';
  if (id.match(/^d/)) {
    document.myform.serie.value = '';
    document.myform.dpt.value = id.replace('d', '');
  }
  if (id.match(/^v/)) {
    document.myform.serie.value = '';
    document.myform.city.value = id.replace('v', '');
  }
  if (id.match(/^c/)) {
    document.myform.serie.value = '';
    document.myform.cat.value = id.replace('c', '');
  }
  if (id.match(/^f/)) {
    document.myform.format.value = id.replace('f', '');
  }
  if (id.match(/^s/)) {
    document.myform.serie.value = id.replace('s', '');
  }
  document.myform.submit();
  return false;
}

function searchSet() {
  var id = $(this).attr('id');
  if (!id) return true;
  return _searchSet(id);
}

function initPhotos() {
  var h = $('#photo').height();
  if (h > contentHeight) {
    $('#content').css('height', h + 'px');
    $('#bnav').css('top', (bnavTop + h) + 'px');
    $('#footer').css('top', (footerTop + h) + 'px');
    return;
  }
  var width = $('#content').width();
  var w = 0; var nbl = 1;
  $('div.photo').each(function(){
    var cw = $(this).width() + photoMargin;
    if (w + cw > nbl * width) {
      w = nbl * width + cw;
      nbl ++;
    }
    else w += cw;
  });
  if (!w) return;
  if (photoHeight * nbl > contentHeight) {
    $('#content').css('height', (photoHeight * nbl) + 'px');
    $('#bnav').css('top', (bnavTop + photoHeight * nbl) + 'px');
    $('#footer').css('top', (footerTop + photoHeight * nbl) + 'px');
  }
  for (i = 0; i < nbl; i ++) {
    $('#content').append('<div class="csep" style="top: '+(photoHeight * i + tnHeight)+'px;"> </div>');
    $('#content').append('<div class="csep" style="top: '+(photoHeight * i + photoHeight - 1)+'px;"> </div>');
  }
  $('div.csep:last').remove();
}

function checkTableSize(id) {
  if (!$('div.content table.'+id).length)
    return;
  if ($('div.content table.'+id).height() <= 260)
    return;
  $('div.content').css('height', (contentHeight + $('div.content table.'+id).height() - 260) + 'px');
  $('#footer').css('top', (footerTop + contentHeight + $('div.content table.'+id).height() - 260) + 'px');
}

function checkTextSize() {
  if (!$('#text').length)
    return;
  var h = 0;
  $('#text p').each(function(){ h += $(this).height() + 15; });
  $('#text h5').each(function(){ h += $(this).height() + 15; });
  $('#text table').each(function(){ h += $(this).height() + 25; });
  if (h <= 280)
    return;
  $('div.content').css('height', (contentHeight + h - 280) + 'px');
  $('#footer').css('top', (footerTop + contentHeight + h - 280) + 'px');
}

function illusSlideStart() {
  if (nbIllus <= 1) return false;
  $('#content').append(files[0]);
  $('#il2').css({zIndex: '50'});
  illusSlideWait();
  curIllus = 1;
}

function illusSlideWait() {
  var nextIllus = curIllus + 2;
  if (nextIllus > nbIllus) nextIllus = 2;
  if (!$('#il'+nextIllus).length)
    $('#content').append(files[nextIllus - 2]);
  if (illusTimerid) clearTimeout(illusTimerid);
  illusTimerid = setTimeout("illusSlideNext();", 2000);
}

function illusSlideNext() {
  var nextIllus = curIllus + 1;
  if (nextIllus > nbIllus) nextIllus = 1;
  $('#legend').empty();
  $('#il'+curIllus).animate({opacity: '0'}, 2000, '', function(){
    $('#il'+curIllus).css({zIndex: '10', opacity: '1'});
    $('#il'+nextIllus).css({zIndex: '100'});
    curIllus = nextIllus;
    nextIllus = curIllus + 1;
    if (nextIllus > nbIllus) nextIllus = 1;
    $('#il'+nextIllus).css({zIndex: '50'});
    $('#legend').html($('#il'+curIllus+' img').attr('alt'));
    illusSlideWait();
  });
}

function _panoramiqueSlideStart() {
  var q = $('#details img').width() - maxImgWidth;
  $('#details img').animate({marginLeft: '-'+q+'px'}, 5000, '', function(){
    panoramiqueSlideNext();
  });
}

function panoramiqueSlideStart() {
  if (illusTimerid) clearTimeout(illusTimerid);
  illusTimerid = setTimeout("_panoramiqueSlideStart();", 2000);
}

function _panoramiqueSlideNext() {
  $('#details img').animate({marginLeft: '0px'}, 5000, '', function(){
    panoramiqueSlideStart();
  });
}

function panoramiqueSlideNext() {
  if (illusTimerid) clearTimeout(illusTimerid);
  illusTimerid = setTimeout("_panoramiqueSlideNext();", 2000);
}

function panoramiqueCheck() {
  if ($('#details img').width() <= maxImgWidth)
    return false;
  $('#details div.img').css({width: maxImgWidth+'px', overflow: 'hidden', cursor: 'pointer'});
  $('#details div.img').click(panoramiqueZoom);
  $('#details img').css({marginLeft: '0px'});
  return panoramiqueSlideStart();
}

function panoramiqueZoom() {
  $('body').append('<div id="zoom"></div>');
  var ml = Math.floor($('#details img').width() / 2);
  var mt = Math.floor($('#details img').height() / 2);
  $('#zoom').css({width: $('#details img').width()+'px', height: $('#details img').height()+'px', opacity: '0',
	marginLeft: '-'+ml+'px', marginTop: '-'+mt+'px'});
  $('#zoom').append($('#details div.img').html());
  $('#zoom img').css('marginLeft', '0px');
  $('#zoom').click(panoramiqueZoomClose);
  $('#zoom').animate({opacity: '1'}, 1000);
}

function panoramiqueZoomClose() {
  $('#zoom').remove();
}

function imgOver() {
  if ($(this).attr('src').match('-sel'))
    $(this).attr('src', $(this).attr('src').replace('-sel', ''));
  $(this).attr('src', $(this).attr('src').replace('.png', '-sel.png'));
}

function imgOut() {
  if (!$(this).attr('src').match('-sel'))
    $(this).attr('src', $(this).attr('src').replace('.png', '-sel.png'));
  $(this).attr('src', $(this).attr('src').replace('-sel', ''));
}

function gotoImage() {
  document.myform.action = $(this).attr('href');
  document.myform.submit();
  return false;
}

function gotoBack() {
  document.myform.submit();
  return false;
}








function userFormSubmit() {
  f = document.regform;
/*  if (!f.societe.value) {
    alert("Merci de bien vouloir indiquer le nom de votre organisme.");
    return false;
  }*/
  s = f.societe.value;
/*  if (!f.secteur.selectedIndex) {
    alert("Merci de bien vouloir indiquer le secteur d'activité de votre organisme.");
    return false;
  }*/
  si = f.siret.value;
  se = f.secteur.options[f.secteur.selectedIndex].value;
  if (!f.adresse.value) {
    alert("Merci de bien vouloir indiquer l'adresse de votre organisme.");
    return false;
  }
  a = f.adresse.value;
  if (!f.cp.value) {
    alert("Merci de bien vouloir indiquer le code postal du siège social de votre organisme.");
    return false;
  }
  c = f.cp.value;
  if (!f.ville.value) {
    alert("Merci de bien vouloir indiquer la ville du siège social de votre organisme.");
    return false;
  }
  v = f.ville.value;
  if (!f.prenom.value) {
    alert("Merci de bien vouloir indiquer votre prénom.");
    return false;
  }
  pay = f.pays.value;
/*  if (!f.pays.value) {
    alert("Merci de bien vouloir indiquer le pays du siège social de votre organisme.");
    return false;
  }*/
  p = f.prenom.value;
  if (!f.nom.value) {
    alert("Merci de bien vouloir indiquer votre nom.");
    return false;
  }
  n = f.nom.value;
  if (!f.tel.value) {
    alert("Merci de bien vouloir indiquer votre numéro de téléphone.");
    return false;
  }
  t = f.tel.value;
  if (!f.email.value) {
    alert("Merci de bien vouloir indiquer votre adresse email.");
    return false;
  }
  if (!f.econfirm.value) {
    alert("Merci de bien vouloir confirmer votre adresse email.");
    return false;
  }
  if (f.econfirm.value != f.email.value) {
    alert("Les deux adresses email que vous avez indiquées sont différentes. Merci de bien vouloir corriger votre saisie.");
    return false;
  }
  e = f.email.value;
  if (!f.passe.value) {
    alert("Merci de bien vouloir choisir un mot de passe.");
    return false;
  }
  if (f.passe.value.length < 5) {
    alert("Votre mot de passe doit comporter au moins 5 caractères. Merci de bien vouloir corriger votre saisie.");
    return false;
  }
  if (!f.pconfirm.value) {
    alert("Merci de bien vouloir confirmer votre mot de passe.");
    return false;
  }
  if (f.pconfirm.value != f.passe.value) {
    alert("Les deux mots de passe que vous avez indiqués sont différents. Merci de bien vouloir corriger votre saisie.");
    return false;
  }
  pa = f.passe.value;
  if (f.cg && !f.cg.checked) {
    alert("Merci de bien vouloir lire et accepter les conditions générales.");
    return false;
  }
  ser = f.service.value;
  i = 0;
  if (f.id && f.id.value)
    i = f.id.value;
  r = f.redirect.value;

  $('#form').html('<p class="mes" align="center">Veuillez patienter...</p>');

  if (i) {
    $.post( "/client-edit.php", { societe: s, siret: si, service: ser, secteur: se, adresse: a, cp: c, ville: v, pays: pay, prenom: p, nom: n, tel: t, email: e, passe: pa, redirect: r }, function(data){
      $('#form').html(data);
      return false;
    });
    return false;
  }

  $.post( "/register.php", { societe: s, siret: si, service: ser, secteur: se, adresse: a, cp: c, ville: v, pays: pay, prenom: p, nom: n, tel: t, email: e, passe: pa, redirect: r }, function(data){
    $('#form').html(data);
    return false;
  });
  return false;
}

function adresseFormSubmit(f) {
  if (!f) f = document.adrform;
/*  if (!f.societe.value) {
    myAlert("Merci d'indiquer le nom de la société.");
    return false;
  }*/
  s = f.societe.value;
/*  if (!f.secteur.selectedIndex) {
    myAlert("Merci d'indiquer le secteur d'activité de la société.");
    return false;
  }*/
  si = f.siret.value;
  se = f.secteur.options[f.secteur.selectedIndex].value;
  if (!f.adresse.value) {
    myAlert("Merci d'indiquer l'adresse de la société.");
    return false;
  }
  a = f.adresse.value;
  if (!f.cp.value) {
    myAlert("Merci d'indiquer le code postal de la société.");
    return false;
  }
  c = f.cp.value;
  if (!f.ville.value) {
    myAlert("Merci d'indiquer la ville de la société.");
    return false;
  }
  v = f.ville.value;
  pay = f.pays.value;
  if (!f.pays.value) {
    myAlert("Merci d'indiquer le pays de la société.");
    return false;
  }
  i = 0;
  if (f.id && f.id.value)
    i = f.id.value;

  $('#form').html('<p class="mes" align="center">Veuillez patienter...</p>');

  $.post( "/adresse-edit.php", { societe: s, siret: si, secteur: se, adresse: a, cp: c, ville: v, pays: pay, id: i }, function(data){
    var aid = data.substring(data.lastIndexOf('<') + 4, data.lastIndexOf('-') - 1);
    document.cform.aid.value = aid;
    document.cform.submit();
    return false;
  });
  return false;
}

function connect() {
  if (!document.logform.user_email.value) {
    myAlert('Merci de bien vouloir indiquer votre adresse email.');
    return false;
  }
  if (!document.logform.user_passe.value) {
    myAlert('Merci de bien vouloir indiquer votre mot de passe.');
    return false;
  }
  document.logform.submit();
  return false;
}

function sendPassword() {
  if (!document.logform.user_email.value) {
    myAlert('Merci de bien vouloir indiquer votre adresse email.');
    return false;
  }
  $.post( "/send-password.php", { email: document.logform.user_email.value }, function(data){
    alert(data);
    return false;
  });
  return false;
}

function selectionAdd(pid) {
  $.post( "/selection-add.php", { pid: pid }, function(data){
    if (data == '-1') {
      myAlert('Cette image est déjà dans votre sélection.');
      return false;
    }
    myAlert('Image rajoutée à votre sélection.');
    return false;
  });
  return false;
}

function selectionDel(pid) {
  if (!confirm('Voulez-vous vraiement retirer cette image de votre sélection ?'))
    return false;
  $.post( "/selection-del.php", { pid: pid }, function(data){
    if (data == '-1') {
      myAlert('Impossible d\'effectuer cette opération.');
      return false;
    }
    myAlert('Image retirée de votre sélection.');
    $('#photo'+pid).remove();
    return false;
  });
  return false;
}

