
var siteW = 0;
var siteH = 0;
var curPhoto = 0;
var curSMenu = 0;
var photoTimerid = null;
var photoNb = 0;

var slideDir = 'next';
var sliding = false;


function prepareDocument(sm) {
  curSMenu = sm;
  $('h2.menu').each(function(){
    var id = $(this).attr('id').substr(1);
    $('.smenu'+id).wrapAll('<div id="smenus'+id+'" class="smenus menu"/>');
  });
  $('div.smenus').css({height: '0px', overflow: 'hidden', display: 'none'});
  $('.menu').wrapAll('<div id="menus" class="page"/>');
  $('.legend').wrapAll('<div id="legends" class="page"/>');
  $('.photo').wrapAll('<div id="photos" class="content"/>');
  $('#photos').append('<div id="next"/><div id="prev"/>');
  photoNb = $('#photos p').length;
  $('.content').wrapAll('<div id="content" class="page"/>');
  $('.page').wrapAll('<div id="page"/>');
  $('#page').append('<div id="filet"/><div id="ccache"/>');
  $('#ccache').css('opacity', '0');
  if ($('h2.sel').length)
    smenuOpen($('h2.sel').attr('id').substr(1));
  $('body').append('<div id="cache"/>');
  resizeDocument();
  $(window).resize(resizeDocument);
  $('h2').click(smenuSlide);
  $('h3').click(menuLoad);
  $('#ccache, #photos').mouseover(navShow);
//  $('#next, #prev').live('mouseout', navHide);
//  $('h1, #menus, #legends').mouseover(navHideAll);
  $('#photos, #page').mouseout(navHideAll);
  $('#next').live('click', slideNext);
  $('#prev').live('click', slidePrev);
  $('p.more').live('click', moreShow);
}

function resizeDocument() {
  var h = $(window).height();
  var w = $(window).width();
  // main sizes
  if (h <= 600)
    siteH = 600;
  else if (h >= 1000)
    siteH = 1000;
  else
    siteH = h - 15;
  siteW = Math.ceil(siteH*785/600);
  if (siteW > w - 30) {
    siteW = w - 30;
    siteH = Math.ceil(siteW*600/785);
  }
  $('#page').css({height: siteH+'px', width: siteW+'px'});
  $('#menus').height(siteH-117);
  // main positions
  if (siteH < h)
    $('#page').css('top', Math.floor((h - siteH)/2)+'px');
  else
    $('#page').css('top', '0px');
  if (siteW < w)
    $('#page').css('left', Math.floor((w - siteW)/2)+'px');
  else
    $('#page').css('left', '0px');
  // elements
  $('#content, #ccache').css({height: (siteH-105-32)+'px', width: (siteW-167)+'px'});
  $('iframe.fb').css({top: (siteH-32)+'px'});
  resizePage();
}

function resizePage() {
  $('#photos').css({height: (siteH-105-32-30)+'px', width: (siteW-167-30)+'px'});
  $('#photos img').each(function(){
    var pw = parseInt($(this).attr('w'));
    var ph = parseInt($(this).attr('h'));
    var dh = siteH-105-32-30;
    var dw = Math.ceil(pw*dh/ph);
    if (dw > siteW-167-30) {
      dw = siteW-167-30;
      dh = Math.ceil(ph*dw/pw);
      $(this).width(dw);
      $(this).css('marginTop', Math.floor((siteH-105-32-30-dh)/2)+'px');
    }
    else
      $(this).height(dh);
  });
  $('#legends').width(siteW-167);
  $('#prev').css('backgroundPosition', '15px '+Math.floor((siteH-105-32-30-16)/2)+'px');
  $('#next').css('backgroundPosition', ($('#next').width()-30)+'px '+Math.floor((siteH-105-32-30-16)/2)+'px');
}

function resizePopup() {
  $('#popup').css({height: (siteH-105-32)+'px', width: (siteW-167)+'px'});
  $('#popup p').css({height: (siteH-105-32-30)+'px', width: (siteW-167-30)+'px'});
  $('#popup img').each(function(){
    var pw = parseInt($(this).attr('w'));
    var ph = parseInt($(this).attr('h'));
    var dh = siteH-105-32-30;
    var dw = Math.ceil(pw*dh/ph);
    if (dw > siteW-167-30) {
      dw = siteW-167-30;
      dh = Math.ceil(ph*dw/pw);
      $(this).width(dw);
      $(this).css('marginTop', Math.floor((siteH-105-32-30-dh)/2)+'px');
    }
    else
      $(this).height(dh);
  });
}

function documentPrepared() {
  preloadImages('/images/dust-logo.jpg');
  if ($('#p0').attr('video'))
    $('#p0').html('<iframe src="http://player.vimeo.com/video/'+$('#p0').attr('video')+'?byline=0&amp;portrait=0&amp;color=000000&amp;autoplay=1" width="'+(siteW-167-30)+'" height="'+(siteH-105-32-30)+'" frameborder="0"></iframe>');
  photoLoad(1, curSMenu);
}

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 photoLoad(id, menu) {
  if (menu != curSMenu)
    return false;
  if (id >= photoNb)
    return false;
  if (!$('#p'+id+' img').length)
    return photoLoad(id+1, menu);
  if (!$('#p'+id+' img').attr('src').match('-tn'))
    return photoLoad(id+1, menu);
  $('#p'+id+' img').attr('src', $('#p'+id+' img').attr('src').replace('-tn', ''));
  $('#p'+id+' img').load(function(){
    photoLoad(id+1, menu);
  });
}

