
var curTem = 1;
var curCanal = 1;
var tvTimerid = null;
var nlTimerid = null;


function prepareDocument() {
  preparePageStage1();
  $('.content').wrapAll('<div id="content" class="page"/>');
  $('.footer').wrapAll('<div id="footer" class="page"/>');
  $('.tv').wrapAll('<div id="tv" class="page"/>');
  $('.tvlegend').wrapAll('<div id="tvlegend" class="page"/>');
  $('#tv').append('<div id="canalp"/><div id="canal1" class="tvbutton sel"/><div id="canal2" class="tvbutton"/><div id="canal3" class="tvbutton"/><div id="canal4" class="tvbutton"/><div id="canal5" class="tvbutton"/>');
  $('.page').wrapAll('<div id="page"/>');
  $('#page').append('<div id="tvbg"/><div id="mbg2" class="mbg"/><div id="mbg3" class="mbg"/><div id="mbg4" class="mbg"/><div id="mbg5" class="mbg"/><div id="popupbg"/><div id="mire"/>');
  if (!$('#content').length)
    $('#page').append('<div id="content"/>');
  if (!$('#pagebg').html().length) bgLoad();
  else bgLoaded();
  preparePage();
  resizeDocument();
  // actions
  $(window).resize(resizeDocument);
  $('#nlform input').keypress(function(e){
    if (e.which == 13) nlSubmit();
  });
  $('h2.menu a, p.menu a').hover(menuOver, menuOut);
  $('h2.menu a, p.hback a, #footer a, #footer2 a, #canalp').live('click', menuLoad);
  $('h3.hist a, p.hist a').live('click', smenuLoad);
  $('#txt p.txt2 a').live('click', menuLoad);
  $('div.tvbutton').click(tvPhotoSet);
  $('#tv h2').hover(presseOver, presseOut);
  $('#canalp').hover(canalOver, canalOut);
  $('#cancel').live('click', formCancel);
  $('#validate').live('click', formValidate);
  $('#allo').live('mouseover', alloOver);
  $('#allo').live('mouseout', alloOut);
  $('#devis').live('click', formShow);
  $('#nexemple p.next').live('click', exempleNext);
  $('#nexemple p.prev').live('click', exemplePrev);
  $('#navbar p.next').live('click', temNext);
  $('#navbar p.prev').live('click', temPrev);
}

function preparePageStage1() {
  $('.txt').wrapAll('<div id="txt" class="content"/>');
  $('#tvphoto').remove();
  $('.tvphoto').wrapAll('<div id="tvphoto" class="page"/>');
  prepareMenuStage1();
  $('.infos').wrapAll('<div id="infos" class="page"/>');
}

function prepareMenuStage1() {
  $('.detail').wrapAll('<div class="inner details"/>');
  $('.details').wrapAll('<div id="details" class="infos"/>');
  $('.exemple').wrapAll('<div id="exemples" class="infos"/>');
}

function preparePage() {
  curTem = 1;
  curCanal = 1;
  if ($('h2.menu a.sel').length)
    $('#mbg'+$('h2.menu a.sel').parent().attr('id').substr(1)).addClass('sel');
  $('#tvlegend p').each(function(){
    $(this).css('marginTop', Math.floor((158-$(this).height())/2 - 5)+'px');
  });
  $('#tvphoto img, #tvlegend p').css('display', 'none');
  $('#pho1, #leg1').css('display', 'block');
  if ($('.waiting').length) {
    $('#details div.detail').clone().appendTo('body');
    $('#details div.detail').attr('id', '');
    $('#tem1').removeClass('detail');
    $('#tem1').addClass('waiting');
    $('.waiting').css('display', 'none');
    $('#page').append('<div id="navbar"><p class="next">suivant</p><p class="prev">précédent</p></div>');
  }
  if (!$('h3.hist').length)
    $('#details div.inner').jScrollPane({scrollbarWidth: 19, showArrows: true, 
	dragMinHeight: 16, dragMaxHeight: 16, scrollbarOnLeft: true});
  prepareMenu();
  if (jQuery.support.opacity)
    $('#infos').css('opacity', '0');
}

function prepareMenu() {
  $('#exemples').css('overflow', 'hidden');
  $('#exemples p').css({position: 'absolute', top: '0', left: '455px'});
  $('#exemples p:first').css('left', '0');
  $('#exemples p:first').addClass('first');
  if ($('#exemples').length)
    $('#infos').append('<div id="nexemple"><p class="next">exemple suivant</p><p class="prev">exemple précédent</p></div>');
  if (jQuery.support.opacity)
    $('#details, #exemples, #texemple, #nexemple').css('opacity', '0');
}

