$(function(){

	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);

	$('img[@src$=.png]').pngfix();
	$('input[@src$=.png]').pngfix();

	$('#outilsContact li, #mainMenu li').not('.on').find('a').each(function(){$(this).switchImage("","> img");})

	$('#pushSousRubriques > li').each(function(){
		$(this).css('backgroundAttachment','scroll').css('backgroundPosition','-564px '+(-1*this.offsetTop)+'px');
	});
	$('#pushSousRubriques').scroll(function(){
		var scrollOffset = this.scrollTop;
		$('#pushSousRubriques > li').each(function(){
			$(this).css('backgroundPosition','-564px '+(-1*this.offsetTop+scrollOffset)+'px');
		});
	})

	$('#texte').scroll(function(){
		$('#texte').css('backgroundPosition-y',this.scrollTop);
	})

	/* HOME */
	$('.itemPush').adjustSize({periode: 3});

	if (ltie7) {
		$('#footer li:first').css('borderLeft','none');

		/* HOME */
		$('.itemPush:first').css('borderLeft','1px solid #02448e');
	}
	adjustSelects();
	banque_images();
	gestion_recrutement();
	gestion_communiques();
	gestion_ootb();
	gestionActifsHammerson();
	gestionDeveloppementDurable();
	lienWeborama_PDF();
	consulterNewsletter();
	inclusionFlash();

	if ($('.squeletteRubriqueGroupe').size() > 0){
		// on charge les images avec la super fonction test !!!!
//		$('#animation').css("z-index",'6');
		test();

	}


});

function imageSwap(image){
	var round = parseInt(Math.random()*4);
	var round = (round == 0)?4:round;

	var aImages = new Array();
	aImages[0] 	= $('#hammersonRotate1');
	aImages[1] 	= $('#hammersonRotate2');
	aImages[2] 	= $('#hammersonRotate3');
	aImages[3] 	= $('#hammersonRotatePool' + round);
	elmt=$(this);

	if (image==1) {
		elmt.find('#contenu, #subMenu, #texte').css('background','none');
		elmt.find('#contenu').css('position','relative');
		first = false;
	}


	if (image==0) {
		swapFade(aImages[0], aImages[1]);
	}
	else{
		var image1 = parseInt(image)-1;
		var image2 = image;
		swapFade(aImages[image1], aImages[image2]);
	}

}
function swapFade(e1,e2) {
	e1.animate({
		opacity: 'hide'
	}, 2000);
	e2.animate({
		opacity: 'show'
	}, 2000);
}

// HOME
$(function(){
	var image1 = $('#animator1');
	var image2 = $('#animator2').hide();
	var image3 = $('#animator3').hide();
	var image4 = $('#animator4').hide();
	var image5 = $('#animator5').hide();

	if (image1.size()) {
		var outgoing = image1;
		var incomming = image2;
		imageSwap();
		imageSwapSetInterval = window.setInterval(imageSwap, 4000);
	}

	function imageSwap() {
		swapFade(outgoing, incomming);
		if (outgoing == image1) outgoing = image2;
		else if (outgoing == image2) outgoing = image3;
		else if (outgoing == image3) outgoing = image4;
		else if (outgoing == image4) outgoing = image5;
		else outgoing = image1;
		if (incomming == image1) incomming = image2;
		else if (incomming == image2) incomming = image3;
		else if (incomming == image3) incomming = image4;
		else if (incomming == image4) incomming = image5;
		else incomming = image1;
	}

	function imageSwap2() {
		swapFade(outgoing, incomming);
		if (outgoing == image4) outgoing = image5;
		else if (outgoing == image5) outgoing = image6;
		else outgoing = image4;
		if (incomming == image4) incomming = image5;
		else if (incomming == image5) incomming = image6;
		else incomming = image4;
	}

	function swapFade(e1,e2) {
		e1.animate({
			opacity: 'hide'
		}, 2000);
		e2.animate({
			opacity: 'show'
		}, 2000);
	}
});
// 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) {
		var src = e.attr('src');
		var srcPathSplit = src.split('/');
		var fileName=srcPathSplit.pop();
		var filePath=srcPathSplit.join('/');
		if (fileName.indexOf(settings.stringDefault) != -1) {
			e.attr('src', filePath+'/'+fileName.replace(settings.stringDefault, settings.stringHover));
		}
	}
	function switchToDefault(e) {
		var src = e.attr('src');
		var srcPathSplit = src.split('/');
		var fileName=srcPathSplit.pop();
		var filePath=srcPathSplit.join('/');
		if (fileName.indexOf(settings.stringHover) != -1) {
			e.attr('src', filePath+'/'+fileName.replace(settings.stringHover, settings.stringDefault));
		}
	}

	return this;
}

