   
   BCILinks = new Object();
   
   BCILinks.debug = false;
   
   /**
   *    Used to retrieve JSON data from BCI server across domains.
   *    Uses dynamic script tag generation.
   *
   *    @url the url to call for the json data
   *    @callback the function to be passed the data
   *    @params the paramaters passed to the callback function in addition to the data.
   *
   */
   BCILinks.getDataWithScript = function(url, callback, params){
       
       var script = document.createElement("script");
       document.body.appendChild(script);
       
       var funcname = "func" + BCILinks.getDataWithScript.counter++;
       
       BCILinks.getDataWithScript[funcname] = function(text){
           
           // call the callback
           if (params){
               callback(text, params);
           }else{
               callback(text);
           }
           
           // clean up
           document.body.removeChild(script);
           delete BCILinks.getDataWithScript[funcname];
       
       }
       var urlComplete = url + "&callback=" + encodeURIComponent("BCILinks.getDataWithScript." + funcname);
       script.src = urlComplete;
   }
   
   /**
   * counter used for temporary function names within getDataWithScript
   */
   BCILinks.getDataWithScript.counter = 0;
   
   /**
   * This function is called when on page load. It works through all the 
   * 'a' tags in the page for any with a class of BCI-*-Link
   * It then calls the relevant function for the class of link.
   *
   */
   BCILinks.parseLinks = function (){
       
        var all = document.getElementsByTagName('a');
        
        for (var e = 0; e < all.length; e++){
            
            if (all[e].className == 'BCI-LSID-Link'){
            
                var anchor = all[e];
                var txt = anchor.innerHTML;
                BCILinks.getDataWithScript('http://biocol.org/json/fetch?lsid=' + txt, BCILinks.renderLsidLink, anchor);
                
            }
            
            if (all[e].className == 'BCI-Code-Link'){
            
                var anchor = all[e];
                var txt = anchor.innerHTML;
                BCILinks.getDataWithScript('http://biocol.org/json/search?code=' + txt, BCILinks.renderCodeLink, anchor);
                
            }
            
            if (all[e].className == 'BCI-Name-Link'){
            
                var anchor = all[e];
                var txt = anchor.innerHTML;
                BCILinks.getDataWithScript('http://biocol.org/json/search?name=' + txt, BCILinks.renderNameLink, anchor);
                
            }
        }
   }
   
   /**
   *    Callback function used for anchors of class BCI-LSID-Link
   *
   */
   BCILinks.renderLsidLink = function (returnObArray, anchor){
      
       // check we have been returned a collection
       if (returnObArray.length == 1){
      
           var collection = returnObArray[0];
	       anchor.innerHTML = collection.name;
	  
           if (collection.web_site != null){
	           anchor.href = collection.web_site;
	           anchor.title = "The website of '" + collection.name + "'";
	       }else{
	           anchor.href = collection.url;
	           anchor.title = "The BCI page for '" + collection.name + "'";
	       }
      
       }else{
           // we have no collection returned so don't alter the anchor 
           // just flag it to the console.
           if(BCILinks.debug) alert('BCI-Links: Failed to retrieve data for LSID ' + anchor.innerHTML );
       }
   }
   
   /**
   *    Callback function used for anchors of class BCI-Code-Link
   *
   */
   BCILinks.renderCodeLink = function (returnObArray, anchor){
      
       // check we have been returned a collection
       if (returnObArray.length == 1){
      
           // we have a single collection so can link to it
           
           var collection = returnObArray[0];
	       anchor.innerHTML = collection.name + " (" + anchor.innerHTML + ")";
	  
           if (collection.web_site != null){
	           anchor.href = collection.web_site;
	           anchor.title = "The website of '" + collection.name + "'";
	       }else{
	           anchor.href = collection.url;
	           anchor.title = "The BCI page for '" + collection.name + "'";
	       }
      
       }else if (returnObArray.length > 1){
           
           // we have more than one collection so link to a search page
           anchor.href = "http://www.biodiversitycollectionsindex.org/search/index?new_search=true&search=@CODE:" + anchor.innerHTML;
           anchor.title = "BCI search for the " + returnObArray.length + " collections with the code '" + anchor.innerHTML + "'";

       }else if (returnObArray.length == 0){
           // we have no collection returned so don't alter the anchor 
           // just flag it to the console.
           if(BCILinks.debug) alert('BCI-Links: Failed to retrieve any collection for code ' + anchor.innerHTML );
       }
   }
   
   /**
   *    Callback function used for anchors of class BCI-Name-Link
   *
   */
   BCILinks.renderNameLink = function (returnObArray, anchor){
      
       // check we have been returned a collection
       if (returnObArray.length == 1){
      
           // we have a single collection that matches that name so build a link
           // in the regular way
           
           var collection = returnObArray[0];
	       anchor.innerHTML = collection.name;
	       
	       // add in the code if it has one
	       if (collection.code != null && collection.code.length > 0){
	           anchor.innerHTML = anchor.innerHTML + " (" + collection.code + ")";
	       }
	  
           if (collection.web_site != null){
	           anchor.href = collection.web_site;
	           anchor.title = "The website of '" + collection.name + "'";
	       }else{
	           anchor.href = collection.url;
	           anchor.title = "The BCI page for '" + collection.name + "'";
	       }
      
       }else{
           
           // we either have no collection or more than one (unlikely unless they are using wild cards)
           anchor.href = "http://www.biodiversitycollectionsindex.org/search/index?new_search=true&n_lang=true&physical_only=true&search=" + encodeURIComponent(anchor.innerHTML);
           anchor.title = "A BCI search for collections containing '" + anchor.innerHTML + "'";

       }
   }
