var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#jsddm > li').bind('mouseover', jsddm_open)
   $('#jsddm > li').bind('mouseout',  jsddm_timer)});

document.onclick = jsddm_close;

/*
--------------------------------------------------
    Titlebar Customizer
--------------------------------------------------
*/

addEvent(window, "load", ContextualizeTitle);
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);
            }
        }
        if(titleSettings.maxLength > -1)
        {
            newTitle = newTitle.substr(0, titleSettings.maxLength);
        }
    }
    if(titleSettings.stripSmugmug)
    {
        var regexp = /- powered by SmugMug/g;
        newTitle = newTitle.replace(regexp, "");
    }
    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;
}


function addCategoryDescription() {
  var categoryDescription = {
      "category1"  : "This is a test.",
      "category2"  : "This is another test.",
      "category3"  : "Final test"
  };

  if ((YD.hasClass(document.body, "category")) && (!YD.hasClass(document.body, "subcategory")))  {

    re = /category_(\S+)/i;
    re.exec(document.body.className);

    breadCrumb = YD.get("breadcrumb");
    if (breadCrumb && categoryDescription[RegExp.$1]) {
      divTag = document.createElement("div");
      divTag.className = "categoryDescription";
      divTag.appendChild(document.createTextNode(categoryDescription[RegExp.$1]));
      breadCrumb.parentNode.insertBefore(divTag, breadCrumb.nextSibling);
    }
  }
  if (YD.hasClass(document.body, "homepage")) {
    re = /\>([\w\-]+)<\/a>/i;

    divTag = YD.get("categoriesBox");
    if (divTag) {
      divTags = YD.getElementsByClassName("albumTitle", "p", divTag);

      for (i=0; i<divTags.length; i++)  {
        re.exec(divTags[i].innerHTML);
        if (categoryDescription[RegExp.$1] != undefined) {
          pTag = document.createElement("p");
          pTag.className = "categoryDescription";
          pTag.appendChild(document.createTextNode(categoryDescription[RegExp.$1]));
          divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
        }
      }
    }
  }
}
YE.addListener(window, "load", addCategoryDescription);

/*==============================*/
/* the subCategory descriptions */
/*==============================*/
function addSubCatDescription() {
  var SubCatDescription = {
      "subcategory1_name"  : "This is a test.",
      "subcategory2"  : "This is a test."
  };

  if (YD.hasClass(document.body, "subcategory"))
  {
    re = /subcategory_(\S+)/i;
    re.exec(document.body.className);

    albumtitle = YD.get("subCatGalleryTitle");
    if (albumtitle && SubCatDescription[RegExp.$1]) {
      divTag = document.createElement("div");
      divTag.className = "SubCatDescription";
      divTag.appendChild(document.createTextNode(SubCatDescription[RegExp.$1]));
      albumtitle.parentNode.insertBefore(divTag, albumtitle.nextSibling);
    }
  }
  if (!YD.hasClass(document.body, "homepage")) {
    re = /\>([\w\-]+)<\/a>/i;

    divTag = YD.get("galleriesBox");
    if (divTag) {
      divTags = YD.getElementsByClassName("subCatGalleryTitle", "p", divTag);

      for (i=0; i<divTags.length; i++)  {
        re.exec(divTags[i].innerHTML);
        if (SubCatDescription[RegExp.$1] != undefined) {
        pTag = document.createElement("p");
        pTag.className = "SubCatDescription";
        pTag.appendChild(document.createTextNode(SubCatDescription[RegExp.$1]));
        divTags[i].parentNode.insertBefore(pTag, divTags[i].nextSibling);
        }
      }
    }
  }
}
YE.addListener(window, "load", addSubCatDescription);