
var nbPhotos = 0;
var siteTimerid = null;
var photosTimerid = null;
var curMenu = 1;
var curSMenu = 0;
var contentHeight = 0;
var contentExpanded = false;

var baseUrl = 0;
var currentUrl = 0;


function prepareDocument(mid, smid) {
  curMenu = mid;
  curSMenu = smid;
  $('.smenu').wrapAll('<div id="smenu" class="page"/>');
  $('.menu').wrapAll('<div id="menu" class="page"/>');
  if ($('#smenu').length)
    $('#menu').addClass('level2');
  $('.details').wrapAll('<div id="details" class="page"/>');
  $('.content').wrapAll('<div id="cinner"/>');
  $('#cinner').wrap('<div id="content" class="page"/>');
  $('#content').height(0);
  $('.pdf').wrapAll('<div id="pdf" class="page"/>');
  $('.vig').wrapAll('<div id="vinner"/>');
  $('#vinner').wrapAll('<div id="vigs" class="page"/>');
  $('#vigs').css('opacity', '0');
  $('.photo').wrapAll('<div id="photos" class="page"/>');
  if ($('#smenu').length)
    $('#photos, #content').addClass('small');
  $('#photos img:first').addClass('ontop');
  $('#photos img:eq(1)').addClass('next');
  nbPhotos = $('#photos img').length;
  $('.page').wrapAll('<div id="page"/>');
  $('#page').append('<div id="bgtop"/><div id="bgbot"/><div id="cache"/><div class="pinner"/>');
  $('body').append('<div id="cachebg"/>');
  $('#cache').css('opacity', '0');
  if ($('#smenu').length)
    $('#cache').addClass('small');
  // prepare animation
  $('#bgtop').height(270);
  $('#bgbot').height(242);
  $('#page h1').css({top: '259px', left: '338px'});
  resizeDocument();

  $.history.init(function(url) {
    if (!url && !currentUrl)
      return false;
    var id = parseInt(baseUrl);
    if (url) id = url;
    var sid = 0;
    if (parseInt(id) >= 1000) {
      sid = parseInt(id) % 1000;
      id = Math.floor(parseInt(id) / 1000);
    }
    if ((curMenu == id) && (curSMenu == sid))
      return false;
    if (sid == 0)
      return _menuGoto(id, true);
    if (curMenu != id) {
      $('#menu a.sel').removeClass('sel');
      $('#menu'+id).addClass('sel');
      return menuPrepare(id, true, sid);
    }
    return _smenuGoto(sid, true);
  });

  // actions
  $(window).resize(resizeDocument);
  $('#menu h2 a, #page h1 a').click(menuGoto);
  $('#smenu h2 a').live('click', smenuGoto);
  $('#vigs img').live('click', slidePos);
  $('#vigs').live('mousemove', vigsMove);
  $('#pdf p').live('click', pdfGoto);
}

function resizeDocument() {
  var h = $(window).height();
  var w = $(window).width();
  if (h < 690) $('#page').css({top: '10px'});
  else  $('#page').css({top: Math.floor((h - 690) / 2)+'px'});
  if (w < 1060) $('#page').css({left: '0px'});
  else $('#page').css({left: Math.floor((w - 1060) / 2)+'px'});
}

