// JavaScript Document for Gallery
var dataSourcePathFresco = "/lib/fresco/app/dataSource.php";
var jsReady = false;
var delayedGalleryHash='';
var lastLocation='';
function showGalleryIndexContainer() {
	var gic=document.getElementById("galleryIndexContainer");
	if (gic != null) {
	gic.style.opacity = 1;
	}
}
function frescoThumbnailMenuAnimate(opening) {
	
}
function setFrescoCookie(cookieName) {
	setCookie(cookieName,'FrescoEditorCookie',1,'/',false);
}
function justDeletedApicture() {
	back();
}
function flashUploadReturn() {
	//trace('flash file upload return');
	var hashString=window.location.hash;
	lastLocation=hashString;	
	window.setTimeout('delayedGalleryReload()',2000);
	//lastHash="";
	//window.location.hash="galleries/index.php";
	//window.location.hash = hashString;
}

/* wot's the problem ?  */
function delayedGalleryReload() {
	lastLocation="galleries/index.php";	
}
function buyThis(iid) {
	if (iid) {
		var str = "func=buyThis&iid="+iid	;
		str = dataSourcePathFresco + "?" + str;
		sendToServerFresco(str);
	}
}
function frescoLogin() {
	var str = "func=login"	;
	str = dataSourcePathFresco + "?" + str;
	//trace(str);
	sendToServerFresco(str);
}
function logout() {
	var str = "logout=1&func=logout"	;
	str = dataSourcePathFresco + "?" + str;
	sendToServerFresco(str);
}
function getImageSoloDetails(uri) {
	var str = "func=getImageSoloDetails&uri="+uri	;
	str = dataSourcePathFresco + "?" + str;
//	trace('str: '+str);
	sendToServerFresco(str);
}
function editGalleries() {
	var hashString=window.location.hash;
	
	window.location.hash = hashString + '&editGalleries=1';
	lastLocation = String(document.location)
	var str = "func=editGalleries";
	str = dataSourcePathFresco + "?" + str;
	sendToServerFresco(str);
}
function newGallery() {
	var str = "func=newGallery";
	str = dataSourcePathFresco + "?" + str;
	sendToServerFresco(str);
}
function deleteImage(iid,path) {	
	var str = "func=deleteImage&iid="+iid+"&path="+path;
	str = dataSourcePathFresco + "?" + str;
//	trace(str);
	sendToServerFresco(str);
	var obj = document.getElementById('screenOverlay');
	if (obj != null) {
		obj.innerHTML = '';	
	}
	var obj = document.getElementById('screen');
	if (obj != null) {
		obj.innerHTML = '';	
	}
	stopGallery();
}


/*
function createXMLHttpObject() {
	if( window.XMLHttpRequest ) {
		xmlhttp = new XMLHttpRequest();
		return true;
	} else {
		return false;
	}
}
*/
function sendToServerFresco(uri) {
	// Create an XMLHttpRequest and open it.
	// send the request with it's parameter string 
	// the response will be in the form of a string of javascript function calls
	// the response is then evaluated using eval(string)
	if (uri) {
		if (uri.substr(0,1) != "/") { uri="/"+uri; };
		xmlhttp = null;
		createXMLHttpObject();
		pathForTheRecord=uri;
		xmlhttp.open("GET", uri, true);
		xmlhttp.onreadystatechange = evaluateServerResponse; // NOTE: No Brackets on the end or it will NOT work.
		xmlhttp.initialise;
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		//trace(uri);
		loading('1','Loading');
		xmlhttp.send(null);
	}
}

