
var datesNb = 0;
var datesCur = 1;
var datesTimerid = null;

var bioCur = 0;
var bioMode = 'main';
var bioNext = 'autre';
var bioLabel = 'Autre distribution';

var menuCur = 0;
var menuLoaded = false;
var menuLoading = false;
var menuReordering = false;
var menuTimerid = null;

var cacheLoaded = false;
var photoCur = 0;
var thumbsSpeed = 0;
var thumbsTimerid = null;

var gatag = '<script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));</script><script type="text/javascript">try {var pageTracker = _gat._getTracker("UA-12503721-1");pageTracker._trackPageview();} catch(err) {}</script>';


function commonPrepare() {
  commonResize();
  $('#page').append('<div id="corner_tr"></div><div id="corner_bl"></div><div id="corner_br"></div><div id="menusbg"></div><div id="mp3player"><object type="application/x-shockwave-flash" data="/swfs/mp3-player-multi.swf" width="160" height="20"><param name="movie" value="/swfs/mp3-player-multi.swf" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="configxml=/audios.php" /></object></div><div id="cache"><img src="/images/loading.gif" /></div>');
  $('h2').wrapAll('<div id="menus"></div>');
  if ($('h2.sel').length) {
    menuCur = $('h2.sel').attr('id').substr(1);
    $('#page').append('<div id="'+$('h2.sel').attr('id')+'illus"></div>');
  }
  $('.dates').wrapAll('<div id="dates"></div>');
  $('#dates p').wrapAll('<div class="list"><div id="dlist1"></div></div>');
  $('.nl').wrapAll('<div id="nl"><form name="nlform" id="nlform" method="post" action="/" onsubmit="return false;"></form></div>');
  // actions
  $(window).resize(commonResize);
  $('h2 img').hover(pngOver, pngOut);
  $('h2').hover(menuOver, menuOut);
  $('h2 a').click(menuLoad);
  $('#nl p.but').click(nlSubmit);
  commonInit();
}