function documentPrepared() {
  if (siteTimerid) clearTimeout(siteTimerid);
  siteTimerid = setTimeout('siteAnimate()', 1000);
  preloadImages('/images/pdf-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 historyPush(id) {
  currentUrl = id;
  $.history.load(id);
}

function siteAnimate() {
  $('#page h1').animate({opacity: '0'}, 600, '', function(){
    $('#page h1').css({top: '0px', left: '610px'});
    $('#bgtop').animate({height: '18px'}, 1000);
    $('#bgbot').animate({height: '38px'}, 1000, '', function(){
      if ($('#content').length)
        contentShow();
      $('#page h1').animate({opacity: '1'}, 600);
      slideStart();
    });
  });
}

function pagePrepare() {
  $('.details').wrapAll('<div id="details" class="page"/>');
  $('.content').wrapAll('<div id="cinner"/>');
  $('#cinner').wrap('<div id="content" class="page"/>');
  var contact = 'contact';
  contact += '@';
  contact += 'oliviermarquet';
  contact += '.com';
  if ($('#cinner').length && $('#cinner').html().match('_ADRESSE_EMAIL_'))
    $('#cinner').html($('#cinner').html().replace('_ADRESSE_EMAIL_', '<a href="mailto:'+contact+'">'+contact+'</a>'));
  $('#content').height(0);
  $('.vig').wrapAll('<div id="vinner"/>');
  $('.pdf').wrap('<div id="pdf" class="page"/>');
  $('#vinner').wrapAll('<div id="vigs" class="page"/>');
  $('#vigs').css('opacity', '0');
  $('.photo').wrapAll('<div id="photos" class="page"/>');
  if ($('#smenu').length)
    $('#photos, #cache, #content').addClass('small');
  else
    $('#photos, #cache, #content').removeClass('small');
  if (!$('#photos img').length) {
    nbPhotos = 0;
    pageAnimate();
  }
//  $('#photos img:first').load(function(){
    $('#photos img:first').addClass('ontop');
    $('#photos img:eq(1)').addClass('next');
    nbPhotos = $('#photos img').length;
    pageAnimate();
//  });
}

function pageAnimate() {
  if ($('#smenu').length && ($('#menu').css('left') != '173px')) {
    $('#smenu').animate({left: '0px'}, 600);
    $('#menu').animate({left: '173px'}, 600);
  }
  $('#cache').animate({opacity: '0'}, 600, '', function(){
    $('#cache, #cachebg').css('display', 'none');
    slideStart();
  });
  if ($('#content').length)
    contentShow();
}

function slideStart() {
  if (nbPhotos <= 1) return false;
  if (photosTimerid) clearTimeout(photosTimerid);
  photosTimerid = setTimeout('slideNext()', 4000);
}

function slideStop() {
  nbPhotos = 0;
  $('#photos').remove();
  if (photosTimerid) clearTimeout(photosTimerid);
}

function slideNext() {
  $('#photos img.ontop').animate({opacity: '0'}, 1500, '', function(){
    var i = $('#photos img.ontop').index();
    $('#photos img.ontop').removeClass('ontop');
    $('#photos img.next').addClass('ontop');
    $('#photos img:eq('+i+')').css('opacity', '1');
    i = $('#photos img.next').index();
    $('#photos img.next').removeClass('next');
    i ++;
    if (i == nbPhotos) i = 0;
    $('#photos img:eq('+i+')').addClass('next');
    slideStart();
  });
}

function slidePos() {
  if (nbPhotos <= 1) return false;
  $('#photos img').stop();
  if (photosTimerid) clearTimeout(photosTimerid);
  $('#photos img').css('opacity', '1');
  $('#photos img.next').removeClass('next');
  var id = $(this).attr('id').substr(3);
  if ($('#pho'+id).hasClass('ontop'))
    return false;
  $('#pho'+id).addClass('next');
  $('#photos img.ontop').animate({opacity: '0'}, 1500, '', function(){
    var i = $('#photos img.ontop').index();
    $('#photos img.ontop').removeClass('ontop');
    $('#pho'+id).addClass('ontop');
    $('#pho'+id).removeClass('next');
    $('#photos img:eq('+i+')').css('opacity', '1');
  });
}

function menuGoto() {
  var id = $(this).attr('id').substr(4);
  return _menuGoto(id);
}

function _menuGoto(id, nohistory) {
  if ((id == curMenu) && (!curSMenu)) return false;
  $('#menu a.sel').removeClass('sel');
  $('#menu'+id).addClass('sel');
  menuPrepare(id);
  curMenu = id;
  curSMenu = 0;
  if (!nohistory)
    historyPush(id);
  return false;  
}

function menuPrepare(id, noload, sid) {
  $('#cachebg').css('display', 'block');
  if ($('#smenu').length) {
    $('#smenu').animate({left: '-173px'}, 600, '', function(){
      $('#smenu').remove();
      menuChilds(id);
    });
    $('#menu').animate({left: '48px'}, 600);
  }
  else
    menuChilds(id);
  $('#cache').css('display', 'block');
  $('#cache').animate({opacity: 1}, 600, '', function(){
    slideStop();
    if (!noload) menuLoad(id);
    else {
      curMenu = id;
      _smenuGoto(sid, true);
    }
  });
  if (!noload) contentHide();
}

function menuLoad(id) {
  $.ajax({
    type: "GET",
    url: '/templates/menu-load.php',
    data: "id="+id,
    success: function(data){
      $('#page').append(data);
      if ($('.photo').length)
	$('.photo:first').load(function(){
	  pagePrepare();
	});
      else
	pagePrepare();
//      if (siteTimerid) clearTimeout(siteTimerid);
//      siteTimerid = setTimeout('pagePrepare()', 300);
    }
  });
}

function menuChilds(id) {
  $.ajax({
    type: "GET",
    url: '/templates/menu-childs.php',
    data: "id="+id,
    success: function(data){
      $('#page').append(data);
      $('.smenu').wrapAll('<div id="smenu" class="page hidden"/>');
      if (curSMenu) $('#smenu'+curSMenu).addClass('sel');
    }
  });
}

function smenuGoto() {
  var id = $(this).attr('id').substr(5);
  return _smenuGoto(id);
}

function _smenuGoto(id, nohistory) {
  if (id == curSMenu) return false;
  $('#smenu a.sel').removeClass('sel');
  $('#smenu'+id).addClass('sel');
  smenuPrepare(id);
  curSMenu = id;
  if (!nohistory)
    historyPush(parseInt(curMenu)*1000+parseInt(id));
  return false;  
}

function smenuPrepare(id) {
  $('#cache, #cachebg').css('display', 'block');
  $('#cache').animate({opacity: 1}, 600, '', function(){
    slideStop();
    smenuLoad(id);
  });
  contentHide();
}

function smenuLoad(id) {
  $.ajax({
    type: "GET",
    url: '/templates/smenu-load.php',
    data: "id="+id,
    success: function(data){
      $('#page').append(data);
      if ($('.photo').length)
	$('.photo:first').load(function(){
	  pagePrepare();
	});
      else
	pagePrepare();
//      if (siteTimerid) clearTimeout(siteTimerid);
//      siteTimerid = setTimeout('pagePrepare()', 300);
    }
  });
}

function vigsMove(e) {
  var w = $('#vinner').width();
  if (w <= 296) return;
  var eps = $('#vigs').offset().left + 5;
  var left = -1 * Math.ceil((e.pageX - eps) * (w + 20 - 296) / 296);
  $('#vinner').css('left', left+'px');
}

function contentShow() {
  contentHeight = $('#content div').height() + 23;
  if (contentExpanded)
    return false;
//  if (contentHeight > contentDefaultH)
  $('#page').animate({height: '+='+contentHeight+'px'}, 600);
  $('#content').animate({height: contentHeight+'px'}, 600);
  $('#vigs').animate({opacity: '1'}, 600);
  contentExpanded = true;
  return false;
}

function contentHide() {
  if (!contentExpanded)
    return false;
//  if (contentHeight > contentDefaultH)
  $('#page').animate({height: '-='+contentHeight+'px'}, 600);
  $('#content').animate({height: '0px'}, 600, '', function(){
    $('#content, #details, #pdf').remove();
  });
  $('#vigs').animate({opacity: '0'}, 600, '', function(){
    $('#vigs').remove();
  });
  contentExpanded = false;
  return false;
}

function pdfGoto() {
  window.location.href = $(this).children('a').first().attr('href');
}


