// récupere la langue
if(document.location.pathname.indexOf('/en/') != -1) langue = 'en';
else if(document.location.pathname.indexOf('/en') != -1) { 
		document.location.pathname = document.location.pathname+'/';
}else langue = 'fr'; 
// des que la page est ouverte
$(document).ready(function() {
	
	menuClass = [];
	contactouvert = false;
	scrolling = false;
	cliquer = false;
	ancreprojet = '';
	mode ='';
	bim = 'abcd';
	boom = 'abcd';
	bam = 'abcd';
	ancredavt = 'accueil';
	flashload = false;
	effacerLoader = false;
	
	$body = $('body');
	$page = $('#page');
	$accueil = $('#accueil');
	$portfolio = $('#portfolio');
	$services = $('#services');
	$actu = $('#actu');
	$contact = $('#contact');
	$actulink = $('#actu a.posttitlelink');
	$header = $('#header');
	$footer = $('#footer');
	$footermenu = $('#footer-g');
	$lapage = $('#lapage');
	$wrap = $('#wrap');
	$bgcontent = $('#bg-content');
	$bgcontact = $('#bg-contact');
	$menuitem = $('#menu .menu-item');
	$menusidebar = $('#sidebar-left .menu li.menu-item');
	$actusvt = $('#actu #separator a.suivant');
	$actupdt = $('#actu #separator a.precedent');
	$separ = $('#actu #separator');
	$flash = $('#portfolio-flash');
		
	$separ.data('rotation',0);
		
	$menuitem.each(function() {
		letexte = $(this).children('a').text().toLowerCase();
		menuClass.push(letexte);
		$(this)
		.attr('class',"menu-item "+letexte)
		.children('a').text('')
		.bind('click',function() { $(this).parent().addClass('current-item'); $('#footer-g .'+$(this).parent().attr('class').split(' ')[1]).addClass('current-item');})
		.append('<img src="'+tempurl+'/images/bouton_'+letexte+'.png"/>')
		.hover(function() {	$(this).children('img').stop(true).fadeTo(600,1); },function() { $(this).stop(true).children('img').fadeTo(600,0); });
	});
	
	$menusidebar.children('a').addClass('colorer');
	
	$('#footer-g .menu-item').each(function() {
		letexte = $(this).children('a').text().toLowerCase();
		$(this)
		.attr('class',"menu-item "+letexte);
	});
	
	if(langue =='en') {
		$('#footer-g .menu-item:first').addClass('current-item').text('').append('<a href="#/home">Home</a>');
		$('#footer-g .menu-item.english').css('display','none')
		$('#choix-langue-header .menu-item.english').addClass('current-item');
	}else{
		$('#footer-g .menu-item:first').addClass('current-item').text('').append('<a href="#/accueil">Accueil</a>');
		$('#footer-g .menu-item.francais').css('display','none')
		$('#choix-langue-header .menu-item.francais').addClass('current-item');
	}
	function taillescrollbar() {
		taillescroll = $(window).width()*100/$bgcontent.width();
		return taillescroll;
	};
	
	function decalScroll() {
		decale = $page.scrollLeft()*100/$('#bg-content .bs').width();
		if(decale > (100-taillescrollbar())/2) decale = (100-taillescrollbar())-decale; if(decale < 0) decale =0;
		//
		$('#cbas').css('marginLeft',(decale*30)+'px');
		$bgcontent.children('.bs').css('marginLeft',(decale*10)+'px');
		$bgcontent.children('.bs:first').css('marginLeft','-'+(decale*20)+'px');
		$bgcontent.children('.bs:last').css('marginLeft',(decale*30)+'px');
		$bgcontent.children('.ht').css('marginLeft','-'+(decale*10)+'px');
		$bgcontent.children('.ht:first').css('marginLeft','-'+(decale*20)+'px');
		$bgcontent.children('.ht:last').css('marginLeft',(decale*30)+'px');
	}
	
	function verifGoTo(ancre,mode) {
		ancrepasse = false;
		//
		if(langue == 'en') {
			ancre = ancre.replace('news','actu');	
			ancre = ancre.replace('home','accueil');	
		}
		//
		ancrepage = ancre.substring(ancre.indexOf('/')+1,ancre.length);
		if(ancrepage.indexOf('/') != -1) {
			ancreprojet = ancrepage.split('/')[1];
			ancrepage = ancrepage.split('/')[0];
		}else ancreprojet = '';
		
		if (document.getElementById(ancrepage)) {
			if(ancreprojet == '') {
				goTo(ancrepage,ancreprojet,mode); // si l'ancrepage existe
			}else { // si il a un article
				if (document.getElementById(ancreprojet)) goTo(ancrepage,ancreprojet,mode); // si il existe	
				else verifRefGoTo(ancrepage,ancreprojet,'page'); // il existe pas on verif si c référencer juste la page
			}; 
		}else{
			// si y a pas l'ancre de la page
			verifRefGoTo(ancrepage,ancreprojet,'quedal');
		};
		if(effacerLoader) $('#loader').delay(400).fadeOut();
	}
	
	function verifRefGoTo(ancrepage,ancreprojet,pass){
		// on verifie si c pas cv ou portfolio
		if(ancreprojet == 'go') flashGo(); // on lance et afficher le flash
		else if(ancrepage == 'cv') {
			window.open(cvurl,'_self', null); // on telecharge le cv
			window.location.hash = '#/'+ancredavt; // on réinitialise l'ancre
		}else { // si non on ouvre la page précente ou la page en cours si c un projet inexistant
			if(pass == ('page')) window.location.hash = '#/'+ancrepage;
			else window.location.hash = '#/'+ancredavt;
		}
	};
	
	function flashGo() {
		//
		titlePage('portfolio','');
		//
		if($flash.css('opacity') < 10) {
			$flash.css('display','block');
			$flash.fadeTo(600,1);
			$flash.focus();
		}
		// <![CDATA[
		if(!flashload) {
			var so = new SWFObject(tempurl+'/flash/loader.swf', 'loader-flash', '100%', '100%', 9, '#fff');
			// so.useExpressInstall('expressinstall.swf');
			so.addParam("allowFullScreen", "true");
			so.addVariable("path", tempurl+"/flash/nuagegraphik.swf");
			so.addVariable("langue",langue);
			so.addVariable("racine", tempurl+'/flash');
			so.addVariable("extensions", '/portfolio/go');
			so.write('portfolio-flash');
			// ]]>
			flashload = true;
		}
	}
	
	function cap(string) {	 
    	return string.charAt(0).toUpperCase() + string.slice(1); 
	} 
	
	function titlePage(ancrepage,ancreprojet) {
		titre1 = cap(ancrepage);
		if(ancreprojet != '') titre2 = cap(ancreprojet);
		document.title = entete+' - '+titre1;//+' - '+titre2;
	};
	
	function goTo(ancrepage,ancreprojet,mode){
		titlePage(ancrepage,ancreprojet);
		if($flash.css('display') == 'block') $flash.fadeTo(600,0,function() {$flash.css('display','none')});
		//
		$('.menu-item.francais a').each(function() {
			$(this).attr('href',homeurl+'#/'+ancrepage+'/'+ancreprojet);
		});
		$('.menu-item.english a').each(function() {
			if(ancrepage == 'accueil') apEN = 'home';
			else if(ancrepage == 'actu') apEN = 'news';
			else apEN = ancrepage;
			$(this).attr('href',homeurl+'en/#/'+apEN+'/'+ancreprojet);
		});
		//
		$menuitem.each(function() {
			//
			if(langue == 'en') {
				if(ancrepage == 'accueil') ancrepageEN = 'home';
				else if(ancrepage == 'actu') ancrepageEN = 'news';
				else ancrepageEN = ancrepage;			
			}else ancrepageEN = ancrepage;
			if($(this).attr('class').split(' ')[1] != ancrepageEN) {
				$('#footer-g .'+$(this).attr('class').split(' ')[1]).removeClass('current-item');
				$(this)
				.removeClass('current-item')
				.children('a').hover(function() { $(this).children('img').stop(true).fadeTo(600,1); },function() { $(this).children('img').stop(true).fadeTo(600,0); }).children('img').fadeTo(600,0);
			}else{
				$('#footer-g .'+$(this).attr('class').split(' ')[1]).addClass('current-item');
				$(this)
				.addClass('current-item')
				.children('a').hover(function() { $(this).children('img').stop(true).fadeTo(600,1); },function() { $(this).children('img').stop(true).fadeTo(600,1); }).children('img').fadeTo(600,1);			}
		});
		
		leclipVue = $('#menu-menu li.'+ancrepage+' img');
		
		if(ancrepage == 'actu' || ancreprojet != '') {
			$separ.each(function() {
				if(ancreprojet == '') ancrepasse = true;
				if(ancrepasse) actif= 'suivant';
					else actif='precedent';
				$(this).attr('title',actif);
				if($(this).parent().attr('id') == ancreprojet) ancrepasse = true;
				if(actif == 'suivant') {
					if($(this).data('rotation') != 0) $(this).data('rotation',0).children('div').children('img').stop().rotate({ angle:180, animateTo:0 });
				}else{
					if($(this).data('rotation') != 180) $(this).data('rotation',180).children('div').children('img').stop().rotate({ angle:0, animateTo:180 });
				}
			});
		}
		
		if(mode =='nop') {
			if(ancreprojet != '') {
				scrollleft = $page.attr('scrollLeft')+$('#'+ancreprojet).offset().left-$actu.css('paddingLeft').split('px')[0]-335;
				$page.stop().scrollTo( scrollleft, 0, { axis:'x'}).scrollTo( 0, 0, {axis:'y'});
			}else 
				$page.stop().scrollTo( '#'+ancrepage, 0, {axis:'x'}).scrollTo( 0, 0, {axis:'y'});
				
			$separ.children('div').children('img.img-colorer').stop().fadeTo(0,0);
			$separ.bind('click',function() { 
				window.location.hash = $(this).children('a.'+$(this).attr('title')).attr('href');
			}).hover(function() { $(this).children('div').children('img.img-colorer').stop().fadeTo(400,1); }, function() { $(this).children('div').children('img.img-colorer').stop().fadeTo(400,0);						 });		
			$('#actu #separator:first').css('cursor','auto').unbind('click');
			decalScroll();
			cliquer = false;
		}else if(mode == 'post' || ancreprojet != '') {
			cliquer = true;
			//
			scrollleft = $page.attr('scrollLeft')+$('#'+ancreprojet).offset().left-$actu.css('paddingLeft').split('px')[0]-335;
			$page.stop().scrollTo( scrollleft, 800, { axis:'x', easing: 'easeInOutQuart', onAfter:function() {
				if($page.scrollTop() > 0) $page.scrollTo( 0, 200, { axis:'y', easing: 'easeInOutQuart', onAfter: function(){
							leclipVue.stop(true).fadeTo(200,1);
							jQuery(ancrepage).attr('tabindex','-1');
							jQuery(ancrepage).focus();
							jQuery(ancrepage).removeAttr('tabindex');
							if(bam != 'abcd') clearTimeout(bam);
							bam = setTimeout(function() {cliquer=false;},200);
						} });
				else {
					leclipVue.stop(true).fadeTo(200,1);
					jQuery(ancrepage).attr('tabindex','-1');
					jQuery(ancrepage).focus();
					jQuery(ancrepage).removeAttr('tabindex');
					if(bam != 'abcd') clearTimeout(bam);
					bam = setTimeout(function() {cliquer=false;},200);
				}
				}
			});
		}else{
			cliquer = true;
			$page.stop().scrollTo( '#'+ancrepage, 800, { axis:'x', easing: 'easeInOutQuart', onAfter:function() {
				if($page.scrollTop() > 0) $page.scrollTo( 0, 200, { axis:'y', easing: 'easeInOutQuart', onAfter: function(){
							leclipVue.stop(true).fadeTo(200,1);
							jQuery(ancrepage).attr('tabindex','-1');
							jQuery(ancrepage).focus();
							jQuery(ancrepage).removeAttr('tabindex');
							if(bam != 'abcd') clearTimeout(bam);
							bam = setTimeout(function() {cliquer=false;},200);
						} });
				else {
					leclipVue.stop(true).fadeTo(200,1);
					jQuery(ancrepage).attr('tabindex','-1');
					jQuery(ancrepage).focus();
					jQuery(ancrepage).removeAttr('tabindex');
					if(bam != 'abcd') clearTimeout(bam);
					bam = setTimeout(function() {cliquer=false;},200);
				}
				}
			});
		}
	  if(ancreprojet != '')  ancredavt = ancrepage+'/'+ancreprojet;
	  else ancredavt = ancrepage;
	  ancreprojet = '';
	};
	
	function resize(mode) {
		ancre = window.location.hash;
		ancrepage = ancre.substring(ancre.indexOf('/')+1,ancre.length);
		
		$page.height('100%');
		
		$separ.height(($('#post-actu').height()-15)+'px');
		$('#post-actu .article').height(($('#post-actu').height()-15)+'px');
							   
		$('#content .content-page').css('paddingLeft',($('#header-content').offset().left+30)+'px');
		
		if($(window).height() > 685) $('#content .content-page').css('paddingTop',180+(($(window).height()-648)/2)+'px');
		else $('#content .content-page').css('paddingTop','200px');
		
		if($(window).height() > 648) {
			//$footer.children('#footer-content').height('30px')
			$lapage.css('minHeight',$page.height());
			$bgcontent.height($page.height()+30);
			decalage = ($bgcontent.height()-330);
			$bgcontent.children('.bs').css('top',decalage).height($(this).height()-decalage+15);
		}else{
			//	.height($(window).height()-15-$footer.offset().top+'px')
			$page.height('648px');
			$lapage.css('minHeight','633px');
			$bgcontent.height(678);
			decalage = ($bgcontent.height()-330);
			$bgcontent.children('.bs').css('top',decalage).height(775-400);
		}
		if(boom != 'abcd') clearTimeout(boom);
		if(mode != 'nop') boom = setTimeout(function() {verifGoTo(window.location.hash),'resize'},200);
		
		lalargeur = $page.attr('scrollLeft')+$('#contact').offset().left+$(window).width()+75;
		$bgcontent.width(lalargeur+'px');
		$lapage.width(lalargeur+'px');
		$wrap.width(lalargeur+'px');
		$bgcontact.width(lalargeur+'px');
	}
	
	$page.scroll(function() {
		if(!cliquer) scrolling = true;
		else scrolling = false
		decalScroll();
	});
	
	$(window).scroll(function() {
		$header.css('marginTop',20-$(window).scrollTop());
		$footer.css('marginTop',-30-$(window).scrollTop());
	});
	   		
	window.onresize = function(){
		resize();
	};
	
	ancre = window.location.hash;
	if(ancre =="" || ancre =="#" || ancre == "#/" || ancre == "/") {
		if(langue=='en') window.location.hash = '#/home';
		else window.location.hash = '#/accueil';
	}
	resize('nop');

	// Event handlers
	$.address.init(function(event) {
		lemode = 'init';
	}).change(function(event) {
		if(lemode == 'init') setTimeout(function() {verifGoTo(window.location.hash,'nop')},800);
		else verifGoTo(window.location.hash,lemode);
		//verifEnVue();
		lemode = '';
	})
	//
});
// Un fois toute la page chargé
$(window).load(function(){
	effacerLoader = true;
	setTimeout(function() {$('#loader').fadeOut();},1000);
	//
	resize('nop');	
});

