﻿var logoInterval;
function initDropFade(){
	var _lt8 = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 7)) ? true : false;
	var navHold = $('div.nav-hold');
	var leftN = $('body').outerWidth(true)/2 - navHold.outerWidth(true)/2;
	/*navHold.css({
		left: leftN
	});
	$(window).resize(function(){
		leftN = $('body').outerWidth(true)/2 - navHold.outerWidth(true)/2;
		navHold.css({
			left: leftN
		});
	})*/
	_nav = $('#nav');
	var _li = _nav.children('li');
	$('body').append('<div id="jquery-overlay"></div>');
	var _fader = $('#jquery-overlay');
	_nav.css({zIndex:1000});
	_fader.css({
		'position':'absolute',
		'top':0,
		'left':0,
		'backgroundColor':'#000',
		'zIndex':'200',
		'opacity':'0'
	 });
	_li.mouseenter(function(){
		$(this).find('a[name^="tab"]:first').click();
		$(this).addClass('hover');
		var _btn = $(this);
		if(_btn.length !=0){
			var _height = 0;
			var _width = 0;
			var _page = $('body > div:eq(0)');
			var _minWidth = _page.outerWidth();
			if (window.innerHeight) {
				_height = window.innerHeight;
				_width = window.innerWidth;
			}
			else {
				_height = document.documentElement.clientHeight;
				_width = document.documentElement.clientWidth;
			}
			if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
			if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
			_fader.stop().animate({'opacity':'0.6'}, 400);
			_btn.show();
		}
		else{
			_fader.stop().animate({'opacity':'0'}, 400, function(){ _fader.css('width',0); });
		}
	}).mouseleave(function(){
		$(this).removeClass('hover');
		var _btn = $(this).children('div.drop');
		_fader.stop().animate({'opacity':'0'}, 400, function(){ _fader.css('width',0); });
	});
	
	$('.menu-opener, .careers h1.logo a').mouseenter(function(){
		clearInterval(logoInterval);
		$('.menu').fadeIn();
		var _height = 0;
		var _width = 0;
		var _page = $('body > div:eq(0)');
		var _minWidth = _page.outerWidth();
		if (window.innerHeight) {
			_height = window.innerHeight;
			_width = window.innerWidth;
		}
		else {
			_height = document.documentElement.clientHeight;
			_width = document.documentElement.clientWidth;
		}
		if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
		if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
		_fader.stop().animate({'opacity':'0.6'}, 400);
		_btn.show();
	});
	
	$('.menu').mouseenter(function(){
		$('.menu-opener').mouseenter();
	}).mouseleave(function(){
		$('.menu').fadeOut();
		_fader.stop().animate({'opacity':'0'}, 400, function(){ _fader.css('width',0); });
	});
	
	$('.careers h1.logo a').mouseleave(function(){
		logoInterval=setTimeout("$('.menu').fadeOut();$('#jquery-overlay').stop().animate({'opacity':'0'}, 400, function(){ $('#jquery-overlay').css({'width':0,'height':0}); });",250);
	});

	$('#nav div[id^="tab"]').hide();
	$('#nav a[name^="tab"]').bind('mouseover click',function(){
		var id=$(this).attr('name');
		if(!$(this).hasClass('active')) $(this).addClass('active');
		$('a[name^="tab"]:not([name='+id+'])').removeClass('active');
		$('div[id^="tab"]:not([id='+id+'])').hide();
		$('div[id='+id+']').show();
	});
};




