// adds a css class to the menu's parent for setting up a menu hover style
function addHoverMenu( triggerId, hoverClass ) {
    document.getElementById(triggerId).onmouseover = function () {
        this.className += " " + hoverClass;
    }
    document.getElementById(triggerId).onmouseout = function () {
        this.className = this.className.replace( hoverClass, "" );
    }
}

// truncates a one-line string to a fixed pixel width
// element must be a <span> or other object without layout
function ellipseListText( elementId, width ) {
    var el   = document.getElementById(elementId);
    if (!el) {
        return;
    }
    
    if( el.offsetWidth > width ) {
      var i = 1;
      var text = el.innerHTML;
      el.innerHTML = ' ';
      while( ( el.offsetWidth < width ) && ( i < text.length ) ) {
         el.innerHTML = text.substr( 0, i ) + '...';
         // el.className = 'ellipsed';
         i++;
      }
      el.title = text;
    }    
}

// expander toggle for breed guides
function toggle(div_id) {
    var div = document.getElementById(div_id);
    if (div) {
        if (div.className == 'show') {
            div.className = 'hide';
        } else {
            div.className = 'show';
        }
    }
}

// constant expander images for right rail category expander tree
var closedImg = 'http://www.petfinder.com/images/library/cat-closed.gif';
var openImg   = 'http://www.petfinder.com/images/library/cat-open.gif';

// function for opening and closing the category tree in the right rail category expander tree
function topicList ( topicImgId, topicSubsId, topicStateId, topicParentId ) {
    
    // topicegory was open. close it.
    if (document.getElementById(topicStateId).innerHTML == 'open') {
        document.getElementById(topicImgId).src = closedImg;
        document.getElementById(topicSubsId).className  = 'category_tree_closed';
        document.getElementById(topicStateId).innerHTML = 'closed';
    }
    
    // topicegory was closed. open it.
    else if (document.getElementById(topicImgId) && document.getElementById(topicSubsId) && document.getElementById(topicStateId)) {
        document.getElementById(topicImgId).src = openImg;
        document.getElementById(topicSubsId).className  = 'category_tree_open';
        document.getElementById(topicStateId).innerHTML = 'open';
        var i = 0;
        var j = 0;
        var k = 0;
        
        // close all other open categories.
        var parent = document.getElementById(topicParentId);
        try {
        for (i in parent.childNodes) {
            var el = parent.childNodes[i];
            if ( (el.className == 'category_tree_open') && (el.id != topicSubsId) ) {
                // close the tree
                el.className = 'category_tree_closed';
            }
            else if ( (el.className == 'category_tree_hidden') && (el.id != topicStateId) && (el.innerHTML != 'closed') ) {
                // set the hidden state field to closed
                el.innerHTML = 'closed';
            }
            else if ( el.nodeName && (el.nodeName.toLowerCase() == 'p') ) {
               for (j in el.childNodes) {
                   var jel = el.childNodes[j];
                   if (jel.nodeName && (jel.nodeName.toLowerCase() == 'a') ) {
                       for (k in jel.childNodes) {
                           var kel = jel.childNodes[k];
	       if ((kel.className == 'cat-img') && (kel.id != topicImgId) && (kel.src == openImg) ) {
                               // change the icon to closed
                               kel.src = closedImg;
	       }
                       }
                   }
               }
            }
        }
        } catch (e) {}
    }
}

// For emailing a friend
function openEmailFriend(subject, text, target_url) {
    var url = "/tools/email-friend/index.cgi?subject=" + subject + "&amp;link_text=" + text + "&amp;target_url=" + target_url;
    newwin = window.open(url,'EmailFriendWindow','top=150,left=150,width=585,height=585,scrollbars=yes,resize=yes');

    // bring the new window to the front, just in case it's hidden
    if (window.focus) {
        newwin.focus();
    }
    
    return false;
}

// For social networking "share" button
function openAddThis() {
    var url = "http://www.addthis.com/bookmark.php";
    newwin = window.open(url,'ShareWindow','top=150,left=150,width=600,height=500,scrollbars=yes,resize=yes');
    
    // bring the new window to the front, just in case it's hidden
    if (window.focus) {
        newwin.focus();
    }
    
    pageTracker._trackPageview('outbound/www.addthis.com');
    return false;
}