function smenuSlide() {
  var id = $(this).attr('id').substr(1);
  if ($(this).hasClass('sel')) {
    if (!$('#smenus'+id+' h3.sel').length)
      return smenuClose(id);
  }
  $('h2.sel').each(function(){
    var i = $(this).attr('id').substr(1);
    if (!$('#smenus'+i+' h3.sel').length)
      smenuClose(i);
  });
  return smenuOpen(id);
}

function smenuOpen(id) {
  $('#cache').css('display', 'block');
  $('#m'+id).addClass('sel');
  $('#smenus'+id).css('display', 'block');
  $('#smenus'+id).stop();
  var h = ($('#smenus'+id+' h3').length + $('#smenus'+id+' p').length) * 17 + 8;
  $('#smenus'+id).animate({height: h+'px'}, 400, '', function(){
    $('#cache').css('display', 'none');
  });
}

function smenuClose(id, open) {
  $('#cache').css('display', 'block');
  $('#m'+id).removeClass('sel');
  $('#smenus'+id).stop();
  $('#smenus'+id).animate({height: '0px'}, 400, '', function(){
    $('#smenus'+id+', #cache').css('display', 'none');
    if (open) smenuOpen(open);
  });
}

function slideWait(quick) {
  if (photoNb <= 1)
    return false;
  if ($('#popup').length)
    return false;
  if (photoTimerid) clearTimeout(photoTimerid);
  if (quick)
    photoTimerid = setTimeout('_slideNext()', 2000);
  else
    photoTimerid = setTimeout('_slideNext()', 4000);
}

function _slideNext(nowait) {
  sliding = true;
  if (photoTimerid) clearTimeout(photoTimerid);
  var n = curPhoto + 1;
  if (slideDir == 'prev') {
    n = curPhoto - 1;
    if (n == -1) n = photoNb - 1;
  }
  else if (n == photoNb) n = 0;

  $('#p'+n).addClass('second');
  $('#l'+n).addClass('second');
  if (jQuery.support.opacity) {
    $('#l'+curPhoto+' p').animate({opacity: '0'}, 1000);
    $('#l'+n+' p').css('opacity', '0');
  }
  else
    $('#l'+curPhoto+' p').css('visibility', 'hidden');

  $('#prev, #next').stop();
  $('#prev, #next').animate({opacity: '0'}, 1000);
  $('#ccache').animate({opacity: '1'}, 1000, '', function(){
    if ($('#p'+curPhoto).attr('video'))
      $('#p'+curPhoto).empty();
    $('#p'+curPhoto).removeClass('first');
    $('#l'+curPhoto).removeClass('first');
    $('#p'+n).addClass('first');
    $('#l'+n).addClass('first');
    $('#p'+n).removeClass('second');
    $('#l'+n).removeClass('second');
    if ($('#p'+n).attr('video'))
      $('#p'+n).html('<iframe src="http://player.vimeo.com/video/'+$('#p'+n).attr('video')+'?byline=0&amp;portrait=0&amp;color=000000&amp;autoplay=1" width="'+(siteW-167-30)+'" height="'+(siteH-105-32-30)+'" frameborder="0"></iframe>');
    if (jQuery.support.opacity) {
      $('#l'+curPhoto+' p').css('opacity', '1');
      $('#l'+n+' p').animate({opacity: '1'}, 1000);
    }
    else
      $('#l'+n+' p').css('visibility', 'visible');
    $('#prev, #next').stop();
    $('#prev, #next').animate({opacity: '1'}, 1000);
    $('#ccache').animate({opacity: '0'}, 1000, '', function(){
      curPhoto = n;
      sliding = false;
      if (!nowait) slideWait();
    });
  });
}

function slideNext() {
  if (sliding) return false;
  slideDir = 'next';
  _slideNext(true);
}

function slidePrev() {
  if (sliding) return false;
  slideDir = 'prev';
  _slideNext(true);
}

function navShow(e) {
  if (photoTimerid) clearTimeout(photoTimerid);
  var o = $('#photos').offset();
  if (e.pageX - o.left < $('#photos').width() / 2)
    $('#prev').css('display', 'block');
  else
    $('#next').css('display', 'block');
}

function navHide() {
  $(this).css('display', 'none');
  slideWait(true);
}

function navHideAll() {
  $('#prev, #next').css('display', 'none');
  slideWait(true);
}