function commonInit() {
  $('#content, #photos, #thumbs, #credits, #queue, #bios').remove();
  $('#pagebg').removeClass('bio');
  $('.content').wrapAll('<div id="content"></div>');
  $('.queue').wrapAll('<div id="queue"></div>');
  $('.photo').wrapAll('<div id="photos"></div>');
  $('.thumb').wrapAll('<div id="thumbs"><div></div></div>');
  $('#photos p.galerie, #photos p.avoir, #photos p.biographie').css('opacity', '0');
  $('#photos p.galerie:first, #photos p.avoir:first, #photos p.biographie:first').css({zIndex: '2', opacity: '1'});
  $('#photos img').css('marginTop', '0px');
  $('#page').append('<div id="credits"></div>');
  var txt = $('#photos p:first').attr('credits');
  if (txt && txt.length)
      $('#credits').html('Crédits photo : '+txt);
  $('#photos img:first').load(function(){
    var h = $(this).height();
    if (h < 505) $(this).css('marginTop', Math.floor((505-h)/2)+'px');
  });
  if ($('h3.enecoute').length) {
    $('#mp3player').css('display', 'none');
    $('#content p.enecoute:first').addClass('sel');
    $('h3.enecoute').html('<object type="application/x-shockwave-flash" data="/swfs/mp3-player-maxi.swf" width="200" height="20"> <param name="movie" value="/swfs/mp3-player-maxi.swf" /> <param name="bgcolor" value="#FFFFFFF" /> <param name="FlashVars" value="mp3='+$('#content p.enecoute:first').attr('url')+'&amp;showstop=1&amp;showvolume=1&amp;autoplay=1&amp;loadingcolor=FFFFFFF&amp;bgcolor=FFFFFFF&amp;bgcolor1=FFFFFFF&amp;bgcolor2=FFFFFFF&amp;slidercolor1=A4A4A4&amp;slidercolor2=A4A4A4&amp;sliderovercolor=C30011&amp;buttoncolor=000000&amp;buttonovercolor=C30011&amp;textcolor=000000" /></object>');
    $('div.enecoute2').html('<div>'+$('div.enecoute2').html()+'</div>');
    $('div.enecoute2 div').jScrollPane({showArrows: true});
  }
  else
    $('#mp3player').css('display', 'block');
  if ($('#photos p.avoir').length)
    $('#mp3player').html('<object type="application/x-shockwave-flash" data="/swfs/mp3-player-multi.swf" width="160" height="20"><param name="movie" value="/swfs/mp3-player-multi.swf" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="configxml=/audios.php?noautoplay=1" /></object>');
  if ($('h3.biographie').length) {
    bioMode = 'main';
    bioNext = 'autre';
    bioLabel = 'Autre distribution';
    bioCur = $('h3.biographie:first').attr('id').substr(1);
    $('#pagebg').addClass('bio');
    $('#page').append('<div id="bios"></div><div id="content"></div>');
    $('#content, #photos').addClass('bio');
    $('#queue #h'+bioCur+', #queue p.c'+bioCur).clone().appendTo('#content');
    $('#queue h3.main').each(function(){
      var i = $(this).attr('id').substr(1);
      if (i != bioCur)
	$('#bios').append('<p id="q'+i+'">'+$(this).html()+'</p>');
    });
    $('#bios').append('<p id="qautre">Autres distributions possibles...</p><p id="qson">Son et lumière</p>');
    $('#bios p').live('click', smenuLoad);
  }
  if ($('div.presse').length) {
    $('div.presse').each(function(){
      $(this).html('<div>'+$(this).html()+'</div>');
    });
    $('div.presse div').jScrollPane({showArrows: true});
    $('div.presse:last').css('display', 'none');
    $('#content p:first').addClass('sel');
    $('#content p').click(presseLoad);
  }
  // actions
//  if ($.browser.msie)
//    $('#thumbs').mousemove(thumbsSetIE);
//  else
    $('#thumbs').mousemove(thumbsSet);
  $('#thumbs p').click(photoLoad);
  $('#content p.enecoute').click(mp3Load);
}

function commonResize() {
  var w = $(window).width();
  if ($.browser.safari)
    w = $(document).width();
  if (w < 980)
    $('#page, #pagebg').css({left: '0px', marginLeft: '0px'});
  else
    $('#page, #pagebg').css({left: '50%', marginLeft: '-490px'});
  var h = $(window).height();
  if ($.browser.safari)
    h = $(document).height();
  if (h < 600)
    $('#page, #pagebg').css({top: '0px', marginTop: '0px'});
  else
    $('#page, #pagebg').css({top: '50%', marginTop: '-300px'});
}

function commonPrepared() {
  preloadImages('/images/presentation-sel.png', '/images/biographies-sel.png', '/images/actualites-presse-sel.png', '/images/galerie-sel.png', '/images/en-ecoute-sel.png', '/images/a-voir-sel.png', '/images/espace-pro-sel.png', '/images/contact-sel.png', '/images/liens-sel.png', '/images/blog-sel.png');
  // animate menus
  var mleft = 'mleft';
  var mright = 'mright';
  if ($('h2.sel').length && $('h2.sel').hasClass('mleft')) {
    mleft = 'mright';
    mright = 'mleft';
  }
  $('h2').each(function(){
    var w = $('#'+$(this).attr('id')+' img').width();
    if ($(this).hasClass(mleft))
      $(this).animate({marginRight: (w-48)+'px'}, 500, '', function(){
        $(this).addClass('left');
        $(this).removeClass(mleft);
      });
    else if ($(this).hasClass(mright))
      $(this).animate({marginLeft: (w-40)+'px'}, 500, '', function(){
        $(this).addClass('right');
        $(this).removeClass(mright);
      });
  });
  // animate dates
  datesNb = $('#dates p.dnom').length;
  if (datesNb > 3) {
    $('#dlist1').clone().appendTo('#dates div.list');
    $('#dates div.list div:eq(1)').attr('id', 'dlist2');
    $('#dlist1').css('marginTop', '0px');
    datesWait();
  }
}

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 emailIsValid( email ) {
  var e = email.toLowerCase(); 
  e = e.replace( /(^\s+|\s+$)/g, '' );
  if (!e.match(/[a-z0-9\.\-]+@[a-z0-9\.\-]+\.[a-z]{2,4}/))
    return null;
  return e;
}

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 menuOver() {
  if (!$(this).hasClass('left') || $(this).hasClass('sel') || menuLoading || menuReordering)
    return false;
  $(this).addClass('mleft');
  var w = $('#'+$(this).attr('id')+' img').width();  
  $(this).stop();
  $(this).animate({marginLeft: (w-40)+'px', marginRight: '0'}, 300);
}

