
var infosMT = 0;
var loading = false;
var currentMenu = 0;
var currentSMenu = 0;
var currentSSMenu = 0;
var baseUrl = 0;

var pageH = 600;
var pageW = 916;
var pageMaxH = 927;
var pageMinH = 500;
var photoH = 353;
var photoW = 692;
var photoX = 1.959808612;
var contentH = 140;
var contentMinH = 140;
var contentMaxH = 220;
var contentX = 0.7225;
var contentMaxW = 550;
var partenX = 0.47;
var parten2X = 0.5;

var photoTimerid = null;
var sizeTimerid = null;

var curPhoto = 0;
var photos = null;


function prepareDocument(curmenu, cursmenu, curssmenu) {
  if (curmenu)
    currentMenu = curmenu;
  if (cursmenu)
    currentSMenu = cursmenu;
  if (curssmenu)
    currentSSMenu = curssmenu;
  $('body').append('<div id="pcache"/>');
  $('.menu').wrapAll('<div id="menus" class="page"/>');
  $('.smenu').wrapAll('<div id="smenus" class="page"/>');
  if (currentMenu != 3)
    $('#smenus').css('display', 'none');
  preparePage();
  if (currentSSMenu)
    projetsLoad(currentSMenu, currentSSMenu);
  prepareProjets();
  $('.page').wrapAll('<div id="page" />');
  $('#page').append('<div id="ccache"/><div id="ccachebg"/><div id="loading"/>');
//  $('#ccache').addClass('loading');
  $('#ccache').css('opacity', '0');
  _resizeDocument();

/*  $.history.init(function(url) {
    if (!url && !currentUrl)
      return false;
    var id = parseInt(baseUrl);
    if (url) id = url;
    var sid = 0;
    if (id >= 1000) {
      sid = id % 1000;
      id = Math.floor(id / 1000);
    }
    if ((currentMenu == id) && (currentSMenu == sid))
      return false;
    if (sid == 0)
      return _menuLoad(id, true);
    if (id == 3)
      return _releaseLoad2(sid);
    if (id == 4)
      return _partLoad2(sid);
    if (id == 5)
      return _artisteLoad(sid);
    if (id == 6)
      _produitLoad(sid);
  });*/

  $(window).resize(resizeDocument);
//  $('#menus a img').hover(pngOver, pngOut);
  $('#menus a').click(menuLoad);
  $('#smenus a').click(smenuLoad);
  $('#partenaires p').live('mouseover', partenOver);
  $('#partenaires p').live('mouseout', partenOut);
  $('#vignettes p').live('click', slideGoto);
  $('#content p.savoirplus').live('click', projetDetails);
  $('#projs #next').live('click', projNext);
  $('#projs #prev').live('click', projPrev);
  $('#projs a').live('click', ssmenuLoad);
  $('#pnavbar img.next').live('click', projetNext);
  $('#pnavbar img.prev').live('click', projetPrev);
  $('#projets a').live('click', ssmenuLoad);
  $('#actus .vig').live('click', actuVigLoad);
  $('#content p.telecharger').live('click', downloadPdf);
}

