 /*************************************************************************
 * Copyright 2006, Trustees of Indiana University. All rights reserved. This
 * file is part of the Chymistry of Isaac Newton website,
 * http://www.dlib.indiana.edu/collections/newton
 * External javascript libraries have been included here; see
 * individual copyright information for those libraries below. 
 
 * $Id: dw_tooltip.js,v 1.5 2009/12/09 03:23:35 whooper Exp $
 *************************************************************************/ 

 
/*************************************************************************
 Name: makePanelTop, imgStyleDrag, imgStyleDragEnd
 Desc: These functions are keyed to events handled by the * dom-drag.js
 	   library, below.  They are called within the showImgPanel function.
*************************************************************************/
	   
var panels = [];
  
 makePanelTop = function(){
 var start = this.style.zIndex;
 for(i=0;i<panels.length;i++){
   if( panels[i].style.zIndex > start ){
     panels[i].style.zIndex--;
   }
 }
    this.style.zIndex=panels.length - 1;
 }
  
    pStyleDrag = function() {
        this.style.opacity = '.70';
        this.style.filter = "alpha(opacity=70)";
    }
    pStyleDragEnd = function() {
       this.style.opacity = '1.0'; 
       this.style.filter = "alpha(opacity=100)";
	
    }
    
/*************************************************************************
 Name: hidePanel
 Desc: Closes a panel. 
	  
 Params: 
 theInstance - the unique id of the panel to hide (i.e. 70r_screen, 'panel_')
*************************************************************************/
function hidePanel(theInstance) { 
   if (!document.getElementById) {
   return null;
}
       
document.getElementById("panel" + "_" + theInstance).style.visibility = "hidden";
// added following line to make sure images inside <div> get hidden :: T Bowman 11.11.2009 //
document.getElementById("panel" + "_" + theInstance).style.display = "none";  	
  
}
       
	
 /*************************************************************************
  Name: showPanel
  Desc: Opens a new panel. 
  Params: 
  e           - the event triggering the swap (typically onClick)
  theInstance - the id of the panel to show (i.e. 70r_screen)
  theImgSrc   - relative path to the img (i.e. /collections/newton/img/70r.gif)
  x           - horizontal offset (in pixels) for new panel
  y           - vertical offset (in pixels) for new panel 
 *************************************************************************/
	
	
function showPanel(e, theInstance, x, y) {
	 var offx = (x)? x - 0 : 20;
	 var offy= (y)? y - 0 : 20;
      var theHandle = document.getElementById("handle" + "_" + theInstance);
	  var theRoot   = document.getElementById("panel" + "_" + theInstance);
		
	 Drag.init(theHandle, theRoot);
	 theRoot.onDragStart=makePanelTop;
	  theRoot.onDrag=pStyleDrag;
	 theRoot.onDragEnd=pStyleDragEnd;
	 panels[panels.length]=theRoot;
     panels[panels.length-1].style.zIndex = panels.length-1;
     positionPanel(e,theRoot,offx,offy);
     theRoot.style.visibility = "visible";
     // added this to change from display:none to display:block  ::  11.11.2009 T Bowman //
     theRoot.style.display = "block";  
	
	}
	
	 /*************************************************************************
	  Name: showImgPanel
	  Desc: Opens a new image panel. Distinct from showPanel in that the
	        content is written when the panel is made visible.
	  NOTE: probably this should be generalized.
	  Params: 
	  e           - the event triggering the swap (typically onClick)
	  theInstance - the id of the panel to show (i.e. 70r_screen)
	  theImgSrc   - relative path to the img (i.e. /collections/newton/img/70r.gif)
	  x           - horizontal offset (in pixels) for new panel
	  y           - vertical offset (in pixels) for new panel 
	 *************************************************************************/
	
	
	function showImgPanel(e, theInstance, theImgSrc, x, y) {
	
	  var offx = (x)? x-0 : 20;
                var offy =  (y)? y-0: 20;
               var theHandle = document.getElementById("handle" + "_" + theInstance);
	  var theRoot   = document.getElementById("panel" + "_" + theInstance);
	  var theContent   = document.getElementById("content" + "_" + theInstance);
	  var theImg = "<img title='Page image' src='/newton"+ theImgSrc + "'>";
	
		
	 Drag.init(theRoot, theRoot);
	 theRoot.onDragStart=makePanelTop;
	 theRoot.onDrag=pStyleDrag;
	 theRoot.onDragEnd=pStyleDragEnd;
     panels[panels.length]=theRoot;
     panels[panels.length-1].style.zIndex = panels.length-1;
     positionPanel(e,theRoot,offx,offy);
    
     theContent.innerHTML = theImg;
     theRoot.style.visibility = "visible";
	
	}
	
	 /*************************************************************************
	  Name: swapImgPanel
	  Desc: Wrapper function, used to bundle operations on the switch between
	        panels.
	  Params: 
	  e           - the event triggering the swap (typically onClick)
	  theInstance - the id of the panel to show (i.e. 70r_screen)
	  theImgSrc   - relative path to the img (i.e. /collections/newton/img/70r.gif)
	  toHide      - the id of the panel to hide (i.e. 72r_screen)
	  x           - horizontal offset (in pixels) for new panel
	  y           - vertical offset (in pixels) for new panel 
	 *************************************************************************/
	
    function swapImgPanel(e, theInstance, theImgSrc, toHide, x, y) {
		    
	    showImgPanel(e, theInstance,theImgSrc, x,y); 
	    hidePanel(toHide);
	}
	
	 /*************************************************************************
	  Name: positionPanel
	  Desc: Uses functions from the dyn-web libraries also in this file to
	        position panels.  
	  NOTE: This is not working so well in i.e., needs to be refactored.
	  
	  Params: 
	  e           - the event from which the position will be determined (typically onClick)
	  o           - the object to position
	  offx        - horizontal offset (in pixels) for new panel
	  offy        - vertical offset (in pixels) for new panel 
	 *************************************************************************/
	
	