function menuOut() {
  if (!$(this).hasClass('mleft') || $(this).hasClass('sel') || $(this).hasClass('right') || menuLoading || menuReordering)
    return false;
  $(this).removeClass('mleft');
  var w = $('#'+$(this).attr('id')+' img').width();  
  $(this).stop();
  $(this).animate({marginRight: (w-48)+'px', marginLeft: '0'}, 300);
}

function menuReorder(id) {
  // treat previous selection
  if ($('h2.sel').length) {
    $('h2.sel img').attr('src', $('h2.sel img').attr('src').replace('-sel', ''));
    $('h2.sel img').removeClass('sel');
    $('h2.sel').removeClass('sel');
  }
  // fix new selection
  $('#'+id+' img').addClass('sel');
  $('#'+id).addClass('sel');
  // menu on the right
  if ($('#'+id).hasClass('right'))
    return false;
  menuReordering = true;
  $('#'+id).removeClass('mleft');
  $('h2').stop();
  $('h2').each(function(){
    var w = $('#'+$(this).attr('id')+' img').width();
    if ($(this).hasClass('left'))
      $(this).animate({marginLeft: (w-40)+'px', marginRight: '0px'}, 500, '', function(){
        $(this).addClass('right');
        $(this).removeClass('left');
      });
    else if ($(this).hasClass('right'))
      $(this).animate({marginRight: (w-48)+'px', marginLeft: '0px'}, 500, '', function(){
        $(this).addClass('left');
        $(this).removeClass('right');
      });
    if ($(this).attr('id') == 'm10')
      menuReordering = false;
  });
}

function datesNext() {
  $('#dlist'+datesCur).animate({marginTop: '-=18px'}, 500, '', function(){
    var mt = -1 * parseInt($('#dlist'+datesCur).css('marginTop').replace('px', ''));
    if (mt >= datesNb * 18) {
      $('#dlist'+datesCur).remove();
      var old = datesCur;
      if (datesCur == 1) datesCur = 2;
      else datesCur = 1;
      $('#dlist'+datesCur).clone().appendTo('#dates div.list');
      $('#dates div.list div:eq(1)').attr('id', 'dlist'+old);
      $('#dlist'+datesCur).css('marginTop', '0px');
    }
    datesWait();
  });
}

function datesWait() {
  if (datesTimerid) clearTimeout(datesTimerid);
  datesTimerid = setTimeout('datesNext()', 3000);
}

function nlSubmit() {
  var f = document.nlform;
  if (f.email.value) f.email.value = f.email.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.email.value) {
    alert('Merci d\'indiquer votre adresse email.');
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    alert('Email incorrect. Merci de bien vouloir corriger.');
    return false;
  }
  f.email.value = e;
  $('#nl p.but').unbind();
  $.ajax({
    type: "POST",
    cache: false,
    url: '/scripts/nl-register.php',
    data: 'email='+e,
    success: function(data){
      alert(data);
      $('#nl p.but').click(nlSubmit);
    }
  });
}

