/* Fonctions a charger en Livequery pour toutes les vues + vues AJAX */
function initOnce() {
	// Gestion des rubriques de la FAQ
	$('a.faq').livequery('click', function() {
		if ($(this).parent().hasClass('active'))
		{
			// On referme la rubrique deja active
			$(this).parent().removeClass('active');
			$(this).parent().next().slideUp('fast');
		}
		if (!$(this).parent().hasClass('active'))
		{
			// On referme toutes les rubriques actives
			$(".faq").parent().removeClass('active');
			$(".faq").parent().next().slideUp('fast');
			// Ouverture d'une rubrique de la FAQ
			$(this).parent().addClass('active');
			$(this).parent().next().slideDown('fast');
		}
		return false;
	});
	
	// Application de tooltip a tous les liens d'id=tooltip
	$('a[rel=tooltip]').livequery('mouseover', function(e) {
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title','');
		
		//Append the tooltip template and its value
		$(this).append('<div id="tooltip"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div></div>');		
		
		//Set the X and Y axis of the tooltip
		$('#tooltip').css('top', e.pageY + 15 );
		$('#tooltip').css('left', e.pageX + 15 );
		
		//Show the tooltip with faceIn effect
		$('#tooltip').fadeIn('500');
		//$('#tooltip').fadeTo('10',0.8);
	});
	
	$('a[rel=tooltip]').livequery('mousemove', function(e) {
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 15 );
		$('#tooltip').css('left', e.pageX + 15 );
	});
	
	$('a[rel=tooltip]').livequery('mouseout', function() {
		//Put back the title attribute's value
		$(this).attr('title',$('.tipBody').html());
		//Remove the appended tooltip template
		$(this).children('div#tooltip').remove();
	});
}

/* Mini plugin de gestion de delay */
jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	return this;
};

/* Affichage du clavier virtuel */
function focus_pwd() {
	$("#mask").hide();
	if ($("#keyboard").css("display") == "none") {
		$("#keyboard").toggle("fast");
	}
}

/* Toggle des onglets principaux de l'espace perso (et du detail des garanties) */
function toggle_tab(num,tot) {
	var current=1;
	var total=eval(tot)+1;
	for (current=1;current<total;current++)
	{	$("#tab"+current).slideUp('slow');
		$("#onglet"+current).removeClass('current');
	}
	$("#tab"+num).slideDown('slow');
	$("#onglet"+num).addClass('current');
	initDOM2();
}

/* Toggle des sous-onglets de l'espace perso */
function toggle_tabtab(num,tot,start) {
	var cur=eval(start);
	var total=eval(tot)+eval(start);
	for (current=cur;current<total;current++)
	{	$("#tabtab"+current).slideUp('slow');
		$("#sousonglet"+current).removeClass('current');
	}
	$("#tabtab"+num).slideDown('slow');
	$("#sousonglet"+num).addClass('current');
	initDOM2();
}

/* Affichage-masquage du champ conjoint */
function toggle_conjoint() {
	if ($("#conjoint-Oui").attr('checked')) {
		$("#conjoint").slideDown('slow');
	}
	else {
		$("#conjoint").slideUp('slow');
	}
}

/* Affichage-masquage du champ conjoint */
function toggle_nomtire() {
	if ($("#paiement-2").attr('checked')) {
		$("#nomtire").slideDown('slow');
	}
	else {
		$("#nomtire").slideUp('slow');
	}
}

/* Affichage-masquage de nb enfants */
function addEnfants(nb,devis) {
   var num= eval(nb)+1;
   var current=1;
   var test = 1;
   $('#enfants').slideUp("slow");
   //$('#enfants').empty();
   for (test=1;test<6;test++)
   {
	   //$("#enfant_"+test).addClass('rowhidden');
	   $("#enfant_"+test).hide();
   }
   for (current=1;current<num;current++)
   {
	   //$("#enfant_"+current).removeClass('rowhidden');
	   //$("#enfant_"+current).addClass('row');
	   $("#enfant_"+current).show();
   }
   $('#enfants').slideDown("slow");
}

/* Affichage-masquage de nb amis */
function addAmis(nb,devis) {
   var num= eval(nb)+1;
   var current=1;
   var test = 1;
   $('#amis').slideUp("slow");
   for (test=1;test<6;test++)
   {
	   $("#ami"+test).hide();
   }
   for (current=1;current<num;current++)
   {
	   $("#ami"+current).show();
   }
   $('#amis').slideDown("slow");
}

/* Creation LightBox */
function lightbox (id_lb,num_lb)
{
  //Verification de l'existence d'une lightbox.
  if($(".receptacle").length==0)
  {	
	//Insertion de la blackbox
	$("body").prepend('<div class="black_box" style="filter: alpha(opacity=40);"></div>');
	// Insertion du receptacle
	$("body").prepend('<div class="receptacle"></div>');
  }
  
  	//Appel Ajax
  	$(".receptacle").append($("#"+id_lb).html());
  	$("#"+id_lb).empty();
  	
	//Centre le contenu
	centre_lightbox($("#"+id_lb+num_lb));
	
	//Hack Dropdownliste IE6
	$(".black_box").bgiframe();
	
	//Apparition
	$('.black_box')
			.css({width: $(window).width(), height: $(document).height()})
			.fadeIn("slow",function(){$("#"+id_lb+num_lb).fadeIn("slow");});
}


/* Destruction de LightBox */
function kill_lightbox (id_lb)
{
  $('#'+id_lb).fadeOut("slow",function(){
				$(".black_box").fadeOut("slow",function(){$('.receptacle, .black_box').remove()})
			});
}


/* Centrage de LightBox */
function centre_lightbox (el)
{
  var top = $(window).scrollTop()+($(window).height()/2)-(el.height()/2);
	var left = ($(window).width()/2)-(el.width()/2);
	el.css({top: top +"px", left: left +"px"});	
}

/* Fonctions appelees au chargement de la page */
$(document).ready(function() {
	
	// Lien d'ouverture-fermeture du footer
	$(".link4").toggle(function(){
		// Ouverture du footer
		$("#toggler").attr("src","public/images/footer-fermer.gif");
		$("#footer-hidden").slideDown('slow');
		$(this).delay(600,function(){
			$(this).scrollTo('max',{duration:500});
		});
	},function(){
		// Fermeture du footer
		$("#toggler").attr("src","public/images/footer-ouvrir.gif");
		$("#footer-hidden").slideUp('slow');
	});
	
	// Affichage-masquage du clavier virtuel quand le masque est clique
	$("#mask").livequery('click', function(e) {
		$("#mask").hide();
		$("#keyboard").toggle("fast");
	});
	
	/* Fonction de recuperation des valeurs cliquees sur le clavier (bind) */
	$("#keyboard input").live('click', function(e) {
		if ($(this).val() == 'Effacer' ) {
			$('#pwd').val("");
		}
		else {	   
			if ($(this).val() == 'Fermer' ) {
				$('#keyboard').toggle("fast");
			}
			else {
				$('#pwd').replaceSelection($(this).val(), true);
			}
		}
	});
	
	// On appelle manuellement les fonctions utiles a toutes les pages (chargees en Livequery)
	initOnce();
	// On appelle les fonctions utiles de form2.js
	initDOM2();
	
});