function unpreparePage() {
  if (tvTimerid) clearTimeout(tvTimerid);
  $('#mire').css('display', 'block');
  $('#tvphoto, #content').empty();
  $('#tvlegend, #navbar, #infos, .waiting, #txt').remove();
  $('div.tvbutton').removeClass('sel');
  $('#canal1').addClass('sel');
}

function unprepareMenu() {
  $('#details, #exemples, #texemple, #nexemple').remove();
}

function resizeDocument() {
  var h = $(window).height();
  var w = $(window).width();
  if (w <= 1096) {
    $('#page').css({left: '-83px'});
    $('#pagebg').width(1096);
  }
  else if (w <= 1216) {
    $('#page').css({left: (w - 1096 - 83)+'px'});
    $('#pagebg').width(1216 + w - 1096 - 83);
  }
  else {
    $('#page').css({left: '37px'});
    $('#pagebg').width(w);
  }
  $('#mbg2, #m2').removeClass('big');
  if (h <= 600)
    $('#page').css('top', '-80px');
  else if (h <= 864) {
    $('#pagebg').height(864);
    $('#page').css('top', Math.floor(-80 + (h - 600) / 3.3)+'px');
  }
  else {
    $('#mbg2, #m2').addClass('big');
    $('#page').css('top', '0px');
    $('#pagebg').height(h);
  }
}

function documentPrepared() {
  preloadImages('/images/allo.gif', '/images/bulle1-sel.png', '/images/bulle1-big-sel.png', '/images/bulle2-sel.png', '/images/bulle3-sel.png', '/images/bulle4-sel.png', '/images/tv-bouton-presse.png');
  pagePrepared();
}

function pagePrepared() {
  if ($('#tvphoto img').length > 1)
    tvPhotoWait();
  if ($('#tvphoto img').length)
    $('#mire').css('display', 'none');
  if (jQuery.support.opacity)
    $('#infos, #details, #exemples, #texemple, #nexemple').animate({opacity: '1'});
//  else
//    $('#infos, #details, #exemples, #texemple, #nexemple').css('opacity', '1');
}

function menuPrepared() {
  if (jQuery.support.opacity)
    $('#details, #exemples, #texemple, #nexemple').animate({opacity: '1'});
//  else
//    $('#details, #exemples, #texemple, #nexemple').animate('opacity', '1');
}

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 alloOver() {
  $(this).attr('src', $(this).attr('src').replace('png', 'gif'));
}

function alloOut() {
  $(this).attr('src', $(this).attr('src').replace('gif', 'png'));
}

function presseOver() {
  if ($(this).hasClass('sel'))
    return false;
  $('#canalp').addClass('over');
}

function presseOut() {
  if ($(this).hasClass('sel'))
    return false;
  $('#canalp').removeClass('over');
}

function canalOver() {
  if ($(this).hasClass('sel'))
    return false;
  $('#tv h2 a').addClass('over');
}

function canalOut() {
  if ($(this).hasClass('sel'))
    return false;
  $('#tv h2 a').removeClass('over');
}

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 menuOver() {
  var id = $(this).parent().attr('id').substr(1);
  if (!$('#mbg'+id).hasClass('sel'))
    $('#mbg'+id).addClass('over');
}

function menuOut() {
  var id = $(this).parent().attr('id').substr(1);
  if (!$('#mbg'+id).hasClass('sel'))
    $('#mbg'+id).removeClass('over');
}

function bgLoad() {
  $.ajax({
    type: "GET",
    url: '/templates/bg.php',
    success: function(data){
      $('#pagebg').append(data);
      bgLoaded();
    }
  });
}

function bgLoaded() {
  var txt = $('#pagebg p').html();
//  var nb = $('#pagebg p span').length;
//  $('#pagebg p span:last').addClass('bold');
//  var n = 2;
  var i = 2;
  for (i = 2; i <= 5; i ++) {
    $('#pagebg p').append('&nbsp;&#8226;&nbsp;'+txt);
//    $('#pagebg p span:eq('+(nb * i - n)+')').addClass('bold');
//    n ++; if (n > nb) n = 1;
  }
}