function preparePage() {
  $('.proj').wrapAll('<div id="rinner"/>');
  $('#rinner').wrap('<div id="projs" class="page"/>');
  $('#projs').append('<div class="navbar"><div id="next"></div><div id="prev"></div></div>');
  $('#projs h4').after('<div style="clear: both;"> </div>');
  $('p.content').wrapAll('<div class="cinner content"/>');
  $('p.content').removeClass('content');
  $('.content').wrapAll('<div id="content" class="page"/>');
  $('#content').addClass('menu'+currentMenu);
  if (jQuery.support.opacity)
    $('#content.menu3 div.cinner').css({opacity: '0'});
  else
    $('#content.menu3 div.cinner').css({visibility: 'hidden'});
  $('.actu').wrapAll('<div class="ainner"/>');
  $('.ainner').wrap('<div id="actus" class="page"/>');
  $('.partenaires').wrapAll('<div id="partenaires" class="page"/>');
  $('#partenaires').html('<div class="pinner">'+$('#partenaires').html()+'</div>');
  $('.infos').wrapAll('<div id="infos" class="page"/>');
  if (jQuery.support.opacity)
    $('#infos').css({opacity: '0'});
  else
    $('#infos').css({visibility: 'hidden'});
  if ($('#content.menu3 div.cinner').length || $('#infos').length)
    $('#content.menu3 h2').after('<p class="savoirplus">en savoir plus</p>');
  if (currentSSMenu > 0)
    $('#content').after('<div class="page" id="pnavbar"><img class="prev" src="/images/projet-precedent.png"/>'+
	'<p>Projets</p><img class="next" src="/images/projet-suivant.png"/></div>');
  $('.eaa').wrapAll('<div id="eaa" class="page"/>');
  $('.photo').wrapAll('<div id="photos" class="page"/>');
  $('#photos').addClass('menu'+currentMenu);
  $('#photos img').addClass('second');
  $('#photos img:first').addClass('first');
  $('#photos img.first').removeClass('second');
  curPhoto = 0;
  if ($('#photos img').attr('photos')) {
    photos = $('#photos img').attr('photos').split('#');
    if (photos.length > 1)
      $('#photos').append('<img src="'+photos[1]+'" class="second"/>');
//    var i = 1;
//    for (i = 1; i < photos.length; i ++)
//      preloadImages(photos[i]);
  }
  $('.vignette').wrapAll('<div id="vignettes" class="page"/>');
}

function prepareProjets() {
  $('.projet').wrapAll('<div id="projets" class="page"/>');
  $('#projets').html('<div class="qinner">'+$('#projets').html()+'</div>');
  $('#projets h4:last').addClass('last');
}

function resizeDocument() {
  if (loading) return false;
  _resizeDocument();
  if (sizeTimerid) clearTimeout(sizeTimerid);
  sizeTimerid = setTimeout('_resizeDocument()', 1000);
}

function _resizeDocument() {
  var h = $(window).height();
  var w = $(window).width();
  pageH = h;

  if (pageH <= pageMinH) {
    pageH = pageMinH;
    $('#page').css({top: '0px', left: '0px'});
  }
  else {
    if (pageH >= pageMaxH)
      pageH = pageMaxH;
  }
  if (pageH <= 600)
    contentH = contentMinH;
  else if (pageH >= pageMaxH)
    contentH = contentMaxH;
  else
    contentH = Math.floor((pageH - 600) * 0.32388664 + 140);
  photoH = pageH - 87 - contentH - 20;
  if (pageH < h)
    contentH = h - 87 - photoH - 20;
  photoW = Math.ceil(photoH * photoX);
  pageW = photoW + 224;
  // too large
  if (pageW + 36 > w) {
    photoW = w - 36 - 224;
    photoH = Math.ceil(photoW / photoX);
    pageW = photoW + 224;
  }
  $('#page').css({height: pageH+'px', width: pageW+'px'});
  $('#ccache').css({height: (pageH-87)+'px', width: photoW+'px'});
  if (pageH > pageMinH)
    $('#page').css({left: Math.floor((w - pageW) / 2 - 36)+'px'});
//    $('#page').css({top: Math.floor((h - pageH) / 2)+'px', left: Math.floor((w - pageW) / 2 - 36)+'px'});
  resizePage();
  resizeProjets();
}