/*--- testimonials popup ---*/
function testimonialsPopup(){
	var _speed = 300; //in ms
	if($('#testimonials-popup').length == 0) $('body').append('<div id="testimonials-popup"></div>');
	var _popup = $('#testimonials-popup');
	var popup_f = false;
	var popup_href = '';
	_popup.css({opacity:0, display:'none'});
	$('ul.testimonials > li:has("div.popup-content")').each(function(){
		var _li=$(this);
		var popup_content = $(this).find('div.popup-content');
		var _el = $(this).children('a');
		_li.mouseenter(function(){
			if(!$(this).find('a:first').hasClass('disable')){
				popup_f = true;
				_popup.stop().css({top: _el.offset().top, left:_el.offset().left, display:'block'}).html(popup_content.html()).animate({opacity: 1}, _speed);
				popup_href = $(this).find('a:first').attr('href');
			}
		});
	});
	_popup.mouseleave(function(){
		popup_f = false;
		_popup.stop().animate({opacity:0}, _speed, function(){ $(this).hide();});
	});
	_popup.click(function(){ document.location.href=popup_href; });
}


/*--- testimonials list ---*/
function testimonialsList(){
	var _speed = 300; //in ms
	$('div.testimonials-holder').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('ul.sub-nav a');
		var _list = _hold.find('ul.testimonials > li');
		for(var i = 0; i < _btn.length; i++){
			if(_btn.eq(i).attr('hash') && _btn.eq(i).attr('hash').length){
				_btn.eq(i).data('list', _list.not('[title="'+_btn.eq(i).attr('hash').substr(1)+'"]').children('a'));
				_btn.eq(i).data('curr_list', _list.filter('[title="'+_btn.eq(i).attr('hash').substr(1)+'"]').children('a'));
			}
		}
		_list.removeAttr('title');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a != -1){
			_btn.removeClass('active').eq(_a).addClass('active');
			if(_btn.eq(_a).data('list')) _btn.eq(_a).data('list').css('opacity', 0.5).addClass('disable');
		}
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		function changeEl(_ind){
			if(_ind != _a){
				if(_a != -1 && _btn.eq(_a).data('list')) _btn.eq(_a).removeClass('active');
				if(_btn.eq(_ind).data('list')){
					_btn.eq(_ind).addClass('active');
					_btn.eq(_ind).data('curr_list').removeClass('disable').animate({opacity: 1}, {queue:false, duration: _speed});
					_btn.eq(_ind).data('list').addClass('disable').animate({opacity: 0.5}, {queue:false, duration: _speed});
				}
				_a = _ind;
			}
			else{
				if(_btn.eq(_ind).data('list')){
					_btn.eq(_ind).removeClass('active');
					_btn.eq(_ind).data('list').removeClass('disable').animate({opacity: 1}, {queue:false, duration: _speed});
				}
				_a = -1;
			}
		}
	});
}
/*--- testimonials tabs ---*/
function testimonialsTabs(){
	var _speed = 400; //in ms
	
	$('div.testimonials-content').each(function(){
		var _hold = $(this);
		var _btn = _hold.find('ul.testimonials-nav a');
		var _a = (_btn.index(_btn.filter('.active:eq(0)')) != -1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
		var box_hold = _hold.find('div.content-holder');
		var _box = box_hold.children('div.testimonials-el');
		if(_btn.length && _box.length){
			_btn.removeClass('active').eq(_a).addClass('active');
			_box.removeClass('active-tab').css({display:'none', opacity:0}).eq(_a).addClass('active-tab').css({display:'block', opacity:1});
			
			_btn.click(function(){
				changeEl(_btn.index(this));
				return false;
			});
			function changeEl(_ind){
				if(_ind != _a){
					box_hold.stop().height(box_hold.height());
					_btn.eq(_a).removeClass('active');
					_btn.eq(_ind).addClass('active');
					_box.eq(_a).stop().removeClass('active-tab').animate({opacity: 0}, _speed, function(){ $(this).hide();});
					_box.eq(_ind).stop().show().addClass('active-tab').animate({opacity: 1}, _speed);
					box_hold.animate({height: _box.eq(_ind).outerHeight()}, _speed/2, function(){ $(this).height('auto');});
					_a = _ind;
				}
			}
			_hold.find('.tabs-nav .btn-prev a').click(function(){
				if(_a > 0) changeEl(_a - 1);
				else changeEl(_btn.length - 1);
				return false;
			});
			_hold.find('.tabs-nav .btn-next a').click(function(){
				if(_a < _btn.length - 1) changeEl(_a + 1);
				else changeEl(0);
				return false;
			});
		}
	});
}
/*--- steps accordion ---*/
function stepsAccordion(){
	var _speed = 400; //in ms
	
	if(id_navigation==62) {
		var deff_h = 77; //in px
		var small_h = 30; //in px
		var opened_h = 280; //in px	
	}
	else {
		var deff_h = 77; //in px
		var small_h = 30; //in px
		var opened_h = 264; //in px
	}
	
	var _hold = $('#steps-list');
	if(_hold.length){
		var list_hold = _hold.children('ul.list');
		var _list = list_hold.children().not('.heading');
		var title_el = list_hold.children('.heading');
		var _a = _list.index(_list.filter('.item-opened:eq(0)'));
		
		var title_list = _list.find('div.block-title');
		var info_list = _list.find('div.steps-hover'); 
		var box_list = _list.find('div.steps-opened');
		var title_el_title = title_el.find('div.title-block');
		for(var i = 0; i < _list.length; i++){
			_list.eq(i).data('title', _list.eq(i).find('div.block-title'));
			_list.eq(i).data('info', _list.eq(i).find('div.steps-hover'));
			_list.eq(i).data('box', _list.eq(i).find('div.steps-opened'));
			_list.eq(i).data('box').find('a.btn-close').click(function(){
				changeEl(_a);
				return false;
			});
		}
		title_el.height(deff_h);
		_list.height(deff_h);
		if($.browser.msie) box_list.css({display:'none'});
		else box_list.css({display:'none', opacity:0});
		if(_a != -1){
			title_el.height(small_h);
			_list.removeClass('item-opened').height(small_h).eq(_a).addClass('item-opened').height(opened_h);
			list_hold.addClass('opened');
			if($.browser.msie) _list.eq(_a).data('box').css({display:'block'});
			else _list.eq(_a).data('box').css({display:'block', opacity: 1});
		}
		
		_list.click(function(){
			if(!$(this).hasClass('item-opened')) changeEl(_list.index(this));
		});
		
		var anim_f = true;
		var _k1 = (deff_h-small_h)/(opened_h-deff_h);
		var t_el;
		function changeEl(_ind){
			if(anim_f){
				anim_f = false;
				if($.browser.msie){
					if(_a == -1){
						info_list.css('visibility', 'hidden');
						title_list.hide();
						title_el_title.hide();
						list_hold.addClass('opened');
						info_list.css('visibility', 'hidden');
						t_el = _list.not(':eq('+_ind+')');
						_list.eq(_ind).addClass('item-opened').animate({height: opened_h}, {
							duration: _speed,
							step:function(t_h){
								t_el.height(deff_h-(t_h-deff_h)*_k1);
								title_el.height(deff_h-(t_h-deff_h)*_k1);
							},
							complete: function(){
								title_list.show();
								title_el_title.show();
								_list.eq(_ind).data('box').show();
								_a = _ind;
								anim_f = true;
							}
						});
					}
					else if(_a == _ind){
						title_list.hide();
						title_el_title.hide();
						_list.eq(_a).data('box').hide();
						t_el = _list.not(':eq('+_ind+')');
						_list.eq(_a).removeClass('item-opened').animate({height: deff_h}, {
							duration: _speed,
							step:function(t_h){
								t_el.height(small_h+(opened_h-t_h)*_k1);
								title_el.height(small_h+(opened_h-t_h)*_k1);
							},
							complete: function(){
								list_hold.removeClass('opened');
								title_list.show();
								title_el_title.show();
								info_list.css('visibility', 'visible');
								_a = -1;
								anim_f = true;
							}
						});
					}
					else{
						_list.eq(_a).data('box').hide();
						t_el = _list.eq(_a);
						_list.eq(_a).removeClass('item-opened');
						_list.eq(_ind).addClass('item-opened').animate({height: opened_h}, {
							duration: _speed,
							step:function(t_h){
								t_el.height(opened_h-t_h+small_h);
							},
							complete: function(){
								_list.eq(_ind).data('box').show();
								_a = _ind;
								anim_f = true;
							}
						});
					}
				}
				else{
					if(_a == -1){
						info_list.css('visibility', 'hidden');
						title_list.animate({opacity:0}, _speed/3);
						title_el_title.animate({opacity:0}, _speed/3, function(){
							list_hold.addClass('opened');
							info_list.css('visibility', 'hidden');
							t_el = _list.not(':eq('+_ind+')');
							_list.eq(_ind).addClass('item-opened').animate({height: opened_h}, {
								duration: _speed,
								step:function(t_h){
									t_el.height(deff_h-(t_h-deff_h)*_k1);
									title_el.height(deff_h-(t_h-deff_h)*_k1);
								},
								complete: function(){
									title_list.animate({opacity:1}, _speed/2);
									title_el_title.animate({opacity:1}, _speed/2);
									_list.eq(_ind).data('box').show().animate({opacity: 1}, _speed/2, function(){
										_a = _ind;
										anim_f = true;
									});
								}
							});
						});
					}
					else if(_a == _ind){
						title_list.animate({opacity:0}, _speed/2);
						title_el_title.animate({opacity:0}, _speed/2);
						_list.eq(_a).data('box').animate({opacity: 0}, _speed/2, function(){
							t_el = _list.not(':eq('+_ind+')');
							_list.eq(_a).removeClass('item-opened').animate({height: deff_h}, {
								duration: _speed,
								step:function(t_h){
									t_el.height(small_h+(opened_h-t_h)*_k1);
									title_el.height(small_h+(opened_h-t_h)*_k1);
								},
								complete: function(){
									list_hold.removeClass('opened');
									title_list.animate({opacity:1}, _speed/3);
									title_el_title.animate({opacity:1}, _speed/3, function(){ info_list.css('visibility', 'visible');});
									_a = -1;
									anim_f = true;
								}
							});
						});
					}
					else{
						_list.eq(_a).data('box').animate({opacity: 0}, _speed/2, function(){
							t_el = _list.eq(_a);
							_list.eq(_a).removeClass('item-opened');
							_list.eq(_ind).addClass('item-opened').animate({height: opened_h}, {
								duration: _speed,
								step:function(t_h){
									t_el.height(opened_h-t_h+small_h);
								},
								complete: function(){
									_list.eq(_ind).data('box').show().animate({opacity: 1}, _speed/2, function(){
										_a = _ind;
										anim_f = true;
									});
								}
							});
						});
					}
				}
			}
		}
	}
}





function initStart(){
	$('body.home-page').each(function(){
		$('#nav, div.news-holder').hide();
		$('body').mouseover(function(){
			$('#nav, div.news-holder').fadeIn(500);
		});
	});
}
function initDetail() {
	$('.post .detail a[href="#"]').live('click',function(){
		var more = $(this).parents('.post').find('div.more');
		if(more.css('display')=='none') {
			$(this).html('FERMER');
			$(this).parent().addClass('less');
			more.slideDown(500);
		}
		else {
			$(this).html('VOIR DÉTAILS');
			$(this).parent().removeClass('less');
			more.slideUp(500);
		}
		return false;
	});
}

$(document).ready(function(){
	initDropFade();
	initStart();
	initDetail();
	
	testimonialsPopup();
	testimonialsList();
	testimonialsTabs();
	stepsAccordion();
	pageText();
	questionView();
	
	$('ul.aside li.list:first').addClass('first');
	$('a[name=matrice]').click(function(){
		matrix();
		return false;
	});
	$('a.postuler').click(function(){
		$.fancybox({
			'autoDimensions'	: false,
			'width'         	: 580,
			'height'        	: 340,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'href'				: chemin_root+'send_apply.php?lg='+lg,
			'type'				: 'iframe'
		});
	});
});

function pageText() {
	$('a[name^="text"]').live('click',function(){
		if(!$(this).hasClass('active')) {
			$(this).parents('.page').find('a').removeClass('active');
			$(this).addClass('active');
			if($(this).parents('.text-holder').length) {
				$(this).parents('.text-holder').find('div.text').hide(500);
				$(this).parents('.text-holder').find('div.text:eq('+$(this).attr('name').substr(4)+')').show(500);
			}
			else if($(this).parents('.question-reponse').length) {
				$(this).parents('.question-reponse').find('div.text').hide(500);
				$(this).parents('.question-reponse').find('div.text:eq('+$(this).attr('name').substr(4)+')').show(500);
			}
			else {
				$('div.text').hide(500);
				$('div.text:eq('+$(this).attr('name').substr(4)+')').show(500);	
			}
		}
	});
}

function questionView() {
	$('a[name^=question]').live('click',function(){
		var a=$(this);
		$('ul.questions').fadeOut(500,function(){
			$('ul.questions').prevAll('.btn-close').hide();
			$('ul.questions').hide();
			$('div#'+a.attr('name')).fadeIn();
		});
	});
	$('a[class=btn-close-2]').live('click',function(){
		$(this).parents('div.question-reponse').fadeOut(500,function(){
			$(this).hide();
			$('ul.questions').prevAll('.btn-close').fadeIn();
			$('ul.questions').fadeIn();
		});
	});
	$('.block-title').live('click',function(){
		$('a[name=text0]').click();
		$('ul.questions').show();
		$('div[id^=question]').hide();
	});
}

function asideView(param) {
	param['lg']=lg;
	$.each(['cas','offre','situation','solution'],function(index,value){  
		param['module']='aside_'+value;
		$.get(chemin_root+'module.php',param,function(data){
			if(data==undefined) var data='';
			if(!$('#ajax-aside-'+value).length) $('ul.aside').prepend('<li class="list" id="ajax-aside-'+value+'"></li>');
			$('#ajax-aside-'+value).fadeOut(500,function(){
				if(data!='') $(this).html(data).fadeIn(500);
				$('ul.aside li').removeClass('first');
				$('ul.aside li:visible:first').addClass('first');
			});
		});
	});
}

function casView(id_groupe_cas) {
	var st=$('html,body').scrollTop();
	$.get(chemin_root+'module.php',{lg:lg,module:'cas',id_groupe_cas:id_groupe_cas},function(data){
		$('#ajax-cas').fadeOut(500,function(){
			$(this).html(data).fadeIn(500);
			$('html,body').scrollTop(st);
		});
	});
	asideView({id_groupe_cas:id_groupe_cas,aside_assoc:'cas'});
}
function solutionView(id_groupe_solution) {
	var st=$('html,body').scrollTop();
	$.get(chemin_root+'module.php',{lg:lg,module:'solution',id_groupe_solution:id_groupe_solution},function(data){
		$('#ajax-solution').fadeOut(500,function(){
			$(this).html(data).fadeIn(500);
			$('html,body').scrollTop(st);
		});
	});
	asideView({id_groupe_solution:id_groupe_solution,aside_assoc:'solution'});
}
function offreView(id_groupe_offre,a) {
	id_groupe_offre_=id_groupe_offre;
	if($('#main .matrix-holder').length) {
		if(!$(a).parent().hasClass('active')) {
			$.get(chemin_root+'module.php',{lg:lg,id_navigation:id_navigation,module:'offre',id_groupe_offre:id_groupe_offre},function(data){
				$('#ajax-content').fadeOut(500,function(){
					$('.inner-holder').fadeIn(500);
					$(this).html(data).fadeIn(500);
				});
			});
		}
		asideView({id_groupe_offre:id_groupe_offre,aside_assoc:'offre'});
	}
	/*else document.location.href=a.attr('href');*/
}
function situationView(id_groupe_situation,a) {
	id_groupe_situation_=id_groupe_situation;
	if($('#main .matrix-holder').length) {
		if(!$(a).hasClass('active')) {
			$.get(chemin_root+'module.php',{lg:lg,id_navigation:id_navigation,module:'situation',id_groupe_situation:id_groupe_situation},function(data){
				$('#ajax-content').fadeOut(500,function(){
					$(this).html(data).fadeIn(500);
				});
			});
			asideView({id_groupe_situation:id_groupe_situation,aside_assoc:'situation'});
		}
	}
	/*else document.location.href=a.attr('href');*/
}
function glossaireView(id_groupe_glossaire_terme) {
	var st=$('html,body').scrollTop();
	$.get(chemin_root+'module.php',{lg:lg,module:'glossaire',id_groupe_glossaire_terme:id_groupe_glossaire_terme},function(data){
		$('#ajax-glossaire').fadeOut(500,function(){
			$(this).html(data).fadeIn(500);
			$('html,body').scrollTop(st);
		});
	});
}
function bureauView(id_groupe_bureau) {
	$.get(chemin_root+'module.php',{lg:lg,id_navigation:id_navigation,module:'bureau',id_groupe_bureau:id_groupe_bureau},function(data){
		$('#ajax-bureau').fadeOut(500,function(){
			$(this).html(data).fadeIn(500);
		});
	});
}
function sendPage(title,url,action) {
	if(!action) {
		$.fancybox({
			'autoDimensions'	: false,
			'width'         	: 500,
			'height'        	: 140,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'href'				: chemin_root+'send.php?lg='+lg+'&title='+title+'&url='+url,
			'type'				: 'ajax'
		});
	}
	else {
		$.post(chemin_root+'send.php',{lg:lg,title:title,url:url,your_name:$('#send_your_name').val(),your_email:$('#send_your_email').val(),his_name:$('#send_his_name').val(),his_email:$('#send_his_email').val(),send:1},function(data){
			eval(data);
		});
	}
}

var font_size=1.2;
function fontSize(valeur) {
	font_size+=valeur;
	$('#content').css({'font-size':font_size+'em','line-height':(font_size+0.05)+'em'});
}

function matrix() {
	$('body').prepend('<div id="matrix"></div>');
	$('#matrix').css({
	'position':'absolute',
	'z-index':'910',
	'top':'0px',
	'left':'0px',
	'padding':'10px',
	'background':'#FFF',
	'display':'none'
	});
	$('#matrix').draggable({
		stop: function(event, ui) {
			$.get(chemin_root+'ajax_matrice.php',{left:ui.offset.left,top:ui.offset.top,action:'save_offset'});
		}
	});
	$.get(chemin_root+'module.php',{'lg':lg,'id_navigation':id_navigation,'module':'holder_matrice','id_groupe_offre':id_groupe_offre_,'id_groupe_situation':id_groupe_situation_},function(data){
		$('#matrix').html(data).fadeIn(500);
		var iebody=(document.compatMode && document.compatMode!="BackCompat")?document.documentElement:document.body;
		var left=document.all? iebody.scrollLeft:pageXOffset;
		var top=document.all? iebody.scrollTop:pageYOffset;
		var width=window.innerWidth?window.innerWidth:document.body.clientWidth;
		var height=window.innerHeight?window.innerHeight:document.body.clientHeight;
		$.get(chemin_root+'ajax_matrice.php',{'scroll_left':left,'scroll_top':top,'screen_width':width,'screen_height':height,'div_width':$('#matrix').width(),'div_height':$('#matrix').height(),action:'load_offset'},function(data){
			eval(data);
		});
	});
	$('body').prepend('<div id="overlay"></div>');
	$('#overlay').css({
	'position':'fixed',
	'z-index':'900',
	'top':'0px',
	'left':'0px',
	'height':'100%',
	'width':'100%',
	'background':'#000',
	'opacity':0.5
	});
	isIE6 = !$.support.opacity && !window.XMLHttpRequest;
	if (isIE6) {
		$('#overlay').css({'position':'absolute','height':document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight});
	}
}