function positionPanel(e, o, offX, offY) {
  var x=0, y=0; viewport.getAll();
  x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
  y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
  
  if ( x + o.offsetWidth + offX > viewport.width + viewport.scrollX )
  x = x - o.offsetWidth - offX;
  else x = x + offX;
  
  if ( y + o.offsetHeight + offY > viewport.height + viewport.scrollY )
  y = ( y - o.offsetHeight - offY > viewport.scrollY )? y - o.offsetHeight - offY : viewport.height + viewport.scrollY - o.offsetHeight;
  else y = y + offY;
  
  o.style.left = x + "px"; o.style.top = y + "px";
 
  }

  /*************************************************************************
	  Name: toggleDiv
	  Desc: This is used to selectively show or hide divs.    
	  NOTE: This is tightly coupled to the XTF browse/search results screen
	        at the moment - should be generalized.
	  
	  Params: 
	  action      - the action to perform (show | hide)
  *************************************************************************/
function toggleDiv(action) {
 
if (!document.getElementById) {
   return null;
}
   var divs =
   document.getElementsByTagName("div");
   for(var i=0; i < divs.length; i++) {
   var div = divs[i];
   var className = div.className;
   if (className == "longDisp") {
      if (action == "hide") {
         div.style.display = "none";
         changeLink = document.getElementById("dispLink");
         changeLink.innerHTML = "<a href='#' class='button' onClick='toggleDiv(\"show\");'>Long Display</a>";
      } else { // show the div 
         div.style.display = "block";
         changeLink = document.getElementById("dispLink");
         changeLink.innerHTML = "<a href='#' class='button' onClick='toggleDiv(\"hide\");'>Short Display</a>";
        }
      }
 }
} 



/*************************************************************************
This code is from Dynamic Web Coding at dyn-web.com
Copyright 2003-5 by Sharon Paine
See Terms of Use at http://www.dyn-web.com/bus/terms.html
regarding conditions under which you may use this code.
This notice must be retained in the code as is!

NOTE: This library has not been altered internally - tlcamero

*************************************************************************/

function doTooltip(e, type, msg) {
if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
Tooltip.show(e, type, msg);
}

function hideTip() {
if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
Tooltip.hide();
}

/*************************************************************************
  dw_tooltip.js   requires: dw_event.js and dw_viewport.js
  version date: May 21, 2005 moved init call to body onload
  (March 14, 2005: minor changes in position algorithm and timer mechanism)
  
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2003-5 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

/*  Readable code available for licensed users */