function resizePage() {
  // projs
  $('#projs').css('overflow', 'hidden');
  $('#rinner').css('marginTop', '0px');
  var h = 63;
  while (h + 9 < photoH + contentH) h += 63;
  h -= 63;
  $('#projs').height(h + 9);
  $('#photos, #photos img, #photos iframe').css({height: photoH+'px', width: photoW+'px'});
  // content
  var w = Math.ceil(photoW * contentX);
  if (w > contentMaxW) w = contentMaxW;
  $('#content').css({top: (photoH + 87)+'px', height: contentH+'px', width: w+'px'});
  $('#content.menu3').width(Math.ceil(photoW / 2));
  $('#content.menu5').width(Math.ceil(photoW * partenX));
  $('#content.menu6').width(photoW);
  // actus
  $('#actus, #actus div.ainner').height(photoH + contentH);
  $('#actus div.ainner').jScrollPane({scrollbarWidth: 4, dragMaxHeight: 30});
  // partenaires
  if ($('#partenaires').length) {
    var o = $('#content p:first').offset();
    var p = $('#page').offset();
    $('#partenaires').css({top: (o.top - p.top)+'px', height: (contentH - (o.top - p.top - photoH - 87))+'px', 
	width: Math.ceil(photoW * parten2X)+'px'});
    $('#partenaires div.pinner').height(contentH - 39);
    $('#partenaires div.pinner').jScrollPane({scrollbarWidth: 4, dragMaxHeight: 30});
  }
  // infos
  if ($('#infos').length) {
    $('#infos').height($('#content').height());
    $('#infos').width(Math.floor(photoW / 2 - 24));
    $('#infos').css('top', (87 + photoH)+'px');
    var o = null;
    var t = 0;
    if ($('#content div.cinner').length) {
      o = $('#content div.cinner').offset();
      t = o.top;
    }
    else {
      o = $('#content h2').offset();
      t = o.top + $('#content h2').height() + 6;
      if ($('#content p.savoirplus').length) t += 16;
    }
    infosMT = t - photoH - 87 + 6;
    if (!jQuery.support.opacity)
      infosMT -= 6;
    $('#infos p.first').css('marginTop', infosMT+'px');
  }
  if ($('#pnavbar').length) {
    var o = $('#content h2').offset();
    var p = $('#page').offset();
    $('#pnavbar').css('top', (o.top - p.top + 2)+'px');
    $('#pnavbar').width(Math.floor(photoW / 2 - 24));
  }
  $('#ccache').css({height: (pageH - 87 - 1)+'px', width: photoW+'px'});
  $('#loading').css('top', (photoH + 87 + 14)+'px');
  $('#eaa').css('top', (photoH + 87 - 37 - 16)+'px');
  $('#vignettes').css('top', (photoH + 87)+'px');
}

function resizeProjets() {
  $('#projets').css('overflow', 'hidden');
  if (photoH + 87 - 356 > 50) {
    $('#projets, #projets div.qinner').height(photoH + 87 - 356);
    $('#projets div.qinner').jScrollPane({scrollbarWidth: 4, dragMaxHeight: 30});
    $('#projets div.jScrollPaneContainer, #projets div.jScrollPaneTrack').height(photoH + 87 - 356 + 10);
  }
  else {
    $('#projets, #projets div.qinner').height(pageH - 356);
    $('#projets div.qinner').jScrollPane({scrollbarWidth: 4, dragMaxHeight: 30});
    $('#projets div.jScrollPaneContainer, #projets div.jScrollPaneTrack').height(pageH - 356 + 10);
  }
}

function documentPrepared() {
  _resizeDocument();
  $('#projs #prev, #projs #next').css('display', 'none');
  $('#content.menu3 div.cinner, #infos').css('display', 'none');
  if ($('#projs h4').length * 63 > $('#projs').height())
    $('#projs #next').css('display', 'block');
  $('#pcache').animate({opacity: '0'}, 1000, '', function(){
    if (($('#photos img').length > 1) && !$('#photos').hasClass('menu5')) slideWait();
    else if (photoTimerid) clearTimeout(photoTimerid);
    $('#pcache').remove();
  });
//  $('#menus img').each(function(){
//    preloadImages( $(this).attr('src').replace('-sel', '').replace('.png', '-sel.png') );
//  });
}

