// ==============================================
// = CENTRER VERTICALEMENT UN DIV (SPLASH PAGE) =
// ==============================================
// http://www.nealgrosskopf.com/tech/thread.asp?pid=37
(function ($) {
	$.fn.vAlign = function() {
		return this.each(function(i){
			var h  = $(this).height();
			var oh = $(this).outerHeight();
			var mt = (h + (oh - h)) / 2;
			$(this).css("margin-top", "-" + mt + "px");
			$(this).css("top", "50%");
			$(this).css("position", "absolute");
		});
	};
})(jQuery);

// ======================
// = FOOTER POSITIONING =
// ======================
footer = {
	initialdocheight : 0,
	contentdiv       : 0,
	contentoffset    : 0,
	contentheight    : 0,
	docheight        : 0,
	windowheight     : 0,
	init : function(){
		footer.initialdocheight = footer.setvalues();
		// 		footer.setfooterposition();
		// footer.setvalues();
		footer.setfooterposition();
		if(window.console) console.log('footer');
	},
	resize : function(){
		footer.setvalues();
		footer.setfooterposition();
		Cufon.replace('h1 span.title');
		// footer.debug();
	},
	setvalues : function(){
		footer.contentdiv    = document.getElementById('content');			// get content element
		footer.contentoffset = footer.contentdiv.offsetTop;					// get content's top offset
		footer.contentheight = footer.contentdiv.offsetHeight;              // get content's height
		footer.docheight     = footer.contentoffset + footer.contentheight; // get document's height
		footer.windowheight  = document.documentElement.clientHeight;       // get window's height
		return footer.docheight;
	},
	setfooterposition : function(){
		// if the document's height is less than the window's height :
		if (footer.windowheight > footer.initialdocheight) {
			// console.log('true', footer.windowheight,'-', footer.docheight, (footer.windowheight - footer.docheight));
			$('#content').css({'height': (footer.windowheight - footer.contentoffset)+ 'px' });
			if (jQuery.browser.msie) {
				$('#content').css({'height': (footer.windowheight - footer.contentoffset) + 20+ 'px' });
			}
			$('#footer').css({'position':'absolute','top': (footer.windowheight - document.getElementById('footer').offsetHeight) + 'px'});
		}else{
			
			// console.log('false', footer.windowheight, '-', footer.docheight, (footer.windowheight - footer.docheight));
			$('#content').css({'height': 'auto'});
			// alert(footer.initialdocheight + ' - ' + footer.contentoffset + ' = ' + (footer.initialdocheight - footer.contentoffset));
			$('#footer').css({'position':'static'});
			
		}
	},
	debug : function(){
		$('#footer').html(
				'footer.contentdiv : ' + footer.contentdiv + ', ' +
				'footer.contentoffset : ' + footer.contentoffset + ', ' +
				'footer.contentheight : ' + footer.contentheight + ', ' +
				'footer.docheight : ' + footer.docheight + ', ' +
				'footer.windowheight : ' + footer.windowheight + ', ' +
				'footer relative position : ' + (footer.windowheight - footer.contentoffset) 
			);
		// console.log(footer.contentdiv);
		// console.log(footer.contentoffset);
		// console.log(footer.contentheight);
		// console.log(footer.docheight);
		// console.log(footer.windowheight);
	}
}

// ===================
// = LANGUAGE TOGGLE =
// ===================
function proactionChangeLang(){
	window.location = $('#menu_principal a#langtoggle').attr('href');
}
// ==============
// = NAVIGATION =
// ==============
function proactionChangePage(index){
	$('#menu_principal ul li a').click(function(){
		window.location = $(this).attr('href');
	});
	$('#menu_principal ul li:eq('+(index)+')').find('a').triggerHandler('click');
}

