function firstNavigate(){
	if(document.location.hash == "" || document.location.hash == "#" ){		
		document.location.hash = _settings_obj.anchor_str;
	}else{
		navigateTo(1);
	}
}

function navigateTo($time_int){
	//
	if(_settings_obj.isMoving_bool == true)return;
	var navigate_array = document.location.hash.split("/");
	if(_settings_obj.anchor_str == document.location.hash)return;
	//
	var new_anchor_array = document.location.hash.split("/");
	var old_anchor_array = _settings_obj.anchor_str.split("/");
	if(new_anchor_array.length == 3){
		navigateFooterSection(new_anchor_array[2]);
		if(new_anchor_array[1] != old_anchor_array[1])navigateSection($time_int);
	}else{
		checkFooterContentOpen();
		navigateSection($time_int);
	}
	
}
function navigateFooterSection($section_str){
	
	_settings_obj.anchor_str = document.location.hash;
	checkFooterContentOpen();
	//
	if($section_str=='awards')manageAwards(true);
	if($section_str=='clients')manageClients(true);
	if($section_str=='contact')manageContact(true);	
	
}
function checkFooterContentOpen(){
	//
	if(_settings_obj.awards_div.hasClass('open'))manageAwards(false);
	if(_settings_obj.clients_div.hasClass('open'))manageClients(false);
	if(_settings_obj.contact_div.hasClass('open'))manageContact(false);	
}
function navigateSection($time_int){
	var last_anchor_array = _settings_obj.anchor_str.split("/"), new_anchor_array = document.location.hash.split("/"),i,j,step;
	//
	for(i = 0;i<_settings_obj.section_array.length;i++){		
		section_obj = _settings_obj.section_array[i]; 
		section_obj.div.hide();
		if(section_obj.div.attr('class') == last_anchor_array[1]){
			for(j = 0;j<_settings_obj.section_array.length;j++){		
				section_obj = _settings_obj.section_array[j]; 
				if(section_obj.div.attr('class') == new_anchor_array[1]){
					step = j-i;
				}
			}
		}
	}
	//
	if(step == 3)step=-1;
	if(step == -2)step=2;
	if(step == -3)step=1;
	//		
	_settings_obj.anchor_str = document.location.hash;
	sectionGoTo(step,$time_int);	
}

function sectionGoTo($step_int,$time_int){
	//	
	if(_settings_obj.isVideoPlaying_bool)stopVideo();
	clearInterval(_settings_obj.homeInterval);
	var section_obj,step_int,i,anchor_array = _settings_obj.anchor_str.split('/');	
	//
	if($step_int<0){
		_settings_obj.sectionContainer.prepend(_settings_obj.sectionContainer.find('section:last').css('left',-_settings_obj.sectionW+'px').remove());
		_settings_obj.sectionContainer.css('left',0+'px');
	}
	//
	initialize();
	//	
	for( i = 0;i<4;i++){
		//
		section_obj = _settings_obj.section_array[i];
		section_obj.div.css('z-index',5-i);
		section_obj.div.show();
	}
	//
	_settings_obj.isMoving_bool = true;
	_settings_obj.sectionContainer.animate({'left':-$step_int * _settings_obj.sectionW+'px'},Math.abs($step_int) * $time_int,'easeInOutCubic',function(){
	//	
		_settings_obj.sectionContainer.css('left','0px');
		if($step_int>0){
			for(i = 0;i<$step_int;i++){
				section_div = _settings_obj.section_array[i];
				_settings_obj.sectionContainer.append(section_div.div.hide().remove());
			} 
		}
		//
		initialize();
		//
		clearInterval(_settings_obj.homeInterval);
		clearInterval(_settings_obj.secondeInterval);
		//
		for(i = 0;i<_settings_obj.section_array.length;i++){
			section_obj = _settings_obj.section_array[i];
			if(i==0){
				if(_settings_obj.currentSectionId_str != section_obj.div.attr('id')){
					_settings_obj.currentSectionId_str = section_obj.div.attr('id');
				
					switch(_settings_obj.currentSectionId_str){
						case "seconde":
							_settings_obj.delay_int = 5;
							_settings_obj.secondeInterval = setInterval(doSeconde,80);
						break;
						case "minute":
							playVideo();
						break;
						case "home":
							_settings_obj.homeInterval = setInterval(increaseHome,10000);
						break;
					}
				}
			}
			section_obj.div.css('left',i*_settings_obj.sectionW+'px');
		}
		_settings_obj.isMoving_bool = false;
		onPosUpdate();
	});
	//
	unselectAll();
	_settings_obj.nav_div.find('.menuItem').each(function (){
		if($(this).attr('id')== anchor_array[1])$(this).find('.select').stop().show().animate({'opacity':1},500,'easeOutCubic');
	})
	setTimeout(onPosUpdate,Math.abs($step_int)*100);
}