function pagePrepared() {
  $('#projs #prev, #projs #next').css('display', 'none');
  $('#content.menu3 div.cinner, #infos').css('display', 'none');
  if ($('#projs h4').length * 63 > $('#projs').height())
    $('#projs #next').css('display', 'block');
  $('#loading').animate({opacity: '0'}, 600);
  $('#ccache').animate({opacity: '0'}, 600, '', function(){
//    $('#ccache').removeClass('loading');
    if (($('#photos img').length > 1) && !$('#photos').hasClass('menu5')) slideWait();
    else if (photoTimerid) clearTimeout(photoTimerid);
    $('#ccache, #ccachebg').css('display', 'none');
  });
  $('#actus .vig').each(function(){
    preloadImages( $(this).attr('url') );
  });
}

function projetsPrepared() {
  var i = projetSelIndex();
  if (jQuery.support.opacity) {
    $('#projets').css('opacity', '0');
    $('#projets').animate({opacity: '1'}, 600);
  }
}

function pageClose() {
  $('#photos, #content, #partenaires, #eaa, #vignettes, #projs, #infos, #pnavbar, #actus').remove();
}

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 pngOver() {
  if ($(this).hasClass('sel')) return false;
  $(this).attr('src', $(this).attr('src').replace('.png', '-sel.png'));
}

function pngOut() {
  if ($(this).hasClass('sel')) return false;
  $(this).attr('src', $(this).attr('src').replace('-sel', ''));
}

function slideWait() {
  if (photoTimerid) clearTimeout(photoTimerid);
  photoTimerid = setTimeout('slideNext()', 3000);
  // preload next image
  i = curPhoto + 1;
  if (i >= photos.length) i = 0;
  i ++;
  if (i >= photos.length) i = 0;
  preloadImages(photos[i]);
}

function slideNext() {
  $('#ccache').css({height: photoH+'px'});
  $('#ccache').css({opacity: '0', display: 'block'});
  $('#vignettes p').removeClass('sel');
  $('#ccache').animate({opacity: '1'}, 1000, '', function(){
    $('#photos img.first').remove();
    $('#photos img.second').addClass('first');
    $('#photos img.second').removeClass('second');
    curPhoto ++;
    if (curPhoto >= photos.length)
      curPhoto = 0;
    var next = curPhoto+1;
    if (next >= photos.length)
      next = 0;
    $('#vignettes p:eq('+curPhoto+')').addClass('sel');
    $('#ccache').animate({opacity: '0'}, 1000, '', function(){
      $('#photos').append('<img src="'+photos[next]+'" class="second" style="width: '+
	photoW+'px; height: '+photoH+'px;"/>');
      $('#ccache').css('display', 'none');
      slideWait();
    });
  });
}

function slideGoto() {
  var i = $(this).index();
  $('#photos').append('<img src="'+photos[i]+'" style="width: '+
	photoW+'px; height: '+photoH+'px;"/>');
  $('#ccache').stop();
  if (photoTimerid) clearTimeout(photoTimerid);
  $('#ccache').css({height: photoH+'px'});
  $('#ccache').css({display: 'block'});
  $('#vignettes p').removeClass('sel');
  $('#ccache').animate({opacity: '1'}, 1000, '', function(){
    $('#photos img.first').remove();
    $('#photos img.second').remove();
    $('#photos img').addClass('first');
    $('#vignettes p:eq('+i+')').addClass('sel');
    $('#ccache').animate({opacity: '0'}, 1000, '', function(){
      $('#ccache').css('display', 'none');
    });
  });
}

function partenOver() {
  $('#photos img').css('opacity', '1');
  var id = $(this).attr('id').substr(2);
  $('#pp'+id).css('zIndex', '5');
}

function partenOut() {
  var id = $(this).attr('id').substr(2);
  $('#pp'+id).css('zIndex', '1');
}