function menuLoad() {
  // lien externe
  if (!$(this).attr('href').match('quain5.com')) {
alert('A venir !');
return false;
    window.location.href = $(this).attr('href');
    return false;
  }
  var id = $(this).parent().attr('id').substr(1);
  if (id == menuCur) return false;
  if (menuLoading) return false;
  menuLoading = true;
  $('div.presse, div.enecoute2').remove();
  var menuPrev = menuCur;
  menuCur = id;
  menuReorder('m'+id);
  menuLoaded = false; cacheLoaded = false;
  $('#cache').css({opacity: '0', display: 'block'});
  $('#cache').animate({opacity: '1'}, 400, '', function(){
    cacheLoaded = true;
    if (menuPrev) $('#m'+menuPrev+'illus').remove();
    $('#page').append('<div id="m'+menuCur+'illus"></div>');
  });
  $.ajax({
    type: "GET",
    url: '/templates/page.php',
    data: 'id='+id,
    success: function(data){
	$('#page').append(data);
	commonInit();
	menuLoaded = true;
	menuShow();
    }
  });
  return false;
}

function smenuLoad() {
  var id = $(this).attr('id').substr(1);
  if (id == 'autre') {
    bioMode = 'autre';
    id = $('#queue h3.autre:first').attr('id').substr(1);
  }
  else if (id == 'main') {
    bioMode = 'main';
    id = $('#queue h3.main:first').attr('id').substr(1);
  }
  else if (id == 'son') {
    bioMode = 'son';
    id = $('#queue h3.son:first').attr('id').substr(1);
  }
  $('#cache').css({opacity: '0', display: 'block'});
  $('#cache').animate({opacity: '1'}, 400, '', function(){
    bioCur = id;
    $('#content').empty();
    $('#queue #h'+bioCur+', #queue p.c'+bioCur).clone().appendTo('#content');
    $('#photos p').css({zIndex: '1', opacity: '0'});
    $('#photos #p'+bioCur).css({zIndex: '2', opacity: '1'});
    var txt = $('#photos #p'+bioCur).attr('credits');
    if (txt && txt.length)
      $('#credits').html('Crédits photo : '+txt);
    else
      $('#credits').html('');
    $('#bios').empty();
    $('#queue h3.'+bioMode).each(function(){
      var i = $(this).attr('id').substr(1);
      if (i != bioCur)
	$('#bios').append('<p id="q'+i+'">'+$(this).html()+'</p>');
    });
    if (bioMode == 'autre')
      $('#bios').append('<p id="qmain">Distribution principale</p><p id="qson">Son et lumière</p>');
    else if (bioMode == 'main')
      $('#bios').append('<p id="qautre">Autres distributions possibles...</p><p id="qson">Son et lumière</p>');
    else 
      $('#bios').append('<p id="qmain">Distribution principale</p><p id="qautre">Autres distributions possibles...</p>');
    $('#cache').animate({opacity: '0'}, 600, '', function(){
      $('#cache').css({display: 'none'});
    });
  });
}

function menuShow() {
  if (menuLoaded && cacheLoaded)
    $('#cache').animate({opacity: '0'}, 600, '', function(){
      $('#cache').css({display: 'none'});
      menuLoading = false;
    });
  else
    menuWait();
}

function menuWait() {
  if (menuTimerid) clearTimeout(menuTimerid);
  menuTimerid = setTimeout('menuShow()', 300);
}

function thumbsSet(e) {
  var h = $('#thumbs div').height();
  if (h < 385) return false;
  var o = $('#thumbs').offset();
  var top = Math.ceil((385 - h - 20) * (e.pageY - o.top) / 385) + 20;
  $('#thumbs div').css('top', top+'px');
}