// ========================
// = PAGINATION NOUVELLES =
// ========================
paginate = {
	init : function(){
		// initialize the arrows beside the digits:
		$('#paginate ul li.prev').hide();
		if ($('#paginate ul li:not(.next, .prev)').length < 2) {
			$('#paginate').hide();
		};
		// initialize the events :
		$('#paginate ul a').click(function(){
			paginate.choose($(this));
			return false;
		});
	},
	choose : function(caller){
		// replace content with ajax :
		if (caller.attr('href').search(/#/) != -1) {
			// trouver lien active
			$parentLI = caller.parent('li');
			$activeLI = $parentLI.siblings('li.active');
			// aller chercher son Next
			if ($parentLI.is('.next')) {
				$nextLI = $activeLI.next();
				// aller chercher le a du Next
				$('#newscontent').load($nextLI.find('a').attr('href'),null,function(){
					footer.init();		 // replacer le footer
					Cufon.replace('h1 span.title'); // Cufonize the H1 tags that we just loaded
				});
				$nextLI.siblings().removeClass('active');
				$nextLI.addClass('active');

				$nextLI.siblings('li').show(); // initially show them all
				if ($nextLI.next().is(':last-child') && $nextLI.next().is('.next')) {
					$nextLI.next().hide();
				}
				if ($nextLI.prev().is(':first-child') && $nextLI.prev().is('.prev')) {
					$nextLI.prev().hide();
				}
			}else if($parentLI.is('.prev')){
				$prevLI = $activeLI.prev();
				// aller chercher le a du Next
				$('#newscontent').load($prevLI.find('a').attr('href'),null,function(){
					footer.init();		 // replacer le footer
					Cufon.replace('h1 span.title'); // Cufonize the H1 tags that we just loaded
				});
				$prevLI.siblings().removeClass('active');
				$prevLI.addClass('active');

				$prevLI.siblings('li').show(); // initially show them all
				if ($prevLI.next().is(':last-child') && $prevLI.next().is('.next')) {
					$prevLI.next().hide();
				}
				if ($prevLI.prev().is(':first-child') && $prevLI.prev().is('.prev')) {
					$prevLI.prev().hide();
				}
			}
			
		}else{
			$('#newscontent').load(caller.attr('href'),null,function(){
				
				footer.init();			// replacer le footer
				Cufon.replace('h1 span.title'); 	// Cufonize the H1 tags that we just loaded
			}); 
			// get parent, set active :
			$parentLI = caller.parent('li'); 
			$parentLI.siblings().removeClass('active');
			$parentLI.addClass('active');
			// hide next if last, or prev if first
			$parentLI.siblings('li').show(); // initially show them all
			if ($parentLI.next().is(':last-child') && $parentLI.next().is('.next')) {
				$parentLI.next().hide();
			}
			if ($parentLI.prev().is(':first-child') && $parentLI.prev().is('.prev')) {
				$parentLI.prev().hide();
			}
		}
	}
}


// ==================
// = EGALISER BLOCS =
// ==================

equalizeblocks = {
	init : function(){
		$firstCol = $('.ltbloc');
		$secondCol = $('.rtbloc');
		// if the difference between the two column's heights is less than 50px :
		if (Math.abs($firstCol.height() - $secondCol.height()) < 50) {
			// equalize them :
			if ($firstCol.height() > $secondCol.height()) {
				equalizeblocks.growRightCol();
				
			}else{
				equalizeblocks.growLeftCol();
			}
		}else if($firstCol.height() < $secondCol.height()){ // if left col is smaller than second col
			equalizeblocks.growLeftCol();
		}
	},
	growLeftCol : function(){
		$firstBloc = $firstCol.find('.inbloc:last');
		$secondCol = $('.rtbloc');
		$secondBloc = $secondCol.find('.inbloc:last');
		$h1 = $firstCol.find('h1');
		// $h1.css({'border':'1px solid red'});
		// $firstBloc.css({'border':'1px solid blue'});
		// alert($h1.height());
		// var offsetBloc = parseInt($firstBloc.offset().top,10);
		// var offsetCol  = parseInt($firstCol.offset().top,10);
		// offsetDiff = offsetBloc - offsetCol;
		paddbtm   = parseInt($firstBloc.css('padding-bottom'),10);
		paddtop   = parseInt($firstBloc.css('padding-top'),10);
		margbtm   = parseInt($firstBloc.css('margin-bottom'),10);
		margtop   = parseInt($firstBloc.css('margin-top'),10);
		h1margtop = parseInt($h1.css('margin-top'));
		h1margbtm = parseInt($h1.css('margin-bottom'));
		h1paddtop = parseInt($h1.css('padding-top'));
		h1paddbtm = parseInt($h1.css('padding-bottom'));
		// if (jQuery.browser.msie) {
		// 			var newheight = $secondBloc.height() - margbtm - margtop - paddbtm - paddtop - h1paddtop - h1paddbtm - h1margbtm - h1margtop - 4 - $h1.height();
		// 		}else{
			var newheight = $secondCol.height() - margbtm - margtop - paddbtm - paddtop - h1paddtop - h1paddbtm - h1margbtm - h1margtop - 4 - $h1.height();
		// }
		// alert($secondCol.height() +"-"+ margbtm +"-"+ margtop +"-"+ paddbtm +"-"+ paddtop +"-"+ h1paddtop +"-"+ h1paddbtm +"-"+ h1margtop +"-"+ h1margbtm+"-"+ 4 +"-"+ $h1.height() +"="+ newheight);
		$firstBloc.css({'height': newheight+'px'});
	},
	growRightCol : function(){
		$secondBloc = $secondCol.find('.inbloc:last');
		paddbtm     = parseInt($secondBloc.css('padding-bottom'),10);
		paddtop     = parseInt($secondBloc.css('padding-top'),10);
		margbtm     = parseInt($secondBloc.css('margin-bottom'),10);
		margtop     = parseInt($secondBloc.css('margin-top'),10);
		$secondBloc.css({'height': ($firstCol.height()-paddbtm-paddtop-margbtm-margtop-3)+'px'});
		// $secondBloc.css({'padding':'0','margin-top':'0'});
		// $secondBloc.css({'height': ($firstCol.height()-3)+'px'});
	},
	landing : function(){
		$firstCol = $('.ltbloc');
		$secondCol = $('.rtbloc');
		// if there is 2 blocs on the right and 1 on the left col
		if ($('.wbloc', $firstCol).length == 1 && $('.wbloc', $secondCol).length == 2) {
			$moreBtn = $('p.more',$secondCol);
			heightDiff = ($secondCol.height()-$moreBtn.height()-parseInt($moreBtn.css('margin-top'))) - $firstCol.height();
			// apply a div around the .inbloc's content :
			$bloc = $('.wbloc:eq(1) .inbloc .bloccontent', $secondCol);
			$bloc.css({'height': ($bloc.height()-heightDiff)+'px'});
		};
	}
}

// =================
// = CSS DEBUGGING =
// =================
iefixes = {
	init : function(){
		if (jQuery.browser.msie) {
			$(".inbloc p:last-child, .profil.implications .inbloc p:last-child, .approche.secteurs .inbloc p:last-child").css({"padding-bottom": '0'});
		}
	}
}

$(function(){
	// LOAD SOME SCRIPTS ONLY IN SOME SPECIFIC PAGES :
	
	// 
	if ($('body.landing, body.profil, body.nouvelles, body.carrieres, body.contacts, body.approche.etude_sur_la_productivite, body.approche.resultats, body.approche.services, body.approche.secteurs').length > 0) {
		var resizeTimer = null;
		$(window).bind('resize', function() {
		    if (resizeTimer) clearTimeout(resizeTimer);
		    resizeTimer = setTimeout(footer.resize, 100);
		});
		footer.init();
		iefixes.init();

	};
	
	// 
	if ($('body.approche.etude_sur_la_productivite, body.approche.resultats').length > 0) {
		equalizeblocks.init();
		if(window.console) console.log('equalizeblocks');
	};
	
	// 
	if ($('body.approche.outils').length > 0) {
		var blocheight = 145;
		// stocker les height originales :
		$('.bloccontent').each(function(i){
			$(this).data('originalheight', $(this).height());
			if ($(this).height() < blocheight) {
				$(this).parent('.inbloc').next('p.more').hide();
			};
		});
		// mettre tout les heights a la hauteur de blocheight :
		$('.bloccontent').css({'height':blocheight+'px'});
		// assigner les evenements de toggle pour ouvrir/fermer :
		$('.more a').toggle(function(){
			$(this).addClass('close');
			var $div = $(this).parent().prev().find('.bloccontent');
			$div.animate({'height':$div.data('originalheight')+'px'});
			return false;
		}, function(){
			$(this).removeClass('close');
			$(this).parent().prev().find('.bloccontent').animate({'height':blocheight+'px'});
			return false;
		});
		iefixes.init();
	};
});
