
var MFSajaxPath = "";

function mfs_SetAjaxPath(ap)
{
	MFSajaxPath = ap;
}

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

function mfs_SendAjaxCallback(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 stripslashes(str)
{
	str = str.replace(/\\'/g,'\'');
	str = str.replace(/\\"/g,'"');
	str = str.replace(/\\0/g,'\0');
	str = str.replace(/\\\\/g,'\\');
	return str;
}

function mfs_AdminGetScheduleItems()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSchedule_AdminGetScheduleItems";
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText)
											{ 
											 	document.body.style.cursor = "default";
												document.getElementById("mfsitemlist").innerHTML = theText;
											} );
}

function mfs_EditScheduleItem(id)
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFScheduleEditItem";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) 
										{ 
											 //alert(theText);
											 theGallery = eval("(" + theText + ")");
											 document.getElementById("itemid").value = theGallery.id;
											 
											 var headline = unescape(theGallery.headline);
											 //headline = stripslashes(headline);
											 document.getElementById("headline").value = headline;
											 
											 var desc = unescape(theGallery.description);
											 //desc = stripslashes(desc);
											 desc = desc.replace( /<br \/>/g, "\n" );
											 document.getElementById("description").value = desc;
											 
											 document.getElementById('starttime').value = theGallery.starttime;
											 document.getElementById('endtime').value = theGallery.endtime;	
											 
											 document.getElementById("submit").value = "Save Changes";
											 document.body.style.cursor = "default";
										} );
}

function mfs_SaveItem()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFScheduleSaveItem";
	requestUrl += "&id=" + escape(document.getElementById("itemid").value);
	requestUrl += "&headline=" + escape(document.getElementById("headline").value);
	
	desc = document.getElementById("description").value;
	desc = desc.replace(/\+/g,"{%plus%}");
	
	requestUrl += "&description=" + escape(desc);
	requestUrl += "&starttime=" + escape(document.getElementById('starttime').value);
	requestUrl += "&endtime=" + escape(document.getElementById('endtime').value);

	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) { 	alert(theText);
											   				document.body.style.cursor = "default";
															mfs_ClearScheduleForm();
															mfs_AdminGetScheduleItems(); 
													  	} );
}

function mfs_ClearScheduleForm()
{
	document.getElementById("itemid").value = -1
	document.getElementById("headline").value = "";
	document.getElementById("description").value = "";
	document.getElementById('starttime').value = "";
	document.getElementById('endtime').value = "";

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

function mfs_DeleteItem(id)
{
	if( confirm( "Are you sure you want to delete this item?" ) )
	{
		requestUrl = MFSajaxPath;
		requestUrl += "?action=MFScheduleDeleteItem";
		requestUrl += "&id=" + id;
		document.body.style.cursor = "wait";
		mfs_SendAjaxCallback( requestUrl, function(theText) { 
												   			document.body.style.cursor = "default";
															mfs_AdminGetScheduleItems(); 
														  } );
	}
}

function mfs_SaveOptions()
{
	// To do: Add field validation
	
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFScheduleSaveOptions";
	requestUrl += "&emptyschedulemsg=" + escape(document.getElementById("emptyschedulemsg").value);

	//alert(requestUrl);
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) { 	document.body.style.cursor = "default";
															alert(theText);
											   				alert("Options Are Saved!\n");
															//document.getElementById("debuginfo").innerHTML = theText;
													  	} );
}

function mfs_GetOptions()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFScheduleGetOptions";	

	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, 
						 function(theText) { 	document.body.style.cursor = "default";
						 						//alert(theText);
												theOptions = eval("(" + theText + ")");															
												document.getElementById("emptyschedulemsg").value = theOptions.emptyschedulemsg;
											} );
}










