
var galleryslideshowtimeout = null;
var galleryinslideshow = false;
var galleryaddtocarturlbase = null;
var gallerycarturl = null;
var galleryopturlbase = null;
var galleryInCart = false;

function gallery_downloadCart()
{
	gallery_navToCartOption('download');
}

function gallery_orderPrints()
{
	gallery_navToCartOption('prints');
}

function gallery_navToCartOption(opt)
{
	galleryimage_setPhotoID(null,null);
	galleryimage_setCurrent(null);
	galleryimage_setTitle(null);

	var	obj	= document.getElementById('galleryimgtitleandnav');

	obj.style.visibility = 'hidden';

	window.open(galleryopturlbase + opt + '.php','galleryphotoframe');
}

function gallery_setAddToCartURL(url)
{
	galleryaddtocarturlbase = url;
}

function gallery_setCartURL(url)
{
	gallerycarturl = url;
}

function gallery_setOptUrlBase(url)
{
	galleryopturlbase = url;
}

function gallery_showCartOptions()
{
	var	obj;

	galleryInCart = true;

	obj = document.getElementById('gallerycartclear');
	obj.style.visibility = 'visible';

	obj = document.getElementById('gallerycartoptions');
	obj.style.visibility = 'visible';

	obj = document.getElementById('gallerybuttonaddtocart');
	obj.style.display = 'none';

	obj = document.getElementById('gallerybuttonremovefromcart');
	obj.style.display = '';
}

function gallery_hideCartOptions()
{
	var	obj;

	galleryInCart = false;

	obj = document.getElementById('gallerycartclear');
	obj.style.visibility = 'hidden';

	obj = document.getElementById('gallerycartoptions');
	obj.style.visibility = 'hidden';

	obj = document.getElementById('gallerybuttonaddtocart');
	obj.style.display = '';

	obj = document.getElementById('gallerybuttonremovefromcart');
	obj.style.display = 'none';
}

function gallerybuttonclick_clearCart()
{
	var	obj	= document.getElementById("gallerycartcontents");

	obj.innerHTML = '';
	obj.appendChild(document.createTextNode('(clearing...)'));

	xmlrpc_sendRequest(galleryaddtocarturlbase + '?operation=clear',gallery_processAddToCartResult);

	var	frames	= null;

	if(window.frames)
	{
		frames = window.frames;
	}
	else if(document.frames)
	{
		frames = document.frames;
	}

	if(frames)
	{
		var	theFrame	= frames['galleryphotoframe'];

		if(theFrame)
		{
			theFrame.location.href = 'http://gallery.dlindquist.com/galleryimage.php';
		}
	}

	gallery_hideCartOptions();
	gallery_returnToGalleriesList();

	obj = document.getElementById('selectedgallery');
	obj.innerHTML = '';
	obj.style.visibility = 'hidden';

	obj = document.getElementById('galleryimagetitle');
	obj.innerHTML = '';

	obj = document.getElementById('galleryimgtitleandnav');
	obj.style.visibility = 'hidden';
}

function gallerybuttonclick_addToCart()
{
	var	obj	= document.getElementById("gallerycartcontents");

	obj.innerHTML = '';
	obj.appendChild(document.createTextNode('(adding...)'));

	xmlrpc_sendRequest(galleryaddtocarturlbase + '?gallery=' + escape(galleryimage_photoIDGallery) + '&image=' + escape(galleryimage_photoIDPhoto),gallery_processAddToCartResult);
}

function gallerybuttonclick_removeFromCart()
{
	var     obj     = document.getElementById("gallerycartcontents");

	obj.innerHTML = '';
	obj.appendChild(document.createTextNode('(removing...)'));

	xmlrpc_sendRequest(galleryaddtocarturlbase + '?gallery=' + escape(galleryimage_photoIDGallery) + '&image=' + escape(galleryimage_photoIDPhoto) + '&operation=remove',gallery_processAddToCartResult);
}

function gallery_processAddToCartResult(result)
{
	var	text	= result.responseText;

	if(text.substring(0,7) == 'ERROR: ')
	{
		alert(text.substring(7));
	}
	else
	{
		var	obj	= document.getElementById("gallerycartcontents");

		obj.innerHTML = '';
		obj.appendChild(document.createTextNode(text));

		if(galleryInCart)
		{
			if(document.frames)
			{
				document.frames['gallerycontentsframe'].location.href = document.frames['gallerycontentsframe'].location.href;
			}
			else if(window.frames)
			{
				window.frames['gallerycontentsframe'].location.reload(true);
			}
		}
	}
}

function gallerybuttonclick_cartContents()
{
	window.parent.gallery_openGallery(gallerycarturl,'Cart');
}

