/*
--------------------------------------------------
	ContextualizeTitle 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.
};

/*
--------------------------------------------------
	ContextualizeTitle Class
--------------------------------------------------
*/
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;
		}

	}

} // end of ContextualTitle class

/*
--------------------------------------------------
	Utility Methods
--------------------------------------------------
*/

function GetPhotoCaption()
{
	var caption = "";
	var photoTitle = document.getElementById("caption_bottom");
	if(!photoTitle)
	{
		photoTitle = document.getElementById("caption_top");
	}
	if(photoTitle)
	{
		caption = GetTextContent(photoTitle);
	}
	return caption;
}

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 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;
}

/*=========================================*/
/*== Position Paypal -                                           ==*/
/*=========================================*/
YE.onContentReady("altViews", MovePaypalContent);
function MovePaypalContent()
{
    var paypalObj = document.getElementById("paypalButtonSet");
    var altViews = document.getElementById("altViews");
    var smugmug= document.getElementById("smugmug");
    if (altViews && paypalObj && smugmug)
    {
        var paypalSetObj = paypalObj.parentNode.removeChild(paypalObj);
        altViews.appendChild(paypalSetObj);
        paypalSetObj.style.display = "inline";
    }    
}