﻿
var currentSection = 0;
var sectionToGo = 0;


//Détection d'un changement d'url
var storedHash = 0;
window.setInterval(function () {
    if (window.location.hash != storedHash) {
        storedHash = window.location.hash;
		
		var s = storedHash.replace("#","");
		
		    if(s == "home")sectionToGo = 0;
			else if(s == "selectedWork")sectionToGo = 1;
			else if(s == "agency")sectionToGo = 2;    
			else if(s == "reference")sectionToGo = 3;
			else if(s == "contact")sectionToGo = 4;
		
		if(sectionToGo != currentSection)
		{		
			changeSection(s);
			$('html, body').animate({scrollTop:0}, 100); 
			place_footer();
		}
    }
}, 10);



$(document).ready(function () {

    //On scann le scroll pour afficher le back to top
     $(window).scroll(function(){
        
        if($(window).scrollTop() > 110){$("#backToTop").slideDown("slow");}
     
        if($(window).scrollTop() < 110){$("#backToTop").slideUp("slow");}
     });

     
     //Fonctrion pour revenir en haut

       $("#backToTop").click(function(){
            $('html, body').animate({scrollTop:0}, 300);       
       });
    

	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'x';
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#sections', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1
	});
	
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#sections', // could be a selector or a jQuery object too.
		queue:true,
		duration:1,
		hash:true,
		onBefore:function( e, anchor, $target ){
			//changeSection();
		},
		onAfter:function( anchor, settings ){
			// The 'this' contains the scrolled element (#content)
		}
	});
	$("#index").click(function(){changeSection("home");});
	$("#menu_0").click(function(){changeSection("home");});
	$("#menu_1").click(function(){changeSection("selectedWork");});
	$("#menu_2").click(function(){changeSection("agency");});
	$("#menu_3").click(function(){changeSection("reference");});
	$("#menu_4").click(function(){changeSection("contact");});
	
	
	 if(location.href.indexOf("#") > -1){
        var secName = location.href.split("#")[1];		
        changeSection(secName);
		
    }else{changeSection('home'); place_footerHome();}
	
	
});

function changeSection(s){	
	
    document.getElementById(s).style.display="none";
    $("#"+s).fadeIn(500);
    
    //var s = location.href.split("#")[1];
    if(currentSection > -1)$("#menu_" + currentSection).removeClass("select");
   
    if(s == "home")sectionToGo = 0;
    else if(s == "selectedWork")sectionToGo = 1;
    else if(s == "agency"){sectionToGo = 2; if(init_agency == 0){init_img_agency();}}    
    else if(s == "reference")sectionToGo = 3;
    else if(s == "contact")sectionToGo = 4;
    
    if(sectionToGo > -1)$("#menu_" + sectionToGo).addClass("select");
    

	if(s == "home"){$("#sw_viewer").hide();$("#viewer").fadeIn(1);}
    if(s == "selectedWork"){$("#viewer").hide();$("#sw_viewer").fadeIn(1);}
	
	if(s == "contact"){$("input").fadeIn(1);}else{$("input").hide();}
	

	

    currentSection = sectionToGo;
    
   $('html, body').animate({scrollTop:0}, 100);  
   place_footer();
   

    
}

$(document).ready(function () {
    
    place_footer();

});


function place_footer()
{
	var secName = location.href.split("#")[1];
	$("#sections").animate({height:(($("#sections").find("#"+secName).height())+'px')}, 1);
}

function place_footerSW()
{	
	$("#sections").animate({height:(($("#sections").find("#selectedWork").height())+'px')}, 1);
}

function place_footerHome()
{	
	$("#sections").animate({height:(($("#sections").find("#home").height())+'px')}, 1);
}
