//var ltie7 = ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent));
var ltie7 = ($.browser.msie && $.browser.version < 7);
var ltie8 = ($.browser.msie && $.browser.version < 8);
var ltie9 = ($.browser.msie && $.browser.version < 9);

$.ifixpng('/design/laposte/images/img/pixel.gif');

$(function(){

	if (ltie8) {
		$('.header_media object').parent().css('z-index','2');
	}

	if (ltie7) {
		$('.pngfix, input[type=image][src$=.png]').ifixpng();
		$('img').filter(function(){return $(this).attr('src').match(/.*\.png([?].*)?$/i) ? true : false;}).ifixpng();
		$('img[usemap]').css('border','none');
		$('input[type="radio"], input[type="checkbox"], input[type="file"]').css('border','none');

		/* Lisser les margin-top des titres de pages */
		$('#centercol h2:first-child, #centerpart h2:first-child').css({marginTop: '0'});
		/* Descendre les images qui sont en d�but de page et qui pourraient mordre dans le dégradé */
		$('#centercol h2:first-child + p .img_center img, #centerpart h2:first-child + p .img_center img').css({marginTop: '15px'});
		$('#liste_footer span:first').not('.lien_presse').css({background: 'none'});// Footer
		if (!$('#antehome').size()) $('#liste_footer li:first').css({background: 'none'});// Footer home sonsommateurs
		$('#antehome #liste_footer .lien_presse + li').css({background: 'none'});// Footer antehome


		$('#centre h1:first').css({marginTop: 0});

		$('#centre h3 + p').css({marginTop: 0});
		$('h2.secteur + p').css({marginTop: 0}); /* entr_article_tableau_secteur.html */


		// page contactez-nous entreprise
		$('.contact_entree #centre .object-right:first, .contact_entree #centre h2:first').css({clear: 'left'});
		$('.contact_entree #centre p + p').css({marginTop: '1em'});
		$('.contact_entree #centre p + ul').css({marginTop: 0});

	}
	if (!ltie7){
		// Gesion PodCast
		$('#podcast .autre, #podcastMetier .autre').show();
		$('#podcast .ie, #podcastMetier .ie').hide();
	}

	if(navigator.userAgent.indexOf('Macintosh') != -1){

		$('.listeDirecteurs .directeur').css('line-height', '1.4em');

		// Gestion envoyer à un ami pour Mac
		$('#envoyer .mac').show();
		$('#envoyer .pc').hide();
	}

	/***** HOMES *****/
	// Ajuster la hauteur des li.homeItem des homes
	gestionHomeItems.init();

	//Gestion des classes sur les listes de top5
	$('#espace_top5').find('li:odd').addClass('no_opacity');
	/***** /HOMES *****/

	/***** HOMES FANTOMES *****/
	$('.particulier_f .actuTitreTexte').adjustSize({'periode':3});

	/***** OUTILS *****/
	$('.liste_col_outils .colonne_outils').adjustSize({'periode':4});

	/***** CAMPAGNES PRESSE *****/
	$('.communicationPresse .spot h2').adjustSize({'periode':2}); /* entr_rubrique_communication_annonce.html */
	$('.communicationPresse .spot').adjustSize({'periode':2}); /* entr_rubrique_communication_annonce.html */

	gestionMenu.init();
	gestionPopup.init();
	fermer.init();
	tablesLignesAlterner.init();
	gestionInputDefaultValue.init();
	popUpVideoLettreMax.init();
	popUpEcourrier.init();

	produitsDeplier.init();

	$('#antehome .visite_guidee').click(function() {
		xt_med('C',s2,'antehome::visite_guidee','N');
		popup();
		return false;
	});
});

var gestionInputDefaultValue = function () {

	var _init = function () {
		$('#q_search_top, #q_search').click(function () {hideValueDefault($(this),'Mots-clés');}).blur(function () {showValueDefault($(this),'Mots-clés');});
	};

	var showValueDefault = function (elt,valDefault) {
		if (elt.val() == '') {
			elt.val(valDefault);
		}
	};

	var hideValueDefault = function (elt,valDefault) {
		if (elt.val() == valDefault) {
			elt.val('');
		}
	};

	return {init:_init};
}();

