/* Copyright (c) 1997-2006
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   $Project: polymake $$Id: contents.js,v 1.22 2006/02/22 10:30:05 gawrilow Exp $
*/

var note_gap=20; // px;

function update_title() {
   var text=top.contents.document.title;
   var title=top.title.document.getElementById("follow");
   title.innerHTML=text;
   if (!title.isVisible) {
      var title_img=top.title.document.getElementById("start");
      title_img.style.visibility="hidden";
      title.style.visibility="visible";
      top.logo.document.getElementById("logo").style.visibility="visible";
      title.isVisible=true;
   }
   if (top.hlTopic) {
      highlight_topic(top.contents.document, top.hlTopic);
      top.hlTopic=null;
   }
}

var pinned_link=null;
var clicked=false;
var hlTopic=null;
var noteBackground;

function show_note(e) {
   if (clicked) {
      clicked=false;
      return;
   }
   if (pinned_link) {
      window.status="Click anywhere to hide the note";
      return true;
   }
   var doc=top.contents.document;
   var note=this.attachedNote;
   if (note.xDim===undefined) {
      note.xDim=note.offsetWidth;
      note.yDim=note.offsetHeight;
   }
   var con=top.contents;
   var x=relMouseX(e,con);
   var y=relMouseY(e,con);
   var x_note=Math.max(0, Math.min(x - note_gap, winW(con) - note.xDim));
   var y_note=y - note_gap - note.yDim;
   if (y_note<=0) y_note=y + note_gap;
   x_note += winL(con);
   y_note += winT(con);
   note.style.left=x_note+"px";
   note.style.top=y_note+"px";
   if (!noteBackground) {
      noteBackground=doc.bgColor || doc.defaultView && doc.defaultView.getComputedStyle && doc.defaultView.getComputedStyle(note.parentNode, '').getPropertyValue("background-color");
      if (!noteBackground || noteBackground=="transparent") noteBackground="white";
   }
   note.style.backgroundColor=noteBackground;
   note.style.visibility="visible";
   if (x >= x_note && x <= x_note+note.offsetWidth && y >= y_note && y <= y_note+note.offsetHeight) {
      pinned_link=this;
      top.contents.onclick=unpin_note;
      window.status="Click anywhere to hide the note";
   } else {
      window.status="Click to pin down the note";
   }
   return true;
}

function unpin_note(e) {
   if (clicked) {
      clicked=false;
      return true;
   }
   if (pinned_link) {
      pinned_link.attachedNote.style.visibility="hidden";
      pinned_link=null;
   }
   top.contents.onclick=null;
   return true;
}

function pin_note(e) {
   if (pinned_link) {
      pinned_link.attachedNote.style.visibility="hidden";
      if (pinned_link === this) {
	 pinned_link=null;
	 clicked=true;
	 if (this.isLink && this.ondblclick) {
	    this.ondblclick(e);
	 }
	 return this.isLink;
      }
      pinned_link=null;
      clicked=false;
      this.onmouseover(e);
   }
   pinned_link=this;
   top.contents.onclick=unpin_note;
   clicked=true;
   return false;
}

function hide_note(e) {
   if (!pinned_link) {
      this.attachedNote.style.visibility="hidden";
      window.status="";
   }
   clicked=false;
   return true;
}

function hl_targets(e) {
   for (var s=0; s<this.Targets.length; ++s) {
      this.Targets[s].style.color="red";
   }
}

function unhl_targets(e) {
   for (var s=0; s<this.Targets.length; ++s) {
      this.Targets[s].style.color="black";
   }
}

function init_note_refs() {
   var doc=top.contents.document;
   var links=doc.getElementsByTagName("a");
   for (var i=0; i<links.length; ++i) {
      var cl=links[i].getAttribute("class");
      if (cl == undefined) continue;
      if (cl.indexOf("note")>=0) {
	 var href=links[i].getAttribute("href");
	 var quest=href.indexOf("?");
	 if (quest>0 && (quest<=1 || href.indexOf("&") < quest)) {
	    var note=doc.getElementById(href.substring(quest+1));
	    if (note) {
	       links[i].attachedNote=note;
	       links[i].onmouseover=show_note;
	       links[i].onmouseout=hide_note;
	       links[i].onclick=pin_note;
	       if ((links[i].isLink= quest>1 && href.charAt(quest-1)!="#")) {
		  links[i].setAttribute("href", href.substr(0,quest));
	       } else {
		  links[i].setAttribute("href", "javascript:void(0)");
	       }
	    }
	 }
      } else if (cl.indexOf("hlarg")>=0) {
	 var txt=links[i].childNodes[0].nodeValue;
	 var code_div=links[i];
	 do {
	    code_div=code_div.parentNode;
	 } while (code_div.nodeName != "DIV" && code_div.nodeName != "P" && code_div.nodeName != "TABLE");
	 do {
	    code_div=code_div.previousSibling.previousSibling;
	 } while (code_div && (cl=code_div.getAttribute("class")) && cl.indexOf("code")<0 && cl.indexOf("topic")<0);
	 var args=code_div.getElementsByTagName("span");
	 var targets=new Array;
	 for (var s=0; s<args.length; ++s) {
	    if (args[s].childNodes[0].nodeValue == txt) {
	       targets.push(args[s]);
	    }
	 }
	 if (targets.length>0) {
	    links[i].Targets=targets;
	    links[i].onmouseover=hl_targets;
	    links[i].onmouseout=unhl_targets;
	 }
      }
   }
   pinned_link=null;
   clicked=false;
}

function highlight_topic(doc, name) {
   if (doc.hlTopic=doc.getElementById("def:" + name)) {
      top.highlight_on(doc.hlTopic);
   } else if (top.highlightIgnoreError) {
      top.highlightIgnoreError=null;
   } else {
      msg='';
      var anchors=doc.getElementsByTagName("a");
      for (var i=0; i<anchors.length; ++i) {
	 var id=anchors[i].getAttribute("id");
	 if (i) msg += i + "\n";
      }
      alert('no ID "def:' + name + '" found - but the following:\n' + msg);
   }
}

function load_hl(a) {
   var target=a.hash;
   if (target) {
      var path=a.pathname;
      if (contents.location.pathname != (path.substr(0,1) == '/' ? path : '/'+path)) {
	 top.hlTopic=target.substr(1);
      } else {
	 var doc=top.contents.document;
	 if (doc.hlTopic) {
	    top.highlight_off(doc.hlTopic);
	 }
	 highlight_topic(doc, target.substr(1));
      }
   }
   return true;
}

