/* Fonctions */
function setIndexPart(){
	var nb = $(".nosPartenaires ul").length;
	$(".nosPartenaires ul").each(function(){
		$(this).css('z-index',nb);
		nb--;
	});
	$(".nosPartenaires ul:gt(0)").hide();
}

function fadePart(ix){
	$(".nosPartenaires ul").eq(ix).fadeOut(600);
	if (ix < ($(".nosPartenaires ul").length-1)) ix++;
	else ix = 0;
	$(".nosPartenaires ul").eq(ix).fadeIn(600);
	setTimeout("fadePart('"+ix+"')", 3000);
}

function setIndex(cls){
	var nb = $("."+cls+" li").length;
	$("."+cls+" li").each(function(){
		$(this).css('z-index',nb);
		nb--;
	});
	$("."+cls+" li:gt(0)").hide();
}

function fade(ix,cls){
	$("."+cls+" li").eq(ix).fadeOut(1000);
	if (ix < ($("."+cls+" li").length-1)) ix++;
	else ix = 0;
	$("."+cls+" li").eq(ix).fadeIn(1000);
	setTimeout("fade('"+ix+"','"+cls+"')", 4000);
}

/* Player flash js */
var videoListener = new Object();
videoListener.onInit = function(){
  play();
  getFlashObject().SetVariable("method:setVolume", 80);
};
videoListener.onClick = function(){};
videoListener.onKeyUp = function(pKey){};
videoListener.onFinished = function(){ stop(); play(); };
videoListener.onUpdate = function(){};

function getFlashObject() {
	return document.getElementById("videoFlashCorps");
}
function play(){
	if (videoListener.position == 0) getFlashObject().SetVariable("method:setUrl", "/video/mozaic.f4v");
  getFlashObject().SetVariable("method:play", "");
}
function stop(){
  getFlashObject().SetVariable("method:stop", "");
}
function setPosition(position){
  getFlashObject().SetVariable("method:setPosition", position);
}

/* Colonne gauche */
var initLeftCol = function(){
  setIndex("temoignagesBox");
  setTimeout("fade('0','temoignagesBox')", 4000);
}

/* Home */
var initHome = function(){
  setIndexPart();
  setTimeout("fadePart('0')", 3000);

  $(".mute").click(function(){
    if(videoListener.volume>0){
      getFlashObject().SetVariable("method:setVolume", 0);
      $(this).removeClass("ON").addClass("OFF");
    }
    else{
      getFlashObject().SetVariable("method:setVolume", 80);
      $(this).removeClass("OFF").addClass("ON");
    }
    return false;
  });
}

/* Parrain */
var initParrain = function(){
	$(".videoBox a").click(function(){
		$(".videoBox").click();
		return false;
	});
	$(".videoBox").click(function(){
		$("#videoModal").modal({
			overlayCss:{ backgroundColor:'#fff' },
			opacity:80,
			position:['130px']
		});
	});
}

/* Témoignages */
var initTemoignage = function(){
  $(".video").click(function(){
		$(this).parent().parent().find(".temoignageVideo").modal({
			overlayCss:{ backgroundColor:'#fff' },
			opacity:80,
			position:['130px']
		});
		return false;
	});
}

/* Partenaires */
var initPartenaires = function(){
	var zIndex = 990;
	$("li.deroule").each(function(){
		$(this).css("z-index",zIndex);
		zIndex--;
	});
	$("li.deroule").mouseenter(function(){
		if($("li.derouleOn").length){
			$("li.derouleOn div").slideUp(200);
			$("li.derouleOn").removeClass("derouleOn");
		}
		$(this).find("div").slideDown(200);
		$(this).addClass("derouleOn");
	});
	$(".partenairesBox li").mouseleave(function(){
		$(this).find("div").slideUp(200);
		$(this).removeClass("derouleOn");
	});
}

