
var GGajaxPath = "";
var GGgalleryId = -1;

function GGsetAjaxPath(ap)
{
	GGajaxPath = ap;
}

//////////////////////////////////////////////////////////////////////////////////////
// Send AJAX response to server, with response to be handled by specified callback function
//////////////////////////////////////////////////////////////////////////////////////

function GGsendAjaxCallback(url,callbackfunction)
{
	// Add a time code onto the end of the request URL so that it will always be
	// different, even if we're asking for the same thing as something else
	// that we may have done in the recent past, in order to prevent browser caching.
	d = new Date()
	t = d.getTime();
	url = url + "&nc=" + t;
	
	var AjaxRequest = null;
	// Get a request object	
	try
	{
		AjaxRequest = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				AjaxRequest = null;
				alert("Your web browser is incompatible with the editing functions on this page.");
			}
		}
	}

	// Send the request
	AjaxRequest.open("GET", url, true);
	
	// Use anonymous inner function as callback
	AjaxRequest.onreadystatechange = function() 
	{
		if (AjaxRequest.readyState == 4)
		{	
			// Check for zero because of stupid goddamn fucking Firefox
			if ( (AjaxRequest.status == 0) || (AjaxRequest.status == 200) )
			{
				if( callbackfunction )
					callbackfunction(AjaxRequest.responseText);
			}
			else if (AjaxRequest.status == 404 )
			{
				// file not found?
				msg = "Error!  Status = " + AjaxRequest.status + "\n";
			}
			else
			{
				//alert("Error! Request status is " + AjaxRequest.status);
			}
		}
	}
	
	AjaxRequest.send(" ");	// Firefox doesn't like NULL so send a space	
}

function getGalleryList(mode)
{
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftGallery_ListGalleries";
	requestUrl += "&mode=" + mode;
//	alert(requestUrl);
	document.body.style.cursor = "wait";
	GGsendAjaxCallback( requestUrl, function(theText)
											{ 
											 	document.body.style.cursor = "default";
												document.getElementById("glamoursoftgallerylist").innerHTML = theText;
											} );
}

function editGallery(id)
{
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftGetGalleryInfo";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	GGsendAjaxCallback( requestUrl, function(theText) 
										{ 
											 //alert(theText);
											 theGallery = eval("(" + theText + ")");
											 document.getElementById("galleryid").value = theGallery.id;
											 document.getElementById("gallerynumber").innerHTML = theGallery.id;
											 document.getElementById("galleryname").value = theGallery.name;
											 document.getElementById("imageurl").value = theGallery.imageurl;
											 document.getElementById("showname").checked = (theGallery.showname>0) ? true : false;
											 document.getElementById("autoslideshow").checked = (theGallery.autoslideshow>0) ? true : false;
											 document.getElementById("membersonly").checked = (theGallery.membersonly>0) ? true : false;
											 document.getElementById("published").checked = (theGallery.published>0) ? true : false;
											 document.getElementById("autoresize").checked = (theGallery.autoresize>0) ? true : false;
											 document.getElementById("maxwidth").value = theGallery.maxwidth;
											 document.getElementById("maxheight").value = theGallery.maxheight;	
											 
											 document.getElementById("submit").value = "Save Changes";
											 document.body.style.cursor = "default";
										} );
}

function displayGallery(id)
{
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftDisplayGallery";
	requestUrl += "&id=" + id;
	
	//alert(requestUrl);
	
	document.body.style.cursor = "wait";
	GGsendAjaxCallback( requestUrl, function(theText) 
										{ 
											 document.getElementById("glamoursoftgallerydisplay").innerHTML = theText;
											 document.body.style.cursor = "default";
										} );
}

function showGallery(id)
{
	GGgalleryId = id;
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftGetGalleryInfo";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	GGsendAjaxCallback( requestUrl, function(theText) 
										{ 
											 //alert(theText);
											 theGallery = eval("(" + theText + ")");
											 document.getElementById("selectedgallery").innerHTML = theGallery.name;
											 document.body.style.cursor = "default";
											 displayGallery(id);
										} );
}

