
//var photoRatio = 0.71509434;
var photoRatio = 0.728571429;
var photoWidth = 0;
var photoHeight = 400;
var pagesHeight = 10;

var listeNbPhotosPerPage = 15;
var listeNbPhotosPerLine = 5;

var listeCurPage = 1;
var listePreviousPage = 1;

var listeTimerid = null;


function listePrepare() {
  $('body').append('<div id="cache1" class="cache"><div><img src="/images/loading.gif" alt="Hourra Models Paris" /></div></div>');
  listeCurId = $('div.liste:first').attr('id').substr(5);
  if (listeCurId != 152)
    $('#sm1').css('display', 'block');
  buildListe(listeCurId);
  if (!$('#liste'+listeCurId+' img').length) {
    $('#cache1').fadeOut(400, function(){
      $('#cache1').remove();
    });
    return false;
  }
  $('#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) - 20;
  contentHeight = 3 * (photoHeight + 20);
  photoWidth = Math.ceil(photoHeight * photoRatio);

  var o = $('#header h2:last').offset();
  var p = $('#footer').offset();
  var l = Math.floor(siteWidth * menusRatio);
  var m = 5 * photoWidth + 4*20 + 5;

  if (m + o.left > w) {
    var eps = (m + o.left - w) + Math.floor((w - m - 138 - 63)/2);
    $('#header h2:first').css('marginRight', 
	eps+parseInt($('#header h2:first').css('marginRight').replace('px', ''))+'px');
    $('#footer h3:first').css('marginRight', 
	eps+parseInt($('#footer h3:first').css('marginRight').replace('px', ''))+'px');
    o.left -= eps;
  }
  else {
    var eps = Math.floor((w - m - 138 - 63)/2);
    var k = o.left - eps - 138 - 63;
    $('#header h2:first').css('marginRight', 
	k+parseInt($('#header h2:first').css('marginRight').replace('px', ''))+'px');
    $('#footer h3:first').css('marginRight', 
	k+parseInt($('#footer h3:first').css('marginRight').replace('px', ''))+'px');
    o.left = eps + 138 + 63;
  }

  $('h1').css('left', (o.left - 138 - 63)+'px');
  $('#content').css({top: '100px', left: '0px', width: w+'px', 
	height: contentHeight+'px'});
  $('div.liste').css({left: o.left+'px', height: contentHeight+'px', width: '3000px'});
  $('div.liste img').height(photoHeight);
  $('div.liste p.people').width(photoWidth);

  // liste 2
  $('div.liste2').css({top: (100+103+39)+'px', left: (o.left - 138 - 63)+'px', 
	height: (contentHeight - 103 - 39)+'px'});
  $('div.liste2 div.lcont').height(contentHeight - 103 - 39);
  $('div.pages').css({left: o.left+'px', width: '500px', top: p.top+'px'});

  listeCurPage = 1;
  $('div.liste div.inner').css({top: '0px'});
  $('div.pages p').removeClass('sel');
  $('div.pages p.p1').addClass('sel');

  $('div.liste2 div.lcont').jScrollPane({showArrows: true});
}

function buildListe(id) {
  $('#liste'+id).html('<div class="inner">'+$('#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>');
  $('#pages'+id).append('<p class="p1">1</p>');
  for (i = 2; i <= nbpages; i++)
    $('#pages'+id).append('<p class="sep">-</p><p class="p'+i+'">'+i+'</p>');
  $('#pages'+id+' p.p1').addClass('sel');
}

function gotoListe() {
  $('#modelname').css('display', 'none');
  _smenuHide('m1');
  $('#m1').addClass('sel');
  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() {
  $('#modelname').css('display', 'none');
  if (sitePreviousPage == 'home') {
    $('#logo').css('display', 'none');
    $('#content img.home').animate({top: '-'+($('#content img.home').height()+5)+'px'}, 400);
  }
  else if (sitePreviousPage == 'fiche') {
    $('#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 == 152)
	$('#ldetails152, #liste152').remove();
    });
  }
  $('#ldetails'+listeCurId+', #liste'+listeCurId).animate({top: '0px'}, 400, '', function(){
    $('h1').css('display', 'block');
    $('#cache2').remove();
  });
  if (listeCurId == 152)
    $('#m152').addClass('sel');
  else
    $('#m1').addClass('sel');
  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 + 20);
  if (nbphotos < id * listeNbPhotosPerPage) {
    var nbp = nbphotos - (id - 1) * listeNbPhotosPerPage;
    var nbl = Math.ceil(nbp / listeNbPhotosPerLine);
    len -= (3 - nbl) * (photoHeight + 20);
  }
  $('#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 == 152))
    return false;
  return _gotoListe(152);
}

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, '', function(){
    $('h1').css('display', 'block');
  });
  $('#liste2'+id).animate({top: (headerHeight + contentHeight)+'px', height: '0px'}, 300, '', function(){
    $('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');
}

