// DOM Ready

/*var $J = jQuery.noConflict();*/
scrollvar = true;



$(function gotoMenu() {

    /*var $el, leftPos, newWidth;
        $mainNav2 = $("#example-two");
    */
    /*
        EXAMPLE ONE
    */
    
    /* Add Magic Line markup via JavaScript, because it ain't gonna work without */
	
    $("#example-one").append("<li id='magic-line'></li>");
    
    /* Cache it */
    var $magicLine = $("#magic-line");
    
    $magicLine
        .width($(".current_page_item").width())
        .css("left", $(".current_page_item a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());
        
    $("#example-one li").find("a").click(function() {
		scrollvar = false;
		timer_scrollvar = setTimeout('scrollvar = true', 1500)
		
		$el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
        
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }/*, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    }*/);    
    
    /* Kick IE into gear */
    $(".current_page_item_two a").mouseenter();
});


function scroll_menu(){
	
    function isScrolledIntoView(elem) {
       var docViewTop = $(window).scrollTop();
    	var docViewBottom = docViewTop + $(window).height();

    	var elemTop = $(elem).offset().top;
    	/*var elemBottom = elemTop + $(elem).height();*/
		var elemBottom = elemTop + 800;

    	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)
     	 && (elemBottom <= docViewBottom) &&  (elemTop >= docViewTop) );
		 
    }
    var $magicLine = $("#magic-line");
	
	var el = document.getElementById('expertises');
        if(isScrolledIntoView(el)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#expert a").position().left),
			width: $magicLine.width($("#expert").width())
	    });
    } 
	
	var el2 = document.getElementById('realisations');
        if(isScrolledIntoView(el2)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#real a").position().left),
			width:   $magicLine.width($("#real").width())
    	});
    } 
	
	var el3 = document.getElementById('slides_accueil');
        if(isScrolledIntoView(el3)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#home a").position().left),
			width:   $magicLine.width($("#home").width())
    	});
    } 
	
	var el4 = document.getElementById('news');
        if(isScrolledIntoView(el4)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#new a").position().left),
			width:   $magicLine.width($("#new").width())
    	});
    } 
	
	var el5 = document.getElementById('approche');
        if(isScrolledIntoView(el5)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#appr a").position().left),
			width:   $magicLine.width($("#appr").width())
    	});
    } 
	
	var el6 = document.getElementById('demarche');
        if(isScrolledIntoView(el6)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#dem a").position().left),
			width:   $magicLine.width($("#dem").width())
    	});
    } 
	
	var el7 = document.getElementById('lab');
        if(isScrolledIntoView(el7)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#la a").position().left),
			width:   $magicLine.width($("#la").width())
    	});
    } 

	var el8 = document.getElementById('contact');
        if(isScrolledIntoView(el8)) {
			$magicLine.stop().animate({
			left: $magicLine.css("left", $("#cont a").position().left),
			width:   $magicLine.width($("#cont").width())
    	});
    } 
}


