/* COPY HOMEPAGE AND GIVE IT A NEW NAME */
function hasPath(sPath)
{ re = new RegExp("\/" + sPath + "(\/|$)");
  return re.test(window.location)
}


/*
--------------------------------------------------
  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) title = "PHOTO";
    if (pieces.Album.length > 0)
    { title += titleSettings.separator;
      title += pieces.Album;
    }
    if (pieces.Gallery.length > 0)
    { title += titleSettings.separator;
      title += pieces.Gallery;
    }
    if (pieces.Main.length > 0)
    { title += titleSettings.separator;
      title += pieces.Main;
    }
    return title;
  }

  function MakeTitleForward ()
  { var title = "Design Photography by Bob Kane | Scenic landscape photographs of Colorado | Dance, Wedding, and Event Photography";
    title += pieces.Main;
    if (title.length == 0) title = "*Design Photography by Bob Kane*";
    if (pieces.Gallery.length > 0)
    { title += titleSettings.separator;
      title += pieces.Gallery;
    }
    if (pieces.Album.length > 0)
    { title += titleSettings.separator;
      title += pieces.Album;
    }
    if (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");
        /*var element = document.getElementById("breadcrumb");*/
        if (element)
        { value = GetTextContent(element);
          /*var wordList = value.split(">");
          var length = wordList.length;
          value = wordList[length-2] + " Gallery: " + wordList[length-1];*/
        }
      }
      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;
}