// JavaScript Document
var xmlHttp = createXmlHttpRequestObject();
var resultId = "";
function createXmlHttpRequestObject(){
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		for(var i=0;i<xmlHttpVersions.length && !xmlHttp;i++){
			try {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);	
			}
			catch (e){}
		}
	}
	if(!xmlHttp)
		alert("Erreur création de l'objet XMLHttpRequest.");
	else return xmlHttp;
}

//read a file from the server
function process(methode,url,param){
	if(xmlHttp){
		//try to connect to the server
		try {
			xmlHttp.open("GET",url+"?"+param,true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			//alert(param);
			xmlHttp.send(null);
			//alert(methode+url+param);
		}
		catch (e){
			alert("Ereur de connection au serveur:\n"+e.toString());	
		}
	}
	else {
		alert("L'objet xmlHttp n'est pas crée.");
	}
}

function showAdress(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			try {
				//handleServerResponse();
				document.getElementById('expoResult').innerHTML = xmlHttp.responseText;
			}
			catch (e){
				alert("Erreur de lecture de resultat:\n"+e.toString());
			}
		}
		else{
			alert("Il y a eu une erreure de chargement:\n"+xmlHttp.statusText);
		}
	}
	
}

function showArticle (){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			try {
				//handleServerResponse();
				document.getElementById('expoResult').innerHTML = xmlHttp.responseText;
			}
			catch (e){
				alert("Erreur de lecture de resultat:\n"+e.toString());
			}
		}
		else{
			alert("Il y a eu une erreure de chargement:\n"+xmlHttp.statusText);
		}
	}
	
}

function handleRequestStateChange (){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			try {
				//handleServerResponse();
				document.getElementById(resultId).innerHTML = xmlHttp.responseText;
			}
			catch (e){
				alert("Erreur de lecture de resultat:\n"+e.toString());
			}
		}
		else{
			alert("Il y a eu une erreure de chargement:\n"+xmlHttp.statusText);
		}
	}
	
}

function handleServerResponse(){
	//read the message from server
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse || !xmlResponse.documentElement)
		throw("Invalid XML structure:\n"+xmlHttp.responseText);
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName=="parsererror")
		throw("Invalid XML structure:\n"+xmlHttp.responseText);
	//obtain the xml's document element
	xmlRoot = xmlResponse.documentElement;
	//obtains arrays with book titles ISBNs.
	titleArray = xmlRoot.getElementsByTagName("title");
	isbnArray = xmlRoot.getElementsByTagName("isbn");
	//generate HMTL output
	var html = "";
	for(var i=0;i<titleArray.length;i++)
		html +=titleArray.item(i).firstChild.data + " , " + isbnArray.item(i).firstChild.data + "<br />";
		
	myDiv = document.getElementById("myDivElement");
	//display the element output
	myDiv.innerHTML = "Server says: <br />"+ html;
}

function createDomObject(){
	var xmlDoc;
	//create XML document
	if(document.implementation && document.implementation.createDocument){
		xmlDoc = document.implementation.createDocument("","",null);
	}
	else if(window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	
	if(!xmlDoc){
		alert("Error creating the DOM object.");
	}
	else return xmlDoc;
}

function overGal(id,source){
	document.getElementById(id).src = source;
}
function overGal(id,source,idArt,art,idFolio,folio,idFolioSize,folioSize,titre,lid){
	if(id != null){
		hideAll(lid);
		var linkobj = document.getElementById(id);
		if(linkobj){
			linkobj.style.display = "block";
			for(var i =0;i<linkobj.childNodes;i++){
				alert(linkobj.childNodes[i]);
			}
		}
	}
	if(idArt != null)document.getElementById(idArt).innerHTML = art;
	if(idFolio != null)document.getElementById(idFolio).innerHTML = folio;
	if(idFolioSize != null)document.getElementById(idFolioSize).innerHTML = folioSize;
	document.getElementById("imgSize").innerHTML = folioSize.substr(8,folioSize.length);
	return null;
}
function hideAll(liste){
 liste = liste.split('/');
 for(var i=0;i<liste.length-1;i++){
	document.getElementById(liste[i]).style.display="none";
	}
}
function outGal(id){
	return null;
}
function overGalAlbum(id,source){
	var newSource = "";
	if(id != null){
		newSource = source.replace("thumbs/thumbs_","");
		document.getElementById(id).innerHTML = 'Chargement image...'
		document.getElementById(id).innerHTML = '<img id="preview_album_area" class="Thumb" alt="newSource" src="'+newSource+'" width="300"/>';
	}
	return null;
}
function outGalAlbum(id){
	//document.getElementById(id).innerHTML = '';
	return null;
}
function getExpositionDetail(idExposition,article,settingId){
	var	param = "idExposition="+idExposition+"&article="+article;
	resultId = settingId;
	process("POST","http://localhost/artyfact/wp-content/themes/artyfact/details-exposition.php",param);
}
function getExpositionDetails(idExposition,article,settingId){
	alert('yes');
}

function expoShowArticle(grdImage,postTitre,postLink,postContent){
	var tdExpoImageGrand = document.getElementById("tdExpoImageGrand");
	var newImg;
	if(tdExpoImageGrand){
		tdExpoImageGrand.innerHTML = '';
		newImg = document.createElement('img');
		newImg.id = 'expoImageGrand';
		newImg.src =  grdImage;
		resize(newImg,350,350);
		tdExpoImageGrand.appendChild(newImg);
	}

	document.getElementById("expoPostTitle").innerHTML = postTitre;
	document.getElementById("lnkPlusInformation").href = postLink;
	document.getElementById("expoContent").innerHTML = postContent + "...";
	return null;
}

function checksize(obj,size){
	if(obj){
		var h = parseInt(obj.height);
		var w = parseInt(obj.width);
		if ( h >= w  && h > size){
			obj.height = size;
		}else if (w > size){
			obj.width = size;
		}else{obj.width = size;}
	}
}

function resize(img, maxh, maxw) {
	  
	  var maxratio = maxh/maxw;
	  var h = img.height;
	  var w = img.width;
	  var hFactor = maxh/h;
	  var wFactor = maxw/w;
	  
	  
	  if (h/w > maxratio){
	    if (h > maxh){
	      img.width = Math.round(w*hFactor);
	      img.height = maxh;
	    }
	  } else {
	    if (w > maxw){
	      img.height = Math.round(h*wFactor);
	      img.width = maxw;
	    }
	  } 
	}
function showGallery(obj){
	document.getElementById('frmShowGallery').action=obj;
	document.getElementById('frmShowGallery').submit();
}
function clearSpace(){
	document.getElementById("preview_gallery").innerHTML = "";
	return null;
}
