/**
 * Converts Salesforce DOM Ids to ones usable by jQuery. jQuery does not like some characters used in their Ids.
 * @param {string} oldId - The Salesforce Id to convert to a usable form for jQuery.
 */
function jQueryFriendlyId(oldId) {
    return oldId.replace( /(:|\.|\[|\]|,)/g, "\\$1" );
}

/**
 * Performs an HTML decode via an in-memory DOM object since the toggle text is HTML encoded in a data attribute.
 * @param {string} value - The string to HTML decode.
 */
function htmlDecode(value){
  return jQuery('<div/>').html(value).text();
}

/**
 * Toggle the expandable description for a given element on the page.
 * @param {object} whichClicked - The element which has been clicked to trigger the description toggle.
 */
function toggleDescription(whichClicked) {
    // the link which was clicked
    var clickedToggleLink = jQuery(whichClicked);

    // the element containing the description
    var elementToToggle = jQuery('#' + jQueryFriendlyId(clickedToggleLink.data('toggle-target')));

    if (elementToToggle.is(':visible')) {
        // collapse and hide
        elementToToggle.addClass('s-collapsed');
        clickedToggleLink.html(htmlDecode(clickedToggleLink.data('default-text')));
    } else {
        // expand and show
        elementToToggle.removeClass('s-collapsed');
        clickedToggleLink.html(htmlDecode(clickedToggleLink.data('expanded-text')));
    }
}