function galleryimage_processSlideshow()
{
	if(galleryinslideshow)
	{
		galleryslideshowtimeout = window.setTimeout(gallerybuttonclick_next,6000);
	}
}

function gallerybuttonclick_stopplay()
{
	var	button	= document.getElementById('gallerybuttonstopplay');

	if(button.className.indexOf('gallerybuttonplay') >= 0)
	{
		button.className = button.className.replace(/\bgallerybuttonplay\b/,'gallerybuttonstop');
		galleryslideshowtimeout = window.setTimeout(gallerybuttonclick_next,6000);
		galleryinslideshow = true;
	}
	else
	{
		button.className = button.className.replace(/\bgallerybuttonstop\b/,'gallerybuttonplay');
		galleryinslideshow = false;
		try { window.clearTimeout(galleryslideshowtimeout); } catch(ex) { }
	}
}

function gallery_stopSlideshow()
{
	var	win	= window;

	while(win && !win.document.getElementById('gallerybuttonstopplay'))
	{
		win = win.parent;
	}

	if(win)
	{
		var	obj	= win.document.getElementById('gallerybuttonstopplay');

		if(obj.className.indexOf('gallerybuttonplay') < 0)
		{
			win.gallerybuttonclick_stopplay();
		}
	}
}

function gallerybuttonclick_previous()
{
	var     obj     = galleryimage_currentPicture;

	if(obj)
	{
		obj = obj.previousSibling;
	}
	while(obj && obj.nodeType != 1)
	{
		obj = obj.previousSibling;
	}

	if(obj)
	{
		if(obj.onclick)
		{
			obj.onclick(null);
		}
	}
}

function gallerybuttonclick_next()
{
	var	obj	= galleryimage_currentPicture;

	try { window.clearTimeout(galleryslideshowtimeout); } catch(ex) { }

	if(obj)
	{
		obj = obj.nextSibling;
	}
	while(obj && obj.nodeType != 1)
	{
		obj = obj.nextSibling;
	}

	if(obj)
	{
		if(obj.onclick)
		{
			obj.onclick(null);
		}
	}
}

function gallerybuttonclick_zoom()
{
	var     obj     = document.getElementById('galleryphotoframe');

	if(obj)
	{
		gallery_sizeFrame(obj,true);

		if(obj.contentDocument)
		{
			obj = obj.contentDocument;

			if(obj)
			{
				if(obj.window)
				{
					obj = obj.window;
				}
				else if(obj.defaultView)
				{
					obj = obj.defaultView;
				}
				else
				{
					obj = null;
				}
			}
		}
		else if(document.frames)
		{
			obj = document.frames['galleryphotoframe'].window;
		}
		else
		{
			obj = null;
		}

		if(obj)
		{
			if(obj.gallery_autoSizeImage)
			{
				var	button	= document.getElementById("gallerybuttonzoom");

				if(button.className.indexOf('gallerybuttonzoomin') >= 0)
				{
					button.className = button.className.replace(/gallerybuttonzoomin/,'gallerybuttonzoomout');
					obj.doAutoSizeImage = false;
				}
				else
				{
					button.className = button.className.replace(/gallerybuttonzoomout/,'gallerybuttonzoomin');

					obj.doAutoSizeImage = true;
				}

				obj.gallery_autoSizeImage();
			}
		}
	}
}

function gallery_setZoomIconTo(zoomedIn)
{
	var	obj		= document.getElementById("gallerybuttonzoom");
	var	oldClass	= obj.className;
	var	newClass	= oldClass;

	if(zoomedIn)
	{
		newClass = oldClass.replace(/gallerybuttonzoomin/,'gallerybuttonzoomout');
	}
	else
	{
		newClass = oldClass.replace(/gallerybuttonzoomout/,'gallerybuttonzoomin');
	}

	if(newClass != oldClass)
	{
		obj.className = newClass;
	}
}

