var checkOK;
var downList;
var pe1,pe2;
var folderOK="/chk_image.php?folder="+baseFolder+"&action=copied";
var folderRetrieve="/chk_image.php?folder="+baseFolder+"&action=retrieve";
var thumbCheck="/chk_image.php?folder="+baseFolder+"&action=thumbcheck&file=";
var updThumb="/chk_image.php?action=thumbupd&aid="+albumID;
var photoList= new Array();
var updImage;
var contaImg=0;
var tentative=0;
var xmlHttp1,xmlHttp2,xmlHttp3,xmlHttp4,xmlHttpUpd;;

function update_foto()
{
	_id=$("fo_id").value;
	_title=$("fo_title").value;
	_contentdate=$("fo_contentdate").value;
	_contentplace=$("fo_contentplace").value;
	_contentdesc=$("fo_contentdesc").value;
	// _tags=$("fo_tags").value;
	_tags=""
	//alert("update "+ 'title='+_title +'&contentdate='+_contentdate +'&contentplace='+_contentplace+'&contentdesc='+_contentdesc +'&tags='+_tags+'&id='+_id+'&action=upd');
	
	if (_contentdate!="") isvalid=isDateCheck(_contentdate); else isvalid=true;
	if ((isvalid)&&(trim(_title)!=""))
	{
		xmlHttp4 = new Ajax.Request('/upd_image.php',
								{
									method: 'post',
									asynchronous: false,
									parameters : 'title='+_title+'&contentdate='+_contentdate+'&contentplace='+_contentplace+'&contentdesc='+_contentdesc+'&tags='+_tags+'&id='+_id+'&action=upd',
									onComplete: function(resp)
									{
										// alert("I dati del contenuto "+_title+" sono stati aggiornati con successo");
										// alert(resp.responseText);
									}
								});
	};
}

function delete_foto()
{
	_id=$("fo_id").value;
	_title=$("fo_title").value;
	_rc=$("fo_rc").value;
	
	xmlHttp4 = new Ajax.Request('/upd_image.php',
								{
									method: 'post',
									parameters : 'id='+_id+'&action=del',
									onComplete: function(resp)
									{
										// alert("Il contenuto "+_title+" e' stato cancellato");
										$(_rc).childElements()[0].remove()
									}
								});
}

function foto_retrieve(id,divID)
{
	var m = new Array("title","contentdate","contentplace","contentdesc","tags","category","album");
	// id=1468;
	
	update_foto();
	
	$("fo_id").value=id;
	$("fo_rc").value=divID;
	
	xmlHttp1 = new Ajax.Request('/upd_image.php',
									{
										method: 'post',
										parameters: 'action=rtv&id='+id,
										onComplete: function(resp)
										{
											var x;
											// controllo lo stato dei diversi campi che vengono restituiti
											// se non sono null allora posso impostare il corrispondente campo di input
											// da controllare: si suppone che ogni nodo che ha almeno un contenuto ha un figlio con il contenuto!!
											// alert(resp.responseText);
											res=xmlHttp1.transport.responseXML;
											var a = res.documentElement;
											// root element (dati)
											for (x=0;x<7;x++)
											{
												l=a.getElementsByTagName(m[x]);
												if ((l!=null)&&(l[0].hasChildNodes()>0))
												{
													v=l[0].firstChild.nodeValue;
													// alert(v);
													$("fo_"+m[x]).value=v;
												}
												else
												{
													// Elemento vuoto e quindi input=""
													$("fo_"+m[x]).value="";
												}
											};
										}
									});
}

function updImage()
{
	// alert(photoList[0]);
	xmlHttp3 = new Ajax.Request(thumbCheck+photoList[contaImg] + "&_=" + Math.random(),
								{
									method: "get",
									onComplete: function()
									{
										tentative++;
										res=xmlHttp3.transport.responseText;
										if (res.substring(0,2)=="OK")
										{
											// Ok la thumbnail c'e'
											// alert(res);
											id=res.substring(3);
											r=parseInt(contaImg/8);
											c=contaImg%8;
											divId="c"+r+c;
											fotoPath=baseFolder + "/thumb/" + photoList[contaImg];
											$(divId).update("<img src=\""+ fotoPath +"\" height=\"63px\" width=\"84px\" onclick=\"javascript:retrieve("+ id + ",'" + divId + "'); preview('"+fotoPath+"'); \"/>");
											contaImg=contaImg+1;
											// alert(contaImg+" "+tentative);
											tentative=0;
										};
										if (tentative>3)
										{
											// c'e' stato qualche errore o time out
											// proviamo con la foto dopo
											contaImg++;
											tentative=0;
										}
										if (contaImg==photoList.length-1)
										{
											// alla fine controllo se devo aggiornare le thumbnail
											pe2.stop();
											$("ongoing").update("Publishing process is finished. Now you can edit photo data.");
											xmlHttpUpd=new Ajax.Request(updThumb + "&_=" + Math.random(),
												{
													method: "get",
													onComplete: function()
													{
														res=xmlHttpUpd.transport.responseText;
														// alert(res);
													}
												})
										}
									}
								})
}

function preview(path)
{
	if (path!='')
	{
		$("boxpreview").update("<img src=\""+ path +"\" height=\"63px\" width=\"84px\"/>");
	}
}

function downloadList()
{
	xmlHttp2 = new Ajax.Request(folderRetrieve + "&_=" + Math.random(),
								{
									method: "get",
									onComplete: function()
									{
										res=xmlHttp2.transport.responseText;
										// alert('download ' + res);
										photoList=res.split(" ");
										nPhoto=photoList.length-1;
										$("ongoing").update("System has found " + nPhoto + " photo(s). You can follow the publishing process on this page");
										pe2 = new PeriodicalExecuter(updImage,2); // periodo di controllo dell'esistenza della thumbnail
									}
								})
	return;
}

function checkOK()
{
		xmlHttp1 = new Ajax.Request(folderOK + "&_=" + Math.random(),
									{
										method: "GET",
										onComplete: function()
										{
// 											// alert(xmlHttp1.transport.responseText);
											if (xmlHttp1.transport.responseText.substring(0,2)=="OK")
											{
												// Ok le foto sono copiate e rinominate
												// fermo lo updater e chiamo la funzione di download
												pe1.stop();
												downloadList();
											}
										}
									})
}
