
var storeFixed = true;
var storeOrig = 0;
var storeMin = 20;
var storeMax = 382;
var contentMin = 0;
var contentMax = 362;
var photoWidth = 628;

var rubCurId = 0;
var refCurId = 0;
var photoCurId = 0;

var hotesseSpeed = 9.375;

var animPlayed = new Array();
animPlayed[0] = false;
animPlayed[1] = false;
animPlayed[2] = false;
var animTimerid = null;
var noAnim = false;

var zoomLevel = 0;
var documentStyle = 'percent';

var clientsList = null;
var clientsTimerid = null;

var contactId = 39;
var aboutId = 38;
var ftpId = 41;
var newsId = 40;

var cursorIE = '';


function prepareDocument(rub, ref) {
  rubCurId = rub; refCurId = ref;
  $('body').append('<p id="r39" class="contact">Contact</p>'); 
  $('body').append('<p id="r38" class="about">Qui sommes-nous</p>'); 
  $('body').append('<div id="siegeg" class="siege"></div>', '<div id="sieged" class="siege"></div>', '<div id="cadretop"></div>', '<div id="cadreleft"></div>', '<div id="cadrebottom"></div>', '<div id="cadreright"></div>', '<div id="cadrebg"></div>', '<div id="images"></div>', '<div id="apercubg"></div>', '<div id="apercu"></div>', '<div id="store"></div>', '<div id="content"></div>', '<div id="logog" class="logo"><img src="/images/logo-petit.png" /></div>', '<div id="logod" class="logo"><img src="/images/logo-petit.png" /></div>', '<p id="r41" class="ftp">Espace FTP</p>');
  $('#apercubg').css('opacity', '0.8');
  $('div.clients').html('<div>'+$('div.clients').html()+'</div>');
  $('#store').height(20);
  $('#content').height(0);
  resizeDocument();

  // init
  if (rub > 0) {
    noAnim = true;
    $('#texte, #image').remove();
    $('h2 a').removeClass('sel');
    $('h3 a').removeClass('sel');
    if (ref > 0)
      $('#r'+rub+'sr'+ref).addClass('sel');
    $('#r'+rub).addClass('sel');
    _loadFinish();
  }

  // curseurs
  if (jQuery.browser.mozilla && navigator.platform.match('Mac')) {
    $('body').append('<div id="cursor"></div>');
    $('#images div img, #images div img.zoom, #store, #content, #apercubg, #apercu').live('mouseover', cursorShow);
    $('#images div img, #images div img.zoom, #store, #content, #apercubg, #apercu').live('mouseout', cursorHide);
    $('#images div img, #images div img.zoom, #store, #content, #apercubg, #apercu').live('mousemove', cursorMove);
  }
  else if (navigator.platform.match('Mac') || !jQuery.browser.mozilla) {
    cursorIE = 'ie';
    $('#apercu, #apercubg').addClass('ie');
  }

  // actions
  $(window).resize(resizeDocument);
  $('div.clients div').jScrollPane({showArrows: true});
  $('h3 a').live('click', refLoad);
  $('h2 a, p.contact, p.about, p.ftp').live('click', rubLoad);
  $('#apercu, #apercubg').click(imageZoomClose);
  $('#images img').live('click', imageClick);
  $('#store.expand'+cursorIE+', #content.expand'+cursorIE).live('click', storeExpand);
  $('#store.collaspe'+cursorIE+', #content.collaspe'+cursorIE).live('click', storeCollapse2);
  $('#ftpcon').live('click', ftpCon);
  $('#ftppasse').live('click', ftpSendPass);
  $('#ftpupload').live('click', ftpUploadForm);
  $('#ftpcan').live('click', ftpUploadBack);
  $('#ftpup').live('click', ftpUpload);
}