function menuLoad() {
  var id = $(this).attr('id').substr(1);
  $('#ccache').stop();
  if (photoTimerid) clearTimeout(photoTimerid);
  if (id == 3)
    smenuShow();
  else {
    if (!jQuery.support.opacity)
      $('#smenus').css('display', 'none');
    else
      $('#smenus').animate({opacity: '0'}, 600, '', function(){
        $('#smenus').css('display', 'none');
      });
  }
  if (!jQuery.support.opacity)
    $('#projets').remove();
  else
    $('#projets').animate({opacity: '0'}, 600, '', function(){
      $('#projets').remove();
    });
  $('#ccachebg').css('display', 'block');
//  if ($('#menus img.sel').length)
//    $('#menus img.sel').attr('src', $('#menus img.sel').attr('src').replace('-sel',  ''));
  $('#menus a').removeClass('sel');
  $('#menus #m'+id).addClass('sel');
  $('#loading').css({opacity: '0', display: 'block'});
//  $('#ccache').addClass('loading');
  $('#ccache').css({height: (pageH - 87 - 1)+'px', display: 'block'});
  $('#loading').animate({opacity: '1'}, 600);
  $('#ccache').animate({opacity: '1'}, 600, '', function(){
    pageClose();
    $.get('/templates/menu-load.php', { id: id }, function(data) {
      $('#page').append(data);
      currentMenu = id;
      currentSMenu = 0;
      currentSSMenu = 0;
      preparePage();
      if ($('#photos img').length) {
	$('#photos img.first').load(function(){
	  pagePrepared();
	});
	resizePage();
      }
      else {
	resizePage();
	pagePrepared();
      }
    });
  });
  return false;
}

function smenuShow() {
  if (!jQuery.support.opacity)
    $('#smenus').css({display: 'block'});
  else {
    $('#smenus').css({opacity: '0', display: 'block'});
    $('#smenus').animate({opacity: '1'}, 600);
  }
  return false;
}

function smenuLoad() {
  var id = $(this).attr('id').substr(2);
  $('#ccache').stop();
  if (photoTimerid) clearTimeout(photoTimerid);
  if (!jQuery.support.opacity)
    $('#projets').remove();
  else {
    $('#projets').animate({opacity: '0'}, 600, '', function(){
      $('#projets').remove();
    });
  }
  $('#ccachebg').css('display', 'block');
  $('#smenus a').removeClass('sel');
  $('#smenus #sm'+id).addClass('sel');
  $('#loading').css({opacity: '0', display: 'block'});
//  $('#ccache').addClass('loading');
  $('#ccache').css({height: (pageH - 87 - 1)+'px', display: 'block'});
  $('#loading').animate({opacity: '1'}, 600);
  $('#ccache').animate({opacity: '1'}, 600, '', function(){
    pageClose();
    $.get('/templates/smenu-load.php', { id: id }, function(data) {
      $('#page').append(data);
      currentSMenu = id;
      currentSSMenu = 0;
      preparePage();
      if ($('#photos img').length) {
	$('#photos img.first').load(function(){
	  pagePrepared();
	});
	resizePage();
      }
      else {
	resizePage();
	pagePrepared();
      }
    });
  });
  return false;
}

function ssmenuLoad() {
  var id = $(this).attr('id').substr(4);
  _ssmenuLoad(id);
  return false;
}

function _ssmenuLoad(id) {
  $('#ccache').stop();
  if (photoTimerid) clearTimeout(photoTimerid);
  $('#ccachebg').css('display', 'block');
  $('#projets a').removeClass('sel');
  $('#projets #proj'+id).addClass('sel');
  $('#loading').css({opacity: '0', display: 'block'});
//  $('#ccache').addClass('loading');
  $('#ccache').css({height: (pageH - 87 - 1)+'px', display: 'block'});
  $('#loading').animate({opacity: '1'}, 600);
  $('#ccache').animate({opacity: '1'}, 600, '', function(){
    pageClose();
    $.get('/templates/ssmenu-load.php', { id: id }, function(data) {
      $('#page').append(data);
      currentSSMenu = id;
      preparePage();
      if ($('#photos img').length) {
	$('#photos img.first').load(function(){
	  pagePrepared();
	});
	resizePage();
      }
      else {
	resizePage();
	pagePrepared();
      }
    });
    if (!$('#projets').length)
      projetsLoad(currentSMenu, id);
  });
}

