/*
 * Sert à afficher les tooltips
 * @param decal_h : Décalage horizontal par rapport au coin supérieur gauche de l'élément survolé
 * @param decal_v : Décalage vertical par rapport au coin supérieur gauche de l'élément survolé
 * @param css_classe : Classe CSS de la Tool Tip
 */
jQuery.fn.az_tooltip = function(options) {
	options = jQuery.extend({
		decal_h : 10,
		decal_v : 10,
		css_classe:"tooltip_div"
	}, options); 
	
	tips = null;
	titre = null;
	//$(this).css('cursor','pointer');
	$(this).load(function(){
			$(this).css('cursor','pointer');		
		}).mouseover(function(){
		tips  = document.createElement('div');
		titre = this.title;	
		tips.innerHTML = titre;
		
		$(tips).css({
			"position":"absolute",
			"top": ($(this).offset().top + this.height + options.decal_h) + "px",
			"left": ($(this).offset().left + this.width + options.decal_v) + "px"
			});
		tips.className = options.css_classe;
		this.title = "";
		$(this).after(tips);
	}).mouseout(function(){
		this.title = titre;
		$(tips).remove();
	});
};
(function($) {

	$.fn.az_toolTips = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 10,		
			yOffset: 25
		}; 
			
		var options = $.extend(defaults, options);  
		var edit;
		this.each(function() {
			contenu = $(this).find(".az_tt_contenu");
			if( contenu.hasClass("az_contenu_edit") )edit=true;
			else edit=false;
			header = $(this).find(".az_tt_header").hover(function(e){
						if( !edit )
						{
							y = e.pageY - options.yOffset;
							x = e.pageX + options.xOffset;
						}
						else
						{
							y = this.offsetTop;
							x = this.offsetLeft + this.offsetWidth + options.xOffset;
							contenu.mouseleave(function(){
									$(this).hide();
								});							
						}
						contenu.css("top",y + "px")
								.css("left",x + "px")
								.fadeIn("fast");
						
					},function(e){
						if( !edit) contenu.hide();
						else
						{
							if( e.pageX < this.offsetLeft  // Part par la gauche
								|| (e.pageY - 1) < this.offsetTop // Part par le haut
								|| e.pageY > (this.offsetTop + this.offsetHeight) // Part par le bas
							 )
								contenu.hide();
						}
					});
			$(this).mousemove(function(e){
				if( !edit )
				{
					contenu
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px");
				}			
			});
		});	  
	};
})(jQuery);
/*
 * Sert à changer le statut des étapes
 * @param couleur : La couleur de l'objet
 * @param Xpage : La page ou est situé l'ajax
 * @param image_* : Chemin des images nécessaires
 */
jQuery.fn.az_Xstatut = function(options) {
	options = jQuery.extend({
		couleur : null,
		Xpage : null,
		image_rouge_on : "fileadmin/templates/img/standbyon.gif",
		image_rouge_off : "fileadmin/templates/img/standbyoffinactif.gif",
		image_orange_on : "fileadmin/templates/img/etaponworkson.gif",
		image_orange_off : "fileadmin/templates/img/sousetaponworksoffinactif.gif",
		image_vert_on : "fileadmin/templates/img/sousetapdoneon.gif",
		image_vert_off : "fileadmin/templates/img/sousetapdoneoffinactif.gif"
	}, options); 
	
	$(this).load(function(){
			$(this).css('cursor','pointer');
		}).click(function(){
			obj = this;
			ssetape = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
			// Mise à jour du statut de la sous etape
			imgs = ssetape.getElementsByTagName('img');
	
			for( i=0; i<imgs.length; i++ )
			{
				if( imgs[i].className=="jsrouge" )
					imgs[i].src = ( options.couleur == "rouge")?options.image_rouge_on:options.image_rouge_off;
				if( imgs[i].className=="jsorange" )
					imgs[i].src = ( options.couleur == "orange")?options.image_orange_on:options.image_orange_off;
				if( imgs[i].className=="jsvert" )
					imgs[i].src = ( options.couleur == "vert")?options.image_vert_on:options.image_vert_off;

			}
			$.post(options.Xpage, { az_op: 1, az_id: ssetape.id, az_couleur: options.couleur }, function(data){});
		
			// Calcul du statut de l'etape
			imgs = ssetape.parentNode.parentNode.parentNode.getElementsByTagName('img');
		
			rouge = orange = vert = 0;
			for( i=0; i<imgs.length; i++ )
			{
				if( imgs[i].src.match( new RegExp(options.image_rouge_on) ) )
					rouge++;
				else if( imgs[i].src.match( new RegExp(options.image_orange_on) ) )
					orange++;
				else if( imgs[i].src.match( new RegExp(options.image_vert_on) ) )
					vert++;
			}
			total = rouge + orange + vert;
		
			num = ( ie )?0:1;

			imgs = ssetape.parentNode.parentNode.parentNode.parentNode.childNodes[num].childNodes[num].getElementsByTagName('img');
			// Permet de vérifier la présence d'une info bulle
			num = ( imgs[1].src.match( new RegExp(options.image_rouge_on) ) || imgs[1].src.match( new RegExp(options.image_rouge_off) ) )?1:2;
			
			imgs[num].src = options.image_rouge_off;
			imgs[num+1].src = options.image_orange_off;
			imgs[num+2].src = options.image_vert_off;
		
			if( vert == total )
				imgs[num+2].src = options.image_vert_on;
			else if( rouge == total )
				imgs[num].src = options.image_rouge_on;
			else
				imgs[num+1].src = options.image_orange_on;
		
			return false;
		});
};
/*
 * Sert à plier/déplier le parcours du porteur
 * @param type : Si différent de normal, on considére qu'il s'agit de tout le parcours
 * @param classe : La classe CSS du type normal
 * @param html_* : Chemin des images nécessaires
 */