function doSeconde(){	
	if(_settings_obj.delay_int == 5 ){
		_settings_obj.seconde_array = [];
		$('#idseconde').find('.secondeImg').each(function (){
			$(this).show();
			_settings_obj.seconde_array.push($(this));
		});
		$('.reset').click(function (){
			_settings_obj.delay_int = 5;
			})
		_settings_obj.delay_int--;
	}
	if(_settings_obj.isSecondeHover_bool == true)return;
	_settings_obj.delay_int--;
	if(_settings_obj.delay_int>=0)return;
	var img_int = _settings_obj.seconde_array.length + _settings_obj.delay_int
	if(img_int<0)return;
	var img = _settings_obj.seconde_array[img_int];
	img.hide();
}
//
function onPosUpdate(){
	var leftPos_num = _settings_obj.sectionContainer.position().left,left;
	//
	if(_settings_obj.sectionPosX == leftPos_num)return;
	_settings_obj.sectionPosX = leftPos_num;
	var i,section_obj;
	for(i = 0;i<_settings_obj.section_array.length;i++){
		
		section_obj = _settings_obj.section_array[i]; 
		if(section_obj.div.css('display') != 'none'){
			left = leftPos_num + section_obj.div.position().left
			if(section_obj.titre_div.get(0)){
				section_obj.titre_div.css('left',left * 2);
				section_obj.content_div.css('left',left * 4);
			}else{
				section_obj.div.find('#articleContainer').css('left',left * 4);
			}
		}
	}
	setTimeout(onPosUpdate,30);
}
function playVideo(){
	_settings_obj.isVideoPlaying_bool = true;
	if($('html').hasClass('no-video')){
		//
        $('#video-player').empty();
        $('#video-player').append("<div id='flash-player'></div>");
      	//
        var flashvars = {			
            url_str:'/video/video_5g.f4v'			
        };
        var params = {			
            menu: "false",
	        allowfullscreen: 'true',
			allowScriptAccess: 'always',
			wmode: 'transparent'			
        };			
        var attributes = {id:"flash-player",name:"flash-player"};
        swfobject.embedSWF("/swf/videoPlayer.swf", "flash-player", '100%', '100%', "9.0.0", "/swf/expressInstall.swf", flashvars, params,attributes);
		//			
    }else{
        $('video').get(0).play();
		
    }
}
function stopVideo(){
	if($('html').hasClass('no-video')){
	}else{
        $('video').get(0).pause();
	}
	_settings_obj.isVideoPlaying_bool = false;
	
}
var _settings_obj = {
	initW:1260,
	initH:846,
	initR:1260/846,
	w:0,
	h:0,
	sectionPosX:0,
	isMoving_bool:false,
	isVideoPlaying_bool:false,
	currentSection:0,
	sectionW:0,
	anchor_str:'#!/home',	
	main_div:null,
	nav_div:null,
	navigateInterval:null,
	sectionContainer:null,
	arrow_div:null,
	contact_div:null,
	awards_div:null,
	clients_div:null,
	secondeInterval:null,
	delay_int:5,
	homeInterval:null,
	isSecondeHover_bool:false,
	currentHome_int:0,
	currentSectionId_str:'',
	section_array:[],
	nav_array:['home','5s','5m','5h'],
	subnav_array:['clients','awards','contacts']
}
//
var section_obj={
	div:null,
	article_div:null
}
//
function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
//
function doSectionHeure(){
	var t;
	var today = new Date()
	var d = new Date(2012,02,00);
	var diff = (d-today);
	var sec = Math.floor(diff/1000)
	diff = diff % 1000
	$('.sec').text(three(diff)+'s')

	var minu = Math.floor(sec/60)
	sec = sec % 60
	$('.sec').text(two(sec)+'s')

	var hr = Math.floor(minu/60)
	minu = minu % 60
	$('.min').text(two(minu)+'mn')

	var day = Math.floor(hr/24)
	hr = hr % 24
	//$('.heure').text(two(hr)+'mn')
	
	$('.heure').text(two(hr)+'h')
	$('.jour').text(day+'j')
	//
	setTimeout(doSectionHeure,10);
}
//
function prepareSite(){
	//
	doSectionHeure();
	_settings_obj.main_div = $('#main');
	_settings_obj.nav_div = _settings_obj.main_div.find('nav');
	_settings_obj.sectionContainer = _settings_obj.main_div.find('#sectionContainer');
	//
	_settings_obj.awards_div = _settings_obj.main_div.find('#awards');
	_settings_obj.contact_div = _settings_obj.main_div.find('#contact');
	_settings_obj.clients_div = _settings_obj.main_div.find('#clients');
	
	$('#seconde').find('script').remove();
	//
	$('footer').find('a').each(function (){
		$(this).click(function (e){
			e.preventDefault();	
			var hash_str = document.location.hash;
			var hash_array = hash_str.split('/');
			document.location = '#!/'+hash_array[1]+'/'+$(this).attr('rel')
		})		
	})
	$('a.fermer').click(function (e){
		e.preventDefault();	
		var hash_str = document.location.hash;
		var hash_array = hash_str.split('/');
		document.location = '#!/'+hash_array[1];
	})		
	//
	_settings_obj.awards_div.animate({'top':-1200+'px','left':735+'px'},10,'easeInOutCubic',function (){$(this).hide();});		
	_settings_obj.clients_div.animate({'top':-1200+'px','left':735+'px'},10,'easeInOutCubic',function (){$(this).hide();});		
	_settings_obj.contact_div.animate({'top':-1000+'px','left':795+'px'},10,'easeInOutCubic',function (){$(this).hide();});		
	//
	initialize();
	createMenu();
	//
	initSwipeAndWheel();
	//	
	resize();
	//
	firstNavigate();
	_settings_obj.navigateInterval = setInterval('navigateTo(1200)',100);
	//
	
	//
	$(window).bind('resize',resize);
}
//
function initSwipeAndWheel(){
	/*_settings_obj.sectionContainer.find('section').touchwipe({
		wipeLeft: function() { goSection(1); },
		wipeRight: function(){ goSection(-1);},
		wipeUp: function(){ resize(); },
		wipeDown: function(){ resize(); }
	})
	$(window).mousewheel(function(e, delta){
		if(delta>0){
			goSection(-1);
		}else{
			goSection(1);
		}
	})*/
	$(window).keydown(function(e, key){
		if(e.which == 39)goSection(1);
		if(e.which == 37)goSection(-1);
	})
}
function goSection(id_int){
	var anchor_array = document.location.hash.split("/")
	if(anchor_array.length == 3)return;
	for(var i=0;i<_settings_obj.nav_array.length;i++){
		if(document.location.hash == '#!/'+_settings_obj.nav_array[i])break;	
	}
	var newId_int = i + id_int;
	if(newId_int<0)newId_int=3;
	if(newId_int>3)newId_int=0;
	document.location = '#!/'+_settings_obj.nav_array[newId_int];
}
//
function initialize(){
	//
	_settings_obj.section_array = [];
	_settings_obj.sectionContainer.find('section').each(function (){
		section_obj = {};
		section_obj.div = $(this);
		section_obj.article_div = section_obj.div.find('article');
		section_obj.titre_div = section_obj.article_div.find('.titre');
		section_obj.content_div = section_obj.article_div.find('.content');	
		if(section_obj.article_div.size()>1){
			section_obj.article_array = [];
			section_obj.article_div.each(function (){
				section_obj.article_array.push($(this));
			});
			section_obj.article_div.hide();
			section_obj.div.find('.bg').hide();
			section_obj.div.find('.bg'+(_settings_obj.currentHome_int)).show().css('opacity',1);
			section_obj.div.find('.article'+(_settings_obj.currentHome_int)).show();
		}
		_settings_obj.section_array.push(section_obj);	
	});
	//
	_settings_obj.arrow_div = _settings_obj.main_div.find('.arrow');
	//
	_settings_obj.arrow_div.find('.next').click(function (e){
			e.preventDefault();
			goHomeArticle(1);			
			clearInterval(_settings_obj.homeInterval);
	});
	_settings_obj.arrow_div.find('.prev').click(function (e){
			e.preventDefault();
			goHomeArticle(-1);			
			clearInterval(_settings_obj.homeInterval);
	});
	if(_settings_obj.currentHome_int == 0)_settings_obj.arrow_div.find('.prev').hide();
	if(_settings_obj.currentHome_int == 8)_settings_obj.arrow_div.find('.next').hide();
	//
	if (!Modernizr.touch){
	if($('.ghinda-video-player').get(0))$('video').unwrap();
	if($('#controls').get(0))$('#controls').remove();
	$('video').gVideo();
	}
	$('video').click(function (){
			if(_settings_obj.isVideoPlaying_bool){
				stopVideo()
			}else{
				playVideo()
			}
		})
	$('#seconde').find('.content').hover(function (){
			_settings_obj.isSecondeHover_bool = true;
		},function(){
			_settings_obj.isSecondeHover_bool = false;
			})
}
function increaseHome(){
		if(_settings_obj.currentHome_int == 8)return;
		goHomeArticle(1);
}
//
function goHomeArticle($step_int){
	if(_settings_obj.isHomeSliding == true)return;
	var currentHome_int = _settings_obj.currentHome_int + $step_int,section_obj=_settings_obj.section_array[0],i,currentArticle,newArticleCss_obj;
	//
	if(currentHome_int < 0)currentHome_int = 0;
	if(currentHome_int == 0)_settings_obj.arrow_div.find('.prev').fadeOut();
	if(currentHome_int == 1)_settings_obj.arrow_div.find('.prev').fadeIn();
	if(currentHome_int > section_obj.article_array.length-1){
		currentHome_int = section_obj.article_array.length-1;
		_settings_obj.arrow_div.find('.next').fadeOut();
	}
	
	if(currentHome_int == section_obj.article_array.length-1)_settings_obj.arrow_div.find('.next').fadeOut();
	if(currentHome_int == section_obj.article_array.length-2)_settings_obj.arrow_div.find('.next').fadeIn();
	if(currentHome_int == _settings_obj.currentHome_int)return;
	//	
	_settings_obj.isHomeSliding = true;
	//
	currentArticle = section_obj.article_array[_settings_obj.currentHome_int ];
	var bgName_str = '.bg'+(_settings_obj.currentHome_int);

	section_obj.div.find(bgName_str).animate({'opacity':0},1000,'easeOutCubic',function (){section_obj.div.find(bgName_str).hide();})
	//
	if($step_int>0){
	
		currentArticle.animate({'top':_settings_obj.h + 300+'px'},1000,'easeInOutCubic',function(){$(this).hide();});
		newArticleCss_obj = {'top':-300+'px'} ;		
	}else{
		currentArticle.animate({'top':- 300+'px'},1000,'easeInOutCubic',function(){$(this).hide();});
		newArticleCss_obj = {'top':_settings_obj.h + 300+'px'} ;		
	}
	_settings_obj.currentHome_int = currentHome_int;
	bgName_str = '.bg'+(currentHome_int)
	
	section_obj.div.find(bgName_str).show().animate({'opacity':1},1000,'easeOutCubic',function (){_settings_obj.isHomeSliding = false;})
	currentArticle = section_obj.article_array[_settings_obj.currentHome_int];
	currentArticle.css(newArticleCss_obj).show().animate({'top':_settings_obj.h*.5-section_obj.article_div.height()*.5-(_settings_obj.h*.5-_settings_obj.sectionHeight*.5)+'px'},1000,'easeInOutCubic');
	//
	section_obj = _settings_obj.section_array[_settings_obj.section_array.length-1];
	section_obj.div.find('.bg').attr('src','/img/5h/bg'+(currentHome_int)+'.jpg');
	
}
//
function createMenu(){
	var count_int=0;
	$('header').find('nav h1').click(function (){
		document.location = $(this).find('a').attr('href');
	});
	_settings_obj.nav_div.find('.menuItem').each(function (){
		var list_elem= $(this),id_str = list_elem.attr('id'),ul_elem = list_elem.find('ul'),imgLoad;
		list_elem.css('margin-left','350px');
		imgLoad = $("<img></img>");
		ul_elem.append("<li class='regular'><img src='img/menu/"+id_str+".png' alt='menu-img'></li>");
		ul_elem.append("<li class='hover'><img src='img/menu/"+id_str+"-1.png' alt='menu-img'></li>");
		ul_elem.append("<li class='select'><img src='img/menu/"+id_str+"-2.png' alt='menu-img'></li>");
		//
		imgLoad = $("<img></img>");
		$(imgLoad).attr("src", '/img/menu/'+id_str+'.png');
		$(imgLoad).unbind("load");
		$(imgLoad).bind("load", function() {
			count_int++;
			if(count_int==3)showMenu();
		});
		//
		ul_elem.find('.hover').css('opacity','0');
		ul_elem.mouseover(function(){
			if($(this).find('.select').css('opacity') == 1)return;
			$(this).find('.hover').stop().animate({'opacity':1},500,'easeOutCubic');				
		});
		ul_elem.mouseout(function(){
				if($(this).find('.select').css('opacity') == 1)return;
				$(this).find('.hover').stop().animate({'opacity':0},500,'easeOutCubic');
			});
		ul_elem.find('.select').css('opacity','0').hide();
		ul_elem.click(function(){
			document.location = $(this).find('.titre a').attr('href')
		});
	});
}
//
function unselectAll(){
	_settings_obj.nav_div.find('.menuItem').each(function (){
		$(this).find('.hover').stop().animate({'opacity':0},500,'easeOutCubic');
		$(this).find('.select').stop().animate({'opacity':0},500,'easeOutCubic',function (){$(this).hide();});		
	});
}
//
function showMenu(){
	var count_int = 0;
	_settings_obj.nav_div.find('.menuItem').each(function (){
		count_int++;
		$(this).delay(count_int * 300).animate({'margin-left':'0px'},1000,'easeOutCubic');
	});
}
//
function manageAwards($open_bool){
	
	if($open_bool){
		_settings_obj.awards_div.addClass('open');
		_settings_obj.awards_div.show().stop().animate({'top':0+'px','left':-100+'px'},1000,'easeInOutCubic');
	}else{		
		_settings_obj.awards_div.stop().animate({'top':-1200+'px','left':735+'px'},1000,'easeInOutCubic',function (){$(this).removeClass('open');$(this).hide();});		
	}
}
//
function manageClients($open_bool){
	if($open_bool){
		_settings_obj.clients_div.addClass('open');
		_settings_obj.clients_div.show().stop().animate({'top':0+'px','left':-100+'px'},1000,'easeInOutCubic');
	}else{		
		_settings_obj.clients_div.stop().animate({'top':-1200+'px','left':735+'px'},1000,'easeInOutCubic',function (){$(this).removeClass('open');$(this).hide();});		
	}	
}
//
function manageContact($open_bool){
	if($open_bool){
		_settings_obj.contact_div.addClass('open');
		_settings_obj.contact_div.show().stop().animate({'top':0+'px','left':102+'px'},1000,'easeInOutCubic');
	}else{
		
		_settings_obj.contact_div.stop().animate({'top':-1000+'px','left':795+'px'},1000,'easeInOutCubic',function (){$(this).removeClass('open');$(this).hide();});		
		
	}	
}
//
function resize(){
	_settings_obj.w = $(window).width();
	_settings_obj.h = $(window).height();
	//
	var screenRatio = _settings_obj.w/_settings_obj.h,sectionCss_obj,sectionWidth,sectionHeight,articleCss_obj,arrowCss_obj,trameCss_obj;
	if(screenRatio>_settings_obj.initR){
		sectionWidth = Math.ceil(_settings_obj.w) ;
		sectionHeight = Math.ceil(sectionWidth / _settings_obj.initR);
	}else{
		sectionHeight = Math.ceil(_settings_obj.h );
		sectionWidth = Math.ceil(sectionHeight * _settings_obj.initR);
	}
	//	
	_settings_obj.sectionW = Math.ceil(sectionWidth*(1458/1260));
	_settings_obj.sectionContainer.width(4 * _settings_obj.sectionW + 'px');
	//
	
	sectionCss_obj = {
		'width':_settings_obj.sectionW+'px',
		'height':sectionHeight+'px',
		'left':'0px'
	}
	_settings_obj.sectionHeight = sectionHeight
	_settings_obj.sectionContainer.find('section .bg').css(sectionCss_obj)
	_settings_obj.sectionContainer.css('top',Math.ceil(_settings_obj.h*.5-sectionHeight*.5)+'px');
	var section_obj,articleCss_obj,i;
	for(var i=0;i<_settings_obj.section_array.length;i++){
		section_obj = _settings_obj.section_array[i];
		
		articleCss_obj = {
			'left':	_settings_obj.w*.5 - section_obj.article_div.width()*.5+'px',
			'top':_settings_obj.h*.5-section_obj.article_div.height()*.5-(_settings_obj.h*.5-sectionHeight*.5)+'px'
		}
		section_obj.article_div.css(articleCss_obj);
	}
	arrowCss_obj = {
			'left':	_settings_obj.w*.5 - _settings_obj.arrow_div.width()*.5+'px',
			'top':_settings_obj.h*.5-_settings_obj.arrow_div.height()*.5-(_settings_obj.h*.5-sectionHeight*.5)+'px'
	}
	_settings_obj.arrow_div.css(arrowCss_obj);
	//
	trameCss_obj = {
		'left':	_settings_obj.w*.5 - $('.trame').width()*.5+'px',
		'top':_settings_obj.h*.5-$('.trame').height()*.5-(_settings_obj.h*.5-sectionHeight*.5)+'px'
	}
	//
	$('.trame').css(trameCss_obj);
	//_settings_obj.sectionContainer.find('article').css(articleCss_obj);
	//
	sectionGoTo(0,1);
	//
}