function postToServerFresco(params) {
	var uri = dataSourcePathFresco;
	if (uri.substr(0,1) != "/") { uri="/"+uri; };
	//trace('post: '+params);
	xmlhttp = null;
	createXMLHttpObject();
	xmlhttp.open("POST", uri, true);
	xmlhttp.onreadystatechange = evaluateServerResponse; // note the absence of brackets!
	xmlhttp.initialise;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	loading('1','Loading');
	xmlhttp.send(params);
}
/*
function loading(s,n) {
	//alert("S: "+s+", n: "+n);
	var obj=document.getElementById('xhrReportImage');
	if (obj!=null) {
		switch(s) {
			case '1':
				obj.src=xhrActiveGraphic;
			break;
			case '0':
				obj.src=xhrStoppedGraphic;
			break;
		}
	} else {
		//alert("L'xhrReport n'existe pas!");
	}
}

function evaluateServerResponse() {
	//trace(xmlhttp.responseText);	
	if (xmlhttp.readyState == 4) {
			
			if (xmlhttp.status == 200) {
				
				var xhrString = xmlhttp.responseText;
	
				xhrString = xhrString.replace("\n",'');
				xhrString = xhrString.replace("~amp~",'&amp;');
		//		trace(xhrString);
				
				if (xhrString.indexOf('<b>Warning</b>:')!=-1 || xhrString.indexOf('<!DOCTYPE HTML PUBLIC')!=-1) {
					note('Server-side error! - header received');
					
					trace(xhrString);
					xhrString="";
		//			alert('server-side error! \n'+xhrString);
				}
				if (xhrString.indexOf('<b>Parse error</b>:')!=-1) {
					note('Server-side error! - parse error');
					
					trace(xhrString);
					xhrString="";
				}
				if (xhrString.indexOf('<b>Fatal error</b>:')!=-1) {
					note('Server-side error! - Fatal error');
					
					trace(xhrString);
					xhrString="";
				}
				if (xhrString.indexOf('<b>Notice</b>:')!=-1) {
					note('Server-side Notice');
					
					trace(xhrString);
					xhrString="";
				}
				if (xhrString) {
					try {
						eval(xhrString);
				//		trace(xhrString);
				loading('0','Complete');
					}
					catch(err) {
						note(err);
						trace(xhrString);
						loading('0','Complete');
					}
				}
			
		} else {
			if (xmlhttp.status==0) {
			} else {
			alert("Error!\n The server returned the following error code: "+xmlhttp.status+"\nRequested path: "+pathForTheRecord);
			loading('0','Complete');
			}
		} // end state 200
	} else {
		if (xmlhttp.readyState == 3) {
//			alert("code: "+xmlhttp.readyState+"\nRequested path: "+pathForTheRecord);
		}	
		
//		alert("code: "+xmlhttp.readyState+"\nRequested path: "+pathForTheRecord);
	} // end ready state 4
}
var traceCalls=0;
function trace(t) {
	var traceOutputDiv = document.getElementById('screen');
	if (traceOutputDiv != null) {
		var traceString = traceOutputDiv.innerHTML;
		if (traceCalls>50) {
		//	var strArray = traceString.split('|');
		//	strArray.pop();
		//	traceString = strArray.join('|');
		}
		if (traceOutputDiv != null) {
			traceOutputDiv.innerHTML = t+'<br/>| '+traceString;
			traceCalls++;
			traceOutputDiv.style.width="300px";
			traceOutputDiv.style.height="auto";
			traceOutputDiv.style.overflow="auto";
			traceOutputDiv.style.backgroundColor="#000000";
			traceOutputDiv.style.color="#FFFFFF";
		}
	}
}
function clearTrace() {
	var traceOutputDiv = document.getElementById('debugDiv');
	if (traceOutputDiv != null) {
		traceOutputDiv.innerHTML = "";
	}
}
*/
var noteTimeout=null;
function frescoNote(t) {
	var obj=document.getElementById('serverResponse');
	if (obj != null) {
		obj.innerHTML = t;
		noteTimeout = window.setTimeout('clearNote()',5000);
	}
}
function clearNote() {
	var obj=document.getElementById('serverResponse');
	window.clearTimeout(noteTimeout);
	if (obj != null) {
		obj.innerHTML = '';
	}
}
/*
var allowPageTransition = true;
function populate(d,t) {
		var element = document.getElementById(d);
	//	trace("text: "+t);
		if (element != null) {
			element.innerHTML = t;
		}	
	
}

function setValue(inpt,val) {
	if (val) {
		var obj=document.getElementById(inpt);
		if (obj != null) {
			obj.value=val;	
		}
	}
}
*/
function postFormDataFresco(formName) {
	var paramArray = new Array();
	var params = "";
	var fldValuesArray = new Array();
	var checksArray = new Array();
	var fieldArray = new Array();
	var n=0;
	if (formName && document.forms[formName] !=null) { n= document.forms[formName].elements.length; }
	var paramsArray = new Array();
	var str='';
	
	for (var i=0; i<n; i++) {
//			trace('type: '+document.forms[formName].elements[i].type+', name: '+document.forms[formName].elements[i].id);
		var str = document.forms[formName].elements[i].id;
		
		if (document.forms[formName].elements[i].name != 'undefined') {
			if (document.forms[formName].elements[i].type!="hidden") {
				if (document.forms[formName].elements[i].type!="checkbox") {
					fldValuesArray[i]=document.forms[formName].elements[i].value;
					fieldArray[i]=document.forms[formName].elements[i].id;
				} else {
					fieldArray[i]=document.forms[formName].elements[i].id;
					if (document.forms[formName].elements[i].checked) {
						fldValuesArray[i]=document.forms[formName].elements[i].value;
					} else {
						fldValuesArray[i]=0;
					}
				} // end checkbox
			} else {

				fldValuesArray[i]=document.forms[formName].elements[i].value;
	
				fieldArray[i]=document.forms[formName].elements[i].id;

			
			} // end if hidden
		} // end if document.forms[formName].elements[i].name
	
	} // end for i
	
	
	var formSaveReport='';
	if (fieldArray.length>0 && fldValuesArray.length>0) {
		for (var i=0; i<fieldArray.length; i++) {
			if (fldValuesArray[i]) {
				var temp = fldValuesArray[i].split('&');
				fldValuesArray[i] = temp.join("~amp~");
			}
			paramArray.push(fieldArray[i] + "=" + fldValuesArray[i]);
		}
	}
	if (checksArray.length>0 && chkValuesArray.length>0) {
		for (var i=0; i<checksArray.length; i++) {
			paramArray.push(checksArray[i] + "=" + chkValuesArray[i]);
		}
	}
	params = '&'+paramArray.join("&");
//	trace(dataSourcePathFresco + '?' + paramArray.join("&"));
	if (params) {
		postToServerFresco(params);
//		trace(paramArray.join("&"));
//		sendToServer(dataSourcePathFresco + "?" + params);
	}
}