function gallery_initPhotoPage()
{
	gallery_autoSizeImage();

	if(document.body.addEventListener)
	{
		document.body.addEventListener("resize",gallery_autoSizeImage,false);
	}
	else if(document.body.attachEvent)
	{
		document.body.attachEvent("onresize",gallery_autoSizeImage);
	}
	else
	{
		document.body.onresize = gallery_autoSizeImage;
	}

	if(window.addEventListener)
	{
		window.addEventListener("resize",gallery_autoSizeImage,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onresize",gallery_autoSizeImage);
	}
	else
	{
		window.onresize = gallery_autoSizeImage;
	}
}

var doAutoSizeImage = true;

function gallery_autoSizeImage()
{
	var	obj	= document.getElementById('theImage');

	parent.gallery_setZoomIconTo(!doAutoSizeImage);

	if(obj)
	{
		var	fallback	= true;

		if(doAutoSizeImage)
		{
			var	origWidth	= obj.getAttribute('x-orig-width');
			var	origHeight	= obj.getAttribute('x-orig-height');

			if(origWidth && origHeight)
			{
				origWidth = parseInt(origWidth);
				origHeight = parseInt(origHeight);

				if(origWidth && origHeight)
				{
					var	bodySize	= getPhotoFrameSize();

					if(bodySize && bodySize.x && bodySize.y)
					{
						var	widthDiff	= bodySize.x - origWidth;
						var	heightDiff	= bodySize.y - origHeight;

						if(widthDiff < 0 || heightDiff < 0)
						{
							var	pct;

							if(widthDiff < 0)
							{
								widthDiff = bodySize.x / origWidth;
							}
							else
							{
								widthDiff = 1;
							}
							if(heightDiff < 0)
							{
								heightDiff = bodySize.y / origHeight;
							}
							else
							{
								heightDiff = 1;
							}

							if(widthDiff < heightDiff)
							{
								pct = widthDiff;
							}
							else
							{
								pct = heightDiff;
							}

							if(pct > 0 && pct < 1)
							{
								origWidth = Math.floor(origWidth * pct);
								origHeight = Math.floor(origHeight * pct);

								obj.style.width = (origWidth-10) + 'px';
								obj.style.height = (origHeight-10) + 'px';
								fallback = false;
							}
						}
					}
				}
			}
		}

		if(fallback)
		{
			obj.style.width = 'auto';
			obj.style.height = 'auto';
		}
	}
}

function getPhotoFrameSize()
{
	var	obj;

	obj = window.parent;

	if(obj.document)
	{
		obj = obj.document;
	}
	else if(obj.ownerDocument)
	{
		obj = obj.ownerDocument;
	}
	else
	{
		obj = null;
	}

	if(obj)
	{
		obj = obj.getElementById('galleryphotoframe');

		if(obj)
		{
			var	result	= new Object();

			result.x = obj.offsetWidth;
			result.y = obj.offsetHeight;

			return(result);
		}
	}

	return(null);
}

function gallery_mouseover(obj)
{
	obj.className = obj.className.replace(/\bgalleryhover\b/,' ').replace(/[ ]+$/,'') + ' galleryhover';
}

function gallery_mouseout(obj)
{
	obj.className = obj.className.replace(/\bgalleryhover\b/,' ').replace(/[ ]+$/,'');
}

function gallery_mouseclick(obj,url,name)
{
	var	obj;

	window.parent.gallery_openGallery(url,name);
	if(name == 'Cart')
	{
		window.parent.gallery_showCartOptions();
	}
	else
	{
		window.parent.gallery_hideCartOptions();
	}


}

function gallery_switchToPhotosList()
{
	if(!gallery_inPhotosView)
	{
		return;
	}

	var	obj;

	obj = document.getElementById('gallerycontentsframe');
	if(obj.style.visibility != 'visible')
	{
		obj.style.visibility = 'visible';

		obj = document.getElementById('galleryframe');
		obj.style.visibility = 'hidden';
	}
}

function gallery_returnToGalleriesList()
{
	if(!gallery_inPhotosView)
	{
		return;
	}

	var	obj;

	obj = document.getElementById('galleryframe');
	if(obj.style.visibility != 'visible')
	{
		obj.style.visibility = 'visible';

		obj = document.getElementById('gallerycontentsframe');
		obj.style.visibility = 'hidden';
	}
}

function gallery_openGallery(url,name)
{
	if(url)
	{
		var	obj;

		window.open(url,"gallerycontentsframe");
		obj = document.getElementById('selectedgallery');
		obj.innerHTML = '';
		obj.appendChild(document.createTextNode(name));
		if(obj.style.visibility != 'visible')
		{
			obj.style.visibility = 'visible';
		}
		gallery_shuffleToGalleryContentsView();

		var	frames	= null;

		if(window.frames)
		{
			frames = window.frames;
		}
		else if(document.frames)
		{
			frames = document.frames;
		}

		if(frames)
		{
			var	theFrame	= frames['galleryphotoframe'];

			if(theFrame)
			{
				theFrame.location.href = 'http://gallery.dlindquist.com/galleryimage.php';
			}
		}
	}
}

function galleryimage_mouseclick(obj,url,name,gallery,photo)
{
	if(url)
	{
		galleryLoading_startLoading();
		parent.galleryimage_setTitle(name);
		parent.galleryimage_setCurrent(obj);
		parent.galleryimage_setPhotoID(gallery,photo);

		window.open(url,'galleryphotoframe');
	}
}

var galleryimage_currentPicture = null;
var galleryimage_photoIDGallery = null;
var galleryimage_photoIDPhoto = null;

function galleryimage_setPhotoID(gallery,photo)
{
	galleryimage_photoIDGallery = gallery;
	galleryimage_photoIDPhoto = photo;
}

function galleryimage_setCurrent(obj)
{
	galleryimage_currentPicture = obj;
}

function galleryimage_setTitle(name)
{
	var     obj     	= document.getElementById('galleryimagetitle');
	var	container	= document.getElementById('galleryimgtitleandnav');

	if(!name)
	{
		name = '(untitled)';
	}

	if(obj)
	{
		obj.innerHTML = '';
		obj.appendChild(document.createTextNode(name));
		container.style.visibility = 'visible';
	}
}

var gallery_inIndexMode = true;
var gallery_inPhotosView = false;

function gallery_shuffleToGalleryContentsView()
{
	if(!gallery_inIndexMode)
	{
		if(gallery_inPhotosView)
		{
			gallery_switchToPhotosList();
		}
		return;
	}

	gallery_inIndexMode = false;

	var	obj;
	var	tmpObj;
	var	tmpTop;
	var	tmpLeft;
	var	tmpHeight;

	obj = document.getElementById('headercontainer');
	obj.style.display = 'none';
	obj = document.getElementById('stubheadercontainer');
	obj.style.display = 'block';

	obj = document.getElementById('galleryframe');
	obj.style.height = '135px';
	obj.style.visibility = 'hidden';

	tmpObj = obj;
	tmpTop = tmpObj.offsetTop;
	tmpLeft = tmpObj.offsetLeft;
	while(tmpObj.offsetParent)
	{
		tmpObj = tmpObj.offsetParent;
		tmpTop += tmpObj.offsetTop;
		tmpLeft += tmpObj.offsetLeft;
	}

	obj = document.getElementById('gallerycontentsframe');
	obj.style.height = '135px';
	obj.style.position = 'absolute';
	obj.style.top = tmpTop + 'px';
	obj.style.left = tmpLeft + 'px';
	obj.style.zIndex = 1000;
	obj.style.visibility = 'visible';
	tmpHeight = obj.offsetHeight;

	obj = document.getElementById('selectedgallery');
	obj.style.visibility = 'visible';

	obj = document.getElementById('galleryphotoframe');
	obj.style.position = 'absolute';
	obj.style.top = (tmpTop + tmpHeight + 2) + 'px';
	obj.style.left = tmpLeft + 'px';
	obj.style.visibility = 'visible';

	gallery_inPhotosView = true;

	gallery_handlePageSize();
	window.setTimeout(gallery_handlePageSize,500);
}

function gallery_initIndexPage()
{
	gallery_inIndexMode = true;
	gallery_inPhotosView = false;

	gallery_handlePageSize();

	if(document.body.addEventListener)
	{
		document.body.addEventListener("resize",gallery_handlePageSize,false);
	}
	else if(document.body.attachEvent)
	{
		document.body.attachEvent("onresize",gallery_handlePageSize);
	}
	else
	{
		document.body.onresize = gallery_handlePageSize;
	}

	if(window.addEventListener)
	{
		window.addEventListener("resize",gallery_handlePageSize,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onresize",gallery_handlePageSize);
	}
	else
	{
		window.onresize = gallery_handlePageSize;
	}
}

function gallery_handlePageSize()
{
	try
	{
		gallery_lockBodySize();
		gallery_sizeIndexPage(!gallery_inPhotosView);
		if(gallery_inPhotosView)
		{
			gallery_sizeIndexPage(false);
			gallery_sizeContentsFrame();
			gallery_sizePhotoFrame();
		}
	}
	catch(ex)
	{
		// no-op
	}
	finally
	{
		gallery_unlockBodySize();
	}
}

function gallery_sizeIndexPage(doHeight)
{
	gallery_sizeFrame(document.getElementById('galleryframe'),doHeight);
}

function gallery_sizePhotoFrame()
{
	var	obj	= document.getElementById('galleryphotoframe');

	if(obj)
	{
		gallery_sizeFrame(obj,true);

		if(obj.contentDocument)
		{
			obj = obj.contentDocument;

			if(obj)
			{
				if(obj.window)
				{
					obj = obj.window;
				}
				else if(obj.defaultView)
				{
					obj = obj.defaultView;
				}
				else
				{
					obj = null;
				}
			}
		}
		else if(document.frames)
		{
			obj = document.frames['galleryphotoframe'].window;
		}
		else
		{
			obj = null;
		}

		if(obj)
		{
			if(obj.gallery_autoSizeImage)
			{
				obj.gallery_autoSizeImage();
			}
		}
	}
}

function gallery_sizeContentsFrame()
{
	gallery_sizeFrame(document.getElementById('gallerycontentsframe'),false);
}

function gallery_sizeFrame(obj,doHeight)
{
	var	tmpObj;
	var	top;
	var	bodySize	= getDocumentBodySize();

	if(obj)
	{
		if(doHeight)
		{
			tmpObj = obj;
			top = tmpObj.offsetTop;
			while(tmpObj.offsetParent)
			{
				tmpObj = tmpObj.offsetParent;
				top += tmpObj.offsetTop;
			}
			top += 10;

			obj.style.height = (bodySize.y-top) + 'px';
		}
		obj.style.width = (bodySize.x-32) + 'px';
	}
}

var gallery_lockedBodySize = null;

function gallery_lockBodySize()
{
	gallery_lockedBodySize = getDocumentBodySize();
}

function gallery_unlockBodySize()
{
	gallery_lockedBodySize = null;
}

function getDocumentBodySize()
{
	if(gallery_lockedBodySize)
	{
		return(gallery_lockedBodySize);
	}

	var fullWidth = null;
	var fullHeight = null;

	try
	{
		if(self.innerWidth)
		{
			fullWidth = self.innerWidth;
			fullHeight = self.innerHeight;
		}
		else if(document.documentElement && document.documentElement.clientWidth)
		{
			fullWidth = document.documentElement.clientWidth;
			fullHeight = document.documentElement.clientHeight;
		}
		else if(document.body.clientWidth)
		{
			fullWidth = document.body.clientWidth;
			fullHeight = document.body.clientHeight;
		}
		else
		{
			var measure = document.getElementById('windowmeasure');

			if(!measure)
			{
				measure = document.createElement('div');
				measure.style.position = 'absolute';
				measure.style.top = '0px';
				measure.style.left = '0px';
				measure.style.height = '100%';
				measure.style.width = '100%';
				document.body.appendChild(measure);
			}

			var coords  = ToolMan.coordinates().topLeftOffset(measure);

			fullWidth = measure.offsetWidth;
			fullHeight = measure.offsetHeight;
		}
	}
	catch(ignored)
	{
		// no-op
	}

	var result  = new Object();
	result.x = fullWidth;
	result.y = fullHeight;

	return(result);
}

/*var __dl_gallery_savedScrollPos;

function getScrollingPosition()
{
	var position = [0, 0];

	if(typeof window.pageYOffset != 'undefined')
	{
		position = [
		window.pageXOffset,
		window.pageYOffset
		];
	}
	else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
	{
		position = [
		document.documentElement.scrollLeft,
		document.documentElement.scrollTop
		];
	}
	else if(typeof document.body.scrollTop != 'undefined')
	{
		position = [
		document.body.scrollLeft,
		document.body.scrollTop
		];
	}

	return position;
}

function setScrollingPosition(position)
{
	try
	{
		window.pageXOffset = position[0];
		window.pageYOffset = position[1];
	}
	catch(ignored)
	{
		// no-op
	}
	try
	{
		document.documentElement.scrollLeft = position[0];
		document.documentElement.scrollTop = position[1];
	}
	catch(ignored)
	{
		// no-op
	}
	try
	{
		document.body.scrollLeft = position[0];
		document.body.scrollTop = position[1];
	}
	catch(ignored)
	{
		// no-op
	}
}

*/
/*function galleryimage_mouseover(obj)
{
	obj.className = obj.className.replace(/\bgalleryhover\b/,' ').replace(/[ ]+$/,'') + ' galleryhover';
}

function galleryimage_mouseout(obj)
{
	obj.className = obj.className.replace(/\bgalleryhover\b/,' ').replace(/[ ]+$/,'');
}

function xmlrpc_createRequest()
{
	var	req;
	var	ok;

	req = false;
	ok = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
			ok = true;
		}
		catch(e)
		{
			req = false;
			ok = false;
		}
	}
	if(!ok)
	{
		// branch for IE/Windows ActiveX version
		if(window.ActiveXObject)
		{
			try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					req = false;
				}
			}
		}
	}

	return(req);
}

function xmlrpcgallery_loadImage(url)
{
	try
	{
		var	req	= xmlrpc_createRequest();

		if(req)
		{
			var obj;

			__dl_gallery_savedScrollPos = getScrollingPosition();

			obj = document.getElementById('gallerycontent');
			obj.style.display = 'none';

			obj = document.createElement('div');
			obj.id = 'gallerypagepopup';
			obj.style.position = 'absolute';
			obj.style.zIndex = 2000;
			obj.style.top = '25px';
			obj.style.left = '0px';
			obj.style.width = '100%';
			obj.style.textAlign = 'center';
			obj.style.verticalAlign = 'middle';
			obj.appendChild(document.createTextNode('Loading...'));
			document.body.appendChild(obj);

			setScrollingPosition(new Array(0,top));

			req.onreadystatechange = xmlrpcgallery_createDisplayImage(req);
			req.open("GET", url + '&xmlrpc=1', true);
			req.send('');

			return(true);
		}
		else
		{
			return(false);
		}
	}
	catch(ex)
	{
		return(false);
	}
}

function xmlrpcgallery_createDisplayImage(req)
{
	var	theReq	= req;

	req = null;

	return(function()
	{
		xmlrpcgallery_displayImage(theReq);
	});
}

function xmlrpcgallery_displayImage(req)
{
	if(req.readyState == 4)
	{
		var	obj	= document.getElementById('gallerypagepopup');

		if(obj)
		{
			obj.innerHTML = req.responseText;
		}
	}
}

function xmlrpcgallery_closeImage()
{
	var	obj	= document.getElementById('galleryimagecontainer');

	slideshowStopXMLRPC();

	if(obj)
	{
		try
		{
			obj.parentNode.removeChild(obj);
			delete obj;
		}
		catch(ignored)
		{
			// no-op
		}
		obj = null;
	}

	obj = document.getElementById('gallerypagepopup');
	if(obj)
	{
		try
		{
			obj.parentNode.removeChild(obj);
			delete obj;
		}
		catch(ignored)
		{
			// no-op
		}
		obj = null;
	}

	obj = document.getElementById('gallerycontent');
	obj.style.display = '';

	setScrollingPosition(__dl_gallery_savedScrollPos);
}

var	slideshowTimerSecs;
var	slideshowStopFlag;
var	slideshowPauseFlag;
var slideshowIsAllXMLRPC = false;
var slideshowOn = false;

function slideshowTimerStart()
{
	var	obj	= document.getElementById('slideshow');

	slideshowTimerSecs = 6;
	slideshowPauseFlag = false;
	slideshowStopFlag = false;

	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.firstChild);
	}

	obj.appendChild(document.createTextNode('Next picture in ' + slideshowTimerSecs + '...'));

	addSlideshowTimerTick(1000);
}

function slideshowTimerTick()
{
	if(slideshowPauseFlag || slideshowStopFlag)
	{
		return;
	}

	var	obj	= document.getElementById('slideshow');

	slideshowTimerSecs = slideshowTimerSecs - 1;

	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.firstChild);
	}

	if(slideshowTimerSecs > 0)
	{
		obj.appendChild(document.createTextNode('Next picture in ' + slideshowTimerSecs + '...'));

		addSlideshowTimerTick(1000);
	}
	else
	{
		obj.appendChild(document.createTextNode('Loading next picture...'));

		if(slideshowIsAllXMLRPC)
		{
			galleryXMLRPCNext();
		}
		else if(obj.getAttribute('urltoload'))
		{
			showLoadingMessage();
			document.location = obj.getAttribute('urltoload');
		}
	}
}

function slideshowPlay(clickedObj)
{
	var	obj;

	slideshowPauseFlag = false;

	obj	= document.getElementById('slideshow');

	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.firstChild);
	}

	slideshowTimerSecs = slideshowTimerSecs + 1;

	obj.appendChild(document.createTextNode('Playing...'));

	obj = document.getElementById('slideshow-play');
	obj.style.display = 'none';

	obj = document.getElementById('slideshow-pause');
	obj.style.display = '';

	obj = document.getElementById('slideshow-stop');
	obj.style.display = '';
	addSlideshowTimerTick(100);
}

function slideshowPause(clickedObj)
{
	var	obj;

	slideshowPauseFlag = true;

	obj	= document.getElementById('slideshow');

	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.firstChild);
	}

	obj.appendChild(document.createTextNode('Paused'));

	obj = document.getElementById('slideshow-play');
	obj.style.display = '';

	obj = document.getElementById('slideshow-pause');
	obj.style.display = 'none';

	obj = document.getElementById('slideshow-stop');
	obj.style.display = '';
}

function slideshowStop(clickedObj)
{
	var	obj;

	slideshowStopFlag = true;
	if(clickedObj.getAttribute('urltoload'))
	{
		showLoadingMessage();
		document.location = clickedObj.getAttribute('urltoload');
	}

	obj	= document.getElementById('slideshow');

	while(obj.hasChildNodes())
	{
		obj.removeChild(obj.firstChild);
	}

	obj.appendChild(document.createTextNode('Stopped'));

	obj = document.getElementById('slideshow-play');
	obj.style.display = 'none';

	obj = document.getElementById('slideshow-pause');
	obj.style.display = 'none';

	obj = document.getElementById('slideshow-stop');
	obj.style.display = 'none';
}

function hideLoadingMessage()
{
	document.getElementById('gallery-picture-loading').style.visibility='hidden';
}

function showLoadingMessage()
{
	var	pic;
	var	loading;

	loading = document.getElementById('gallery-picture-loading');
	if(loading)
	{
		pic = document.getElementById('gallery-display-object');
		if(pic)
		{
			if(pic.width)
			{
				loading.style.width = (pic.width-4) + 'px';
			}
			if(pic.height)
			{
				loading.style.height = (pic.height-4) + 'px';

				var	obj	= document.getElementById('gallery-picture-container');

				if(obj)
				{
					obj.style.marginTop = '-' + (pic.height) + 'px';
				}
			}
		}

		loading.style.visibility='visible';
	}
}

var galleryDuringImageLoad = false;

function galleryBeginImageLoad()
{
	galleryDuringImageLoad = true;

	var	pic;

	pic = document.getElementById('gallery-display-object');
	if(pic)
	{
		pic.height = '';
		pic.width = '';
	}

	window.setTimeout(galleryImageLoadTick,500);
}

function galleryImageLoadTick()
{
	var	ok = false;
	var	pic;
	var	loading;

	loading = document.getElementById('gallery-picture-loading');
	if(loading && loading.style.visibility != 'hidden')
	{
		ok = true;
		pic = document.getElementById('gallery-display-object');
		if(pic)
		{
			if(pic.width)
			{
				loading.style.width = (pic.width-4) + 'px';
			}
			if(pic.height)
			{
				loading.style.height = (pic.height-4) + 'px';

				var	obj	= document.getElementById('gallery-picture-container');

				if(obj)
				{
					obj.style.marginTop = '-' + (pic.height) + 'px';
				}
			}
		}
	}

	if(ok && galleryDuringImageLoad)
	{
		window.setTimeout(galleryImageLoadTick,500);
	}
}

function galleryEndImageLoad()
{
	galleryDuringImageLoad = false;

	galleryResizeImage();

	if(slideshowOn)
	{
		slideshowTimerStart();
	}
}

function galleryXMLRPCPrev()
{
	galleryXMLRPCNextPrev('prev');
}

function galleryXMLRPCNext()
{
	galleryXMLRPCNextPrev('next');
}

function galleryXMLRPCNextPrev(type)
{
	var	obj	= document.getElementById('gallery-movie');

	if(obj)
	{
		obj.parentNode.removeChild(obj);
	}

	obj = document.getElementById('gallery-display-object');
	if(obj)
	{
		obj.style.display = '';
	}

	var	req	= xmlrpc_createRequest();

	req.onreadystatechange = xmlrpcgallery_createShowImg(req);
	req.open("GET", galleryGetImageURL(type), true);
	req.send('');
}

function xmlrpcgallery_createShowImg(req)
{
	var	theReq	= req;

	req = null;

	return(function()
	{
		xmlrpcgallery_showImage(theReq);
	});
}

function xmlrpcgallery_showImage(req)
{
	if(req.readyState == 4)
	{
		var	details	= req.responseText;

		if(details)
		{
			var	image;
			var	imageName;
			var	imageExt;

			image = details.split('~');
			imageName = image[1];
			imageExt = image[2];
			image = image[0];

			var	gallery	= document.getElementById('xmlrpc-gallery-current-gallery').value;
			var	url		= document.getElementById('xmlrpc-gallery-imageurlroot').value;

			if(image)
			{
				var	obj	= document.getElementById('gallery-display-object');

				if(imageExt == 'picture')
				{
					url = url + '?gallery=' + escape(gallery) + '&image=' + escape(image) + '&size=800';
				}
				else
				{
					url = url + '?gallery=' + escape(gallery) + '&image=' + escape(image) + '&size=128';
				}

				if(obj)
				{
					showLoadingMessage();
					document.getElementById('xmlrpc-gallery-current-image').value = image;
					document.getElementById('gallery-item-name').innerHTML = '';
					document.getElementById('gallery-item-name').appendChild(document.createTextNode(imageName));

					galleryDuringImageLoad = true;

					try
					{
						obj.height = '';
						obj.width = '';
					}
					catch(ignored)
					{
						// no-op
					}

					try
					{
						obj.height = 'auto';
						obj.width = 'auto';
					}
					catch(ignored)
					{
						// no-op
					}

					try
					{
						obj.removeAttribute('height');
						obj.removeAttribute('width');
					}
					catch(ignored)
					{
						// no-op
					}

					window.setTimeout(galleryImageLoadTick,500);

					obj.src = url;

					if(imageExt == 'movie')
					{
						var	objTag;
						var	paramTag;
						var	aTag;
						var	tmpURL;
						var	containerTag;

						objTag = document.getElementById('gallery-movie');
						if(objTag)
						{
							objTag.parentNode.removeChild(objTag);
						}

						objTag = document.createElement('object');

						tmpURL = url;
						tmpURL = tmpURL.replace('image.php','movie.php');

						objTag.id = 'gallery-movie';
						objTag.className = 'gallery-movie';
						objTag.type = 'video/x-msvideo';
						objTag.width = 300;
						objTag.height = 300;
						objTag.data = tmpURL;

						try
						{
							paramTag = document.createElement('param');
							paramTag.name = 'src';
							paramTag.value = tmpURL;

							objTag.appendChild(paramTag);

							containerTag = document.createElement('span');
							containerTag.appendChild(document.createTextNode('Click here to play/download the movie: '));

							aTag = document.createElement('a');
							aTag.href = tmpURL;
							aTag.appendChild(document.createTextNode(imageName));

							containerTag.appendChild(aTag);
							objTag.appendChild(containerTag);
						}
						catch(ex)
						{
							objTag = document.createElement('span');
							objTag.id = 'gallery-movie';
							objTag.innerHTML = '<object class="gallery-movie" type="video/x-msvideo" width="300" height="300" data="' + tmpURL + '"><param name="src" value="' + tmpURL + '" />Click here to play/download the movie: <a href="' + tmpURL + '">' + imageName + '</a></object>';
						}

						if(obj.nextSibling)
						{
							obj.parentNode.insertBefore(objTag,obj.nextSibling);
						}
						else
						{
							obj.parentNode.appendChild(objTag);
						}

						obj.style.display = 'none';
					}

					if(imageExt != 'picture')
					{
						if(slideshowOn)
						{
							slideshowStopXMLRPC();
						}
					}
				}
			}
		}
	}
}

function galleryGetImageURL(type)
{
	var	gallery	= document.getElementById('xmlrpc-gallery-current-gallery').value;
	var	image	= document.getElementById('xmlrpc-gallery-current-image').value;
	var	url		= document.getElementById('xmlrpc-gallery-urlroot').value;

	url = url + '?gallery=' + escape(gallery) + '&image=' + escape(image) + '&type=' + escape(type);

	return(url);
}

function galleryXMLRPCStartSlideshow()
{
	document.getElementById('gallery-slideshow').style.display = 'none';

	document.getElementById('slideshow').style.display = '';
	document.getElementById('slideshow-controls').style.display = '';

	slideshowIsAllXMLRPC = true;
	slideshowOn = true;
	slideshowStopFlag = false;
	slideshowPauseFlag = false;

	slideshowTimerStart();

	document.getElementById('slideshow-play').style.display = 'none';
	document.getElementById('slideshow-pause').style.display = '';
	document.getElementById('slideshow-stop').style.display = '';
}

function slideshowPlayXMLRPC()
{
	document.getElementById('slideshow-play').style.display = 'none';
	document.getElementById('slideshow-pause').style.display = '';
	document.getElementById('slideshow-stop').style.display = '';

	slideshowPlay();
}

function slideshowPauseXMLRPC()
{
	document.getElementById('slideshow-play').style.display = '';
	document.getElementById('slideshow-pause').style.display = 'none';
	document.getElementById('slideshow-stop').style.display = '';

	slideshowPause();
}

function slideshowStopXMLRPC()
{
	if(slideshowOn)
	{
		document.getElementById('gallery-slideshow').style.display = '';

		document.getElementById('slideshow').style.display = 'none';
		document.getElementById('slideshow-controls').style.display = 'none';

		slideshowStopFlag = true;
		slideshowPauseFlag = true;
		slideshowOn = false;
	}
}

var hasSlideshowTimerTick = false;

function addSlideshowTimerTick()
{
	if(!hasSlideshowTimerTick)
	{
		hasSlideshowTimerTick = true;
		window.setTimeout(doSlideshowTimerTick,1000);
	}
}

function doSlideshowTimerTick()
{
	hasSlideshowTimerTick = false;
	slideshowTimerTick();
}

function galleryResizeImage()
{
	var	imgObj	= document.getElementById('gallery-display-object');

	if(imgObj)
	{
		if(screen)
		{
			var	availWidth	= screen.width;
			var	availHeight	= screen.height;
			var	picWidth	= imgObj.width;
			var	picHeight	= imgObj.height;

			if(availWidth && availHeight && picWidth && picHeight)
			{
				availWidth = Math.floor(availWidth * 0.8);
				availHeight = Math.floor((availHeight - 200) * 0.8);

				if(picWidth > picHeight)
				{
					if(picWidth > availWidth)
					{
						imgObj.width = availWidth;
					}
					else if(picHeight > availHeight)
					{
						imgObj.height = availHeight;
					}
				}
				else
				{
					if(picHeight > availHeight)
					{
						imgObj.height = availHeight;
					}
					else if(picWidth > availWidth)
					{
						imgObj.width = availWidth;
					}
				}
			}
		}
	}
}*/