var gestionHomeItems = function () {
	var periodeHome, homeItemList, homeItemSize, tranches, trancheItems, index;
	function init() {
		periodeHome = 2;
		if ($('#contenu').is('.particulier_f')) periodeHome = 3;

		adjustHeight($('.homeItem').not('.grand_zoom').not('.classement'),periodeHome);
		adjustHeight($('.classement'),periodeHome);
	}

	function adjustHeight(elmts, periode) {

		elmtsSize = elmts.size();
		tranches = Math.ceil(elmtsSize/periode);

		for (var i=tranches; i>0; i--) {
			index = (tranches-i)*periode;
			trancheItems=elmts.slice(index, index+periode);
			trancheItems.find('.actuTitreTexte').adjustSize({periode: periode}).end().adjustSize({periode: periode}).each(function(){
				var li=$(this);
				var totalHeight=li.height();
				var h2Height=li.find('h2').height();
				var btmHeight=li.find('.bottom_actu').height();
				var contenuHeight=parseInt(totalHeight-h2Height-btmHeight-20)+'px'; // -20 : padding sur .contenu_actu
				if (ltie7) li.find('.contenu_actu').height(contenuHeight);
				else li.find('.contenu_actu').css({minHeight: contenuHeight});
			});
			if (trancheItems.filter('.zoom').size()) {
				// gestion un peu compliquée pour que l'alignement avec les zooms flash soit correct sans qu'on ait de décallages dans les blocs à causes des arrondis de hauteurs sous FF.
				if(ltie7) trancheItems.height(parseInt(trancheItems.height()-1)+'px');
				else trancheItems.css('minHeight',parseInt(trancheItems.css('minHeight'))-1);
			}
		}
	}

	return {init:init}
}();

var gestionMenu = function () {
	function _init () {
		$('div.top_menu', $('#menu_nav')).click(function(){
			var sousMenu = $(this).parent().find('.sous_menu_nav');
			if (sousMenu.attr('class').indexOf('sous_menu_nav_afficher') !== -1) menuRemoveClasses(sousMenu);
			else menuAddClasses(sousMenu);
		});
		$('#menu_nav .sous_menu_nav a').focus(function(){menuAddClasses($(this).parents('.sous_menu_nav'))}).blur(function(){menuRemoveClasses($(this).parents('.sous_menu_nav'))});
	};
	function menuAddClasses(elmt){
		elmt.addClass('sous_menu_nav_afficher').siblings('.sous_menu_nav_bottom').addClass('sous_menu_nav_bottom_afficher');
	};
	function menuRemoveClasses(elmt){
		elmt.removeClass('sous_menu_nav_afficher').siblings('.sous_menu_nav_bottom').removeClass('sous_menu_nav_bottom_afficher');
	};
	return {init:_init}
}();


var popUpVideoLettreMax=function () {
	function _init() {
	$('.popUpVideoLettreMax').click(function(){
	 var location=$(this).attr('href');
	 window.open(location,'PopUp','height=510,width=695,resizable=yes,scrollbars=yes,status=no'); return false;})
	};
	return {init:_init}
}();

var popUpEcourrier=function () {
	function _init() {
	$('.popup_enSavoirPlus').click(function(){
	 var location=$(this).attr('href');
	 window.open(location,'PopUp','height=510, width=437, resizable=yes,scrollbars=yes,status=no'); return false;})
	};
	return {init:_init}
}();

var gestionPopup=function () {
function _init() {
$('#popup').click(function(){
 var location=$(this).attr('href');
 window.open(location,'PopUp','height=550,width=500,resizable=yes,scrollbars=yes,status=no'); return false;})
};
return {init:_init}
}();

var fermer=function () {
function _init() {
$('.fermer').click(function(){
 window.close();
 return false;})
};
return {init:_init}
}();

var tablesLignesAlterner = function () {
	function init(){
		$('#centre table').each(function(){
			$(this).find('tr:even').addClass('pair');
		});
	}
	return {init:init}
}();

//ltie7 : $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
// Nouvelle fonction jquery de retaillage de blocks
$.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = ($.browser.msie && $.browser.version < 7);
	if (options) jQuery.extend(settings, options);

	var totalSize = $(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = parseInt($(this).height());
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = parseInt(blockTotalHeight)+1;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = parseInt(maxTotalHeightArray[blockId]);
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',parseInt(blockComputedHeight)+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $(this).width() ? $(this).width() : $(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $(this).width(blockComputedWidth+'px');
			else $(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}

	return this;
}

var fermer=function () {
function _init() {
$('.fermer').click(function(){
 window.close();
 return false;})
};
return {init:_init}
}();

/******* POP UP animation flash Homepage *******/
function popup() {
	window.open('http://laposte-video-visite-guidee.cvf.fr/index.html','popup','scrollbars=no, width=840, height=580, resizable=no');
}

