
var nbVignettes = 0;
var curVignette = 1;

var menuOpened = false;
var menuTimerid = null;
var menuWidth = 554;
var smenuHeight = 16;
var smenuTopItem = 'marginTop';

var mousePressed = false;
var mouseTimerid = null;

var photoWidth = 675;
var navWidth = 169;
var currentPage = 1;
var nbPages = 0;
var currentPhoto = 0;

var photos = null;


function completeDocument() {
  $('#smenus').html('<div id="sminner">'+$('#smenus').html()+'</div>');
  $('#photos').html('<div id="pinner">'+$('#photos').html()+'</div>');
  if ($('#photos img').attr('photos'))
    photos = $('#photos img').attr('photos').split('|');
  else
    $('#photos img').css('marginBottom', '0px');
  resizeDocument();
  $(window).load(animateHome);
  $('#portfolio, #menu, #menus').hover(menuShow, menuHide);
  $('#portfolio img, #menus img').hover(imgOver, imgOut);

  // default
  $('#menu').css('width', $('#menu img').width());

  prepareSlideshow();

  $(window).resize(resizeDocument);
}

function resizeDocument() {
  var w = $(window).width();
  var h = $(window).height();
  var sh = h - 191 - 10;
  if (sh < 374) sh = 374;
  $('#smenus, #sminner').height(sh);
  if ($('#sminner').length) {
    $('#sminner').jScrollPane({scrollbarWidth: 8, showArrows: false, 
	dragMinHeight: 35, dragMaxHeight: 35, animateTo: true});
    if ($('#sminner a.sel').length) {
      var i = $('#sminner a').index($('#sminner a.sel'));
      if ((i-1)*18 > sh)
        $('#sminner')[0].scrollTo((i-1)*18);
    }
  }
  var ih = h - 150 - 10;
  if (ih < 440) ih = 440;
  $('#photos, #pinner').height(ih);
  $('#pinner img').each(function(){
    if (parseInt($(this).attr('myheight')) <= ih)
      $(this).css('height', 'auto');
    if (parseInt($(this).attr('myheight')) > ih)
      $(this).height(ih);
  });
  if ($('#pinner').length)
    $('#pinner').jScrollPane({scrollbarWidth: 8, showArrows: false, 
	dragMinHeight: 35, dragMaxHeight: 35});
}

/* MENU ANIMATION */
function imgOver() {
  if ($(this).attr('class') == 'sel') return;
  $(this).attr('src', $(this).attr('src').replace('.jpg', '-sel.jpg'));
}

function imgOut() {
  if ($(this).attr('class') == 'sel') return;
  $(this).attr('src', $(this).attr('src').replace('-sel', ''));
}

function calcMenuWidth() {
  nb = $('#menus h2').length;
  mw = 0;
  for (i = 0; i < nb; i ++)
    mw += $('#m'+(i+1)+' img').width();
  menuWidth = mw;
  $('#menus div').css('left', '-'+menuWidth+'px');
  $('#menus').css('width', '0px');
}

function menuShow() {
  if (menuTimerid) clearTimeout(menuTimerid);
  if (menuOpened) return;
  menuOpened = true;
  $('#menus').css('width', menuWidth+'px');
  $('#menus div').stop();
  $('#menus div').animate({left: '0px'}, 800, "easeOutExpo");
}

function menuHide() {
  if (!menuOpened) return;
  menuOpened = false;
  if (menuTimerid) clearTimeout(menuTimerid);
  menuTimerid = setTimeout("_menuHide()", 500);
}

function _menuHide() {
  $('#menus div').animate({left: '-'+menuWidth+'px'}, 800, "easeOutExpo", function(){
    $('#menus').css('width', '0px');
  });
}


/* HOME ANIMATION */
function _showVignette() {
  $('#h'+curVignette).animate({opacity: '1'}, 300, '', function(){
    curVignette ++;
    if (curVignette <= nbVignettes)
      _showVignette();
  });
}

function _animateHome() {
  $('#logobig').animate({opacity: '0'}, 600, '', function(){
    $('#logobig').remove();
    menuShow();
    setTimeout("_showVignette()", 850);
  });
  $('#banner img').animate({opacity: '1'}, 600);
}

function animateHome() {
  calcMenuWidth();
  if ($('#logobig').length) {
    nbVignettes = $('div.home img').length;
    setTimeout("_animateHome()", 1000);
  }
  else
    $('#banner img').css({opacity: '1'});
  if (photos)
    photoLoad(0);
}

