var requestedUrl = window.location.href;
if(requestedUrl.indexOf("garyglass.smugmug.com") > -1)
{
	requestedUrl = requestedUrl.replace("garyglass.smugmug.com", "shutterglass.com");
	window.location.replace(requestedUrl);
}

var cachedImages = new Array();

var thumbsManager;

var vanityTable =
{
	arizona       : "http://www.shutterglass.com/gallery/3102146",
	assateague    : "http://www.shutterglass.com/gallery/839364",
	attic         : "http://www.shutterglass.com/gallery/2200673",
	bullring      : "http://www.shutterglass.com/gallery/4191493",
	blog          : "http://www.shutterglass.com/gallery/3351490",
	canyon        : "http://www.shutterglass.com/gallery/1895195",
	cicada        : "http://www.shutterglass.com/gallery/839485",
	enlightenment : "http://www.shutterglass.com/gallery/838809",
	fishmarket    : "http://www.shutterglass.com/gallery/849119",
	forestfire    : "http://www.shutterglass.com/gallery/2592967",
	hockey        : "http://www.shutterglass.com/gallery/1065052",
	hotel         : "http://www.shutterglass.com/gallery/2066860",
	oldhotel      : "http://www.shutterglass.com/gallery/2066860",
	japan         : "http://www.shutterglass.com/gallery/860953",
	merchandise   : "http://www.shutterglass.com/gallery/1080943",
	paintedrocks  : "http://www.shutterglass.com/gallery/2908748",
	// rachel        : "http://www.shutterglass.com/gallery/1065782",
	// rachel1       : "http://www.shutterglass.com/gallery/1065782",
	// rachel2       : "http://www.shutterglass.com/gallery/840227",
	// rachel3       : "http://www.shutterglass.com/gallery/1583802",
	skyride       : "http://www.shutterglass.com/gallery/1069084",
	tangier       : "http://www.shutterglass.com/gallery/838452",
	testing       : "http://www.shutterglass.com/gallery/1119615",
	wintercorn    : "http://www.shutterglass.com/gallery/2574507",
	wintertrees   : "http://www.shutterglass.com/gallery/1091205",
	wintertrees1  : "http://www.shutterglass.com/gallery/1091205",
	wintertrees2  : "http://www.shutterglass.com/gallery/1121818"
};

function DoOnload()
{
	CheckRedirects();
	ContextualizeTitle();

	// precache slideshow rollover image
	var newImage = new Image();
	cachedImages[cachedImages.length] = newImage;
	newImage.src = "http://www.shutterglass.com/photos/50291873-L.gif";

	//FixIeBug();
	
	// start featured images thumbs
	var speed = 2000;
	if (typeof(MakeFeaturedImages) != "undefined")
	{
		var featuredImages = MakeFeaturedImages();
		var thumbContainers = MakeThumbContainers();
		this.thumbsManager = new ThumbsManager(speed, featuredImages, thumbContainers);
		this.thumbsManager.Start();
	}
}

function MakeThumbContainers()
{
	var thumbContainers = new Array();
	thumbContainers[thumbContainers.length] = new ThumbContainer("gg_thumb1", "gg_thumb1link");
	thumbContainers[thumbContainers.length] = new ThumbContainer("gg_thumb2", "gg_thumb2link");
	thumbContainers[thumbContainers.length] = new ThumbContainer("gg_thumb3", "gg_thumb3link");
	return thumbContainers;
}

/*
--------------------------------------------------
	customize page title settings
--------------------------------------------------
*/
var titleSettings = {
	separator    : ": ",    // Text to insert between parts of title.
	maxLength    : -1,      // Limits length of title (-1 == no limit).
	doPhotos     : true,    // true == append photo captions
	doAlbums     : true,    // true == append album names
	doGalleries  : true,    // true == append gallery names
	stripSmugmug : true,    // true == remove " - powered by SmugMug" from title bar text
	inverse      : false,   // true == reverse order of home/gallery/album/photo
	siteTitle    : null     // null == use normal site title. "" == suppress site title. "Any Value" == replaces normal site title.
};