jQuery.fn.az_plierDeplierPorteur = function(options) {
	options = jQuery.extend({
		type : "normal",
		classe : "fermer",
		html_ouvrir : 'Ouvrir<img src="fileadmin/templates/img/flechebasactive.gif" style="padding-left: 5px;padding-top: 7px;"/>',
		html_fermer : 'Fermer<img src="fileadmin/templates/img/flechedroite.gif" style="padding-left: 5px;padding-top: 7px;"/>'
	}, options); 
	
	$(this).load($(this).css('cursor','pointer'))
                .click(function(){
			if( options.type == "normal" )
			{
				/*obj = this;
	
				while( obj.nodeName != 'LI' )
					obj = obj.parentNode;
				num = ( ie )?1:3;
		
				$(obj.childNodes[num]).toggle("slow");

		
		
				num = ( ie )?0:1;
				obj = this.childNodes[num].childNodes[num].childNodes[num];
				obj.innerHTML = ( obj.innerHTML.match( new RegExp("Ouvrir") ) )?options.html_fermer:options.html_ouvrir;*/
				
				num = ( ie )?0:1;
				
				obj = this;
				while( obj.nodeName != 'LI' )
					obj = obj.parentNode;
				if( obj.className == "accordeon_ssetape" )
				{
					ssetape = obj;
					obj = obj.parentNode;
					while( obj.className != "accordeon_etape" )
						obj = obj.parentNode;
					etape = obj;
				}
				else
				{
					ssetape = null;
					etape = obj;
				}
				
				$(".contenuetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					if( tobj == etape )
					{
						if( !ssetape && tdobj.innerHTML.match( new RegExp("Fermer") ) ) // Si on clique pour fermer juste enfin bref voila quoi
						{
							num = ( ie )?1:3;
							$(tobj.childNodes[num]).hide("slow");
							tdobj.innerHTML = options.html_ouvrir;
						}
						else
						{
							num = ( ie )?1:3;
							$(tobj.childNodes[num]).show("slow");
							tdobj.innerHTML = options.html_fermer;
						}
					}
					else
					{
						num = ( ie )?1:3;
						$(tobj.childNodes[num]).hide("slow");
						tdobj.innerHTML = options.html_ouvrir;
					}
				});
				$(".contenusousetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					if( ssetape && tobj == ssetape )
					{
						if( tdobj.innerHTML.match( new RegExp("Fermer") ) ) // Si on clique pour fermer juste enfin bref voila quoi
						{
							num = ( ie )?1:3;
							$(tobj.childNodes[num]).hide("slow");
							tdobj.innerHTML = options.html_ouvrir;
						}
						else
						{
							num = ( ie )?1:3;
							$(tobj.childNodes[num]).show("slow");
							tdobj.innerHTML = options.html_fermer;
						}
					}
					else
					{
						num = ( ie )?1:3;
						$(tobj.childNodes[num]).hide("slow");
						tdobj.innerHTML = options.html_ouvrir;
					}
				});
			}
			else if( options.type == "ttouvrir" )
			{
				$(".contenuetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					num = ( ie )?1:3;
					$(tobj.childNodes[num]).show("slow");
					tdobj.innerHTML = options.html_fermer;
						
				});
				$(".contenusousetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					num = ( ie )?1:3;
					$(tobj.childNodes[num]).show("slow");
					tdobj.innerHTML = options.html_fermer;
						
				});
			}
			else if( options.type == "ttfermer" )
			{
				$(".contenuetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					num = ( ie )?1:3;
					$(tobj.childNodes[num]).hide("slow");
					tdobj.innerHTML = options.html_ouvrir;
					
				});
				$(".contenusousetap_accor").each(function (){
					tobj = tdobj = this;
					while( tobj.nodeName != 'LI' )
						tobj = tobj.parentNode;
					
					num = ( ie )?1:3;
					$(tobj.childNodes[num]).hide("slow");
					tdobj.innerHTML = options.html_ouvrir;
					
				});
			}
			/*else
			{	
				$("."+options.classe).each(function (){
					num = ( ie )?0:1;
					obj = this.childNodes[num].childNodes[num].childNodes[num];
					if( obj.innerHTML.match( new RegExp("Ouvrir") ) )
						$(this).trigger("click");
				});

			}*/
			return false;
		});
};	
/*
 * Permet d'autoriser des référent sur une sous étape du parcours du porteur
 * @param padding : Decalage de la div flottante
 */
jQuery.fn.az_autoriser = function(options) {
	options = jQuery.extend({
		padding : 10
	}, options);
	
	$(this).css('cursor','pointer');

	$(this).click(function(){
			$(".autoriser_referent").hide();
			num = ( ie )?1:1;
			//alert(this.childNodes[1].innerHTML);

			//$(this.childNodes[num])
$(this).find(".autoriser_referent").show()
						.mouseout(function() {
							gauche = parseInt(this.offsetLeft);
							haut = parseInt(this.offsetTop);
							fin_largeur = gauche + parseInt(this.offsetWidth);
							fin_hauteur = haut + parseInt(this.offsetHeight);
							if( x-options.padding < gauche || x > fin_largeur-options.padding || y < haut || y > fin_hauteur-options.padding )
								$(this).hide();		
						}).mousemove(function(e){
							x = e.pageX;
							y = e.pageY;
						}); 
			});
};
