/*
--------------------------------------------------
	Titlebar Customizer
--------------------------------------------------
*/
// On IE/FF set the title before the Document OnLoad takes place
document.title = "improving our member's photographic skills";
addEvent( window, "load", CustomizeTitle );
function CustomizeTitle()
{
    var baseTitle = "Saskatoon Camera Club";
    var separator = " - ";
    var albumTitle = GetText( document.getElementById("albumTitle") );
    var galleryTitle = GetText( document.getElementById("galleryTitle") );
    var subCatTitle = GetText ( document.getElementById("subCatGalleryTitle") );
    var singleImage = document.body.className && document.body.className.indexOf("singleImage") > -1 ? true : false;
    var pageTypeDefined = typeof( pageType ) != "undefined";
    var pageTypeDetailsDefined = typeof( pageTypeDetails ) != "undefined";
// Don't change the title from the above document.title on the homepage
    if( document.body.className && document.body.className.indexOf("homepage") > -1 )
    {
        document.title = baseTitle + separator + "Home";
        return;
    }
 
// An album page (holds a bunch of photos, could be "zoomed in" on a specific photo)
    if( albumTitle )
    {
        var photoTitle = GetPhotoTitle();
        if( photoTitle )
            document.title = baseTitle + separator + albumTitle + separator + photoTitle;
        else
            document.title = baseTitle + separator + albumTitle;
        return;
    }
// A gallery page (holds a bunch of albums and/or a bunch of sub categories)
    if( galleryTitle )
    {
// Strip " sub-categories" off the end of the category text
        var finalPositionCategory = galleryTitle.indexOf(" sub-categories");
        if( finalPositionCategory > -1 )
            galleryTitle = galleryTitle.substr( 0, finalPositionCategory );
        else
            {
// Strip " galleries" off the end of the category/sub-category text
              var finalPositionSubCategory = galleryTitle.indexOf(" galleries");
              if( finalPositionSubCategory > -1 )
          galleryTitle = galleryTitle.substr( 0, finalPositionSubCategory );
             }
 
            document.title = baseTitle + separator + galleryTitle;
    return;
    }
// Pick up sub-category
    if( subCatTitle )
    {
// Strip " sub-categories" off the end of the category text
        var finalPositionCategory = subCatTitle.indexOf(" sub-categories");
        if( finalPositionCategory > -1 )
            galleryTitle = subCatTitle.substr( 0, finalPositionCategory );
        else
            {
// Strip " galleries" off the end of the category/sub-category text
              var finalPositionSubCategory = subCatTitle.indexOf(" galleries");
              if( finalPositionSubCategory > -1 )
          galleryTitle = subCatTitle.substr( 0, finalPositionSubCategory );
             }
 
            document.title = baseTitle + separator + galleryTitle;
    return;
    }
 
// A single image (like the kind you get when you click an image in a keyword page)
    if( singleImage )
        {
 var photoTitle = GetPhotoTitle();
 if( photoTitle )
            document.title = baseTitle + separator + photoTitle;
 else
     document.title = baseTitle + separator + "untitled photo";
 return;
 }
// A single keyword page
    if( pageTypeDefined && pageType == 'Keyword' && pageTypeDetailsDefined )
 {
// Keyword page
             document.title = baseTitle + separator + "Keyword: " + pageTypeDetails;
     return;
 }
// A multiple keyword page
    if( pageTypeDefined && pageType == 'Keywords' && pageTypeDetailsDefined )
 {
// Multiple keywords page
// Put " + " between each keyword instead of -
             var keywordList = pageTypeDetails;
     keywordList = keywordList.replace(/\-/g, " + ");
     document.title = baseTitle + separator + "Keywords: " + keywordList;
     return;
 }
// The main keywords page. Note: single image takes precedence over this type
    if( document.body.className && document.body.className.indexOf("keywordPage") > -1 )
        {
// Main Keywords Page
            document.title = baseTitle + separator + "Keywords";
            return;
 }
// None of the rules above set the title! Oh well, fall back on the base title.
    document.title = baseTitle;
} // CustomizeTitle
function Trim( text )
{
    text = text.replace(/(^\s+)|(\s+$)/g, ""); // trim leading and trailing white space
    return text;
} // Trim
function GetText( node )
{
    if( !node )
        return "";
    if( node.innerText )
        return Trim( node.innerText ); // For IE
    if( node.textContent )
        return Trim( node.textContent ); // For others
    return "";
} // GetText
function GetPhotoTitle()
{
// If the photo title is set, it starts with the breadcrumb.
    var breadCrumbStart = "Denise Goldberg > ";
    var mainPhoto = document.getElementById("mainPhoto");
    if( !mainPhoto || !mainPhoto.title || mainPhoto.title.indexOf( breadCrumbStart ) != 0 )
 return "";
    return Trim( mainPhoto.title.substr( breadCrumbStart.length ) );
} // GetPhotoTitle