function ContextualizeTitle ()
{
	var pieces = new TitlePieces();
	var newTitle = "";
	if (titleSettings.inverse)
	{
		newTitle = MakeTitleBackward();
	}
	else
	{
		newTitle = MakeTitleForward();
	}
	if (titleSettings.maxLength > -1)
	{
		newTitle = newTitle.substr(0, titleSettings.maxLength);
	}
	if (titleSettings.stripSmugmug == false)
	{
		newTitle += " - powered by SmugMug";
	}
	document.title = newTitle;
	
	// METHODS
	
	function MakeTitleBackward ()
	{
		var title = "";
		title += pieces.Photo
		if (title.length > 0
			&& pieces.Album.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Album;
		if (title.length > 0
			&& pieces.Gallery.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Gallery;
		if (title.length > 0
			&& pieces.Main.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Main;
		return title;
	}
	
	function MakeTitleForward ()
	{
		var title = "";
		title += pieces.Main;
		if (title.length > 0
			&& pieces.Gallery.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Gallery;
		if (title.length > 0
			&& pieces.Album.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Album;
		if (title.length > 0
			&& pieces.Photo.length > 0)
		{
			title += titleSettings.separator;
		}
		title += pieces.Photo
		return title;
	}
	
	// CLASSES
	
	function TitlePieces ()
	{
		this.Main = GetMainTitle();
		this.Gallery = GetGalleryTitle();
		this.Album = GetAlbumTitle();
		this.Photo = GetPhotoTitle();
	    
		function GetMainTitle ()
		{
			var value = titleSettings.siteTitle;
			if (value == null)
			{
				var index = document.title.indexOf("- powered by SmugMug");
				value = document.title.substr(0, index);
			}
			return value;
		}
		 
		function GetGalleryTitle ()
		{
			var value = "";
			if (titleSettings.doGalleries)
			{
				var element = document.getElementById("galleryTitle");
				if (element)
				{
					value = GetTextContent(element);
					if (value.length > 0)
					{
						 // remove " galleries" from title
						var index = value.indexOf(" galleries");
						if (index > -1)
						{
							value = value.substr(0, index);
						}
					}
				}
			}
			return value;
		}
		
		function GetAlbumTitle ()
		{
			var value = "";
			if (titleSettings.doAlbums)
			{
				var element = document.getElementById("albumTitle");
				if (element)
				{
					value = GetTextContent(element);
				}
			}
			return value;
		}
		
		function GetPhotoTitle ()
		{
			var value = "";
			if (titleSettings.doPhotos)
			{
				var element = document.getElementById("caption_bottom");
				if (!element)
				{
					element = document.getElementById("caption_top");
				}
				if (element)
				{
					value = GetTextContent(element);
				}
			}
			return value;
		}
		
	}
	
}

function GetTextContent(node)
{
	var text = "";
	if(node)
	{
		if(node.children)
		{
			text = GetTextContent(node.firstChild);
		}
		else
		{
			if(node.nodeValue)
			{
				text = node.nodeValue; // IE
			}
			else
			{
				text = node.textContent; // mozilla
			}
		}
	}
	text = Trim(text);
	return text;
}

function InsertString(text, insertText, index)
{
	var newText = "";
	if(index > -1 && index < text.length)
	{
		newText = text.substring(0, index) + insertText + text.substring(index);
	}
	else
	{
		newText = text + insertText;
	}
	return newText;
}

function Trim(text)
{
	var regexp = /^\s+|\s+$/g;
	text = text.replace(regexp, "");
	return text;
}

function IsHomePage()
{
	var isHomePage = false;
	// test for the "homepage" class name in the <BODY> tag
	var classStr = document.body.className;
	if (classStr && (classStr.indexOf("homepage") != -1))
	{
		isHomePage = true;
	}
	return isHomePage;
}
 
function CheckRedirects()
{
	if(IsHomePage())
	{
		// get the path from the current URL, 
		// convert it to lowercase and remove the leading slash
		var path = window.location.pathname.toLowerCase().substr(1);
		var newURL = vanityTable[path];
		// if we found it in the table && newURL is different than where we are
		if (newURL && (newURL != window.location))
		{
			 window.location.replace(newURL);	    // go to the new URL
		}
	}
}
 
function FixIeBug()
{
	if(navigator.appName.indexOf("Internet Explorer") > -1)
	{
		FixDiv("albumNav_top");
		FixDiv("albumNav_bottom");
	}
}

function FixDiv(name)
{
 	var albumNav = document.getElementById(name);
 	if(albumNav !== null)
 	{
 		var pageNav = FindPageNav(albumNav);
 		if(pageNav !== null && pageNav.style !== null)
 		{
 			pageNav.style.position = "relative";
 		}
 	}
}

function FindPageNav(elem)
{
	var pageNav = null;
	if(elem.className.indexOf("pageNav") > -1)
	{
		return elem;
	}
	else
	{
 		for(var i = 0; i < elem.children.length; i++)
 		{
 			var childElem = elem.children[i];
 			pageNav = FindPageNav(childElem);
 			if(pageNav !== null)
 			{
 				return pageNav;
 			}
 		}
	}
}

function MakeSubscribeLink()
{
	var v2="DE382H638BCM8ZB";
	var v7=unescape("%23%24AAr%25UPM./cW%28%25");
	var v5=v2.length;
	var v1="";
	for(var v4=0;v4<v5;v4++)
	{
		v1+=String.fromCharCode(v2.charCodeAt(v4)^v7.charCodeAt(v4));
	}
	document.write('<a href="javascript:void(0)" onclick="window.location=\'mail\u0074o\u003a'+v1+'?subject=Subscribe'+'\'">'+'click here</a>');
}

function addPhotoTools() 
{
	if (!YD.hasClass(document.body,'loggedIn'))
	{
		return;
	}
	if (!YD.hasClass(document.body,'traditional') 
		&& !YD.hasClass(document.body,'allthumbs') 
		&& !YD.hasClass(document.body,'journal'))
	{
		return;
	}
	re = /photos\/(\d+)/;
	oPhotos = YD.getElementsByClassName('photo', 'div');
	for (i=0; i < oPhotos.length;i++) 
	{
		re.exec(oPhotos[i].innerHTML);
		oID = RegExp.$1;
		oEl = document.createElement('div');
		oEl.innerHTML = "<form method=\"get\" name=\"image_tools" 
			+ oID 
			+ "\" action=\"/photos/tools.mg\"><input type=\"hidden\" name=\"ImageID\" value=\""
			+ oID 
			+ "\"><input type=\"hidden\" name=\"Type\" value=\"Album\"><select name=\"tool\" onChange=\"document.image_tools" 
			+ oID 
			+ ".submit();\">"
			+ "<option value=\"\">&lt; photo tools &gt;</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"rotatebulk\">Rotate Photos</option>"
			+ "<option value=\"newcrop\">Crop Photo</option>"
			+ "<option value=\"newthumb\">Zoom Thumbnail</option>option value=\"color\">Color Effects</option>"
			+ "<option value=\"colorbulk\">Color Effects (Bulk)</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"highlight\">Feature Photo</option>"
			+ "<option value=\"unhighlight\">Remove Featured Photo</option>"
			+ "<option value=\"thisisme\">This Is Me! (Bio Photo)</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"caption\">Edit this Caption/Keywords</option>"
			+ "<option value=\"bulkcaption\">Edit Captions/Keywords (Bulk)</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"coordinates\">Edit Geography</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"sort\">Sort Gallery</option>"
			+ "<option value=\"move\">Arrange this Photo</option>"
			+ "<option value=\"dragndrop\">Drag &amp; Drop Photos</option>"
			+ "<option value=\"newnewbulkmove\">Arrange Photos</option>"
			+ "<option value=\"bulkmove\">Arrange Photos (Expert)</option>"
			+ "<option value=\"splitbulk\">Move Photos</option>"
			+ "<option value=\"bulkmovegallery\">Move Photos (Expert)</option>"
			+ "<option>-----------------------</option>"
			+ "<option value=\"makecopy\">Make 2nd Copy</option>"
			+ "<option value=\"replace\">Replace Photo</option>"
			+ "<option value=\"delete\">Delete this Photo</option>"
			+ "<option value=\"newbulkdelete\">Delete Photos (Bulk)</option></select></form>";
		oPhotos[i].appendChild(oEl);
	}
}
YE.addListener(window, "load", addPhotoTools);

// resize journals -- BEGIN HERE

/*
	PHOTOSIZERCLASS resizes images in a journal gallery.

	Version: 1.1
	Author:  Gary Glass, based on excellent work by rutt and dmc.
                 David Parry (2008-02-08), modified code to support SmugMug security changes

	HOW TO

	1. Copy the PhotoSizerClass into the javascript on your site page.
	2. Also copy the three methods: isJournalPage, hasClass, ResizeJournal.
	3. Within a script tag in your custom footer copy the statement that
		executes the code: ResizeJournal();

	The PhotoSizer will search for and apply sizing rules that you specify.
	The rule always has the same form: "Journal<width>x<height>Rule"
	<width> and <height> are the maximum width and height for an image.
	The sizer will select among the standard image sizes
	(see: http://friend.smugmug.com/help/display-quality )
	provided by SmugMug, and use the one that does not exceed in either
	dimension the maximum size specified by your rule.

	There are three ways to apply sizing rules.
	1. With a CSS selector.
	2. Inserting into the gallery description.
	3. Inserting into a photo caption.

	Individual photo rules take precedence over gallery rules.
	Gallery rules take precedence over CSS rules.

	If you specify more than one rule in CSS, the first one found is the one
	that will be used at the CSS level. Likewise for the other levels, the
	first one found is the one that is applied at that level.

	EXAMPLE

	Suppose you wanted to make a general rule that journal images should scale
	up to 800 pixels wide and 600 pixels high. This is the rule:

	Journal800x600Rule

	To apply it site-wide (to all journal galleries in your site) insert into
	your CSS like this:

		#Journal800x600Rule { }

	(It's just a selector. No body is required.)

	Now suppose in a particular gallery you want to override with a 600 x 600
	rule. To do that, insert this HTML into the description for that gallery:

		<span name="Journal600x600Rule"></span>

	Finally to override the rule for one particular image, insert the same
	sort of tag into the image caption.

	MORE INFO

	See discussion here:	http://www.dgrin.com/showthread.php?p=675298
*/
function PhotoSizerClass(standardSizes)
{
	/*
		properties
	*/
	this.ruleStatementRegex = /Journal(\d+)[xX](\d+)Rule/; // matches a journal sizing rule
	this.limits = new SizeClass();
	if (standardSizes == null)
	{
		this.standardSizes = new StandardSizesClass();
	}
	else
	{
		this.standardSizes = standardSizes;
	}
	this.standardScalings = new StandardScalingsClass(this.standardSizes);

	/*
		public methods
	*/
	this.apply = Apply;

	/*
		private methods
	*/
	this.getGalleryRule = GetGalleryRule;
	this.getCssRule = GetCssRule;
	this.init = Init;
	this.resizeJournalEntry = ResizeJournalEntry;
	this.resizeImage = ResizeImage;

	/*
		-------------------------------
		Method Definitions
		-------------------------------
	*/

	/*
		Resize the gallery.
	*/
	function Apply()
	{
		this.init();
		var divs = document.getElementsByTagName('DIV');
		for (var i = 0; i < divs.length; i++)
		{
			if (divs[i].className == 'journal_entry')
			{
				this.resizeJournalEntry(divs[i]);
			}
		}
	}

	/*
		Finds global rules (rules applied via gallery description or stylesheet).
	*/
	function Init()
	{
		if (this.getGalleryRule() == false)
		{
			this.getCssRule();
		}
	}

	/*
		Look for a sizing rule in album description.
		Returns: A boolean value indicating whether a rule was found.
	*/
	function GetGalleryRule()
	{
		var foundRule = false;
		var description = document.getElementById('albumDescription');
		if (description)
		{
			if (this.ruleStatementRegex.test(description.innerHTML))
			{
				this.limits.set(RegExp.lastMatch);
				foundRule = true;
			}
		}
		return foundRule;
	}

	/*
		Search stylesheets for a journal sizer rule.
		The first rule found is used.
		Returns: A boolean value indicating whether a rule was found.
	*/
	function GetCssRule()
	{
		var found = false;
		for (var i = 1; i < document.styleSheets.length; i++)
		{
			if (found)
			{
				break;
			}
			var stylesheet = document.styleSheets[i];
			var rules = stylesheet.cssRules ? stylesheet.cssRules : (stylesheet.rules ? stylesheet.rules : stylesheet.css);
			for (var j = 0; j < rules.length; j++)
			{
				var cssRule = rules[j].selectorText;
				if (this.ruleStatementRegex.test(cssRule))
				{
					this.limits.set(RegExp.lastMatch);
					found = true;
					break;
				}
			}
		}
		return found;
	}

	/*
		Finds the journal images in an entry and resizes them.
	*/
	function ResizeJournalEntry(div)
	{
		var imgs = div.getElementsByTagName('IMG');
		//var re = /(http:\/\/[^\/]*\/photos\/(\d+)-)S((-\d+)?.jpg)/
		var re = /(http:\/\/.*\/photos\/(\d+)_(\w+)-)S((-\d+)?.jpg)/
		for (var i = 0; i < imgs.length; i++)
		{
			if (re.test(imgs[i].src))
			{
				this.resizeImage(imgs[i], RegExp.$2);
			}
		}
	}

	/*
		Gets the final rule for an image and applies it.
	*/
	function ResizeImage(img, imageNumber, prefix, suffix)
	{
		var finalRule = new FinalRuleClass(imageNumber, this.limits);
		var imageSizes = new ImageSizesClass(img, this.standardScalings);
		var size = "S";
		if (finalRule.limits.width >= imageSizes.x3large.width
			&& finalRule.limits.height >= imageSizes.x3large.height)
		{
			size = "X3L"; // go extra extra extra large
		}
		else if (finalRule.limits.width >= imageSizes.x2large.width
			&& finalRule.limits.height >= imageSizes.x2large.height)
		{
			size = "X2L"; // go extra extra large
		}
		else if (finalRule.limits.width >= imageSizes.xlarge.width
			&& finalRule.limits.height >= imageSizes.xlarge.height)
		{
			size = "XL"; // go extra large
		}
		else if (finalRule.limits.width >= imageSizes.large.width
			&& finalRule.limits.height >= imageSizes.large.height)
		{
			size = "L"; // go large
		}
		else if(finalRule.limits.width >= imageSizes.medium.width
			&& finalRule.limits.height >= imageSizes.medium.height)
		{
			size = "M"; // go medium
		}
		if (size != "S")
		{
			// resize
			switch (size)
			{
				case "M":
					img.width = imageSizes.medium.width;
					img.height = imageSizes.medium.height;
					break;
				case "L":
					img.width = imageSizes.large.width;
					img.height = imageSizes.large.height;
					break;
				case "XL":
					img.width = imageSizes.xlarge.width;
					img.height = imageSizes.xlarge.height;
					break;
				case "X2L":
					img.width = imageSizes.x2large.width;
					img.height = imageSizes.x2large.height;
					break;
				case "X3L":
					img.width = imageSizes.x3large.width;
					img.height = imageSizes.x3large.height;
					break;
			}
			// set new image source to matching size
			var re = /(http:\/\/.*\/photos\/(\d+)_(\w+)-)S((-\d+)?.jpg)/;
			img.src = RegExp.$1 + size + RegExp.$4;
		}
	}

	/*
		-------------------------------
		Inner Classes
		-------------------------------
	*/

	/*
		Defines a height & width dimension.
		If scaleFactor is provided, the specified width & height will be multiplied by it.
	*/
	function SizeClass(width, height, scaleFactor)
	{

		if (width == null)
		{
			this.width = -1;
		}
		else
		{
			this.width = width;
		}
		if (height == null)
		{
			this.height = -1;
		}
		else
		{
			this.height = height;
		}
		this.ruleStatementRegex = /Journal(\d+)[xX](\d+)Rule/; // matches a journal sizing rule
		this.set = Set;
		this.scale = Scale;
		if (scaleFactor != null)
		{
			this.scale(scaleFactor);
		}

		/*
			Sets dimensions from a rule statement.
		*/
		function Set(ruleStatement)
		{
			if (this.ruleStatementRegex.test(ruleStatement))
			{
				this.width = RegExp.$1;
				this.height = RegExp.$2;
			}
		}

		/*
			Scales the size by the specified factor.
		*/
		function Scale(factor)
		{
			this.width = Math.round(this.width * factor);
			this.height = Math.round(this.height * factor);
		}

	} // SizeClass

	/*
		Defines dimensions of standard image sizes.
	*/
	function StandardSizesClass()
	{

		this.small = new SizeClass(400, 300);
		this.medium = new SizeClass(600, 450);
		this.large = new SizeClass(800, 600);
		this.xlarge = new SizeClass(1024, 768);
		this.x2large = new SizeClass(1280, 960);
		this.x3large = new SizeClass(1600, 1200);

	} // StandardSizesClass

	/*
		Defines scaling factors for the members of a StandardSizesClass.
		standardSizes: a StandardSizesClass instance
	*/
	function StandardScalingsClass(standardSizes)
	{

		this.small = 1;
		this.medium = standardSizes.medium.width / standardSizes.small.width;
		this.large = standardSizes.large.width / standardSizes.small.width;
		this.xlarge = standardSizes.xlarge.width / standardSizes.small.width;
		this.x2large = standardSizes.x2large.width / standardSizes.small.width;
		this.x3large = standardSizes.x3large.width / standardSizes.small.width;

	} // StandardScalingsClass

	/*
		Defines the standard sizes for a specified image.
		img: the image to get sizes for
		scalings: a StandardScalingsClass instance.
	*/
	function ImageSizesClass(img, scalings)
	{

		this.small = new SizeClass(img.width, img.height, scalings.small);
		this.medium = new SizeClass(img.width, img.height, scalings.medium);
		this.large = new SizeClass(img.width, img.height, scalings.large);
		this.xlarge = new SizeClass(img.width, img.height, scalings.xlarge);
		this.x2large = new SizeClass(img.width, img.height, scalings.x2large);
		this.x3large = new SizeClass(img.width, img.height, scalings.x3large);

	} // ImageSizesClass

	/*
		The FinalRuleClass class defines the final rule to apply to an image.
		The final rule is either the global rule or an individual image rule.
		Image rules override global rules.
		The constructor requires a reference to the image, its number,
		and the PhotoSizerClass instance containing the global settings.
		imageNumber: the index of the image on the page (used to get the image
			caption and extract its rule, if any).
		limits: a SizeClass instance specifying the global rules.
	*/
	function FinalRuleClass(imageNumber, limits)
	{

		this.limits = new SizeClass(limits.width, limits.height); // copy global limits from specified PhotoSizer class
		var caption = document.getElementById('caption' + imageNumber);
		if (caption)
		{
			var ruleStatementRegex = /Journal(\d+)[xX](\d+)Rule/; // matches a journal sizing rule
			if (ruleStatementRegex.test(caption.innerHTML)) // look for image-specific rule
			{
				this.limits.set(RegExp.lastMatch); // override global limits
			}
		}

	} // FinalRuleClass

} // PhotoSizerClass

/*
	Returns true if the current page is a journal gallery.
*/
function isJournalPage()
{
	return hasClass("journal");
}

/*
	Returns true if the body of the current page has the specified CSS class applied.
*/
function hasClass(cssClass)
{
	var className = document.body.className;
	var re = new RegExp(cssClass + "( |$)")
	if (!className)
	{
		return false;
	}
	return re.test(className);
}

/*
	Resizes a journal gallery.
*/
function ResizeJournal()
{
	if (isJournalPage())
	{
		var scaler = new PhotoSizerClass();
		scaler.apply();
	}
}

// resize journals -- END HERE

// Bulk Zoom Thumbnail -- START HERE
/*==============================*/
/*== Bulk Zoom thumbnail v2.6 ==*/
/*==============================*/
var thumbnailArray = new Array();
var thumbnailCount;
var thumbnailItem;
var thumbnailEXIFArray = new Array();
var reqIncrement = 0;

YE.on(window, "load", addBulkThumbnail);

function addBulkThumbnail()
{
  if (YD.hasClass(document.body,"galleryPage") && YD.hasClass(document.body,"loggedIn"))
  {
    if (YD.hasClass(document.body,"traditional") || YD.hasClass(document.body,"allthumbs") ||  YD.hasClass(document.body,"smugmug"))
    {
      onStartBulk();

      divTag = YD.get("photoTools");

      YD.get("bulkSave").disabled = true;

      objSpan = document.createElement("span");
      objSpan.className = "foreground";
      divTag.appendChild(objSpan);

      objButton = document.createElement("input");
      objButton.type = "checkbox";
      objButton.id = "bulkzoom";
      objSpan.appendChild(objButton);
      YE.on(objButton, "click", toggleBulkZoom);

      objText = document.createTextNode(' bulk zoom thumbnail');
      objSpan.appendChild(objText);
    }
  }
}

function toggleBulkZoom()
{
  if (YD.get('bulkzoom').checked)
    processThumbnails();
  else
    window.location = window.location = (window.location.href).split('#')[0];
}

function processThumbnails()
{
  YD.get("ajaxWorking").innerHTML = "<strong><strong>Loading...</strong></strong>"
  toggleDIV('ajaxWorking','','block');

  YD.get("bulkzoom_toolbox").style.display = "";
  thumbnailEXIFArray = imageIDs.slice(0);
  getThumbnailEXIF();
}

function maskThumbnails(objThumbnail, j)
{
  re1 = /\/gallery\/\d+\/\d+\/(\d+)/;
  re2 = /javascript\:openLB\(([0-9]+),/;
  re3 = /#(\d+)/;

  divTag = YD.get("thumbnails") ? YD.get("thumbnails") : YD.get("photos");

  if (divTag)
  {
    divTags = YD.getElementsByClassName("photo", "div", divTag);

    for (i=0; i<divTags.length; i++)
    {
      if (re1.test(divTags[i].innerHTML))
        re1.exec(divTags[i].innerHTML);
      else
      {
        if (re2.test(divTags[i].innerHTML))
          re2.exec(divTags[i].innerHTML);
        else {
          if (re3.test(divTags[i].innerHTML))
            re3.exec(divTags[i].innerHTML);
          else
            continue;
        }
          
      }

      thumbnailId = RegExp.$1;
      aTags = divTags[i].getElementsByTagName("a");
      imgTags = aTags[0].getElementsByTagName("img");

      if (objThumbnail.thumbnailId == thumbnailId)
      {
        maskedDiv = document.createElement("div");
        maskedDiv.id = thumbnailId + "_masked";
        maskedDiv.className = j;
        maskedDiv.style.cssText = "position: relative; background: transparent url('" + webServer+"/photos/toolthumbs.mg?tool=cropmask&Size=Small&ImageID="+ thumbnailId + "') no-repeat; width: " + objThumbnail.imgWidth + "px; height: " + objThumbnail.imgHeight + "px; z-index: 1; margin:0px auto; text-align:left;";

        clearedDiv = document.createElement("div");
        clearedDiv.id = thumbnailId + "_cleared";
        clearedDiv.className = j;
        clearedDiv.style.cssText = "position: absolute; background: url('" + webServer+"/photos/"+ thumbnailId + "-S" + objThumbnail.modifier + ".jpg') no-repeat; width: " + objThumbnail.imgWidth + "px; height: " + objThumbnail.imgHeight + "px; z-index: 2; clip:rect(0px," + objThumbnail.imgWidth + "px," + objThumbnail.imgHeight + "px,0px);";

        maskedDiv.appendChild(clearedDiv);

        cropboxDiv = document.createElement("div");
        cropboxDiv.id = thumbnailId + "_cropbox";
        cropboxDiv.className = j;
        cropboxDiv.style.cssText = "position: absolute; left: 0px; top: 0px; width: " + (objThumbnail.imgWidth-4) + "px; height: " + (objThumbnail.imgHeight-4) + "px; border: solid 2px blue; z-index: 3; cursor: move";
        YE.on(cropboxDiv, "mousedown", testMouseBulk);
        clearedDiv.appendChild(cropboxDiv);

        ratioboxDiv = document.createElement("div");
        ratioboxDiv.id = thumbnailId + "_ratiobox";
        ratioboxDiv.className = j;
        ratioboxDiv.style.cssText = "position: absolute; bottom: 0px; right: 0px; width: 25px; height: 25px; border-top: solid 2px blue; border-left: solid 2px blue;  cursor: pointer; cursor: hand; z-index: 4;";
        YE.on(ratioboxDiv, "mousedown", testZoomBulk); 
        cropboxDiv.appendChild(ratioboxDiv);
          
        divTags[i].replaceChild(maskedDiv, aTags[0]);
        divTags[i].style.width = objThumbnail.imgWidth + "px";
        divTags[i].style.height = objThumbnail.imgHeight + "px";
      }
    }
  } 
}

function getThumbnailEXIF()
{
  var thumbnail = thumbnailEXIFArray.pop();

  if (thumbnail)
  {
    postArray = new Array();
    postArray['tool'] = 'lightBoxImage';
    postArray['ImageID'] = thumbnail;
    postArray['size'] = 'Original';
    postArray['pageType'] = pageType;
    postArray['pageTypeDetails'] = pageTypeDetails;
    postArray['siteUser'] = siteUser;
    ajax_query(processThumbnailEXIF, webServer+'/hack/RPC/gallery.mg', postArray, true);
    window.setTimeout(getThumbnailEXIF, 100);
  }
  else
  {
    toggleDIV('','ajaxWorking','block');

    if (YD.hasClass(document.body,"loggedIn"))
      YD.get("bulkSave").disabled = false;
  }

}

function processThumbnailEXIF(response)
{
  re = /\\\/photos\\\/(\d+)\-(O|L|M|S)(\-\d+)?\.jpg[\s\S]+,"newWidth":"(\d+)","newHeight":"(\d+)"/;
  re.exec(response);

  if (RegExp.$2 != "O")
  {
    toggleDIV('','ajaxWorking','block');
    alert("Error: bulk zoom thumbnail requires access to originals.\n\"" + RegExp.$1 + "\" \"" + RegExp.$2 + "\" \"" + RegExp.$4 + "\" \"" + RegExp.$5 + "\"");
    return;
  }
  sThumbnailId = RegExp.$1;
  sThumbnailModifier = RegExp.$3;
  imgWidth = parseInt(RegExp.$4);
  imgHeight = parseInt(RegExp.$5);

  temp = new Array();
  thumbnailArray[thumbnailArray.length] = temp;
  thumbnailArray[thumbnailArray.length-1].thumbnailId = sThumbnailId;
  thumbnailArray[thumbnailArray.length-1].modifier = sThumbnailModifier;

  if (imgWidth < imgHeight)
  {
    thumbnailArray[thumbnailArray.length-1].imgHeight = 300;
    thumbnailArray[thumbnailArray.length-1].imgWidth = Math.round((imgWidth / imgHeight) * 300);
  }
  else
  {
    thumbnailArray[thumbnailArray.length-1].imgWidth = 400;
    thumbnailArray[thumbnailArray.length-1].imgHeight = Math.round((imgHeight / imgWidth) * 400);
  }

  thumbnailArray[thumbnailArray.length-1].ratio = 0;
  thumbnailArray[thumbnailArray.length-1].invRatio = 0;
  thumbnailArray[thumbnailArray.length-1].marginTop = 0;
  thumbnailArray[thumbnailArray.length-1].marginLeft = 0;
  thumbnailArray[thumbnailArray.length-1].cropHeight = thumbnailArray[thumbnailArray.length-1].imgHeight;
  thumbnailArray[thumbnailArray.length-1].cropWidth = thumbnailArray[thumbnailArray.length-1].imgWidth;
  thumbnailArray[thumbnailArray.length-1].oRatio = (imgWidth / thumbnailArray[thumbnailArray.length-1].imgWidth);
  thumbnailArray[thumbnailArray.length-1].cropDiff = 0;

  maskThumbnails(thumbnailArray[thumbnailArray.length-1], thumbnailArray.length-1);
}

function zoomThumbnails()
{
  if (!thumbnailItem)
  {
    thumbnailItem = 0;
    thumbnailCount = thumbnailArray.length;
  }

  var thumbnail = thumbnailArray.pop();
  
  if (thumbnail)
  {
    thumbnailItem++; 

    postArray = new Array();
    postArray['tool'] = 'newthumb';
    postArray['ImageID'] = thumbnail.thumbnailId;
    postArray['action'] = "doit";
    postArray['crop_h'] = Math.round(parseInt(thumbnail.cropHeight) * parseFloat(thumbnail.oRatio));
    postArray['crop_w'] = Math.round(parseInt(thumbnail.cropWidth) * parseFloat(thumbnail.oRatio));
    postArray['crop_x'] = Math.round(parseInt(thumbnail.marginLeft) * parseFloat(thumbnail.oRatio));
    postArray['crop_y'] = Math.round(parseInt(thumbnail.marginTop) * parseFloat(thumbnail.oRatio));

    reqIncrement ++;
    updateAjaxStatus();
    window.setTimeout('ajax_query(thumbReqHandler, webServer+\'\/photos\/tools.mg\', postArray, true, true);', 10);
    window.setTimeout(zoomThumbnails, 50);
  } 
}


function thumbReqHandler() {
  updateAjaxStatus();

  reqIncrement--;

  if (thumbnailArray.length == 0 && reqIncrement == 0) {
      window.location = (window.location.href).split('#')[0];
  }
}

function updateAjaxStatus(sStr) {
  var ajx = YD.get("ajaxWorking");

  if (thumbnailArray.length == 0)
    ajx.innerHTML =  'Processing thumbnail ' + (thumbnailCount - reqIncrement + 1) + ' of ' + thumbnailCount;
  else
    ajx.innerHTML = 'Sending request ' + reqIncrement + ' of ' + thumbnailCount;


  if (sStr) {
    ajx.innerHTML = ajx.innerHTML + sStr;
  }

  toggleDIV('ajaxWorking','','block');
}

var smallestBulk = 75;   
var mouseXBulk = 0;
var mouseYBulk = 0;
var mouseToolBulk = "move";
var hideRatioBulk = false;    
var scaleBulk = 1;
var pageLoadedBulk = false;

function onStartBulk()
{
  pageLoadedBulk = true;
}        

function moveBulk(X,Y,e)
{
  if (pageLoadedBulk == true)
  {
    try
    {
      var objClass;

      if (e.srcElement)
        objClass = e.srcElement.className
      else
        objClass = e.target.className

      X = X*scaleBulk;
      Y = Y*scaleBulk;
      newLeft = thumbnailArray[objClass].marginLeft+X;
      newTop = thumbnailArray[objClass].marginTop+Y;                                
      stopX = thumbnailArray[objClass].imgWidth-thumbnailArray[objClass].cropWidth;
      stopY = thumbnailArray[objClass].imgHeight-thumbnailArray[objClass].cropHeight;

      if (newLeft < 0)
        newLeft = 0;

      if (newLeft > stopX)
        newLeft = stopX;

      if (newTop < 0)
        newTop = 0;

      if (newTop > stopY)
        newTop = stopY;

      thumbnailArray[objClass].marginLeft = newLeft;
      thumbnailArray[objClass].marginTop = newTop;
      if (e.srcElement)
        objNode = e.srcElement;
      else
        objNode = e.target;
      while (objNode.id != thumbnailArray[objClass].thumbnailId + "_cropbox")
        objNode = objNode.parentNode;

      updateCropBulk(objNode);
    }
    catch(e) {}
  }
  else
  {
    window.alert("Page loading, please wait.");
  }
}        

function zoomBulk(X,Y,e)
{
  if (pageLoadedBulk == true)
  {
    try
    {
      var objClass;

      if (e.srcElement)
        objClass = e.srcElement.className
      else
        objClass = e.target.className                

      if (thumbnailArray[objClass].ratio == 0)
      {
        Xadj = thumbnailArray[objClass].cropWidth+(X*scaleBulk);
        Yadj = thumbnailArray[objClass].cropHeight+(Y*scaleBulk);
      }
      else
      {
        Xadj = 0;
        Yadj = 0;
      }

      newWidth = thumbnailArray[objClass].cropWidth+(X*scaleBulk);
      newHeight = Math.round(newWidth*thumbnailArray[objClass].ratio)+Yadj;

      if (newWidth < smallestBulk)
      {
        newWidth = smallestBulk;
        newHeight = Math.round(newWidth*thumbnailArray[objClass].ratio)+Yadj;
      }

      if (newHeight < smallestBulk)
      {
        newHeight = smallestBulk;
        newWidth = Math.round(newHeight*thumbnailArray[objClass].invRatio)+Xadj;
      } 

      if (newWidth+thumbnailArray[objClass].marginLeft > thumbnailArray[objClass].imgWidth)
      {
        newWidth = thumbnailArray[objClass].imgWidth-thumbnailArray[objClass].marginLeft;
        newHeight = Math.round(newWidth*thumbnailArray[objClass].ratio)+Yadj;
      } 

      if (newHeight+thumbnailArray[objClass].marginTop > thumbnailArray[objClass].imgHeight)
      {
        newHeight = thumbnailArray[objClass].imgHeight-thumbnailArray[objClass].marginTop;
        newWidth = Math.round(newHeight*thumbnailArray[objClass].invRatio)+Xadj;
      }                

      thumbnailArray[objClass].cropWidth = newWidth;
      thumbnailArray[objClass].cropHeight = newHeight;
 
      if (e.srcElement)
        objNode = e.srcElement;
      else
        objNode = e.target;

      while (objNode.id != thumbnailArray[objClass].thumbnailId + "_cropbox")
        objNode = objNode.parentNode;

      updateCropBulk(objNode);
    }
    catch(e) {}
    
  }
  else
  {
    window.alert("Page loading, please wait.");
  }
}        

function changeRatioBulk(newRatios)
{
  if (pageLoadedBulk == true)
  {
    newRatio = newRatios.split(",");

    for (i=0; i<thumbnailArray.length; i++)
    {
      if (thumbnailArray[i].imgHeight > thumbnailArray[i].imgWidth)
      {
        thumbnailArray[i].ratio = newRatio[1];
        thumbnailArray[i].invRatio = newRatio[0];
      }
      else
      {
        thumbnailArray[i].ratio = newRatio[0];
        thumbnailArray[i].invRatio = newRatio[1];
      }

      if (thumbnailArray[i].ratio == 0)
      {
        YD.get('customRatio').style.display = "none";
        thumbnailArray[i].marginTop = 0;
        thumbnailArray[i].marginLeft = 0;
        thumbnailArray[i].cropHeight = thumbnailArray[i].imgHeight;
        thumbnailArray[i].cropWidth = thumbnailArray[i].imgWidth;
      }
      else
      {
        if (newRatios == "custom")
        {
          thumbnailArray[i].ratio = document.cropTool.ratioH.value/document.cropTool.ratioW.value;
          thumbnailArray[i].invRatio = document.cropTool.ratioW.value/document.cropTool.ratioH.value;
          YD.get('customRatio').style.display = "block";
        }
        else
          YD.get('customRatio').style.display = "none";

        thumbnailArray[i].cropWidth = Math.round(1*thumbnailArray[i].imgWidth);

        thumbnailArray[i].cropHeight = Math.round(thumbnailArray[i].cropWidth*thumbnailArray[i].ratio);        

        if ((thumbnailArray[i].cropHeight > thumbnailArray[i].imgHeight)||(thumbnailArray[i].cropWidth > thumbnailArray[i].imgWidth))
        {
          thumbnailArray[i].cropHeight = thumbnailArray[i].imgHeight;  
          thumbnailArray[i].cropWidth = Math.round(thumbnailArray[i].cropHeight*thumbnailArray[i].invRatio);
        }   

        thumbnailArray[i].marginTop = ((thumbnailArray[i].imgHeight-thumbnailArray[i].cropHeight)/2);
        thumbnailArray[i].marginLeft = ((thumbnailArray[i].imgWidth-thumbnailArray[i].cropWidth)/2);
      }

      updateCropBulk(YD.get(thumbnailArray[i].thumbnailId + "_cropbox"));
    }
  }
  else
  {
     window.alert("Page loading, please wait.");
  }
}        

function rotateBulk()
{
  if (pageLoadedBulk == true)
  {
    for (i=0; i<thumbnailArray.length; i++)
    {
      if (thumbnailArray[objClass].ratio == 0)
      {
        Xadj = thumbnailArray[i].cropHeight;
        Yadj = thumbnailArray[i].cropWidth;
      }
      else
      {
        Xadj = 0;
        Yadj = 0;
      }

      temp = thumbnailArray[objClass].invRatio;
      thumbnailArray[objClass].invRatio = thumbnailArray[objClass].ratio;
      thumbnailArray[objClass].ratio = temp;
      temp = thumbnailArray[i].cropWidth;
      thumbnailArray[i].cropWidth = thumbnailArray[i].cropHeight;
      thumbnailArray[i].cropHeight = temp;  

      if (thumbnailArray[i].cropWidth+thumbnailArray[i].marginLeft > thumbnailArray[i].imgWidth)
      {
        thumbnailArray[i].marginLeft = thumbnailArray[i].imgWidth-thumbnailArray[i].cropWidth;

        if (thumbnailArray[i].marginLeft < 0)
        {
          thumbnailArray[i].marginLeft = 0;
          thumbnailArray[i].cropWidth = thumbnailArray[i].imgWidth-thumbnailArray[i].marginLeft;
          thumbnailArray[i].cropHeight = Math.round(thumbnailArray[i].cropWidth*thumbnailArray[objClass].ratio)+Yadj;                        
        }
      } 

      if (thumbnailArray[i].cropHeight+thumbnailArray[i].marginTop > thumbnailArray[i].imgHeight)
      {
        thumbnailArray[i].marginTop = thumbnailArray[i].imgHeight-thumbnailArray[i].cropHeight;

        if (thumbnailArray[i].marginTop < 0)
        {
          thumbnailArray[i].marginTop = 0;
          thumbnailArray[i].cropHeight = thumbnailArray[i].imgHeight-thumbnailArray[i].marginTop;
          thumbnailArray[i].cropWidth = Math.round(thumbnailArray[i].cropHeight*thumbnailArray[objClass].invRatio)+Xadj;
        }
      }

      if (hideRatioBulk != true)
      {
        //temp = document.cropTool.ratioH.value
        //document.cropTool.ratioH.value = document.cropTool.ratioW.value;
        //document.cropTool.ratioW.value = temp;
      }

      updateCropBulk(YD.get(thumbnailArray[i].thumbnailId + "_cropbox"));
    }
  }
  else

    window.alert("Page loading, please wait.");
}        

function updateCropBulk(e)
{
  objClass = e.className;
  e.parentNode.style.clip = "rect("+thumbnailArray[objClass].marginTop+"px "+(thumbnailArray[objClass].marginLeft+thumbnailArray[objClass].cropWidth)+"px "+(thumbnailArray[objClass].marginTop+thumbnailArray[objClass].cropHeight)+"px "+thumbnailArray[objClass].marginLeft+"px)";
  e.style.top = thumbnailArray[objClass].marginTop+"px";
  e.style.left = thumbnailArray[objClass].marginLeft+"px";
  e.style.width = thumbnailArray[objClass].cropWidth-thumbnailArray[objClass].cropDiff+"px";
  e.style.height = thumbnailArray[objClass].cropHeight-thumbnailArray[objClass].cropDiff+"px";
}        

function mouseMoveBulk(e)
{
  if (!e) var e = window.event;

  var objClass

  if (e.srcElement)
      objClass = e.srcElement.className;
    else
      objClass = e.target.className;

  if (lookMouse)
  {
    tempX = mouseXBulk;
    tempY = mouseYBulk;
    mouseXBulk = e.clientX;
    mouseYBulk = e.clientY;
    x = mouseXBulk-tempX;
    y = mouseYBulk-tempY;

    if (mouseToolBulk == "zoom")
    {
      if (thumbnailArray[objClass].ratio == 0)
        zoomBulk(x,y,e);
      else
        zoomBulk(x,0,e);
    }
    else
      moveBulk(x,y,e)
  }
}

function testMouseBulk(e)
{
  state = true;
  if (!e) var e = window.event;

  if (mouseToolBulk != "zoom")
  {
    lookMouse = state;

    if (state == true)
    {
      mouseToolBulk = "move";
      mouseXBulk = e.clientX;
      mouseYBulk = e.clientY;
      document.onmousemove = mouseMoveBulk;
    }
  }
}

function testZoomBulk(e)
{
  state=true;
  if (!e) var e = window.event;

  lookMouse = state;
  if (state == true)
  {	            
    mouseToolBulk = "zoom"
    mouseXBulk = e.clientX;
    mouseYBulk = e.clientY;
    document.onmousemove = mouseMoveBulk;
  }
}

function cancelMouse()
{
  mouseToolBulk = "move";
  lookMouse = false;
}

document.onmouseup = cancelMouse;
// Bulk Zoom Thumbnail -- END HERE
