//declaration : si la page est charg?
$(function() {
//popup information

var hdnMessObj = document.getElementById('hdnMess');
if(hdnMessObj && hdnMessObj.value=="Information"){
	document.getElementById('hdnMess').value="";//r??initialisation de la valeur de hdnMess pour la prochaine saisie.
	$(function() { 
    	$(document).ready(function(){
			resizeHeight('popup');
    		$.blockUI({ message: $('#popup'), css: { width: 'auto' } }); 
    	}); 

        $('#close').click(function(){ 
            $.unblockUI(); 
            return false; 
        }); 
        
        $('#buttonclose').click(function(){
        	$.unblockUI();
        	return false;
        });
        $('#imgclose').click(function(){
        	$.unblockUI();
        	return false;
        }); 
    }); 
}

//Message d'erreur
if(hdnMessObj && hdnMessObj.value=="Erreur"){
	openPopupError(); 
}
else{
	showLoading();
}
if(hdnMessObj && hdnMessObj.value=="Confirmation"){
	document.getElementById('hdnMess').value="";//r??initialisation de la valeur de hdnMess pour la prochaine saisie.
	$(function() { 
    	$(document).ready(function(){
			resizeHeight('popupConfirmation');
    		$.blockUI({ message: $('#popupConfirmation'), css: { width: 'auto' } }); 
    	}); 

        $('#close2').click(function(){ 
            $.unblockUI(); 
            return false; 
        }); 
        
        $('#buttonclose2').click(function(){
        	$.unblockUI();
        	return false;
        });
        $('#imgclose2').click(function(){
        	$.unblockUI();
        	return false;
        }); 
    }); 
}
//Fin declaration 
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
};
$.preloadImages("../assets/images/loading.gif");

function showTheLoading() {
var hdnMessObj = document.getElementById('hdnMess');
if(hdnMessObj && hdnMessObj.value!="Erreur"){
	$(function() { 
		$.blockUI(
			{ 
				message:$('#popupLoad'),
				css: { border: 'none', padding: '15px', backgroundColor:'transparent', 
					   '-webkit-border-radius': '10px','-moz-border-radius': '10px', 
					   opacity: '.5', color: '#fff'}
			}
		);
		}); 
		
		$(document).ready(function(){setTimeout("$.unblockUI();",700);});
		}
}
function showLoading(action) {
	var showLoading = document.getElementById('showLoading');
	if(action || (showLoading && showLoading.value == "true")){
		
		msg = '<div style="color:#fff;font-weight: bold;">'
			+ '   Chargement en cours ...   '
			+ '</div>'
			+ '<br><img src="../assets/images/loading.gif" />';
		
		if(showLoading) showLoading.value="false";
		
		$(function() { 
			$(document).ready(function(){
				$.blockUI(
					{ 
						message:$('#popupLoad'),
						css: { border: 'none', padding: '15px', backgroundColor:'transparent', 
							   '-webkit-border-radius': '10px','-moz-border-radius': '10px', 
							   opacity: '.5', color: '#fff'}
					}
				);
				if(!action){
					action = document.getElementById('params').value+'&showLoading=false';
				}
				else{
					action += '&showLoading=false';
				}
				setTimeout("",400);
				
				// Si on vient de la recherche ou du toc on doit mettre dans l'historique 
				// du navigateur.
				if(action.indexOf('fromSearch')>-1 || action.indexOf('from=toc')>-1){
					document.location=action;	
				}
				else{
					document.location.replace(action);					
				}
			}); 
		}); 
	}
}
function openPopupError(){
	var hsize = resizeHeight('popupError');
	var h = document.documentElement.clientHeight;
	var startH = (h - hsize)/2;	
	var hdnMessObj = document.getElementById('hdnMess');
	if(hdnMessObj) {
		hdnMessObj.value="";//r??initialisation de la valeur de hdnMess pour la prochaine saisie.
	}
	$(function() { 
    	$(document).ready(function(){
    		$.blockUI({ message: $('#popupError'), css: { width: 'auto',top: startH+'px'} }); 
			setOnClick();
    	}); 
    }); 
}
function resizeHeight(id){
	var h = document.documentElement.clientHeight;
	var hsize = $("#"+id).height();
	if(h < hsize) {
		t = (hsize + 15 ) - h;
		window.resizeBy(0,t);
	}
	return hsize;
}
function waiting() {
}
function setOnClick() {
	var loading = document.getElementById('showLoading');
	if(loading) loading.value="false";
	var idFromSearch = document.getElementById('idFromSearch');
	var idFrom = document.getElementById('idFrom');
	if(idFromSearch && idFromSearch.value == "true"){
		returnTo(-1);
	}else if(idFrom && idFrom.value == 'toc'){
		returnTo(-1);
	}else{
		returnTo(-2);
	}
}
function returnTo(page){
	var hrefclose1 = document.getElementById("close1");
	if(hrefclose1){
		hrefclose1.onclick=function () { window.history.go(page); } ;
	}
	var hrefimgclose1 = document.getElementById("imgclose1");
	if(hrefimgclose1){
		hrefimgclose1.onclick=function () { window.history.go(page); };
	}
	hrefclose2 = document.getElementById('hrefclose');
	if(hrefclose2){
		hrefclose2.onclick=function () { window.history.go(page); };	
	}
}