function tvPhotoWait() {
  if (tvTimerid) clearTimeout(tvTimerid);
  tvTimerid = setTimeout('tvPhotoNext()', 8000);
}

function tvPhotoNext() {
  var n = curCanal + 1;
  if (n > $('#tvphoto img').length)
    n = 1;
  _tvPhotoSet(n);
  curCanal = n;
  tvPhotoWait();
}

function tvPhotoSet() {
  var id = $(this).attr('id').substr(5);
  if (parseInt(id) > $('#tvphoto img').length)
    return false;
  _tvPhotoSet(id);
}

function _tvPhotoSet(id) {
  if (tvTimerid) clearTimeout(tvTimerid);
  $('#tvphoto img, #tvlegend p').css('display', 'none');
  $('#pho'+id+', #leg'+id).css('display', 'block');
  $('div.tvbutton').removeClass('sel');
  $('#canal'+id).addClass('sel');
}

function ppAlert(mes) {
  $('#popup .alert').html(mes);
}

function formShow() {
  if (!jQuery.support.opacity)
    $('#popup').css('display', 'block');
  else {
    $('#popup').css({opacity: '0', display: 'block'});
    $('#popup').animate({opacity: '1'}, 600);
  }
  $('#popupbg').css('display', 'block');
}

function formCheck() {
  var f = document.myform;
  f.prenom.value = f.prenom.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.prenom.value) {
    ppAlert("Merci d'indiquer votre prénom");
    return false;
  }
  f.nom.value = f.nom.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.nom.value) {
    ppAlert("Merci d'indiquer votre nom");
    return false;
  }
  f.email.value = f.email.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.email.value) {
    ppAlert("Merci d'indiquer votre email");
    return false;
  }
  var e = emailIsValid(f.email.value);
  if (e == null) {
    ppAlert('email incorrect');
    return false;
  }
  f.email.value = e;
  f.tel.value = f.tel.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.tel.value) {
    ppAlert("Merci d'indiquer votre téléphone");
    return false;
  }
  f.cp.value = f.cp.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.cp.value) {
    ppAlert("Merci d'indiquer votre code postal");
    return false;
  }
  f.ville.value = f.ville.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.ville.value) {
    ppAlert("Merci d'indiquer votre ville");
    return false;
  }
  f.periode.value = f.periode.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.periode.value) {
    ppAlert("Merci d'indiquer la période souhaitée");
    return false;
  }
  if (!f.nature.selectedIndex) {
    ppAlert("Merci d'indiquer la nature de l'intervention");
    return false;
  }
  f.description.value = f.description.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '');
  if (!f.description.value) {
    ppAlert("Merci de décrire l'intervention");
    return false;
  }
  return true;
}

function formCancel() {
  $('#popup').fadeOut(500, function(){
    $('#popupbg, #popup').css('display', 'none');
  });
}

function formValidate() {
  var f = document.myform;
  if (!formCheck())
    return false;
  ppAlert("Envoi en cours...");
  $('#popupbg').css('zIndex', '1000000');
  $.ajax({
    type: "POST",
    cache: false,
    url: '/scripts/contact.php',
    data: 'prenom='+f.prenom.value+'&nom='+f.nom.value+'&email='+f.email.value+
	'&tel='+f.tel.value+'&ville='+f.ville.value+'&cp='+f.cp.value+'&nature='+
	f.nature.options[f.nature.selectedIndex].value+'&description='+f.description.value+
	'&periode='+f.periode.value,
    success: function(data){
      $('#details div.inner').prepend('<p>'+data+'</p>');
      $('#popupbg').css('zIndex', '10000');
      formCancel();
    }
  });
}

function nlSubmit() {
  var f = document.nlform;
  f.nlemail.value = f.nlemail.value.replace(/^(\s+)/, '').replace(/(\s+)$/, '').replace('entrez ici votre email', '');
  if (!f.nlemail.value) {
    nlAlert("email manquant");
    return false;
  }
  var e = emailIsValid(f.nlemail.value);
  if (e == null) {
    nlAlert('email incorrect');
    return false;
  }
  $.ajax({
    type: "POST",
    cache: false,
    url: '/scripts/nl.php',
    data: 'email='+e,
    success: function(data){
      nlAlert(data, true);
    }
  });
}

function nlAlert(mes, clean) {
  if (nlTimerid) clearTimeout(nlTimerid);
  if (!$('#nlalert').length)
    $('#page').append('<div id="nlalert"/>');
  $('#nlalert').html(mes);
  if (!clean) return false;
  nlTimerid = setTimeout("nlAlert('')", 5000);
}