function thumbsSetIE(e) {
  var h = $('#thumbs div').height();
  if (h < 385) return false;
  var orig = Math.ceil($(window).height() / 2);
  if (e.pageY - orig < -150) thumbsSpeed = 15;
  else if (e.pageY - orig < -100) thumbsSpeed = 8;
  else if (e.pageY - orig < -75) thumbsSpeed = 4;
  else if (e.pageY - orig < -50) thumbsSpeed = 1;
  else if (e.pageY - orig < 50) {
    thumbsSpeed = 0;
    return true;
  }
  else if (e.pageY - orig < 75) thumbsSpeed = -1;
  else if (e.pageY - orig < 100) thumbsSpeed = -4;
  else if (e.pageY - orig < 150) thumbsSpeed = -8;
  else thumbsSpeed = -15;
  return _thumbNavMove();
}

function _thumbNavMove() {
  if (!thumbsSpeed) return true;
  var top = $('#thumbs div').css('top');
  top = parseInt(top.replace('px', ''));
  if (top + thumbsSpeed > 0) {
    $('#thumbs div').css('top', '0px');
    return true;
  }
  if (top + thumbsSpeed < -1 * (385 - $(window).height())) {
    $('#thumbs div').css('top', '-'+(385 - $(window).height())+'px');
    return true;
  }
  $('#thumbs div').css('top', (top + thumbsSpeed)+'px');
  if (thumbsTimerid) clearTimeout(thumbsTimerid);
  thumbsTimerid = setTimeout('_thumbNavMove()', 1);
  return true;
}

function thumbsNavStop() {
  thumbsSpeed = 0;
}

function photoLoad() {
  if (!photoCur)
    photoCur = $('#photos p:first').attr('id').substr(1);
  var id = $(this).attr('id').substr(1);
  if (id == photoCur)
    return false;
  var h = $('#photos #p'+id+' img').height();
  if ((h > 0) && (h < 505) && ($('#photos #p'+id+' img').css('marginTop') == '0px'))
    $('#photos #p'+id+' img').css('marginTop', Math.floor((505-h)/2)+'px');
  $('#photos p').stop();
  $('#photos p').css({zIndex: '1', opacity: '0'});
  $('#photos #p'+photoCur).css({zIndex: '2', opacity: '1'});
  $('#photos #p'+photoCur).animate({opacity: '0'}, 800, '', function(){
    $('#photos #p'+photoCur).css('zIndex', '1');
  });
  $('#photos #p'+id).animate({opacity: '1'}, 800, '', function(){
    $('#photos #p'+id).css('zIndex', '2');
    photoCur = id;
  });
  var txt = $('#photos #p'+id).attr('credits');
  if (txt && txt.length)
    $('#credits').html('Crédits photo : '+txt);
  else
    $('#credits').html('');
}

function mp3Load() {
  if ($(this).hasClass('sel'))
    return false;
  $('p.enecoute').removeClass('sel');
  $(this).addClass('sel');
  $('h3.enecoute').html('<object type="application/x-shockwave-flash" data="/swfs/mp3-player-maxi.swf" width="200" height="20"> <param name="movie" value="/swfs/mp3-player-maxi.swf" /> <param name="bgcolor" value="#FFFFFFF" /> <param name="FlashVars" value="mp3='+$(this).attr('url')+'&amp;showstop=1&amp;showvolume=1&amp;autoplay=1&amp;loadingcolor=FFFFFFF&amp;bgcolor=FFFFFFF&amp;bgcolor1=FFFFFFF&amp;bgcolor2=FFFFFFF&amp;slidercolor1=A4A4A4&amp;slidercolor2=A4A4A4&amp;sliderovercolor=C30011&amp;buttoncolor=000000&amp;buttonovercolor=C30011&amp;textcolor=000000" /></object>');
}

function presseLoad() {
  if ($(this).hasClass('sel'))
    return false;
  $('#content p').removeClass('sel');
  $(this).addClass('sel');
  var id = $(this).attr('id').substr(2);
  $('#cache').css({opacity: '0', display: 'block'});
  $('#cache').animate({opacity: '1'}, 400, '', function(){
    $('div.presse').css('display', 'none');
    $('#smenu'+id).css('display', 'block');
    $('#cache').animate({opacity: '0'}, 600, '', function(){
      $('#cache').css({display: 'none'});
    });
  });
}


