// Fonctions Rouge Interactif
// Auteur : Hervé FRACKOWIAK
// Date : 09/06/2010


// Au chargement de la page
$(document).ready(function()
{ 
  //Détection IE6
  if($.browser.msie && $.browser.version== '6.0')
  {
    window.location.replace("ie6.php");
  }
  
  //Initialisation Tooltip
  init_tooltip();
  
  //Initialisation du menu droite
  if($("#donner").length>0)
  {
    //Initialisation
    $("#menu").css('height','132px');
    $("#menu .categories").hide();
    
    //Gestion menu
    $("#menu .entete,#donner").click(function(){
      if($("#menu").height()==132)
      {
        $("#menu").animate({height:'100%'},700,function(){
          //Callback
          $("#menu .categories").show(400);
        }).css('overflow','visible');
      }
      else
      {
        $("#menu .categories").hide(400,function(){
          //Callback
          $("#menu").animate({height:'132px'},700).css('overflow','visible');
        });
      }
     });
  }
  
  //Gestion logo Sens
  $("#donner").animate({left:395},800);
  
  //Over du menu
  $("#menu .categories img").hover(
  function(){
    //Survol
    if(!$(this).hasClass("nohover"))
    $(this).attr("src",$(this).attr("src").replace("_off.png","_on.png"));
  },
  function(){
    //Sortie
    if(!$(this).hasClass("nohover"))
    $(this).attr("src",$(this).attr("src").replace("_on.png","_off.png"))
  });
  
  
  //Page expertise / Une seule fois
  var first = getCookie("expert");
  if($("#contenu .expert").length>0 && first=="")
  {
    setCookie('expert','oui');
    $("#contenu").height('0');$("#contenu .expert").hide();
    $("#contenu").animate({height:'100%'},1000,function(){$("#contenu .expert").show(500)});
  }
  
  
  //Page References
  if($("#contenu .references").length>0)
  {
    $("table img").hover(
      //Survol
      function(){
        //Recuperation de l'index
        var idx  = $(this).parent().index();
        var ligne = $(this).parent().parent().index();
        //Calcul de l'index du calque
        var idxc = 2;//par defaut sur le dernier
        var style="left:0";
        if(idx==3)
          style="right:0";
        else
          idxc=idx+1;
        //Insertion des données
        $("table tr:eq("+ligne+") td:eq("+idxc+")").prepend('<div class="realisationP"><div class="realisation" style="'+style+'">'+$(this).nextAll(".infos").html()+'</div></div>');
        $(".realisation").animate({'width':'show'},300);
        
        $(this).attr("src",$(this).attr("src").replace("_off.png","_on.png"));
      },
      //Sortie
      function(){
        $(this).attr("src",$(this).attr("src").replace("_on.png","_off.png"));
        $(".realisationP").remove();
        
      }
    );
  }
  
  //Page Recrutement
  if($("#contenu .recrutement").length>0)
  {
    //Initialisation
    $(".annonce").hide();
    
    //Au click
    $(".intitule").click(function(){
      //Repli unique
      $(".annonce:visible").slideUp(500);
      $(".intitule").removeClass("on");
      
      //Si l'index du visible est different de celui deplie
      if($(".annonce:visible").index()!=$(this).next(".annonce").index())
        $(this).addClass("on").next(".annonce").slideDown(500);
    });
  }
});


//Fonction concernant les cookies
//Set Cookie
function setCookie(c_name, value, expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//Get Cookie
function getCookie(c_name) {
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""; 
}

//Tooltip
function init_tooltip()
{
	$("img[title]").hover(
			//Survol
			function(){
				if($(this).attr("title"))
					$(this).data("title",$(this).attr("title")).removeAttr("title");
				$("body").append('<div id="tooltip">'+$(this).data('title')+'</div>');
				$("#tooltip").animate({width:"show"},400);
				//Si trop grand
        if($("#tooltip").width()>250){$("#tooltip").width(250);}
        $(this).bind("mousemove",function(e){
          $("#tooltip").css({"left": e.pageX + 20, "top" : e.pageY + 10});
        });
			},
			//Quit
			function(){
				$(this).unbind("mousemove");
			    $("#tooltip").animate({width:"hide"},400,function(){
            $("#tooltip").remove();
          });
			}
	);	
}