$.fn.adjustSize=function(options) {
	var settings = {
		periode: 2,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	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 = $(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId];
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',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;
}

/** BANQUE D'IMAGES **/
function banque_images() {
	if($('#liste_images').size() == 0 || $('#zoom_image').size() == 0) return false;

	$('#lien_nav_banque_image').html('<a href="" id="img_pre"><img src="_media/img/ActDoc_BqImg_Fl-Ret.gif" width="7" height="14" alt="précédant" /></a><a href="" id="img_suiv"><img src="_media/img/ActDoc_BqImg_Fl-Av.gif" width="7" height="14" alt="suivant" /></a>');

	$('#img_pre').click( function () {
		if ($('#zoom_image div.on').prev().size()) {
			$('#zoom_image div.on').toggleClass("on").toggleClass("off").prev().toggleClass("on").toggleClass("off");
			$('#liste_image_rep li.on').toggleClass("on").prev().toggleClass("on");
		}
		return false;
	});

	$('#img_suiv').click( function () {
		if($('#zoom_image div.on').next().html() != $('#lien_nav_banque_image').html()) {
			$('#zoom_image div.on').toggleClass("on").toggleClass("off").next().toggleClass("on").toggleClass("off");
			$('#liste_image_rep li.on').toggleClass("on").next().toggleClass("on");
		}
		return false;
	});

	$('.image_banque').click( function() {
		$('.image_banque').removeClass("on");
		$('#'+this.id).addClass("on");
		$('.zoom_image_banque').addClass("off").removeClass("on");
		$('#zoom_'+this.id).addClass("on").removeClass("off");
		return false;
	});

}

/* GESTION DES SELECT */
function adjustSelects() {
	if ($('.adjustSelect').size() > 0){
		if ($.browser.msie) {
			$('.adjustSelect').each(function(){
				$(this).css('width', '');
				$(this).css('position','absolute');
				$(this).css('left','3px');
				$(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', '');
					$(this).css('z-index','100');
					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');
				}
			});
		}
	}
}

/* OFFRES DE STAGE / EMPLOIS */
function gestion_recrutement() {

	if ($('#recrutement').size == 0) return false;

	$(".detail_offre").each(function () {
		$(this).css('display','none');
	});

	$('#recrutement .offre_resume').click( function () {
		$('#recrutement').css('display','none');

		$('#detail_'+ this.id).css('display','block').after('<p class="spip retour"><a href="" id="retour_liste_offre">Retour à la recherche</a></p><br class="clear" />');

			$('#retour_liste_offre').click( function () {
				$('#texte').css('backgroundPosition-y','');
				$('#retour_liste_offre').remove();
				$(".detail_offre").each(function () {
					$(this).css('display','none');
				});
				$('#recrutement').css('display','');

				return false;
			});


		return false;
	});
}

/* =COMMUNIQUES DE PRESSE */
function gestion_communiques() {

	if($('#date_deb_id').size == 0 || $('#date_fin_id').size == 0) return false;



	$('#date_deb_id').calendar({firstDay: 1,onSelect: updateDateRange}); //dateFormat: 'YMD-'
	$('#date_fin_id').calendar({firstDay: 1,onSelect: updateDateRange});

	calendarFrom = $('#date_deb_id');
	calendarTo = $('#date_fin_id');

	function updateDateRange() {
		var dateFrom = popUpCal.getDateFor(calendarFrom[0]);
		var dateTo = popUpCal.getDateFor(calendarTo[0]);
		popUpCal.reconfigureFor(calendarFrom[0], {maxDate: dateTo});
		popUpCal.reconfigureFor(calendarTo[0], {minDate: dateFrom});
//		popUpCal.hideCalendar(calendarTo[0],{speed: 10});
	}
}

function gestion_ootb() {
	if ($('#OOTB').size()==0) return false;
	$('#OOTB').show("OOTB");

	var ootb = new SWFObject("_media/swf/ootb_HUK3.swf", "ootbswf", "538", "386", "8", "");
	ootb.addParam("wmode", "transparent");

	ootb.write("OOTB");

	}
function CloseDiv(choix) {
	choix = "www.hammerson.fr";
	$('#OOTB').load("redirection_home.php",{choix :choix});
	$('#OOTB').hide();
}
function CloseDivFlashBulle() {
	$('#flashBulles').hide();
}

// GESTION ACTIFS
function gestionActifsHammerson(){
	if ($('#carteFranceActifs').size() == 0) return;

	$('#psDeuxColonnes').hide();
	$('#texte').css({
		width: '383px',
		marginLeft: '322px'
	});
	$('#deuxColonnes').css('width', '383px');

	$('#carteIDFActifs').hide();
	$('#lienIdf a').hover(function(){
		$(this).find('img').css({
			right: 0,
			bottom: 0
		}).animate({
			width: 50,
			height: 37,
			right: 12,
			bottom: 10
		})
	},function(){
		$(this).find('img').animate({
			width: 25,
			height: 18,
			right: 0,
			bottom: 0
		}, function(){$(this).css({
			width: 25,
			height: 18,
			right: 0,
			bottom: 0
		})});
	}).click(function(){
		$('#carteIDFActifs').show();
		$('#carteFranceActifs #carte, #carteFranceActifs ul .pointCarte').hide();
		$('#legende_carte_france').hide();
		$('#texte').css({
			width: '',
			marginLeft: ''
		});
		$('#psDeuxColonnes > *').not('#sloganActifs').hide();
		$('#psDeuxColonnes, #sloganActifs').show();
		$('#carteIDFActifs').append('<p id="retourCarteFrance"><a href="'+window.location+'">Retour à la vue des implantations en France</a></p>');
		$('#retourCarteFrance').click(function(){
			$('#legende_carte_france').show();
			$('#carteIDFActifs').hide();
			$('#carteFranceActifs #carte, #carteFranceActifs ul .pointCarte').show();
			$(this).remove();
			return false;
		});
		return false;
	});

	$('#psDeuxColonnes > *').not('#sloganActifs').hide();
	$('.pointCarte').filter(function(){
		return $(this).parents('#lienIdf').size() == 0 ? true:false;
	}).mouseover(function(){
		$('#texte').css({
			width: '',
			marginLeft: ''
		});
		$('#psDeuxColonnes').show();
		if (this.tagName == 'LI') var targetAnchorName = $(this).get(0).id.replace('lien','');
		else var targetAnchorName = $(this).parents('li').get(0).id.replace('lien','');
		var targetAnchorName = targetAnchorName.slice(0,1).toLowerCase()+targetAnchorName.slice(1);
		var targetAnchor = $('a[@name='+targetAnchorName+']', $('#psDeuxColonnes'));
		if (targetAnchor.size()==0) return;
		$('#psDeuxColonnes > *').hide();
		var startH2 = targetAnchor.parent('h2');
		var next=startH2;
		var compteur=0;
		while (next.size() != 0 && (compteur == 0 || next.get(0).tagName!='H2')) {
			next.show();
			next=next.next();
			compteur++;
		}
	});
}

// Developpement durable
function gestionDeveloppementDurable(){
	if ($('#developpementDurable').size()==0) return;

	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);

	$('#texte >*').not('h2, h3').hide();
	$('#texte > h3').find(':first').show();


	$('#texte > h3:first').each(function() {
		var elmt=$(this);
		if (elmt.next().is('h3') || elmt.next().size()==0) return false;

		var mode='montrer';
		if (elmt.next().is(':visible')) mode='cacher';

		if (mode=='montrer' && ltie7) elmt.css({filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/Fl2_DevDur.gif")'});
		else if (mode=='montrer' && !ltie7) elmt.css({background: 'url(_media/img/Fl2_DevDur.gif) no-repeat'});
		else if (mode=='cacher' && ltie7) elmt.css({filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/ferme.png")'});
		else elmt.css({background: 'url(_media/img/ferme.png) no-repeat'});

		var next=elmt.next();
		while (next.size() != 0 && next.get(0).tagName!='H3' && next.get(0).tagName!='H2') {
			if (mode=='montrer') next.slideDown();
			else next.slideUp();
			next=next.next();
		}
		return false;

	});
	if (ltie7) {
		$('#texte h3').css({
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/Fl1_DevDur.gif")',
			paddingLeft: '15px',
			zoom: 1
		});
		$('#texte h3:first').css({
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/Fl2_DevDur.gif")',
			paddingLeft: '15px',
			zoom: 1
		});
	} else {
		$('#texte h3').css({
			background: 'url(_media/img/Fl1_DevDur.gif) no-repeat',
			paddingLeft: '15px'
		});
		$('#texte h3:first').css({
			background: 'url(_media/img/Fl2_DevDur.gif) no-repeat',
			paddingLeft: '15px'
		});
	}
	var defaultBckgndImage = $('#contenu').css('background-image');
	var defaultBckgndImageElmtsArray = defaultBckgndImage.split('.')
	var defaultBckgndImageExtension = defaultBckgndImageElmtsArray.pop();
	var defaultBckgndImageFileName = defaultBckgndImageElmtsArray.join('.');
	$('h3',$('#texte')).click(function(){
		var elmt=$(this);
		var index=$('h3',$('#texte')).index(this);

		if (index>0) $('#contenu').css('background-image', defaultBckgndImageFileName+(index+1)+'.'+defaultBckgndImageExtension);
		else $('#contenu').css('background-image', defaultBckgndImage);

		$('#texte > :visible').not('h2, h3').slideUp();

		if (ltie7) {
		$('#texte h3').css({
			filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/Fl1_DevDur.gif")',
			paddingLeft: '15px',
			zoom: 1
		});
		} else {
			$('#texte h3').css({
				background: 'url(_media/img/Fl1_DevDur.gif) no-repeat',
				paddingLeft: '15px'
			});
		}

		if (elmt.next().is('h3') || elmt.next().size()==0) return false;

		var mode='montrer';
		if (elmt.next().is(':visible')) mode='cacher';

		if (mode=='montrer' && ltie7) elmt.css({filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src="_media/img/Fl2_DevDur.gif")'});
		else if (mode=='montrer' && !ltie7) elmt.css({background: 'url(_media/img/Fl2_DevDur.gif) no-repeat'});


		var next=elmt.next();
		if (mode=='montrer') {
			while (next.size() != 0 && next.get(0).tagName!='H3' && next.get(0).tagName!='H2') {
				next.slideDown();
				next=next.next();
			}
		}
		return false;
	});
}

function lienWeborama_PDF() {
	var regle = new RegExp("(([a-zA-Z0-9_]*).pdf$)");
	var pdf = ""
	var rub = $('li.on img').attr('alt');
	var sousRub = $('dd.on h1').html();

	$('a').click(function() {
		if (regle.test($(this).attr('href'))) {
			pdf = regle.exec($(this).attr('href'))[0];
			nomFichier = regle.exec($(this).attr('href'))[2];
			wreport_click_groupe(pdf, rub, nomFichier,331062, 331062, rub, 0, sousRub);
			return true;
		}
	});
}


function consulterNewsletter() {
	if ($('#consulterNewsletter').size() == 0) return false;

	cpt=1;
	/*$('#consulterNewsletter h3').each(function() {
		$(this).attr('id', 'rub'+cpt);
		cpt++;
	});*/

	if (($('#cadreEdito div div.contenu').height() - 20) > $('#CadreSommaireNews div div.contenu').height()) {
		$('#CadreSommaireNews div div').height($('#cadreEdito div div.contenu').height());
		$('#CadreSommaireNews div div.contenu').height($('#cadreEdito div div.contenu').height() - 20);
		$('#cadreEdito div div').height($('#cadreEdito div div.contenu').height());
	}
	else {
		$('#cadreEdito div div').height($('#CadreSommaireNews div div.contenu').height());
		$('#CadreSommaireNews div div').height($('#CadreSommaireNews div div.contenu').height());
		$('#CadreSommaireNews div div.contenu').height($('#CadreSommaireNews div div.contenu').height()-20);
	}

	cpt = 1;
	_url = document.location;
	var regle = new RegExp("#rub[0-9]{1,}$");


	$('#sommaireNews li').each(function() {
		var aretirer = regle.exec(_url);
		var url =_url+"#rub"+cpt;
		url = url.replace(aretirer,'');
		cpt++;
		$(this).click(function() {
			document.location = url;
		/*}).mouseover(function() {
			$(this).css('cursor','pointer');
			$(this).css('color','#00428E');
		}).mouseout(function() {
			$(this).css('color','#000');*/
		});

	});

	//redirection à la balise #rubXX (gestion des liens appelant cette page avec une ancre de definie)
	if (regle.exec(_url)) {
		window.location.hash=window.location.hash;
	}
}

function inclusionFlash() {

	$('#slogans').each(function() {
		var so = new SWFObject("_media/swf/anim_news.swf", "ootbswf", "198", "150", "8", "");
		so.addParam("wmode", "transparent");
		so.write("slogans");
	});
}