$(document).ready(function() {  

	//Appelle la fonction initialize js avec les paramètres renvoyés par ajax/php
	if($('#carteGmap').size()) //test si le div conteneur de carte existe
	{	
		if($('#id_department').val() != "")
		{
			$.ajax({ 
				url: "/geolocalisation/agences-vauban-humanis/geolocalize?function=initializeByDepart&id="+$('#id_department').val(),
				cache: false, 
				success:function(html){initialize(html);},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert('Une erreur est survenue durant l\'éxécution de la requête ajax.');
					}
			});	
		}
		else
		{
			
			$.ajax({ 
				url: "/geolocalisation/agences-vauban-humanis/geolocalize?function=initializeById&id="+$('#id_agency').val(),
				cache: false, 
				success:function(html){initialize(html);},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert('Une erreur est survenue durant l\'éxécution de la requête ajax.');
					}
			});			
		}
		
		
		$('.centerGeolocLink').click(function() {
			$.ajax({ 
				url: "/geolocalisation/agences-vauban-humanis/geolocalize?function=initializeById&id="+this.id,
				cache: false, 
				success:function(html){initialize(html);},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert('Une erreur est survenue durant l\'éxécution de la requête ajax.');
					}
			});
		});		
		
	}
	
	if($('#carteGmapDelegations').size()) //test si le div conteneur de carte existe
	{		
			$.ajax({ 
				url: "/geolocalisation-delegations-sociales/geolocalize?function=initializeDelegationsById&id="+$('#id_agency').val(),
				cache: false, 
				success:function(html){initializeDelegations(html,true);},
				error:function(XMLHttpRequest, textStatus, errorThrows){ 
					alert('Une erreur est survenue durant l\'éxécution de la requête ajax.');
					}
			});	
			
			
			$('.centerGeolocLinkDelegation').click(function() {				
				$.ajax({ 
					url: "/geolocalisation-delegations-sociales/geolocalize?function=initializeDelegationsById&id="+this.id,
					cache: false, 
					success:function(html){initializeDelegations(html);},
					error:function(XMLHttpRequest, textStatus, errorThrows){ 
						alert('Une erreur est survenue durant l\'éxécution de la requête ajax.');
						}
				});
			});	
	}	
});

function majChampMailNewsletter(event, champMail, defaultValue) {
    var mailValue = champMail.value;

    if (mailValue != defaultValue) {
        if ($.trim(mailValue) == '') {
            champMail.value = defaultValue;
        }
    } else {
        if (event.type == 'focus') {
           champMail.value = '';
        }
    }
}

// liens archives
function overTitreArchive(idArchiveIntro) {
	var div = document.getElementById(idArchiveIntro);

	div.className = "archiveColumnText archiveColumnTextSelected";
}

function outTitreArchive(idArchiveIntro) {
	var div = document.getElementById(idArchiveIntro);
	
	div.className = "archiveColumnText";
}

// Derouler/enrouler les paragraphes pour business objects
$(document).ready(function() {
	$('.displayArchivesYear').click(function() {
		var idPere = this.id;
		var inputYearOpened = $('#inputYearOpened');
		var delay = 300;
		
		var strProp = "";
		for(prop in inputYearOpened) {
			strProp += "   -   " + prop;
		}
		
		//alert(strProp);
 
 		if ($('#para' + idPere).css("display") == "none") {
			//alert('_year' + inputYearOpened.attr('value'));
			// Remplace l'image de l'annee a fermer par une fleche orientee vers la droite
			$('#_year' + inputYearOpened.attr('value') + ' .accordionArchivesTitleImg').attr('src', '/images/fleche_bl_fd_orange.gif');
			// Ferme l'annee a fermer, et ouvre la nouvelle
			$('#para_year' + inputYearOpened.attr('value')).slideUp(delay, function() {updateAfterAccordion(idPere, inputYearOpened, delay);});
		} else {
			// Remplace l'image par une fleche orientee vers la droite
			$('#' + idPere + ' .accordionArchivesTitleImg').attr('src', '/images/fleche_bl_fd_orange.gif');
			$('#para' + idPere).slideUp(delay);
			
			// Met a jour l'inputYearOpened pour connaitre la nouvelle annee ouverte
			//inputYearOpened.attr('value', idPere.substr(idPere.length-4, 4));
		}
	});
});


function updateAfterAccordion(idPere, inputYearOpened, delay) {
	$('#para' + idPere).slideDown(delay);
	// Remplace l'image par une fleche orientee vers le bas
	$('#' + idPere + ' .accordionArchivesTitleImg').attr('src', '/images/fleche_bl_fd_orange_bas.gif');
	
	// Met a jour l'inputYearOpened pour connaitre la nouvelle annee ouverte
	inputYearOpened.attr('value', idPere.substr(idPere.length-4, 4));
}

function setFooter() {
    if (document.getElementById) {
        var windowHeight=getWindowHeight();
        if (windowHeight>0) {
            var contentHeight=
            document.getElementById('page-td').offsetHeight;
            var footerElement=document.getElementById('mention-container');
            var footerHeight=footerElement.offsetHeight;
        if (windowHeight-(contentHeight+footerHeight)>=0) {
            footerElement.style.position='relative';
            footerElement.style.top=
            (windowHeight-(contentHeight+footerHeight))+'px';
        }
        else {
            footerElement.style.position='static';
        }
       }
      }
}
 
function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientHeight) {
         windowHeight = document.documentElement.clientHeight;
    }
    else {
     if (document.body&&document.body.clientHeight) {
         windowHeight=document.body.clientHeight;
      }
     }
    }
    return windowHeight;
}

window.onload = function() {
setFooter();
}
window.onresize = function() {
setFooter();
}
