function hasPath(sPath)
{
re = new RegExp("\/" + sPath + "(\/|$)");
return re.test(window.location)
}

/*
----------------------------
Titlebar Customizer
----------------------------
*/

var titleSettings = new Array();

titleSettings.separator = ": "; // text to insert between parts of title
titleSettings.maxLength = -1; // limits length of title (-1 == no limit)
titleSettings.doPhotos = true; // true == append photo captions
titleSettings.doAlbums = true; // true == append album names
titleSettings.doGalleries = true; // true == append gallery names
titleSettings.stripSmugmug = true; // true == remove " - powered by SmugMug" from title bar text

function ContextualizeTitle()
{
var smugmugSuffix = " - powered by SmugMug";
var insertAt = document.title.indexOf(smugmugSuffix);
var newTitle = document.title;
var newText = null;

if(IsHomePage() == false)
{
// Add gallery title
if(titleSettings.doGalleries)
{
var galleryTitle = document.getElementById("galleryTitle");
if(galleryTitle)
{
var galleryTitleText = GetTextContent(galleryTitle);
if(galleryTitleText.length > 0)
{
var index = galleryTitleText.indexOf(" galleries");
if(index > -1)
{
galleryTitleText = galleryTitleText.substr(0, index);
}
newText = titleSettings.separator + galleryTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
}
}

// Add album title
if(titleSettings.doAlbums)
{
var albumTitle = document.getElementById("albumTitle");
if(albumTitle)
{
var albumTitleText = GetTextContent(albumTitle);
if(albumTitleText.length > 0)
{
newText = titleSettings.separator + albumTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
}
}

// Add photo title
if(titleSettings.doPhotos)
{
var photoTitleText = GetPhotoCaption();
if(photoTitleText.length > 0)
{
newText = titleSettings.separator + photoTitleText;
newTitle = InsertString(newTitle, newText, insertAt);
}
}

// Set the new title
if(titleSettings.maxLength > -1)
{
newTitle = newTitle.substr(0, titleSettings.maxLength);
}
}

// Remove the "- powered by SmugMug" segment
if(titleSettings.stripSmugmug)
{
var regexp = /- powered by SmugMug/g;
newTitle = newTitle.replace(regexp, "");
}

// Set the title in the document
document.title = newTitle;
}

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 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;
}

/* Add a handler for the window load event. Calls ContextualizeTitle on window load */
addEvent(window, "load", ContextualizeTitle); 