/* LMDT en photos */
var initLmdtPhotos = function(){
  // Variable
  widthVignettes = 159;
  nbrVignettes = $('#top-slide-box ul li').length;
  enMouvement = false;
  incSlide = 1;
  nbrSlide = Math.ceil(nbrVignettes/4);

  // Attribution de la taille de la liste
  $('#top-slide-box ul').css('width',(widthVignettes*nbrVignettes)+'px');

  // Clic fleche
  $('#top-slide #next').click(function(){
    if (!enMouvement && incSlide<nbrSlide){
      enMouvement = true;
      incSlide++;
      $('#top-slide-box ul').animate({'left':parseInt($('#top-slide-box ul').css('left'))-(widthVignettes*4)+'px'}, 800, function(){enMouvement = false;});
    }
    return false;
  });
  $('#top-slide #prev').click(function(){
    if (!enMouvement && incSlide>1){
      enMouvement = true;
      incSlide--;
      $('#top-slide-box ul').animate({'left':parseInt($('#top-slide-box ul').css('left'))+(widthVignettes*4)+'px'}, 800, function(){enMouvement = false;});
    }
    return false;
  });

  // Clic sur une vignette
  $('#top-slide ul li a').click(function(){
    image = $(this).attr('href');
    $('#top-slide ul li').removeClass('ON');
    $(this).parent().addClass('ON');
    $('#apercu').css('background','url('+$('#apercu img').attr('src')+') no-repeat');
    $('#apercu img').fadeOut('0', function(){ $('#apercu img').attr('src',image); });
    $('#apercu img').fadeIn();
    $('#texte-apercu li').hide();
    $('#texte-apercu li:eq('+$(this).parent().index()+')').show();
    return false;
  });
}

var initActualites = function(){
  for(i=1;i<=nbVisu;i++){
    $(".visu-"+i+" a").lightBox({
      overlayBgColor: '#fff',
      imageBtnClose: '/imagerie/actus/bt_fermer.gif',
      txtOf: 'sur'
    });
  }
  $(".openVideo").click(function(){
    $("#videoModal2").modal({
  		overlayCss:{ backgroundColor:'#fff' },
  		opacity:80,
  		position:['130px']
    });
    return false;
  });
}

var initActivites = function(){
  $(".domaine").mouseenter(function(){
    $(this).css("border-color","#ff8004");
    $(this).find("img").css("opacity","0.2");
    $(this).find("ul").show();
  }).mouseleave(function(){
    $(this).css("border-color","#dbdbdb");
    $(this).find("ul").hide();
    $(this).find("img").css("opacity","1");
  });
}

/* Contactez-nous */
var initContact = function () {
  if($(window).height() > 800)
    topHeight = 100;
  else
    topHeight = 0;
  $("#demande-brochure").click(function(){
    $("#type_form").val("demande_brochure");
    $(".modal-contact #header-modal-contact-brochure, .modal-contact .modal-brochure").hide();
    $(".modal-contact #header-modal-contact-inscription, .modal-contact #header-modal-contact-inscription-titre").show();
    $(".modal-contact").modal({
  		overlayCss:{ backgroundColor:'#000' },
  		opacity:80,
  		position:[topHeight+'px']
  	});
    return false;
  });

  $("#demande-inscription").click(function(){
    $("#type_form").val("demande_inscription");
    $(".modal-contact #header-modal-contact-inscription, .modal-contact #header-modal-contact-inscription-titre").hide();
    $(".modal-contact #header-modal-contact-brochure, .modal-contact .modal-brochure").show();
    $(".modal-contact").modal({
  		overlayCss:{ backgroundColor:'#000' },
  		opacity:80,
  		position:[topHeight+'px']
  	});
    return false;
  });

  if(type_modal_formulaire != '') {
    if (type_modal_formulaire == "demande_brochure")
      $("#demande-brochure").click();
    else if (type_modal_formulaire == "demande_inscription")
      $("#demande-inscription").click();
    else if (type_modal_formulaire == "succes1") {
      $(".modal-contact-succes h3").html("S'inscrire à une de nos réunions d'informations");
      $(".modal-contact-succes").modal({
    		overlayCss:{ backgroundColor:'#000' },
    		opacity:80,
    		position:[topHeight+'px']
    	});
    }
    else if (type_modal_formulaire == "succes2") {
      $(".modal-contact-succes h3").html("Recevoir par email notre brochure<br /> «Devenir courtier en travaux»");
      $(".modal-contact-succes").modal({
    		overlayCss:{ backgroundColor:'#000' },
    		opacity:80,
    		position:[topHeight+'px']
    	});
    }
  }
}

$(document).ready(function() {
  if($(".mainNav").length > 0) initLeftCol();
	if($(".MessagePDGBox").length > 0) initHome();
	if($(".parrainBox").length > 0) initParrain();
	if($(".temoignageBox").length > 0) initTemoignage();
	if($(".partenairesBox").length > 0) initPartenaires();
	if($(".lmdt-photosBox").length > 0) initLmdtPhotos();
	if($(".actusBox").length > 0) initActualites();
	if($(".activitesBox").length > 0) initActivites();
  if($(".modal-contact").length > 0) initContact();
});
