function replaceAcronymText(dom_id, acronyms) {
    if ($(dom_id) != null) {
        var content = $(dom_id).innerHTML;
        // Search the content and insert acronym markers
        for (var i = 0; i < acronyms.length; i++) {
            var tmp = [];
            tmp.push('(\\b|\\W)');
            tmp.push(acronyms[i].term);
            tmp.push('(\\W)');
            str = tmp.join('');
            var sRegExInput = new RegExp(str, "gi");
			content = content.replace(sRegExInput, "$1" + acronyms[i].term + "<a href='#' class='tt acronym'>" + " [?]" + '<span class=tooltip><span class=top></span><span class=middle>' + 't' + i + 'acronyms_term' + '</span><span class=bottom></span></span>' + '</a>' + "$2");			
        }
        $(dom_id).innerHTML = content;
    }
}

function replaceAcronymPlaceholder(dom_id, acronyms) {
    content = $(dom_id).innerHTML;
    for (var i = 0; i < acronyms.length; i++) {
        var val = new RegExp('t' + i + 'acronyms_term', "gi")
        content = content.replace(val, acronyms[i].definition)
    }
    $(dom_id).innerHTML = content;
}

function highlightAcronym(){
    if ($('highlight_acronym').checked) {
        // Show acronyms if they exist
        if ($$('a.acronym').length > 0) {
            $$('a.acronym').each(Element.show);
        // Otherwise search and find the acronyms
        } 
		else {
            // Get the content
            // On the ID page we have a GoogleMap which needs to be avoided so test for the page before we do our stuff
            if ($('profile_map') != null) {
				replaceAcronymText('profile_display_name', acronyms);
				replaceAcronymText('identification_copy', acronyms);
				replaceAcronymText('profile_info_new', acronyms);
				replaceAcronymText('profile_species_new', acronyms);
			}
			else if ($('graph_content')) {				
				replaceAcronymText('profile_display_name', acronyms);
				replaceAcronymText('summary_content', acronyms);
			}
			else{
				replaceAcronymText('profile_display_name', acronyms);
                replaceAcronymText('general_content', acronyms);
            }            
            // Now run through the content once more and set the acronym definitions
            if ($('profile_map') != null) {
				replaceAcronymPlaceholder('profile_display_name', acronyms);
                replaceAcronymPlaceholder('identification_copy', acronyms);
                replaceAcronymPlaceholder('profile_info_new', acronyms);
                replaceAcronymPlaceholder('profile_species_new', acronyms);
            } 
			else if ($('graph_content')) {				
				replaceAcronymPlaceholder('profile_display_name', acronyms);
				replaceAcronymPlaceholder('summary_content', acronyms);
			}
			else {
				replaceAcronymPlaceholder('profile_display_name', acronyms);
                replaceAcronymPlaceholder('general_content', acronyms);
            }           
        }
    // Hide the acronyms        
    } else {
        $$('a.acronym').each(Element.hide);
    }
}