function exempleNext() {
  var id = $('#exemples p.first').index();
  id ++;
  $('#nexemple p.prev').css('display', 'block');
  if (id == $('#exemples p').length - 1)
    $('#nexemple p.next').css('display', 'none');
  $('#exemples p.first').animate({left: '-455px'}, 600);
  $('#exemples p:eq('+id+')').animate({left: '0px'}, 600, '', function(){
    $('#exemples p.first').removeClass('first');
    $('#exemples p:eq('+id+')').addClass('first');
  });
}

function exemplePrev() {
  var id = $('#exemples p.first').index();
  id --;
  $('#nexemple p.next').css('display', 'block');
  if (id == 0)
    $('#nexemple p.prev').css('display', 'none');
  $('#exemples p.first').animate({left: '455px'}, 600);
  $('#exemples p:eq('+id+')').animate({left: '0px'}, 600, '', function(){
    $('#exemples p.first').removeClass('first');
    $('#exemples p:eq('+id+')').addClass('first');
  });
}

function temNext() {
  var id = curTem + 1;
  $('#navbar p.prev').css('display', 'block');
  if (!$('#tem'+(id+1)).length)
    $('#navbar p.next').css('display', 'none');
  if (!jQuery.support.opacity) {
    $('#details').css('display', 'none');
    temRefresh(id);
  }
  else
    $('#details').animate({opacity: '0'}, 500, '', function(){
      temRefresh(id);
    });
}

function temPrev() {
  var id = curTem - 1;
  $('#navbar p.next').css('display', 'block');
  if (id == 1)
    $('#navbar p.prev').css('display', 'none');
  if (!jQuery.support.opacity) {
    $('#details').css('display', 'none');
    temRefresh(id);
  }
  else
    $('#details').animate({opacity: '0'}, 500, '', function(){
      temRefresh(id);
    });
}

function temRefresh(id) {
  $('#details').html('<div class="inner"><div class="detail">'+$('#tem'+id).html()+'</div></div>');
  if (!jQuery.support.opacity)
    $('#details').css('display', 'block');
  $('#details div.inner').jScrollPane({scrollbarWidth: 19, showArrows: true, 
	dragMinHeight: 16, dragMaxHeight: 16, scrollbarOnLeft: true});
  if (jQuery.support.opacity)
    $('#details').animate({opacity: '1'}, 500);
  curTem = id;
}

function menuLoad() {
  if ($(this).hasClass('direct'))
    return true;
  var id = 0;
  if ($(this).attr('id') == 'canalp')
    id = 6;
  else
    id = $(this).parent().attr('id').substr(1);
  var container = 'infos';
  if (!$('#infos').length)
    container = 'besoin';
  $('#navbar, #exemples, #texemple, #nexemple').animate({opacity: '0'}, 500);
  $('#popupbg').css('display', 'block');
  $('#txt, #allo, #allo2').animate({opacity: '0'}, 500);
  $('#'+container).animate({opacity: '0'}, 500, function(){
    unpreparePage();
    _menuLoad(id);
  });
  return false;
}

function _menuLoad(id) {
  $('h2.menu a, .mbg, #canalp').removeClass('sel');
  $.ajax({
    type: "GET",
    cache: false,
    url: '/templates/rubrique.php',
    data: 'id='+id,
    success: function(data){
      $('#page').append(data);
      $('#m'+id+' a').addClass('sel');
      if (id == 6)
	$('#canalp').addClass('sel');
      preparePageStage1();
      preparePage();
      pagePrepared();
      $('#popupbg').css('display', 'none');
    }
  });
}

function smenuLoad() {
  var id = $(this).parent().attr('id').substr(2);
  $('#exemples, #texemple, #nexemple').animate({opacity: '0'}, 500);
  $('#popupbg').css('display', 'block');
  $('#details').animate({opacity: '0'}, 500, function(){
    unprepareMenu();
    _smenuLoad(id);
  });
  return false;
}

function _smenuLoad(id) {
  $.ajax({
    type: "GET",
    cache: false,
    url: '/templates/srubrique.php',
    data: 'id='+id,
    success: function(data){
      $('#infos').append(data);
      prepareMenuStage1();
      prepareMenu();
      menuPrepared();
      $('#popupbg').css('display', 'none');
    }
  });
}