function projetsLoad(id, pid) {
  $.get('/templates/projets.php', { id: id, pid: pid }, function(data) {
    $('#page').append(data);
    prepareProjets();
    resizeProjets();
    projetsPrepared();
  });
  return false;
}

function projetDetails() {
  loading = true;
  $('#content div.cinner, #infos').css('display', 'block');
  $('#content, #infos').css('height', 'auto');
/*  if ($('#infos').height() == 0) {
    $('#infos').html('<div>'+$('#infos').html()+'</div>');
    $('#infos').height($('#infos div').height() + infosMT);
  }*/
  if (jQuery.support.opacity) {
    $('#content div.cinner').animate({opacity: '1'}, 600);
    $('#infos').animate({opacity: '1'}, 600, '', function(){
      loading = false;
    });
  }
  else {
    $('#infos p.first').css('marginTop', infosMT+'px');
    $('#content div.cinner, #infos').css({visibility: 'visible'});
    loading = false;
  }
  if ($('#content h2').attr('pdf')) {
    $('#content h2').after('<p class="telecharger">Télécharger le PDF du projet<p>');
  }
  else
    $('#content h2').after('<p class="spacer">&nbsp;<p>');
  $('#content p.savoirplus').remove();
}

function projNext() {
  var mt = -1 * parseInt($('#rinner').css('marginTop').replace('px', ''));

//  if (mt + photoH + contentH >= $('#rinner').height())
//    return false;

  $('#projs #next, #projs #prev').addClass('v2');
  $('#projs #prev').css('display', 'block');
  mt += $('#projs').height() - 9;

  if (mt + photoH + contentH >= $('#rinner').height()) {
    $('#projs #next').css('display', 'none');
    $('#projs #prev').removeClass('v2');
  }
  $('#rinner').stop();
  $('#rinner').animate({marginTop: '-='+($('#projs').height() - 9)+'px'}, 600);
}

function projPrev() {
  var mt = -1 * parseInt($('#rinner').css('marginTop').replace('px', ''));
  if (mt == 0)
    return false;
  $('#projs #next, #projs #prev').addClass('v2');
  $('#projs #next').css('display', 'block');
  mt -= $('#projs').height() - 9;
  if (mt == 0) {
    $('#projs #prev').css('display', 'none');
    $('#projs #next').removeClass('v2');
  }
  $('#rinner').stop();
  $('#rinner').animate({marginTop: '+='+($('#projs').height() - 9)+'px'}, 600);
}

function projetNext() {
  var i = projetSelIndex() + 1;
  if (i >= $('#projets a').length) {
    $('#pnavbar img.next').css('display', 'none');
    return false;
  }
  $('#pnavbar img.prev').css('display', 'block');
  _ssmenuLoad($('#projets a:eq('+i+')').attr('id').substr(4));
  if (i == $('#projets a').length - 1)
    $('#pnavbar img.next').css('display', 'none');
}

function projetPrev() {
  var i = projetSelIndex() - 1;
  if (i < 0) {
    $('#pnavbar img.prev').css('display', 'none');
    return false;
  }
  $('#pnavbar img.next').css('display', 'block');
  _ssmenuLoad($('#projets a:eq('+i+')').attr('id').substr(4));
  if (i == 0)
    $('#pnavbar img.prev').css('display', 'none');
}

function projetSelIndex() {
  var i = 0;
  while ((i < $('#projets a').length) && !$('#projets a:eq('+i+')').hasClass('sel')) i ++;
  return i;
}

function actuVigLoad() {
  if ($(this).hasClass('sel'))
    return false;
  var id = $(this).parent().attr('id').substr(4);
  var obj = this;
  $('#illus'+id).animate({opacity: '0'}, 600, '', function(){
    $('#illus'+id+' img').attr('src', $(obj).attr('url'));
    $('#illus'+id).animate({opacity: '1'}, 600);
    $('#vigs'+id+' .vig').removeClass('sel');
    $(obj).addClass('sel');
  });
}

function downloadPdf() {
  window.location.href = $('#content h2').attr('pdf');
}