/*
function mfs_DisplayGallery(id)
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryDisplayGallery";
	requestUrl += "&id=" + id;
	
	//alert(requestUrl);
	
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) 
										{ 
											 document.getElementById("mfgallerydisplay").innerHTML = theText;
											 document.body.style.cursor = "default";
										} );
}

function mfs_ShowGallery(id)
{
	MFSgalleryId = id;
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryGetGalleryInfo";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) 
										{ 
											 //alert(theText);
											 theGallery = eval("(" + theText + ")");
											 document.getElementById("selectedgallery").innerHTML = theGallery.name;
											 document.body.style.cursor = "default";
											 mfs_DisplayGallery(id);
										} );
}

function mfs_ShowLogfiles()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryDumpLogs";
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) 
										{ 
											 document.getElementById("mfgallery_uploadlog").innerHTML = theText;
											 document.body.style.cursor = "default";
										} );
}

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


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

function mfs_EditImageInfo(id)
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryGetPhotoInfo";
	requestUrl += "&id=" + id;
	
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) { 	document.body.style.cursor = "default";
															//thePhotoInfo = eval("(" + theText + ")");															
															document.getElementById("mfgalleryphotoform").innerHTML = theText	;
													  	} );
}


function mfs_SavePhotoInfo()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSallerySavePhotoInfo";
	requestUrl += "&id=" + escape(document.getElementById("photoid").value);
	requestUrl += "&modelname=" + escape(document.getElementById("modelname").value);
	requestUrl += "&photographer=" + escape(document.getElementById("photographer").value);
	requestUrl += "&copyright=" + escape(document.getElementById("copyright").value);
	requestUrl += "&description=" + escape(document.getElementById("description").value);
	
	//alert(requestUrl);	
	mfs_SendAjaxCallback( requestUrl, function(theText) { //alert(theText);
														//document.getElementById("debuginfo").innerHTML = theText;
														alert("Information Saved!");
														mfs_ClearGalleryForm();
														mfs_GetGalleryList(0); 
													  } );
}

function mfs_ClearPhotoForm()
{
	document.getElementById("photoid").value = -1
	document.getElementById("photoinfoform").innerHTML = "NEW";
	document.getElementById("modelname").value = "";
	document.getElementById("photographer").value = "";
	document.getElementById("copyright").value = "";
	document.getElementById("description").value = "";

	document.getElementById("submit").value = "Save Photo";
}
////////////////////////////

function mfs_ShowImage(imgpath, displayelem, galleryelem, w, h, mw, mh, caption)
{
	mfs_displayelem = displayelem;
	mfs_galleryelem = galleryelem;
	
	document.getElementById(galleryelem).style.visibility = 'hidden';
	document.getElementById(galleryelem).style.display = 'none';

	var wratio = w / mw;
	var hratio = h / mh;
	var multiplier = 1 / ((wratio > hratio) ? (wratio) : (hratio));

	w *= multiplier;
	h *= multiplier;

	imagecode = "Click image to return to thumbnail view<br />";
	imagecode += "<img class='mfgalleryimagedisplay' width='" + w + "' height='" + h + "' src='" + imgpath + "' onClick='mfs_HideImage(" + '"' + displayelem + '", "' + galleryelem + '");' + "'>";
	
	imagecode += unescape(caption);
	
	//alert(imagecode);
	document.getElementById(displayelem).innerHTML = imagecode;
	
	document.getElementById(displayelem).style.visibility = 'visible';
	document.getElementById(displayelem).style.display = 'block';
	
}

function mfs_HideImage(displayelem, galleryelem)
{
	document.getElementById(displayelem).innerHTML = "";
	
	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 mfs_RebuildThumbnails(id,displayflag)
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryRebuildGalleryThumbnails";
	requestUrl += "&id=" + id;
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) { 
											   			if( theText.length > 0 )
															alert(theText);
														
														if( displayflag )
															mfs_DisplayGallery(id);															
															
											   			document.body.style.cursor = "default";
													    alert("The thumbnails for this gallery have been rebuilt!");
													  } );
}


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

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

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

function mfs_GetThumbnails(id,start, targetelem)
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryGetThumbnails";
	requestUrl += "&id=" + id;
	requestUrl += "&start=" + start;
	requestUrl += "&targetelem=" + targetelem;
	
	document.body.style.cursor = "wait";
	mfs_SendAjaxCallback( requestUrl, function(theText) { 	document.body.style.cursor = "default";
															//alert(theText);
											   				document.getElementById(targetelem).innerHTML = theText;
													  } );
}

//////////////////////////
// Image rollover code
//////////////////////////

function mfs_ImageRollover( elem, normalstate, overstate, downstate, currentstate )
{
	switch( currentstate )
	{
		case 0:
			document.getElementById(elem).src = normalstate;
			break;
		case 1:
			document.getElementById(elem).src = overstate;
			break;
		case 2:
			document.getElementById(elem).src = downstate;
			break;
	}	
}

//////////////////////////
// This block of code contains functions for the Display Options page
//////////////////////////


function mfs_UpdateThumbnailPreview()
{
	var w = document.getElementById("thumbnailsize").value * 0.6666;
	try
	{
		document.getElementById("mfgallery_thumbnail_box").style.width = document.getElementById("thumbgridsize").value + "px";
		document.getElementById("mfgallery_thumbnail_box").style.height = document.getElementById("thumbgridsize").value + "px";
		
		var toppadding = (document.getElementById("thumbgridsize").value - document.getElementById("thumbnailsize").value) / 2;
		document.getElementById("mfgallery_thumbnail_img").style.paddingTop = toppadding + "px";
		
		document.getElementById("mfgallery_thumbnail_box").style.backgroundColor = document.getElementById("thumbcolor").value;
	
		if( document.getElementById("thumbborder").checked )
		{
			var bw = document.getElementById("thumbborderwidth").value
			document.getElementById("mfgallery_thumbnail_box").style.border = bw + "px solid " + document.getElementById("thumbbordercolor").value;
		}
		else
		{
			document.getElementById("mfgallery_thumbnail_box").style.border = "none";
		}
		
		document.getElementById("mfgallery_thumbnail_img").style.width = w + "px";
		document.getElementById("mfgallery_thumbnail_img").style.height = document.getElementById("thumbnailsize").value + "px";
	}
	catch(e)
	{
	}
}

function mfs_GetDisplayOptions()
{
	requestUrl = MFSajaxPath;
	requestUrl += "?action=MFSalleryGetDisplayOptions";	

	document.body.style.cursor = "wait";

	mfs_SendAjaxCallback( requestUrl, 
						 function(theText) { 	document.body.style.cursor = "default";
						 						//alert(theText);
												theGalleryOptions = eval("(" + theText + ")");															
												document.getElementById("thumbgridsize").value = theGalleryOptions.thumbgridsize;
												document.getElementById("thumbnailsize").value = theGalleryOptions.thumbnailsize;
												document.getElementById("thumbcolor").value = theGalleryOptions.thumbcolor;
												document.getElementById("thumbjpeg").value = theGalleryOptions.thumbjpeg;
												document.getElementById("thumbborder").checked = theGalleryOptions.thumbborder ? true : false;
												document.getElementById("thumbborderwidth").value = theGalleryOptions.thumbborderwidth;
												document.getElementById("thumbbordercolor").value = theGalleryOptions.thumbbordercolor;
												document.getElementById("thumbsperpage").value = theGalleryOptions.thumbsperpage;
												document.getElementById("gwidth").value = theGalleryOptions.gwidth;
												document.getElementById("gheight").value = theGalleryOptions.gheight;
												document.getElementById("maximagewidth").value = theGalleryOptions.maximagewidth;
												document.getElementById("maximageheight").value = theGalleryOptions.maximageheight;	

												document.getElementById("showphotographer").checked = theGalleryOptions.showphotographer ? true : false;
												document.getElementById("showcopyright").checked = theGalleryOptions.showcopyright ? true : false;
												document.getElementById("showdescription").checked = theGalleryOptions.showdescription ? true : false;

												mfs_UpdateThumbnailPreview();											
											} );
}

function mfs_ShowHelp(elem)
{
	var src = "&nbsp;";
	
	if( elem != -1 )
		src = document.getElementById(elem).innerHTML;
	
	document.getElementById("mfgallery_settings_help").innerHTML = src;
}

function mfs_InsertCopyright()
{
	var d = new Date();
	document.getElementById("copyright").value = "Copyright &copy; " + d.getFullYear() + ", All Rights Reserved";
}

//////////////////////////
// This block of code contains functions called from Flash Uploader.
//////////////////////////

function mfs_FlashAlert(msg)
{
	alert(msg);
}

function mfs_ImageClicked()
{
	mfs_HideImage( mfs_displayelem, mfs_galleryelem );
}

function mfs_FlashUploadFinished(msg)
{
	mfs_ShowGallery(MFSgalleryId);
	mfs_ShowLogfiles();
}

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

function mfs_GetGalleryId()
{
	return MFSgalleryId;
}

*/
