
//var photoRatio = 0.71509434;
var photoRatio = 0.728571429;
var photoWidth = 0;
var photoHeight = 420;
var pagesHeight = 10;

var listeNbPhotosPerPage = 15;
var listeNbPhotosPerLine = 5;

var listeCurPage = 1;
var listePreviousPage = 1;

var listeTimerid = null;


function listePrepare() {
  listeCurId = $('div.liste:first').attr('id').substr(5);
  buildListe(listeCurId);
  $('#liste'+listeCurId+' img:last').load(function(){
    $('#cache1').fadeOut(400, function(){
      $('#cache1').remove();
    });
  });
}

function listeInitSizes(w, h) {
  detailsWidth = Math.ceil(siteWidth * greyRatio);
  photoHeight = Math.floor(contentHeight / 3);
  photoWidth = Math.ceil(photoHeight * photoRatio);

  var i = photoWidth * 5 + 10;
  if (i > siteWidth - detailsWidth)
    detailsWidth = siteWidth - i;

  if (contentHeight > 3 * photoHeight)
    contentHeight = 3 * photoHeight;

  $('h1').css('marginLeft', '-'+Math.ceil(siteWidth / 2 - 30)+'px');
  $('#content').css({width: siteWidth+'px', height: contentHeight+'px', marginLeft: '-'+Math.ceil(siteWidth / 2)+'px'});
  $('div.ldetails').css({width: detailsWidth+'px', height: contentHeight+'px'});
  $('div.liste img').height(photoHeight);
  $('div.liste').css({left: detailsWidth+'px', height: contentHeight+'px', width: i+'px'});
  $('div.liste div').width(i);
  $('div.pages').css({top: Math.floor(headerHeight+contentHeight+footerHeight/2-pagesHeight/2)+'px', 
	marginLeft: Math.ceil(0 - siteWidth / 2 + detailsWidth)+'px', width: Math.ceil(i/2)+'px'});

  listeCurPage = 1;
  $('div.liste div').css({top: '0px'});
  $('div.pages p').removeClass('sel');
  $('div.pages p.p1').addClass('sel');

  // liste 2
  $('div.liste2').css({top: (headerHeight + contentHeight)+'px', left: '50%', 
	marginLeft: '-'+Math.ceil(siteWidth / 2)+'px'});
}

function buildListe(id) {
  $('#liste'+id).html('<div>'+$('#liste'+id).html()+'</div>');
  var nbpages = Math.ceil($('#liste'+id+' img').length / listeNbPhotosPerPage);
  if (nbpages <= 1) return;
  $('body').append('<div id="pages'+id+'" class="pages"></div>');
  for (i = 1; i <= nbpages; i++)
    $('#pages'+id).append('<p class="p'+i+'">'+i+'</p>');
  $('#pages'+id+' p.p1').addClass('sel');
  pagesHeight = $('#pages'+id).height();
}

function gotoListe() {
  _smenuHide('m1');
  var id = $(this).attr('id').substr(3);
  if ((siteCurPage == 'liste') && (listeCurId == id))
    return false;
  return _gotoListe(id);
}

function backToListe() {
  return _gotoListe(listeCurId);
}

function _gotoListe(id) {
  sitePreviousPage = siteCurPage;
  siteCurPage = 'liste';
  listePreviousId = listeCurId;
  listeCurId = id;

  if ($('#liste'+id).length) {
    $('#pages'+id).css('display', 'block');
    initSizes();
    $('#ldetails'+id+', #liste'+id).css('top', contentHeight+'px');
    return gotoListeTerminate();
  }

  $('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",
    cache: false,
    url: '/templates/liste.php',
    data: 'id='+id+'&lang='+siteLang,
    success: function(data){
	$('#content').append(data);
	buildListe(id);
	initSizes();
	$('#ldetails'+id+', #liste'+id).css('top', contentHeight+'px');
	$('#cache2 div').css({top: (2*headerHeight+contentHeight-38)+'px'});
	if (!$('#liste'+id+' img').length)
	  return gotoListeTerminate();
//      if ($.browser.msie)
//	  return gotoListeTerminate();
	$('#liste'+id+' h3 img:last').load(function(){
	  gotoListeTerminate();
	});
    }
  });
  return false;
}

function gotoListeTerminate() {
  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 == 'fiche') {
    $('#back').css('display', 'none');
    $('#fdetails'+ficheCurId+', #photo'+ficheCurId+', #thumbs'+ficheCurId).animate({top: '-'+($('#photo'+ficheCurId).height()+5)+'px'}, 400);
  }
  else if (sitePreviousPage == 'liste') {
    $('#pages'+listePreviousId).css('display', 'none');
    $('#ldetails'+listePreviousId+', #liste'+listePreviousId).animate({top: '-'+contentHeight+'px'}, 400, '', function(){
      if (listePreviousId == 1000)
	$('#ldetails1000, #liste1000').remove();
    });
  }
  $('#ldetails'+listeCurId+', #liste'+listeCurId).animate({top: '0px'}, 400, '', function(){
    $('#cache2').remove();
  });
  return false;
}

function gotoPage() {
  var id = parseInt($(this).attr('class').substr(1));
  var nbphotos = $('#liste'+listeCurId+' img').length;
  if (id == listeCurPage) return;
  var len = (id - 1) * 3 * photoHeight;
  if (nbphotos < id * listeNbPhotosPerPage) {
    var nbp = nbphotos - (id - 1) * listeNbPhotosPerPage;
    var nbl = Math.ceil(nbp / listeNbPhotosPerLine);
    len -= (3 - nbl) * photoHeight;
  }
  $('#liste'+listeCurId+' div').stop();
  $('#liste'+listeCurId+' div').animate({top: '-'+len+'px'}, 200);
  listeCurPage = id;
  $('#pages'+listeCurId+' p').removeClass('sel');
  $('#pages'+listeCurId+' p.p'+id).addClass('sel');
  return false;
}

function gotoFavorites() {
  if ((siteCurPage == 'liste') && (listeCurId == 1000))
    return false;
  return _gotoListe(1000);
}

function liste2Show() {
  var id = $(this).attr('id').substr(2);
  if (listeTimerid) clearTimeout(listeTimerid);
  $('h1').css('display', 'none');
  $(this).animate({bottom: (contentHeight - 22)+'px'}, 400);
  $('#liste2'+id+' div').height(contentHeight - 24);
  $('#liste2'+id).animate({top: (headerHeight + 22)+'px', height: (contentHeight - 22)+'px'}, 400, '', function(){
    $('#liste2'+id+' div').jScrollPane({showArrows: true});
  });
}

function liste2Stay() {
  if (listeTimerid) clearTimeout(listeTimerid);
}

function liste2Hide() {
  var id = $(this).attr('id').substr(6);
  if (listeTimerid) clearTimeout(listeTimerid);
  listeTimerid = setTimeout('_liste2Hide('+id+')', 700);
}

function _liste2Hide(id) {
  $('#pl'+id).animate({bottom: '0px'}, 300);
  $('#liste2'+id).animate({top: (headerHeight + contentHeight)+'px', height: '0px'}, 300);
  $('h1').css('display', 'block');
}

function liste2Cancel() {
  if (listeTimerid) clearTimeout(listeTimerid);
  $('div.ldetails p.liste').css({bottom: '0px'});
  $('div.liste2').css({top: (headerHeight + contentHeight)+'px', height: '0px'});
  $('h1').css('display', 'block');
}
