/*************************************************************************
  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!
*************************************************************************/
// COMPLETE REQUESTED ACTION - EITHER ADD OR REMOVE BOOK.  PHP CODE DECIDES
// ONCE COMPLETE THEM UPDATE readingListAction HTML TEXT
function readingListCommand(sId) {
	var oXmlHttp = zXmlHttp.createRequest();
	var linkText = document.getElementById("reading-list-command" + sId);
	
	oXmlHttp.open("get", "scripts/reading-list-action.php?id=" + sId, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
					var response = oXmlHttp.responseText;
					var compResponse = response.substring(2,8);
					
					if(compResponse == 'insert'){
						linkText.innerHTML = "Remove from Reading List";
					} else{
						linkText.innerHTML = "Add to Reading List";
					}
					
					var pageLocation = location.href;
					var len = pageLocation.length;
					var subPL = pageLocation.substring(len - 12);
					if(subPL == 'booklist.php'){
						document.location.href = pageLocation;	
					}
					
			}
			else{
				document.write('error');
			}
		}            
	};
	oXmlHttp.send(null);
}

// DETERMINE WHAT READING LIST ACTION SHOULD BE
// OPTIONS:
// ADD TO READING LIST
// REMOVE FROM READING LIST
function readingListAction(isbn, user_id){
	var linkText = document.getElementById("reading-list-command" + isbn);
	
	if(user_id != ""){
	
		var oXmlHttp = zXmlHttp.createRequest();
		oXmlHttp.open("get", "scripts/check-reading-list.php?id=" + isbn, true);
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					var response = oXmlHttp.responseText;
					var compResponse = response.substring(2,6);
					
					if(compResponse == 'true'){
						linkText.innerHTML = "Remove from Reading List";
					} else{
						linkText.innerHTML = "Add to Reading List";
					}
				}
				else{
					document.write('error');
				}
			}            
		};
		oXmlHttp.send(null);
	}
}

var menuLayers = {
  timer: null,
  activeMenuID: null,
  offX: 4,   // horizontal offset 
  offY: 6,   // vertical offset 
  show: function(id, e, isbn, user_id) {
    var mnu = document.getElementById? document.getElementById(id): null;
    if (!mnu) return;
    this.activeMenuID = id;
    if ( mnu.onmouseout == null ) mnu.onmouseout = this.mouseoutCheck;
    if ( mnu.onmouseover == null ) mnu.onmouseover = this.clearTimer;
    viewport.getAll();
    this.position(mnu,e);
	readingListAction(isbn, user_id);
  },
  
  hide: function() {
    this.clearTimer();
    if (this.activeMenuID && document.getElementById) 
      this.timer = setTimeout("document.getElementById('"+menuLayers.activeMenuID+"').style.visibility = 'hidden'", 200);
  },
  
  position: function(mnu, e) {
    var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
    var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;
    
    if ( x + mnu.offsetWidth + this.offX > viewport.width + viewport.scrollX )
      x = x - mnu.offsetWidth - this.offX;
    else x = x + this.offX;
  
    if ( y + mnu.offsetHeight + this.offY > viewport.height + viewport.scrollY )
      y = ( y - mnu.offsetHeight - this.offY > viewport.scrollY )? y - mnu.offsetHeight - this.offY : viewport.height + viewport.scrollY - mnu.offsetHeight;
    else y = y + this.offY;
    
    mnu.style.left = x + "px"; mnu.style.top = y + "px";
    this.timer = setTimeout("document.getElementById('" + menuLayers.activeMenuID + "').style.visibility = 'visible'", 200);
  },
  
  mouseoutCheck: function(e) {
    e = e? e: window.event;
    // is element moused into contained by menu? or is it menu (ul or li or a to menu div)?
    var mnu = document.getElementById(menuLayers.activeMenuID);
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( mnu != toEl && !menuLayers.contained(toEl, mnu) ) menuLayers.hide();
  },
  
  // returns true of oNode is contained by oCont (container)
  contained: function(oNode, oCont) {
    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 (menuLayers.timer) clearTimeout(menuLayers.timer);
  }
  
}