var Tooltip = {
  followMouse: true,
  offX: 8,
  offY: 12,
  
  ready: false,
  t1: null,
  t2: null,
  tipID: "tipDiv",
  tip: null,
  timerId: 0,
  
  init: function() {
  if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
  var el = document.createElement("DIV");
  el.className = "tooltip";
  el.id = this.tipID;
  document.body.appendChild(el);
  this.ready = true;
  }
  },
  
  hoverTipInit: function() {
  // adjust horizontal and vertical offsets here
  // (distance from mouseover event which activates tooltip)
  Tooltip.offX = 4;  
  Tooltip.offY = 4;
  tipID = "hoverTipDiv";
  Tooltip.followMouse = false;  // must be turned off for hover-tip
  dw_event.add(window, "unload", Tooltip.unHookHover, true);
  },
  
  hoverTipFinal: function() {
  // adjust horizontal and vertical offsets here
  // (distance from mouseover event which activates tooltip)
  Tooltip.offX = 8;  
  Tooltip.offY = 12;
  tipID = "tipDiv";
  Tooltip.followMouse = true;  // must be turned off for hover-tip
  },
  
  show: function(e, type, msg) {
  if (this.t1) clearTimeout(this.t1);	
  if (this.t2) clearTimeout(this.t2); 
  this.tip = document.getElementById( this.tipID );
  // set up mousemove 
  if (this.followMouse) 
  dw_event.add( document, "mousemove", this.trackMouse, true );
  this.writeTip("");  // for mac ie
  
  /* TOOLTIP CODES for JavaScript */
  switch (type) {
	case 'H': ttype = ''; allital = 'yes'; break;		
	case 'DATE' : ttype = ''; allital = 'yes'; break; 
	case 'DA' : ttype = 'Disjunctive alternative; '; allital = 'yes'; break; 
	case 'DACB' : ttype = 'Disjunctive alternative; '; allital = 'yes'; break;
	case 'IC': ttype = 'illegible; reason: cancelled and illegible'; allital = 'yes'; break;  
	case 'IU': ttype = 'illegible; reason: unknown '; allital = 'yes'; break;
	case 'IK': ttype = 'illegible; reason: '; allital = 'yes'; break;  
	case 'U': ttype = 'unclear; reason: '; allital = 'yes'; break;
	case 'C': ttype = 'correct: '; allital = 'no'; break;  
	case 'ER': ttype = 'erroneous repetition'; allital = 'yes'; break;
	case 'S': ttype = 'supplied; reason: '; allital = 'yes'; break;
	default : ttype = ''; allital = 'yes'; break;		 
  }
  
  if (allital == 'yes') { this.writeTip('<em>' + ttype + msg + '</em>'); }
  else {this.writeTip('<em>' + ttype + '</em>' + msg); }
  viewport.getAll();
  this.positionTip(e);
  this.t1 = setTimeout("document.getElementById('" + Tooltip.tipID + "').style.visibility = 'visible'",200);	
  },
  
  writeTip: function(msg) {
  if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
  },
  
  positionTip: function(e) {
  var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
  var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
  
  if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX )
  x = x - this.tip.offsetWidth - this.offX;
  else x = x + this.offX;
  
  if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY )
  y = ( y - this.tip.offsetHeight - this.offY > viewport.scrollY )? y - this.tip.offsetHeight - this.offY : viewport.height + viewport.scrollY - this.tip.offsetHeight;
  else y = y + this.offY;
  
  this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
  },
  
  hide: function() {
  if (this.t1) clearTimeout(this.t1);	
  if (this.t2) clearTimeout(this.t2); 
  this.t2 = setTimeout("document.getElementById('" + this.tipID + "').style.visibility = 'hidden'",200);
  // release mousemove
  if (this.followMouse) 
  dw_event.remove( document, "mousemove", this.trackMouse, true );
  this.tip = null;
  },
  
  tipOutCheck: function(e) {
  e = dw_event.DOMit(e);
  // is element moused into contained by tooltip?
  var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
  if ( this != toEl && !this.contained(toEl, this) ) this.hide();
  },

  contained: function(oNode, oCont) { // returns true of oNode is contained by oCont (container)
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
  },
  
  clearTimer: function() {
  if (this.timerId) { clearTimeout(this.timerId); this.timerId = 0; }
  },

  unHookHover: function () {
    var tip = document.getElementById? document.getElementById(this.tipID): null;
    if (tip) {
        tip.onmouseover = null; 
        tip.onmouseout = null;
        tip = null;
    }
  },
  
  trackMouse: function(e) {
  e = dw_event.DOMit(e);
  Tooltip.positionTip(e);	
  }
  
  }

/*************************************************************************
    dw_event.js (version date Feb 2004)
    
    This code is from Dynamic Web Coding at http://www.dyn-web.com/
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    regarding conditions under which you may use this code.
    This notice must be retained in the code as is!
    *************************************************************************/
    
    var dw_event = {
    
    add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
    }, 
    
    remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
    }, 
    
    DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
    
    return e;
    }
    
    }
    
    
    /*************************************************************************
    
    dw_viewport.js
    version date Nov 2003
    
    This code is from Dynamic Web Coding 
    at http://www.dyn-web.com/
    Copyright 2003 by Sharon Paine 
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    regarding conditions under which you may use this code.
    This notice must be retained in the code as is!
    
    *************************************************************************/  
    
    var viewport = {
    getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
    this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
    this.width = document.body.clientWidth;
    },
    
    getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
    else if (document.documentElement && document.documentElement.clientHeight) 
    this.height = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight) 
    this.height = document.body.clientHeight;
    },
    
    getScrollX: function () {
    this.scrollX = 0;
    if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
    this.scrollX = document.documentElement.scrollLeft;
    else if (document.body && document.body.scrollLeft) 
    this.scrollX = document.body.scrollLeft; 
    else if (window.scrollX) this.scrollX = window.scrollX;
    },
    
    getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
    this.scrollY = document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop) 
    this.scrollY = document.body.scrollTop; 
    else if (window.scrollY) this.scrollY = window.scrollY;
    },
    
    getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
    }
    
    }
    
 /**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * This library was borrowed from youngpup.net,
 * it has not been altered internally - tlcamero
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};






    