function menuLoad() {
  var id = parseInt($(this).attr('id').substr(1));
  if (id == curSMenu) return false;
  curSMenu = id;
  $('#cache').css('display', 'block');
  $('h3.sel').removeClass('sel');
  $(this).addClass('sel');
  $('h2.sel').each(function(){
    var i = $(this).attr('id').substr(1);
    if (!$('#smenus'+i+' h3.sel').length)
      smenuClose(i);
  });
  if (photoTimerid) clearTimeout(photoTimerid);
  $('#legends p').stop();
  $('#ccache').stop();
  $('#ccache').addClass('loading');
  $('#ccache').animate({opacity: '1'}, 400, '', function(){
    _menuLoad();
  });
  return false;
}

function _menuLoad() {
  $('#photos, #legends').empty();
  $('#photos').append('<div id="next"/><div id="prev"/>');
  if (!menuImages[curSMenu].length)
    return false;
  var i = 0;
  // first
  var arr = menuImages[curSMenu][0].split('|');
  if (arr[1] == 'video')
    $('#photos').append('<p id="p0" class="photo first" video="'+arr[0]+'"></p>');
  else
    $('#photos').append('<p id="p0" class="photo first"><img src="'+arr[0]+'" w="'+
	arr[1]+'" h="'+arr[2]+'"/></p>');
  var myclass = '';
  if (curSMenu == 15) myclass = ' legenddust';
  var leg = '<div id="l0" class="legend first'+myclass+'"><p>'+arr[3].replace('$', "'")+'</p>';
  if (arr[4].length) leg += '<p><a href="http://'+arr[4]+'" target="_blank">'+arr[4]+'</a></p>';
  // more
  if (arr.length > 5) {
    leg += '<p class="more" id="q0"';
    if (arr[7] == 'video')
      leg += ' video="'+arr[6]+'"';
    else
      leg += ' photo="'+arr[6]+'" photoW="'+arr[7]+'" photoH="'+arr[8]+'"';
    leg += '>'+arr[5]+'</p>';
  }
  $('#legends').append(leg+'</div>');
  // next
  myclass = ' second';
  for (i = 1; i < menuImages[curSMenu].length; i++) {
    arr = menuImages[curSMenu][i].split('|');
    if (arr[1] == 'video')
      $('#photos').append('<p id="p'+i+'" class="photo'+myclass+'" video="'+arr[0]+'"></p>');
    else
      $('#photos').append('<p id="p'+i+'" class="photo'+myclass+'"><img src="'+
	arr[0].replace('.jpg', '-tn.jpg')+'" w="'+arr[1]+'" h="'+arr[2]+'"/></p>');
    if (curSMenu == 15) myclass += ' legenddust';
    leg = '<div id="l'+i+'" class="legend'+myclass+'"><p>'+arr[3].replace('$', "'")+'</p>';
    if (arr[4].length) leg += '<p><a href="http://'+arr[4]+'" target="_blank">'+arr[4]+'</a></p>';
    if (arr.length > 5) {
      leg += '<p class="more" id="q'+i+'"';
      if (arr[7] == 'video')
        leg += ' video="'+arr[6]+'"';
      else
        leg += ' photo="'+arr[6]+'" photoW="'+arr[7]+'" photoH="'+arr[8]+'"';
      leg += '>'+arr[5]+'</p>';
    }
    $('#legends').append(leg+'</div>');
    myclass = '';
  }
  photoNb = $('#photos p').length;
  resizePage();
  curPhoto = 0;
  photoLoad(1, curSMenu);
  $('#ccache').removeClass('loading');
  $('#ccache').animate({opacity: '0'}, 400, '', function(){
    $('#cache').css('display', 'none');
    slideWait();
  });
}

function moreShow() {
  if ($(this).hasClass('blocked'))
    return false;
  if ($(this).hasClass('close'))
    return moreClose();
  $(this).addClass('blocked');
  if (photoTimerid) clearTimeout(photoTimerid);
  $('#page').append('<div id="popup" pere="'+$(this).attr('id')+'" class="loading"></div>');
  if ($(this).attr('photo')) {
    $('#popup').append('<p><img src="'+$(this).attr('photo')+'" w="'+
	$(this).attr('photoW')+'" h="'+$(this).attr('photoH')+'"/></p>');
    resizePopup();
    _moreShow();
  }
  else {
      $('#popup').html('<p><iframe src="http://player.vimeo.com/video/'+$(this).attr('video')+'?byline=0&amp;portrait=0&amp;color=000000&amp;autoplay=1" width="'+(siteW-167-30)+'" height="'+(siteH-105-32-30)+'" frameborder="0"></iframe></p>');
    resizePopup();
    _moreShow();
  }
}

function _moreShow() {
  if (photoTimerid) clearTimeout(photoTimerid);
  $('#popup').removeClass('loading');
  var obj = '#'+$('#popup').attr('pere');
  $(obj).removeClass('blocked');
  $(obj).attr('label', $(obj).html());
  $(obj).html('Revenir au diaporama');
  $(obj).addClass('close');
}

function moreClose() {
  var obj = '#'+$('#popup').attr('pere');
  $(obj).html($(obj).attr('label'));
  $(obj).removeClass('close');
  $('#popup').remove();
  slideWait(true);
}