var produitsDeplier = function(){
	function _init() {
		$('.deplierContenu a').click(function(){
			var elmt = $(this);
			var img3634 = $('#produitCadre3634');
			if (elmt.is('.contenuDeplie')) {
				elmt.removeClass('contenuDeplie');
				elmt.parent().next('.cadreCPlie').slideUp('swing', function(){img3634.hide().show();});
			} else {
				elmt.addClass('contenuDeplie');
				elmt.parent().next('.cadreCPlie').slideDown('swing', function(){img3634.hide().show();});
			}

			return false;
		});
	}
	return {init:_init}
}()

/*
function adjustSelects() {
	if($('.adjustSelect').size() > 0 && $.browser.msie) {
		$('.adjustSelect').each(function(){
			$(this).css('width', '');
			$(this).css('position','absolute');
			$(this).css('left','0');
			$(this).css('z-index','100');
			var passe=0;
		}).mouseover(function(){
				$(this).css('width','auto');
				$(this).css('z-index','1000');
			passe=0;
		}).click(function(){
			if (passe == 1) {
				$(this).css('width', 'auto');
				$(this).css('z-index','1000');
				passe = 0;
			}
			passe=1;
		}).blur(function(){
			$(this).css('width', '');
			$(this).css('z-index','100');
			passe=0;

		}).mouseout(function(){
			if (passe==0){
				$(this).css('width', '');
				$(this).css('z-index','100');
			}
		});
	}
}*/


