/*
<!--                                            -->
<!--      Script used by Evangelos Pafilis      -->
<!-- as part of his PhD Tagging service project -->
<!--        August - September 2007             -->
<!--                                            -->
<!--              pafilis@embl.de               -->
<!--                                            -->
<!--               version 0.1.6                -->
<!--                                            -->
*/

var currentDelayedPopup;

//
//IE fix for getElementsByName
//
function getElementsByName_iefix(tag, name) {
  var elem = document.getElementsByTagName(tag);
  var arr = new Array();
  for(i = 0,iarr = 0; i < elem.length; i++) {
               att = elem[i].getAttribute("name");
               if(att == name) {
                        arr[iarr] = elem[i];
                        iarr++;
               }
  }
  return arr;
}

function showReflectPopup(node_list, name){
	
	// Allows parent page to disable reflect popups by setting reflectTagsEnabled to "0"
  if(window.reflectTagsEnabled != undefined){
    if (reflectTagsEnabled != 1){
      return;
    }
  }
  clearInterval(currentDelayedPopup);
  node_list = resolveEntityIdentifier(node_list);

  node_list = "entities=" + node_list + "&tagged_name=" + escape(name) + "&document_id=" + encodeURI(document.getElementById("reflect_div_doi").innerHTML);

  name = "Reflect - " + name;
	
  var width = "401px";
  var height = "243px";

  var returnString = "";
  returnString  = '<iframe id="item_info_frame" style="width: ' + width + ';height: ' + height + ';overflow: hidden; z-index: 80;" frameborder="0"\' src=\'http://reflect.ws/fcgi-bin/solveAmbig.fcgi?' + node_list + '\'></iframe>';
  returnString += '<div id="overLayDiv" style="width: ' + width + ';height: ' + height + '; position: absolute; z-index: 100; top: 20px; left: 2px; background-color: #EEEEEE; display: none;">';
  returnString += '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Moving the PopUp...<br>&nbsp;&nbsp;&nbsp;&nbsp;Information will reappear if you release the mouse button.';
  returnString += '</div>';

  overlib(returnString,STICKY,CAPTION,name,DRAGCAP,CLOSECLICK,HAUTO,OFFSETY,0);
}

