/* KaBox v 1.0.1 */
/* Author : IEB */

// Effet Fade In


var KaBoxFunJSONResult = null;

function KafadeIn(objId,opacity) {
	if(document.getElementById(objId)){
		//obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(objId, opacity);
			if (document.all) {
				opacity += 20;
			}			
			else {
				opacity += 10;
			}
			window.setTimeout("KafadeIn('"+objId+"',"+opacity+")", 0);
		}
	}
}

function setOpacity(obj, Kaopacity) {
	if (document.all) {
		Kaopacity = ((Kaopacity == 100)?99.999:Kaopacity) - 35;
	}
	else {
		Kaopacity = ((Kaopacity == 100)?99.999:Kaopacity) - 70;
	}	

	document.getElementById(obj).style.filter = "alpha(opacity="+Kaopacity+")";

	document.getElementById(obj).style.KHTMLOpacity = Kaopacity/50;

	document.getElementById(obj).style.MozOpacity = Kaopacity/50;

	document.getElementById(obj).style.opacity = Kaopacity/50;
 
}

// Préparation de l'objet XHR
function new_xhr(){
	var xhr_object = null;
	if(window.XMLHttpRequest) 
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject){  
	   try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			}
	}
	else {  
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   xhr_object = false;
	}
	return xhr_object;
}

// Exécution de la requête XHR
function charge_page(strURL) {
	
	var xhr2 = new_xhr(); 
		xhr2.open('POST', strURL, true) ;
		xhr2.onreadystatechange = function(){
		if (document.getElementById('KaboxAjaxLoader')) {
			if ( xhr2.readyState == 4 ) { 
				if(xhr2.status  != 200){ 
					document.getElementById("KaboxAjaxLoader").innerHTML ="Error code " + xhr2.status;
				} else { 
					function requetexhr () {

                                                if(xhr2.getResponseHeader("Content-Type").substring(0,9) == "text/json")
                                                    {
                                                        KaBoxFunJSONResult = eval(xhr2.responseText);
        						document.getElementById("KaboxAjaxLoader").innerHTML = KaBoxFunJSONResult.innerHTML;
                                                    }else {
            						document.getElementById("KaboxAjaxLoader").innerHTML = xhr2.responseText;
                                                    }
                                                document.getElementById("KaboxAjaxLoader").style.background = '#fff';
					}
					
					setTimeout(requetexhr, 1000);
				}
			} else { 
				document.getElementById("KaboxAjaxLoader").innerHTML = "<img src='/images/ajax-loader.gif' alt='' style='margin-top:270px; ' />";
				document.getElementById("KaboxAjaxLoader").style.background = 'none';
			}
			}
		}
		xhr2.send(strURL);
}	

// Construction des éléments Kabox directement dans le DOM
function KaboxFun(strURL, closeCallback) {
	
	// Conteneur global
	var KaboxContainer = document.createElement('div');
	KaboxContainer.setAttribute('id', 'KaboxContainer');
	
	// Conteneur du retour AJAX
	var KaboxAjaxLoader = document.createElement('div');
	KaboxAjaxLoader.setAttribute('id', 'KaboxAjaxLoader');	
	
	// Image de fermeture
	KaboxClose = document.createElement('div');
	KaboxClose.setAttribute('id', 'fermer-kabox');
	
	// Création des éléments dans le body
	document.body.appendChild(KaboxContainer);
	document.body.appendChild(KaboxAjaxLoader);
	document.body.appendChild(KaboxClose);
	
	// Calcul de la valeur top par rapport au viewport
	var KaBoxPosition = document.documentElement.scrollTop;
	
	// Centrage verticale du bloc AJAX par rapport au viewport
	var KaboxAjaxLoaderHeight =  document.getElementById('KaboxAjaxLoader').offsetHeight;
	var WindowHeight = document.documentElement.clientHeight;
	
	var x = Math.ceil((WindowHeight - KaboxAjaxLoaderHeight) / 2);
	y = x + KaBoxPosition;
		if(y < 0) {
			y = 0;
		}	
	
	z = y - 17;
	
	
    // Construction de l'attribut style avec les propriétés calculées dynamiquement 	
	if (document.all) { // Syntaxe IE qui gère mal le setAttribute
		KaboxContainer.style.cssText = 'top:'+ KaBoxPosition +'px';
		KaboxAjaxLoader.style.cssText = 'top:'+ y +'px';
		KaboxClose.style.cssText = 'top:'+ z +'px';
	}
	
	else { // Syntaxe navigateurs alternatifs
		KaboxContainer.setAttribute('style', 'top:'+ KaBoxPosition +'px');
		KaboxAjaxLoader.setAttribute('style', 'top:'+ y +'px');
		KaboxClose.setAttribute('style', 'top:'+ z +'px');
	}		
	
	KafadeIn('KaboxContainer', 0); setOpacity('KaboxContainer', 0);
	
	// Cacher le scroll dans l'activation de la Kabox
	document.body.style.overflow='hidden';
    if(document.getElementById("id_select_page"))
    	document.getElementById('id_select_page').style.display='none';
	
	// Desctruction des objets crées
	function removeBox() {
		document.body.style.overflow='visible';
		document.body.removeChild(KaboxContainer);
		document.body.removeChild(KaboxAjaxLoader);
		document.body.removeChild(KaboxClose);
        if(document.getElementById("id_select_page"))
            document.getElementById('id_select_page').style.display='block';
	}
	
	KaboxContainer.onclick = removeBox;
    if (closeCallback != undefined)
    {
    	KaboxClose.onclick = closeCallback;
    } else {
        KaboxClose.onclick = removeBox;
    }    
	charge_page(strURL);
}

function removeKaboxFun()
{
    document.body.style.overflow='visible';

    if(document.getElementById("fermer-kabox"))
        document.body.removeChild(document.getElementById("fermer-kabox"));

    if(document.getElementById("KaboxContainer"))
        document.body.removeChild(document.getElementById("KaboxContainer"));

    if(document.getElementById("KaboxAjaxLoader"))
        document.body.removeChild(document.getElementById("KaboxAjaxLoader"));

    if(document.getElementById("id_select_page"))
		document.getElementById('id_select_page').style.display='block';

}