function resizeDocument() {
  if (($(window).height() < 600) && (documentStyle == 'percent')) {
    documentStyle = 'pix';
    $('#cadrebg, #images, #animation').css({top: '19px', marginTop: '0px'});
    $('#store, #content').css({top: '27px', marginTop: '0px'});
    $('#cadretop').css({top: '11px', marginTop: '0px'});
    $('#cadreleft, #cadreright').css({top: '29px', marginTop: '0px'});
    $('#cadrebottom').css({top: '407px', marginTop: '0px'});
    $('div.siege').css({top: '239px', marginTop: '0px'});
    $('h1').css({top: '563px', marginTop: '0px'});
    $('#slogan').css({top: '528px', marginTop: '0px'});
    $('#logog').css({top: '244px', marginTop: '0px'});
    $('#logod').css({top: '250px', marginTop: '0px'});
    $('p.contact').css({top: '379px', marginTop: '0px'});
    $('p.about').css({top: '454px', marginTop: '0px'});
    $('p.ftp').css({top: '460px', marginTop: '0px'});
    $('#skills, #clients').css({top: '15px', marginTop: '0px'});
    $('div.skills, div.clients').css({top: '42px', marginTop: '0px'});
    $('#apercu').css({top: '0', marginTop: '0px'});
  }
  else if (($(window).height() >= 600) && (documentStyle == 'pix')) {
    documentStyle = 'percent';
    $('#cadrebg, #images, #animation').css({top: '50%', marginTop: '-281px'});
    $('#store, #content').css({top: '50%', marginTop: '-273px'});
    $('#cadretop').css({top: '50%', marginTop: '-289px'});
    $('#cadreleft, #cadreright').css({top: '50%', marginTop: '-271px'});
    $('#cadrebottom').css({top: '50%', marginTop: '107px'});
    $('div.siege').css({top: '50%', marginTop: '-61px'});
    $('h1').css({top: '50%', marginTop: '263px'});
    $('#slogan').css({top: '50%', marginTop: '228px'});
    $('#logog').css({top: '50%', marginTop: '-56px'});
    $('#logod').css({top: '50%', marginTop: '-50px'});
    $('p.contact').css({top: '50%', marginTop: '79px'});
    $('p.about').css({top: '50%', marginTop: '154px'});
    $('p.ftp').css({top: '50%', marginTop: '160px'});
    $('#skills, #clients').css({top: '50%', marginTop: '-285px'});
    $('div.skills, div.clients').css({top: '50%', marginTop: '-258px'});
    $('#apercu').css({top: '50%', marginTop: '-300px'});
  }
}

