function AddReferralCode()
  {
  	var footerDiv = document.getElementById("footer");
  	if (footerDiv)
  	{
  		var links = footerDiv.getElementsByTagName("A");
  		if (links && (links.length != 0))
  		{
  			var smugLink = links.item(0);
 			smugLink.href = "http://www.smugmug.com/?referrer=LpqhIaA6wcPS6";
  		}
  	}
  }
  
  function OnLoadHandler()
  {
  	AddReferralCode();
  	ContextualizeTitle();
  }


  var titleSettings = new Array();
  /*
  --------------------------------------------------
      customize page title settings
  --------------------------------------------------
  */
  titleSettings.separator   = ": "; // text to insert between parts of title
  titleSettings.maxLength   = -1;   // limits length of title (-1 == no limit)
  titleSettings.doPhotos    = false; // true == append photo captions
  titleSettings.doAlbums    = true; // true == append album names
  titleSettings.doGalleries = true; // true == append gallery names

  function ContextualizeTitle() {
    var re1 = /([\s\S]*)( - powered by smugmug)/;
    var re2 = /([\s\S]*) (galleries|sub-categories)/;
    var newTitle = document.title;
    var newText = null;
    if(!IsHomePage()) 
    {
      // add gallery title
      if(titleSettings.doGalleries)
      {
        var galleryTitle = document.getElementById("galleryTitle");
        if(galleryTitle)
        {
          var galleryTitleText = GetTextContent(galleryTitle);
          if(galleryTitleText.length > 0)
          {
            galleryTitle = galleryTitle.replace(re2, "$1")
  
            newText = titleSettings.separator + galleryTitle;
            newTitle = newTitle.replace(re1, "$1" + newText + "$2");
          }
        }
      }

      // 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 = newTitle.replace(re1, "$1" + newText + "$2");
          }
        }
      }

      // add photo title
      if(titleSettings.doPhotos)
      {
        var photoTitleText = GetPhotoCaption();
        if(photoTitleText.length > 0)
        {
          newText = titleSettings.separator + photoTitleText;
          newTitle = newTitle.replace(re1, "$1" + newText + "$2");
        }
      }

      if(titleSettings.maxLength > -1)
        newTitle = newTitle.substr(0, titleSettings.maxLength);

      document.title = newTitle;
    }
  }

  function GetPhotoCaption() {
    var photoTitle = document.getElementById("caption_bottom");

    if(!photoTitle)
      photoTitle = document.getElementById("caption_top");

    if(!photoTitle)
      return "";
         
    return GetTextContent(photoTitle);
  }

  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 classStr = document.body.className;
    var re = /homepage /;

    if (!classStr)
      return false;

    return re.test(classStr);
  }
  /* End of customized page titles
  */