/*
// Target est un s�lecteur jQuery qui permet d'indiquer quel est l'�lement (image ou input) qui doit changer.
// Sa valeur par d�faut est '$myself', ce qui indique que l'objet est sa propre cible.
// Ex : $('#menu > li').each(function(){$(this).switchImage("","> a > img");});

$.fn.switchImage=function(eventTargetSelector, changeTargetSelector, options) {
	var eventTargetSelector, changeTargetSelector, eventTarget, changeTarget;
	if (!eventTargetSelector) eventTargetSelector = '?myself';
	if (!changeTargetSelector) changeTargetSelector = '?myself';
	var settings = {
		stringDefault: '_off',
		stringHover: '_on'
	};
	if (options) {
		jQuery.extend(settings, options);
	}
	if (eventTargetSelector == '?myself') eventTarget=$(this);
	else eventTarget = $(this).find(eventTargetSelector);
	if (changeTargetSelector == '?myself') changeTarget=$(this);
	else changeTarget = $(this).find(changeTargetSelector);
	eventTarget.filter(function(index){
		return (changeTarget.is("input[type=image][src]") || changeTarget.is("img[src]"));
	}).mouseover(function(){switchToHover(changeTarget)}).focus(function(){switchToHover(changeTarget)}).mouseout(function(){switchToDefault(changeTarget)}).blur(function(){switchToDefault(changeTarget)});

	function switchToHover(e) {
		e.each(function(){
			getSrcElmts.init($(this));
			if (getSrcElmts.fileName().indexOf(settings.stringDefault) != -1) {
				$(this).attr('src', getSrcElmts.filePath()+'/'+getSrcElmts.replaceFileName(settings.stringDefault, settings.stringHover));
			}
		});
	}
	function switchToDefault(e) {
		e.each(function(){
			getSrcElmts.init($(this));
			if (getSrcElmts.fileName().indexOf(settings.stringHover) != -1) {
				$(this).attr('src', getSrcElmts.filePath()+'/'+getSrcElmts.replaceFileName(settings.stringHover, settings.stringDefault));
			}
		});
	}

	var getSrcElmts = function() {
		var src,srcPathSplit, srcPathSplitLength;
		function init(e){
			src = e.attr('src');
			srcPathSplit = src.split('/');
			srcPathSplitLength = srcPathSplit.length;
		}
		function filePath(){
			if (srcPathSplitLength)	return srcPathSplit.slice(0,-1).join('/');
		}
		function fileName(){
			if (srcPathSplitLength)	return srcPathSplit[srcPathSplitLength-1];
		}
		function replaceFileName(stFrom,stTo){
			elemts=fileName().split(stFrom);
			end=elemts.pop();
			begin=elemts.join(stFrom);
			return begin+stTo+end;
		}
		return {init:init,filePath:filePath,fileName:fileName,replaceFileName:replaceFileName}
	}()

	changeTarget.each(function(){
		// preload
		getSrcElmts.init($(this));
		var preload = new Image;
		preload.src=getSrcElmts.filePath()+'/'+getSrcElmts.replaceFileName(settings.stringDefault, settings.stringHover);
	});


	return this;
}

$.fn.fakeMiddleCenter=function(options) {
	var settings = {
		paddingHeight : 6,
		triggerHeight: 18
	};
	if (options) jQuery.extend(settings, options);

	var ltie7 = ($.browser.msie && $.browser.version < 7);
	// Reset des CSS
	if (!ltie7) {
		minHeight=parseInt($(this).css('min-height'));
		$(this).css('min-height','0');
	} else {
		height=parseInt($(this).height());
		$(this).height('auto');
	}
	$(this).each(function(){
		// Si l'�l�ment est assez grand, on lui redonne les styles CSS inchang�s.
		if($(this).height() > settings.triggerHeight) {
			if (minHeight) $(this).css('min-height',minHeight);
			else $(this).height(height);
			return;
		}
		// On ajuste le padding-top pour faire illusion.
		$(this).css('padding-top',settings.paddingHeight+'px');
		if (minHeight) $(this).css('min-height',(minHeight-settings.paddingHeight)+'px');
		else $(this).height((height-settings.paddingHeight)+'px');
	});
	return this;
}

var footerTicker = function() {
	var afpTicker, line, lineLength, lineTotalMoveToDo, moveDuration;
	var swapDelay=100;
	var moveDelay=30;
	var moveLength=2;
	var displayWindowSize=370;
	function init() {
		afpTicker = $('#afp_ticker');
		line = $(' > li:first',afpTicker);
		if (line.is('li')) lineMove(line);
	}

	function lineMove(line) {
		lineLength=getLineLength(line);
		lineTotalMoveToDo=lineLength+displayWindowSize*2;
		moveDuration=lineTotalMoveToDo*moveDelay/moveLength;

		afpTicker.hover(function(){line.stop();},function(){lineAnimate(line)}).find('>li:visible >a').focus(function(){line.css({left: '2px'}).stop();}).blur(function(){lineAnimate(line);});

		lineAnimate(line);
	}

	function lineSwap(line) {
		line.hide().css({left: displayWindowSize});
		afpTicker.unbind('mouseover',function(){line.stop();}).unbind('mouseout',function(){lineAnimate(line);}).find('>li:visible >a').unbind('focus',function(){line.css({left: '2px'}).stop();}).unbind('blur',function(){lineAnimate(line);});
		line=line.next().is('li')?line.next():line.prevAll().eq(0).is('li')?line.prevAll().eq(0):line;
		line.css({display: 'inline', left: displayWindowSize});
		lineMove(line);
	}

	function getLineLength(line) {return parseInt(line.width());}
	function lineAnimate(line){line.animate({left: -lineLength},moveDuration, function(){lineSwap($(this));})}

	return{init:init};
}();


var corriger_email_safari = function(){
	var aMailto;
	function init(){
		//if (navigator.appVersion.indexOf("Mac")==-1) return;
		aMailto=$('a[href^=mailto:]');
		aMailto.each(function(){
			var href=accentsReplace($(this).attr('href'));
			$(this).attr('href',href);
			alert($(this).attr('href'));
		});
	}

	function accentsReplace(string){
		string=string.replace(/%E1/g,'�');
		string=string.replace(/%E0/g,'�');
		string=string.replace(/%E2/g,'�');
		string=string.replace(/%E4/g,'�');
		string=string.replace(/%E9/g,'�');
		string=string.replace(/%E8/g,'�');
		string=string.replace(/%EA/g,'�');
		string=string.replace(/%EB/g,'�');
		string=string.replace(/%ED/g,'�');
		string=string.replace(/%EC/g,'�');
		string=string.replace(/%EF/g,'�');
		string=string.replace(/%EE/g,'�');
		string=string.replace(/%F2/g,'�');
		string=string.replace(/%F3/g,'�');
		string=string.replace(/%F4/g,'�');
		string=string.replace(/%F6/g,'�');
		string=string.replace(/%F9/g,'�');
		string=string.replace(/%FB/g,'�');
		string=string.replace(/%FC/g,'�');
		string=string.replace(/%C1/g,'�');
		string=string.replace(/%C0/g,'�');
		string=string.replace(/%C4/g,'�');
		string=string.replace(/%C2/g,'�');
		string=string.replace(/%C9/g,'�');
		string=string.replace(/%C8/g,'�');
		string=string.replace(/%CB/g,'�');
		string=string.replace(/%CA/g,'�');
		string=string.replace(/%CC/g,'�');
		string=string.replace(/%CD/g,'�');
		string=string.replace(/%CF/g,'�');
		string=string.replace(/%CE/g,'�');
		string=string.replace(/%D2/g,'�');
		string=string.replace(/%D3/g,'�');
		string=string.replace(/%D4/g,'�');
		string=string.replace(/%D6/g,'�');
		string=string.replace(/%D9/g,'�');
		string=string.replace(/%DC/g,'�');
		string=string.replace(/%DB/g,'�');
		return string;
	}

	return {init:init}
}();

*/
