//-----------------------------------------------------------------------------------------------//
//AJAX : Switch de photos de Spm, affichage de la personne correspondant à un numéro de téléphone//
//-----------------------------------------------------------------------------------------------//



//-----------------------------------
function write_div(id, text) {
	document.getElementById(id).innerHTML = text;
}


//-----------------------------------
function createRequestObject() {
    var ro;
    var browser = navigator.appName;

    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

// ##############################
imgLoading = new Image();
imgLoading.src = '../images/ajax_loading.gif';
imgLoading.alt = '';

var xhr = createRequestObject();
var Global_action = '';
var Global_ann_id = '';
// ##############################



//-----------------------------------
function _ajaxRqst(action, params, id){
	Global_action = action;
	Global_ann_id = id;

    xhr.open("GET", '../libphp/ajax_response.php?a='+action+'&'+params);
	xhr.onreadystatechange = handleResponse;
    xhr.send(null);
}


//-----------------------------------
function handleResponse() {
	HTML_Load = '<div style="text-align: center; width: 270px;"><img src="../images/ajax_loading.gif" style="margin: 86px auto; border: none;" alt="*" /></div>';

	if(xhr.readyState == 1 || xhr.readyState == 2 || xhr.readyState == 3) {
		if(Global_action == "illustr"){
			write_div('illustr-bloc', HTML_Load);
		}
	}

    if(xhr.readyState == 4) {
		if(Global_action == "illustr"){
			_switchImg(xhr.responseText);
		}
		if(Global_action == "annu"){
			_Num2Name(Global_ann_id, xhr.responseText);
		}
		if(Global_action == "tombola"){
			_checkTomNum(xhr.responseText);
		}
	}
	else {
		return(false);
	}
}


//-----------------------------------
function _switchImg(resAjax) {
	var tabR = resAjax.split("#");
	var illustrFilename = tabR[0];
	var illustrTitle = tabR[1];
	var illustrAuthor = tabR[2];
	var illustrHeight = tabR[3];
	var hd_exists = tabR[4];
	var illustrID = tabR[5];

	//---Affichage du nom du photographe---
	spanAuteur = document.getElementById("illustr-author");
	while( spanAuteur.hasChildNodes() ){
		spanAuteur.removeChild(spanAuteur.firstChild);
	}
	auteur = document.createTextNode(' ('+illustrAuthor+')');
	spanAuteur.appendChild(auteur);

	//---Affichage de l'image---
	photo = document.createElement("img");
	photo.setAttribute("src", '../photos/illustr_spm/'+illustrFilename);
	photo.setAttribute("alt", illustrTitle);
	divPhoto = document.getElementById('illustr-bloc');
	divPhoto.style.height = parseInt(illustrHeight)+2+'px';

	while( divPhoto.hasChildNodes() ){
		divPhoto.removeChild(divPhoto.firstChild);
	}

	divPhoto.appendChild(photo);

	//---Création du lien pop-up sur le bouton Zoom---
	imgZoom = document.getElementById('btn-zoom-id');
	if(hd_exists == '1'){
		imgZoom.setAttribute('alt', illustrID);
		imgZoom.style.display = "block";
	}
	else {
		imgZoom.style.display = "none";
	}
}


//-----------------------------------
function _Num2Name(ann_id, resAjax){
	var divAffich = document.getElementById(ann_id);

	if(divAffich.style.display == "block"){
		divAffich.style.display = "none";
	}
	else {
		divAffich.innerHTML = resAjax + '<img src="../images/btn_close.gif" alt="" class="btn-close" onclick="_closeAjaxNum(\''+ann_id+'\')" />';
		divAffich.style.display = "block";
	}
}

//-----------------------------------
function _closeAjaxNum(ann_id){
	var divAffich = document.getElementById(ann_id);
	while( divAffich.hasChildNodes() ){
		divAffich.removeChild(divAffich.firstChild);
	}
	divAffich.style.display = "none";
}