function saveGallery()
{
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftSaveGalleryInfo";
	requestUrl += "&id=" + escape(document.getElementById("galleryid").value);
	requestUrl += "&name=" + escape(document.getElementById("galleryname").value);
	requestUrl += "&imageurl=" + escape(document.getElementById("imageurl").value);
	requestUrl += "&maxwidth=" + escape(document.getElementById("maxwidth").value);
	requestUrl += "&maxheight=" + escape(document.getElementById("maxheight").value);
	
	requestUrl += "&autoslideshow=" + (document.getElementById("autoslideshow").checked ? "1" : "0");
	requestUrl += "&showname=" + (document.getElementById("showname").checked ? "1" : "0");
	requestUrl += "&membersonly=" + (document.getElementById("membersonly").checked ? "1" : "0");
	requestUrl += "&published=" + (document.getElementById("published").checked ? "1" : "0");
	requestUrl += "&autoresize=" + (document.getElementById("autoresize").checked ? "1" : "0");
	
	//alert(requestUrl);	
	GGsendAjaxCallback( requestUrl, function(theText) { //alert(theText);
														//document.getElementById("debuginfo").innerHTML = theText;
														clearGalleryForm();
														getGalleryList(0); 
													  } );
}

function clearGalleryForm()
{
	document.getElementById("galleryid").value = -1
	document.getElementById("gallerynumber").innerHTML = "NEW";
	document.getElementById("galleryname").value = "";
	document.getElementById("imageurl").value = "";
	document.getElementById("autoslideshow").checked = false;
	document.getElementById("showname").checked = false;
	document.getElementById("membersonly").checked = false;
	document.getElementById("published").checked = false;
	document.getElementById("autoresize").checked = false;
	document.getElementById("maxwidth").value = "";
	document.getElementById("maxheight").value = "";

	document.getElementById("submit").value = "Save New Gallery";
}

//////////////////////////

function flashUploadFinished(msg)
{
	showGallery(GGgalleryId);
}

function uploadStarted(msg)
{
	document.getElementById("flashuploadstatus").innerHTML = msg;
}

function getGalleryId()
{
	return GGgalleryId;
}

function ggDeleteImage(id)
{
	if( confirm( "Are you sure you want to delete this image?" ) )
	{
		requestUrl = GGajaxPath;
		requestUrl += "?action=GlamoursoftDeleteImage";
		requestUrl += "&id=" + id;
		//alert(requestUrl);
		document.body.style.cursor = "wait";
		GGsendAjaxCallback( requestUrl, function(theText) { document.body.style.cursor = "default";
															getGalleryList(1); 
															displayGallery(GGgalleryId);															
														  } );
	}
}

function ggShowImage(imgpath, displayelem, galleryelem)
{
	document.getElementById(galleryelem).style.visibility = 'hidden';
	document.getElementById(galleryelem).style.display = 'none';
	
	imagecode = "<img class='glamoursoftgalleryimagedisplay' width='80%' height='80%' src='" + imgpath + "' onClick='ggHideImage(" + '"' + displayelem + '", "' + galleryelem + '");' + "'>";
	imagecode += "<br />Click image to return to thumbnail view";
	
	//alert(imagecode);
	document.getElementById(displayelem).innerHTML = imagecode;
	
	document.getElementById(displayelem).style.visibility = 'visible';
	document.getElementById(displayelem).style.display = 'block';
	
}

function ggHideImage(displayelem, galleryelem)
{
	document.getElementById(galleryelem).style.visibility = 'visible';
	document.getElementById(galleryelem).style.display = 'block';
	document.getElementById(displayelem).style.visibility = 'hidden';
	document.getElementById(displayelem).style.display = 'none';
}

function ggRebuildGalleryXML(id)
{
	requestUrl = GGajaxPath;
	requestUrl += "?action=GlamoursoftRebuildGalleryXML";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	GGsendAjaxCallback( requestUrl, function(theText) { document.body.style.cursor = "default";
													    alert("The slideshow XML for this gallery has been rebuilt!");
													  } );
}

function ggDeleteGallery(id)
{
	if( confirm( "Are you sure you want to delete gallery #" + id + "?\nThis will also delete all images associated with this gallery." ) )
	{
		requestUrl = GGajaxPath;
		requestUrl += "?action=GlamoursoftDeleteGallery";
		requestUrl += "&id=" + id;
		document.body.style.cursor = "wait";
		GGsendAjaxCallback( requestUrl, function(theText) { document.body.style.cursor = "default";
															getGalleryList(0); 
															alert("The gallery has been deleted!");
														  } );
	}
}

function ggPhotoManagement(id)
{
	window.location = '/wp-admin/admin.php?page=GlamoursoftGalleryPhotoManager&gallery=' + id;
}