function photoLoad(i) {
  if (i >= photos.length)
    return false;
  $('#pinner').append('<br/><img src="'+photos[i]+'"/>');
  $('#pinner img:last').load(function(){
    if ($(this).height() > $('#photos').height())
      $(this).height($('#photos').height());
    $('#pinner').jScrollPane({scrollbarWidth: 8, showArrows: false, 
	dragMinHeight: 35, dragMaxHeight: 35});
    photoLoad(i+1);
  });
}

function numOver() {
  if ($(this).children('p').attr('class') == 'sel') return;
  $(this).css('color', 'black');
}

function numOut() {
  if ($(this).children('p').attr('class') == 'sel') return;
  $(this).css('color', 'white');
}

function mouseReleased() {
  if (mouseTimerid) clearTimeout(mouseTimerid);
  mousePressed = false;
}

function mouseWait(func) {
  if (mouseTimerid) clearTimeout(mouseTimerid);
  mouseTimerid = setTimeout(func+"()", 100);
}

function _smenusSlideUp(mwheel) {
  mousePressed = true;
  i = parseInt(($('#smenus div').css(smenuTopItem)).replace("px", ""));   if (i == 0) return;

  if ($('#down').css('display') == 'none')
    $('#down').css('display', 'block');

  i += 16;
  if (i > 0) i = 0;
  if (i == 0) $('#up').css('display', 'none');
  $('#smenus div').css(smenuTopItem, i+'px');
  if (!mwheel && mousePressed) mouseWait('smenusSlideUp');
}

function _smenusSlideDown(mwheel) {
  mousePressed = true;
  i = parseInt(($('#smenus div').css(smenuTopItem)).replace("px", ""));
  l = 0 - $('#smenus div').height() + $('#smenus').height() - 16;

  if (i == l) return;

  if ($('#up').css('display') == 'none')
    $('#up').css('display', 'block');

  i -= 16;
  if (i < l) i = l;
  if (i == l) $('#down').css('display', 'none');
  $('#smenus div').css(smenuTopItem, i+'px');
  if (!mwheel && mousePressed) mouseWait('smenusSlideDown');
}

function smenusSlideUp() {
  _smenusSlideUp(false);
}

function smenusSlideDown() {
  _smenusSlideDown(false);
}

function smenusMWheel(e, d) {
  if (d < 0) _smenusSlideDown(true);
  else _smenusSlideUp(true);
}

function prepareSlideshow() {
  $('div.photo').css('left', photoWidth+'px');
  $('#photo1').css('left', '0');
  $('#leg').html($('#photo1').children('img').attr('alt'));
  $('div.num, div.num2').click(loadPhoto);

  nbPages = $('#nav div.page').length;
  if (nbPages <= 1) {
    $('#next').remove(); $('#previous').remove();
    return;
  }
  $('div.page').css('left', navWidth+'px');
  $('#page1').css('left', '0px');

  $('#next img, #previous img').hover(imgOver, imgOut);
  $('#next').click(nextPage);
  $('#previous').click(previousPage);
  $('#previous').css('display', 'none');
}

function nextPage() {
  nextPage = currentPage + 1;
  $('#page'+currentPage).animate({left: '-'+navWidth+'px'}, 1000);
  $('#page'+nextPage).animate({left: '0'}, 1000);
  if (currentPage == 1)
    $('#previous').css('display', 'block');
  currentPage = nextPage;
  if (currentPage == nbPages)
    $('#next').css('display', 'none');
}

function previousPage() {
  prevPage = currentPage - 1;
  $('#page'+currentPage).animate({left: navWidth+'px'}, 1000);
  $('#page'+prevPage).animate({left: '0'}, 1000);
  if (currentPage == nbPages)
    $('#next').css('display', 'block');
  currentPage = prevPage;
  if (currentPage == 1)
    $('#previous').css('display', 'none');
}

function loadPhoto() {
  nextPhoto = $(this).children('p').html();
  $('div.num p, div.num2 p').removeClass('sel');
  $('div.num, div.num2').css('color', 'white');
  $(this).children('p').addClass('sel');
  $('#photo'+currentPhoto).animate({opacity: '0'}, 500, '', function(){
    $('#photo'+currentPhoto).css({left: photoWidth+'px', opacity: '1'});
    currentPhoto = nextPhoto;
    $('#photo'+currentPhoto).css('left', '0');
    $('#leg').html($('#photo'+currentPhoto).children('img').attr('alt'));
  });
}

function linkPhotoShow(id) {
  $('#photo'+currentPhoto).css({left: photoWidth+'px'});
  currentPhoto = id;
  $('#photo'+currentPhoto).css('left', '0');
}