function documentPrepared() {
  preloadImages('/images/zoom-in.png', '/images/zoom-out.png', '/images/store-up.png', '/images/store-down.png', '/swf/crash.swf', '/swf/avion.swf', '/swf/vache.swf');
  if (images[newsId])
    preloadImages(images[newsId]);
  $('h2 a').each(function(){
    var i = parseInt($(this).attr('id').substr(1));
    if (images[i]) preloadImages(images[i]);
  });
  $('h3 a').each(function(){
    var rub = parseInt($(this).attr('id').substring(1, $(this).attr('id').indexOf('sr')));
    var ref = parseInt($(this).attr('id').substr($(this).attr('id').indexOf('sr')+2));
    if (simages[rub] && simages[rub][ref]) preloadImages(simages[rub][ref]);
  });
//  $('#logog').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="50" height="50"><param name="movie" value="swf/logo.swf"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="swf/logo.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="50" height="50" wmode="transparent"></embed></object>');
//  $('#logod').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="50" height="50"><param name="movie" value="swf/logo.swf"><param name="quality" value="high"><param name="wmode" value="transparent"><embed src="swf/logo.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="50" height="50" wmode="transparent"></embed></object>');

if (rubCurId || refCurId)
  return false;

rubCurId = newsId; refCurId = 0;
_loadPrepare();
return false;

  if (noAnim) {
    noAnim = false;
    return false;
  }
  noAnim = true;
  // play anim
  var i = Math.floor(Math.random()*2);
//  var i = Math.floor(Math.random()*3);
  animPlayed[i] = true;
  $('body').append('<div id="animation"></div>');
  if (documentStyle == 'pix')
    $('#animation').css({top: '19px', marginTop: '0px'});
  var flashvars = {};
  var params = {
    quality: "high",
    wmode: "transparent",
    scale: "showall"
  };
  // load crash
  if (i == 2)
    swfobject.embedSWF("/swf/crash.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
  // load avion
  else if (i == 1)
    swfobject.embedSWF("/swf/avion.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
  // load vache
  else
    swfobject.embedSWF("/swf/vache.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
  // load flash
/*  var flashvars = {};
  var params = {
    quality: "high",
    wmode: "transparent"
  };
  swfobject.embedSWF("/swf/hotesse.swf", "hotesse", 361, 476, "9.0.0", "", flashvars, params);
  var w = $(window).width();
  $('#hotesse').animate({left: w+'px'}, hotesseSpeed * w, '', function(){
    $('#hotessec').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;
    var 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 imageClick() {
  if (zoomLevel) return imageZoom();
  else storeCollapse($(this));
}

function storeCollapse(obj) {
  zoomLevel = 1;
  $(obj).addClass('zoom'+cursorIE);
  $('#store, #content').removeClass('collaspe'+cursorIE);
  var h = $('#content div').height();
  if (h)
    $('#store, #content').addClass('expand'+cursorIE);
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  cursorSet(obj);
}

function storeCollapse2() {
  zoomLevel = 1;
  $('#images img:first').addClass('zoom'+cursorIE);
  $('#store, #content').removeClass('collaspe'+cursorIE);
  var h = $('#content div').height();
  if (h)
    $('#store, #content').addClass('expand'+cursorIE);
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  cursorSet($('#images img:first'));
}

function storeExpand() {
  zoomLevel = 0;
  $('#images img.zoom'+cursorIE).removeClass('zoom'+cursorIE);
  $('#store, #content').removeClass('expand'+cursorIE);
  $('#store, #content').removeClass('collaspe'+cursorIE);
  if (rubCurId != ftpId)
    $('#store, #content').addClass('collaspe'+cursorIE);
  var h = $('#content div').height();
  if (!h) return storeCollapse2();
  h += 10;
  if (h > contentMax) h = contentMax;
  $('#store').animate({height: (h+20)+'px'}, 400);
  $('#content').animate({height: h+'px'}, 400);
  cursorHide();
}

function _loadPrepare() {
  // close store & move photo
  $('#store').css({height: storeMin+'px'});
  $('#content').css({height: contentMin+'px'});
  $('#zoom').css('display', 'none');
  if ($('#p'+photoCurId).length) {
    $('#p'+photoCurId).animate({left: photoWidth+'px'}, 500, '', function(){
      $(this).remove();
      return _loadAnim();
    });
  }
  else
    return _loadAnim();
}

function _loadAnim() {
  if (!refCurId && (rubCurId != contactId) && (rubCurId != aboutId) && (rubCurId != ftpId) && (rubCurId != newsId)) refsLoad();
  if (noAnim) {
    noAnim = false;
    return _loadFinish();
  }
  noAnim = true;
  var i = Math.floor(Math.random()*3);
  if ((i == 2) || animPlayed[i])
//  var i = Math.floor(Math.random()*4);
//  if ((i == 3) || animPlayed[i])
    return _loadFinish();
  $('#animation').remove();
  $('body').append('<div id="animation"></div>');
  if (documentStyle == 'pix')
    $('#animation').css({top: '19px', marginTop: '0px'});
  var flashvars = {};
  var params = {
    quality: "high",
    wmode: "transparent"
  };
  animPlayed[i] = true;
  // load crash
  if (i == 2) {
    swfobject.embedSWF("/swf/crash.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
    if (animTimerid) clearTimeout(animTimerid);
    animTimerid = setTimeout("_loadFinish()", 5000);
    return false;
  }
  // load avion
  if (i == 1) {
    swfobject.embedSWF("/swf/avion.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
    if (animTimerid) clearTimeout(animTimerid);
    animTimerid = setTimeout("_loadFinish()", 4500);
    return false;
  }
  // load vache
  swfobject.embedSWF("/swf/vache.swf", "animation", 628, 398, "9.0.0", "", flashvars, params);
  if (animTimerid) clearTimeout(animTimerid);
  animTimerid = setTimeout("_loadFinish()", 8000);
  return false;
}

function _loadFinish() {
  $('#animation').remove();
  // add input & photo
  if (refCurId) {
    $('#content').html('<div><h4>'+stitles[rubCurId][refCurId]+'</h4>'+scontents[rubCurId][refCurId]+'</div>');
    if (simages[rubCurId][refCurId])
      $('#images').html('<div id="p'+refCurId+'"><img src="'+simages[rubCurId][refCurId]+'" class="'+cursorIE+'" /></div>');
    photoCurId = refCurId;
  }
  else if (rubCurId == ftpId) {
    photoCurId = rubCurId;
    return ftpLoad();
  }
  else {
    $('#content').html('<div><h4>'+titles[rubCurId]+'</h4>'+contents[rubCurId]+'</div>');
    if (images[rubCurId]) {
      $('#images').html('<div id="p'+rubCurId+'"><img src="'+images[rubCurId]+'" class="'+cursorIE+'" /></div>');
      $('#p'+rubCurId+' img').load(function(){
//	if (rubCurId == newsId)
//	  $(this).css({height: '384px', marginTop: '14px'});
	var w = $(this).width();
	if (w < 614) $(this).css('marginLeft', Math.floor((614-w)/2)+'px');
      });
    }
    photoCurId = rubCurId;
  }
  if (!$('#p'+photoCurId).length) {
    storeExpand();
    return false;
  }
  $('#p'+photoCurId).animate({left: '0px'}, 500, '', function(){
    $('#zoom').css('display', 'block');
    storeExpand();
  });
}

function rubLoad() {
  var rub = $(this).attr('id').substr(1);
  if ((rub == rubCurId) && (refCurId == 0))
    return false;
/*  zoomLevel = 0;
  $('#images img.zoom'+cursorIE).removeClass('zoom'+cursorIE);
  $('#store').removeClass('expand'+cursorIE);
  if (rub != ftpId)
    $('#store').addClass('collaspe'+cursorIE);
else
alert('no collaspe');
*/
  rubCurId = rub; refCurId = 0;
  $('h2 a').removeClass('sel');
  $(this).addClass('sel');
  $('h3 a').removeClass('sel');
  _loadPrepare();
  return false;
}

function _refsLoad(i) {
  $('div.clients a').removeClass('sel');
  $('div.clients div').append('<h3><a href="/" id="r'+rubCurId+'sr'+clientsList[i]+
	'">'+snames[rubCurId][clientsList[i]]+'</a></h3>');
  if (i == clientsList.length - 1) {
    $('div.clients div').jScrollPane({showArrows: true});
    return false;
  }
  $('div.clients h3:last a').addClass('sel');
  if (clientsTimerid) clearTimeout(clientsTimerid);
  clientsTimerid = setTimeout('_refsLoad('+(i+1)+')', 300);
}

function refsLoad() {
  $('div.clients').empty();
  $.ajax({
    type: "GET",
    cache: false,
    url: '/templates/rubriques.php',
    data: 'parent='+rubCurId,
    success: function(data){
      clientsList = data.split('#');
      $('div.clients').html('<div></div>');
      _refsLoad(0);
    }
  });
}

function refLoad() {
  var rub = $(this).attr('id').substring(1, $(this).attr('id').indexOf('sr'));
  var ref = $(this).attr('id').substr($(this).attr('id').indexOf('sr')+2);
  if ((rub == rubCurId) && (ref == refCurId))
    return false;
  zoomLevel = 0;
  $('#images img.zoom'+cursorIE).removeClass('zoom'+cursorIE);
  $('#store, #content').removeClass('expand'+cursorIE);
  $('#store, #content').addClass('collaspe'+cursorIE);
  rubCurId = rub; refCurId = ref;
  $('h3 a').removeClass('sel');
  $(this).addClass('sel');
  _loadPrepare();
  return false;
}

function imageZoom() {
  $('#apercu').html($('#images div:first').html());
  $('#apercu img').removeClass('ie');
  $('#apercu img').css('marginLeft', '0px');
  $('#apercubg, #apercu').css('display', 'block');
}

function imageZoomClose() {
  $('#apercubg, #apercu').css('display', 'none');
}

function ftpLoad() {
  $.ajax({
    type: "GET",
    cache: false,
    url: '/templates/ftp.php',
    success: function(data){
      $('#content').html('<div><h4>Espace FTP</h4>'+data+'</div>');
      if (!$('#p'+photoCurId).length) {
	storeExpand();
	return false;
      }
      $('#p'+photoCurId).animate({left: '0px'}, 500, '', function(){
	$('#zoom').css('display', 'block');
	storeExpand();
      });
    }
  });
}

function ftpCon() {
  var f = document.myform;
  if (!f.user_email.value) {
    $('#ftpmes').html('Merci d\'indiquer votre adresse email.');
    return false;
  }
  if (!f.user_passe.value) {
    $('#ftpmes').html('Merci d\'indiquer votre mot de passe.');
    return false;
  }
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  $.ajax({
    type: "POST",
    cache: false,
    url: '/templates/ftp.php',
    data: 'user_email='+f.user_email.value+'&user_passe='+f.user_passe.value,
    success: function(data){
//alert(data);
      $('#content').html('<div><h4>Espace FTP</h4>'+data+'</div>');
      storeExpand();
    }
  });
}

function ftpSendPass() {
  var f = document.myform;
  if (!f.user_email.value) {
    $('#ftpmes').html('Merci d\'indiquer votre adresse email.');
    return false;
  }
  $('#ftpmes').html('Veuillez patienter...');
  $.ajax({
    type: "POST",
    cache: false,
    url: '/scripts/ftp-password.php',
    data: 'user_email='+f.user_email.value,
    success: function(data){
      $('#ftpmes').html(data);
    }
  });
}

function ftpUploadForm() {
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  $.ajax({
    type: "GET",
    cache: false,
    url: '/templates/ftp.php',
    data: 'upload=1',
    success: function(data){
//alert(data);
      $('#content').html('<div><h4>Espace FTP</h4>'+data+'</div>');
      storeExpand();
    }
  });
}

function ftpUploadBack() {
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  ftpLoad();
}

function ftpUpload() {
  $('#store').animate({height: storeMin+'px'}, 400);
  $('#content').animate({height: contentMin+'px'}, 400);
  $('#myform').ajaxSubmit({
    type: "POST",
    url: '/scripts/ftp-upload.php',
    success: function(data){
//alert(data);
      if (data == '1')
	$('#ftpmes').html('Le document soumis ne convient pas.');
      else
	$('#content').html('<div><h4>Espace FTP</h4>'+data+'</div>');
      storeExpand();
    }
  });
}

function cursorSet(obj) {
  if (obj.attr('id').match('apercu'))
    $('#cursor').html('<img src="/images/zoom-out.png" />');
  else if ((obj.attr('id') == 'store') || (obj.attr('id') == 'content')) {
    if (obj.hasClass('expand'))
      $('#cursor').html('<img src="/images/store-down.png" />');
    else
      $('#cursor').html('<img src="/images/store-up.png" />');
  }
  else if (obj.hasClass('zoom'))
    $('#cursor').html('<img src="/images/zoom-in.png" />');
  else
    $('#cursor').html('<img src="/images/store-up.png" />');
}

function cursorShow(e) {
  cursorSet($(this));
  $('#cursor').css('display', 'block');
  cursorMove(e);
}

function cursorHide() {
  $('#cursor').css('display', 'none');
}

function cursorMove(e) {
  $('#cursor').css({left: (e.pageX+15)+'px', top: (e.pageY-15)+'px'});
}

