// $Id: bibtip_ub_hsu.js,v 1.6 2009-10-20 08:44:59 uhahn Exp $

var gHrefRecommender="http://recommender.bibtip.de/service/bibtip4.php";

init_bibtip();

function init_bibtip()
{
if(!document.getElementById('bibtip_id')) return; // no data
if(!document.getElementById('bibtip_run')) return; // not enabled

  var isxn=getBibtipData(document.getElementById('bibtip_isxn'));
  var shortTitle=getBibtipData(document.getElementById('bibtip_shorttitle'));
  var nd=getBibtipData(document.getElementById('bibtip_id'));

insertScriptSrc("http://ub.hsu-hh.de/GBS/OPCutils.js");
loadStyle("http://ub.hsu-hh.de/bibtip/bibtip.css");

  checkForRecs(isxn,shortTitle,nd);
}

/**
	load URL as a new stylesheet into the HEAD of the current Document
	@param{url} URL of type text/css or..
*/
function loadStyle(url){
var newsty = document.createElement('link');
newsty.setAttribute('type','text/css');
newsty.setAttribute('rel','stylesheet');
document.getElementsByTagName('head')[0].appendChild(newsty);
newsty.setAttribute('href',url);
}

/**
	concat all the CDATA elements out of elem
	@param{elem} HTML subtree within the document
*/
function getBibtipData(elem){
	if (!elem) return "";
	if (elem.hasChildNodes()){
	var res="";
	for(var i=0;i<elem.childNodes.length;i++)
		res+= getBibtipData(elem.childNodes.item(i));
	
	return res;
	}	
	if(elem.data)return elem.data;
	return "";
};

function getFbt(){
    result = document.URL.match(/&fbt=[0-9]{7}/);
    if (result != null) {
        return result[0];
    }
    return "";
};



function checkForRecs(isxn,shortTitle,nd)
{
     
  var catalog='UB_HSU';

  var params="?catalog="+catalog;
  if(isxn) params+="&isxns="+isxn;
  if(shortTitle) params+="&title="+encodeURIComponent(shortTitle);
  if(nd) params+="&nd="+nd;
//  params+="&test=";
  params +=getFbt();
  params+="&url="+encodeURIComponent(document.URL);
  params+="&referer="+encodeURIComponent(document.referrer);
  insertScriptSrc(gHrefRecommender+params);
     
}



function insertScriptSrc(src)
{
  var scriptElement=document.createElement("script");
  scriptElement.setAttribute("src",src);
  var bodyElement=document.getElementsByTagName("body")[0];
  bodyElement.appendChild(scriptElement);
}



function showCompleteList()
{
    document.getElementById('short_list').style.display = 'none';
    document.getElementById('long_list').style.display = 'block';
    document.getElementById('more_button').style.display = 'none';
}

function showRecs(recs)
{
  if(recs.length > 0) {
//      var recDiv = document.getElementById("bibtip_reclist");
      var recDiv = document.createElement('div');
      recDiv.id='bibtip_reclist';
      var newHeader = createHeader();
      recDiv.appendChild(newHeader);
      var LIST_LENGTH = 10;

      var shortList = createList(recs,'short_list',LIST_LENGTH);
      var longList = createList(recs,'long_list',100);
      longList.style.display='none';
      recDiv.appendChild(shortList);
      recDiv.appendChild(longList);

      if (recs.length > LIST_LENGTH) {
        var newButton = document.createElement("div");
        newButton.setAttribute('id','more_button');
        var newAnchor = document.createElement("a");
        newAnchor.setAttribute("href","javascript:showCompleteList()");
        newAnchor.appendChild(document.createTextNode('weitere Empfehlungen ...'));
        newButton.appendChild(newAnchor);
        recDiv.appendChild(newButton);
      }

addOpcTitleLine('Empfehlungen:',recDiv);
      recDiv.style.display='block';

  }

}

function createHeader() 
{
      var newHeader = document.createElement("div");
      newHeader.setAttribute('id','bibtip_header');

      var newImage=document.createElement("img");
      newImage.setAttribute("src",'http://recommender.bibtip.de/service/Bibtip_Logo_Final_ohne_Subt_52x16.gif');
      newImage.setAttribute("border",'0');
      var newAnchor = document.createElement("a");
      newAnchor.setAttribute("href","http://www.bibtip.org");
      newAnchor.setAttribute("target","blank");
      newAnchor.setAttribute('title',"BibTip zeigt Ihnen Katalogtreffer, die schon einmal zusammen mit diesem Werk gefunden wurden.");
      newAnchor.appendChild(newImage);
      newHeader.appendChild(newAnchor);
      return newHeader; // nicht hier!

      var newTitle = document.createElement("span");
      newTitle.setAttribute('id','bibtip_title');
      newAnchor.appendChild(document.createTextNode(' (was ist das?)'));
      newHeader.appendChild(newTitle);
      return newHeader;
}

function createList(recs,id,lengthLimit) 
{
      var newDiv= document.createElement("div");
      newDiv.setAttribute("id",id);
      var newList = document.createElement("ul");
      newDiv.appendChild(newList);

      for (var i = 0; (i < recs.length) && (i < lengthLimit); i++) {
          var newLi = document.createElement("li");
          var newAnchor = document.createElement("a");
          var entry = recs[i];
          newAnchor.setAttribute("href","CMD?ACT=SRCHA&TRM=ppn+" + entry[1]);
          newAnchor.appendChild(document.createTextNode(entry[0]));
          newLi.appendChild(newAnchor);
          newList.appendChild(newLi);
      }
      
      return newDiv;
}


