function lookupNodes(nodearray, thesaurusId, letter) {
    var url = '/bmpix/controller/nodeLookupLegacy.htm';
    if (exists(thesaurusId, letter)) {
        var body = 'thesaurusId=' + thesaurusId + '&currentLetter=' + letter + '&mode=catalog';
        $.getJSON(url, body, handleResponse);
    } else {
        for (i = 0; i < nodearray.length; i++) {
            var body = 'entryString=' + nodearray[i] + '&thesaurusId=' + thesaurusId + '&currentLetter=' + letter + '&mode=list';
            $.getJSON(url, body, handleResponse);
        }
    }
}

function exists(thesaurusId, letter) {
    var urlText = '/bmpix/controller/nodeLookupLegacy.htm';
    var body = 'thesaurusId=' + thesaurusId + '&currentLetter=' + letter + '&mode=query';
    $.ajax({type:'POST', dataType:'json', url:urlText, data:body, async:'false', sucess:'handleResponse'});
}

function handleResponse(e) {
    if (e.mode == 'query') {
      return e.query;
    } else if (e.mode == 'list') {
        var nodearray = e.list;
        for (var i = 0; i < nodearray.length; i++) {
            var term = nodearray[i].term;
            var nodeId = nodearray[i].nodeId;
            var description = nodearray[i].description;

            if (description != null && description.length > 0 && description != term) {
                $("#" + nodeId).append('<a href="#" class="thesDesc" title="' + term + "|" + description + '"><img src="/bmpix/images/gui/global/information.png" alt="" /></a>');
            }
            $("a.thesDesc").cluetip({ width:'200px', height:'225px', splitTitle:'|' });
        }
    }
}


function lookupNode(nodeId, thesaurusId, baseURL) {
    var nodeLookupUri = baseURL + '/controller/nodeLookup.htm';
    var body = 'thesaurusId=' + thesaurusId + '&nodeId=' + nodeId;
    $.getJSON(nodeLookupUri, body, handleNodeLookupResponse);
}

function handleNodeLookupResponse(e) {
    var x = 0;

    var noteText = e.term + "|";

    if(e.description != null && typeof(e.description) != 'undefined') {
        noteText += e.description;
    }

    for(var myKey in e.attributes) {
        var myValue = e.attributes[myKey];

        var myValueLength = myValue.length;
        var myText = "";
        for(var i = 0; i < myValueLength; i++) {
            if(i != 0) {
                myText += ",";
            }
            myText += " " + myValue[i];
        }
        noteText += "|" + myKey + " -" + myText;
    }

    $("a#" + e.nodeId).attr("title", noteText);
    $("a#" + e.nodeId).addClass("thesDesc");
    //$("a.thesDesc").cluetip({ sticky: true, width:'200px', height:'225px', splitTitle:'|' });

}


/***********************
ClueTip AJAX Requests
***********************/

/**
 * ClueTip is activated on the Mouse Click Event
 * @param contextPath
 * @param target
 */

function initClueTip( contextPath, target ){
    $( target ).each(function(){
        var clueTipID = $(this).parent().attr("id");

        $(this).attr("rel", contextPath + "/controller/nodeLookup.htm").cluetip({
            activation: 'click',
            sticky: true,
            width:'250px',
            height:'225px',
            closePosition: 'title',
            arrows: true,
            ajaxProcess: onAjaxProcessHandler,
            ajaxCache: true,
            ajaxSettings : {
                type : "POST",
                data : 'thesaurusId=' + 'bmpixn' + '&nodeId=' +  clueTipID,
                dataType: 'json'
            }
        });
    });
}

/**
 * Process the completed AJAX Request 
 * @param data
 */
function onAjaxProcessHandler( data ){
    //Capture the Title
    var term = ( data.term ) ? data.term : "";
    //Capture the Description
    var description = ( data.description ) ? data.description : "";

    //Format JSON into HTML
    var html = "";
        html += displayHeader( term );
        html += displayParagraph( description );

    //If Attributes are available, format them aswell
    if( data.attributes ){
        $.each( data.attributes,
            function(name,value){
                html += displayAttributes( name, value );
            }
        );
    }
    //Return the Processed Data for Final Display
    return html ;
}

/***********************
HTML Formatters
***********************/
function displayHeader( header ){
    return "<h3>" + header + "</h3><br />";
}

function displayParagraph( paragraph ){
    return "<p>" + paragraph + "</p>";
}

function displayAttributes( name, value ){
    return "<p><strong>" + name + ":</strong> " + value + "</p>";
}