function startReflectPopupTimer(node_list, name){
  clearInterval(currentDelayedPopup);
	name = name.replace( /\'/g, '\\\'');
	name = name.replace( /\"/g, '\\\"');
  currentDelayedPopup = window.setTimeout("showReflectPopup('"+node_list+"', '"+name+"')", 2000);
}

function stopReflectPopupTimer(){
  clearInterval(currentDelayedPopup);
}

function resolveEntityIdentifier(node_list){
  var nodeArray = node_list.split("+");
  for (node in nodeArray){
		if(node.match(/^\d+$/)){
			nodeID = nodeArray[node];
      // Old style with digits
			if(nodeID.length != 32	){
  			var span = document.getElementById("reflectEntity"+nodeID);
	  		if(span.getAttribute("name")){
		  		nodeArray[node] = encodeURI(span.getAttribute("name"));
			  }else{
          nodeArray[node] = encodeURI(span.innerHTML);
			  }
			// New style using hashsum
		  }else{
				var span = document.getElementsByName(nodeID)[0];
				// IE fix for getElementsByName
        if(!span){
					span = getElementsByName_iefix("span", nodeID)[0];
				}
        return encodeURI(span.innerHTML);
			}
		}
  }
  node_list = nodeArray.join(";");
  return node_list;
}

//by Michael Kuhn, kuhn@embl.de, November 2007
function protein_option_text (node, add_text, add_nodes, width) {
//    return "<table border=\"0\" cellspacing=\"10\"><tr><td><b>Information</b><br /><iframe src=\"http://stitch.embl.de/services/iteminfo?node="+ node + "&autosize=0\" id=\"item_info_frame\" style=\"width: " + width + "em; height: " + ((width*4)/5) + "em;\" frameborder=\"0\" /><td></tr></table>";
    return popup_info(node);
}

function chemical_option_text (node, cid, node_annotation, add_text, add_nodes, width) {
//    return "<table border=\"0\" cellspacing=\"10\"><tr><td><b>Information</b><br /><iframe src=\"http://stitch.embl.de/services/iteminfo?node="+ node + "&autosize=0\" id=\"item_info_frame\" style=\"width: " + width + "em; height: " + ((width*4)/5) + "em;\" frameborder=\"0\" /><td></tr></table>";
    return popup_info(node);
}

function popup_info(node_list) {
  var doi = "dummy";
	try{
    var doi = document.getElementById('reflect_div_doi').innerHTML;
	}catch(err){
	}

  var nodeArray = node_list.split("+");
  for (node in nodeArray){
    if(nodeArray[node].match(/^-67/)){
      nodeArray[node] = nodeArray[node] + ".DOI:" + doi;
    }
  }
  node_list = nodeArray.join("+");
//    alert(document.URL);
//    console.log(entities[0]);
    var width = "401px";
    var height = "243px";
    var returnString = "";
    returnString  = '<iframe id="item_info_frame" style="width: ' + width + ';height: ' + height + ';overflow: hidden; z-index: 80;" frameborder="0"\' src=\'http://reflect.cbs.dtu.dk/fcgi-bin/solveAmbig.fcgi?' + node_list + '\'></iframe>';
    returnString += '<div id="overLayDiv" style="width: ' + width + ';height: ' + height + '; position: absolute; z-index: 100; top: 20px; left: 2px; background-color: #EEEEEE; display: none;">';
    returnString += '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Moving the PopUp...<br>&nbsp;&nbsp;&nbsp;&nbsp;Information will reappear if you release the mouse button.';
    returnString += '</div>';
    return returnString;
}

function checkSize(){
  var synonyms = document.getElementsByName("synonym");
  for(var i = 0; i<synonyms.length; i++){
    setSizeTA(synonyms.item(i));
  }
}
function setSizeTA(object){
  var buffer;
  if(navigator.appVersion.match(/Safari/)){
    object.style.height = object.scrollHeight;
    buffer = 18;
  }else{
    buffer = 2;
  }
  object.style.width = 10;
  object.style.width = object.scrollWidth + buffer;
}

function removeDiv(event){
  if(document.getElementById("overLayDiv")){
    document.getElementById("overLayDiv").style.display = "none";
  }
}

function addEventHandlerToElement(elementToAttachListener, typeOfEvent, eventFunction, preventBubbling)
{
        if(elementToAttachListener.addEventListener) 
        { 
                //you are using a w3c DOM compliant browser
                elementToAttachListener.addEventListener(typeOfEvent, eventFunction , preventBubbling); //capture event ie prevent bubbling: T/F
        }
        else if(elementToAttachListener.attachEvent) 
        {
                //you are using IE      
                elementToAttachListener.attachEvent("on"+typeOfEvent, eventFunction );
        }      
}

var openingClickOccured = false; 

function closePopup(event)
{
	if (over) //over: popup object in overlib  
	{//true if a a popup is open
//	  console.log("PopUp open");
//	  console.log(cursorOff());
		if (cursorOff(event))
		{//true if the mouse is outside the popup upon "onClick"
			if (openingClickOccured)
			{
//			  console.log("Opening click occured");
				nd();// overlib method to close the popup
				openingClickOccured = false;
			}else{
//			  console.log("Opening click NOT occured");
				openingClickOccured = true;
			}
		}else{
//		    console.log("Cursor NOT offside");
			openingClickOccured = true;
		}
	}else{
		//openingClickOccured = false;
	}
}
addEventHandlerToElement(document,"mouseup", function(e) { removeDiv(e);  } , false);
addEventHandlerToElement(document,"mouseup", function(e) { closePopup(e);  } , false);
