/**
 * Return current page number by looking at the #pageN anchor
 * @return int
 */
function pageInHash() {
    if (window.location.hash.match(/^#page[0-9]+$/))
        return parseInt(window.location.hash.replace(/#page/, ''), 10)
    return 0
}

/**
 * Adds a "hover" class when the mouse moves over the elements
 * (needs JQuery)
 * @param elements String CSS selector
 */
function addHover(elements) {
	if ( $(elements).hasClass("pass") ){
		$(elements).mouseover(function(){
			$(elements).css({
				color: "#e33c45",
				backgroundPosition: "left -28px",
				cursor: "pointer"
			});
			
			
		});
		$(elements).mouseout(function(){
			$(elements).css({
				color: "#0187ad",
				backgroundPosition: "left top",
				cursor: "normal"
			});
		});
		return;
	}
	$(elements).hover(
	    function(){ $(this).addClass("hover");},
	    function() { $(this).removeClass("hover");}
	);
}

/**
	Masque le contenu alternatif
	parametre : idBalise = id du contenu alternatif à masquer
*/
function hideAlternativeContent(idBalise) {
	$(idBalise).wrapInner("<div class=\"alt\"></div>");
}

// permet de scroller sur les éléments de la page
function scrollerAncre(){
  function filterPath(string) {
    return string
      .replace(/^\//,'')  
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
      .replace(/\/$/,'');
  }
  $('a[href*=#]').each(function() {
    if ( filterPath(location.pathname) == filterPath(this.pathname)
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
      var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
       if ($target) {
         var targetOffset = $target.offset().top;
         $(this).click(function() {
           $('html, body').animate({scrollTop: targetOffset}, 400);
           return false;
         });
      }
    }
  });
}

// On page load :
$(document).ready(function(){
	scrollerAncre();
	//menuEffectHover();
    addHover("input[type=submit]");
    addHover("#advices #toc li");

	$('#top-menu li a').wrapInner('<span style="visibility:hidden"></span>');

	/* Effet d'apparition en fading lorsque la souris sors de l'élément */

	$("#top-menu li a").mouseover(function(){
		//$(this).fadeIn("slow");
		$(this).children().css({'opacity': 0.1, visibility:"visible"}).stop().fadeTo(250, 1);
	});
	
	/* Effet de disparition en fading lorsque la souris pointe sur l'élément */
	$("#top-menu li a").mouseout(function(){
		$(this).children().css("opacity", 1).stop().fadeTo(250, 0);
	});

});


// Ouverture des liens externes dans une nouvelle fenêtre
$(function(){
	// fix for target=”_blank”
	$("a[@rel~='external']").click(function(){
		window.open($(this).attr("href"));
		return false;
	});
});
