//
// for the following to work need:
// DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN
//
var theBookID;
var thePopupType;
var showingPopup = false;

var __isIE =  navigator.appVersion.match(/MSIE/);
var __userAgent = navigator.userAgent;
var __isFireFox = __userAgent.match(/firefox/i);
var __isFireFoxOld = __isFireFox && 
   (__userAgent.match(/firefox\/2./i) || __userAgent.match(/firefox\/1./i));
var __isFireFoxNew = __isFireFox && !__isFireFoxOld;

$(document).ready(function(){
   $(document).mousemove(function(e){
   	  if (showingPopup) {
				eBID = document.getElementById("bookInfoDiv");
				eBIDxy = getElementAbsolutePos(eBID);
	      // $('#mousePos').html('BID: ' + eBID.clientWidth + ' x ' + eBID.clientHeight + '@ ' + eBIDxy.x + ', ' + eBIDxy.y + '---' + 'Mouse: ' + e.pageX +', '+ e.pageY);

				if (popupPos == "R") {
					yLimitTop = eBIDxy.y;       yLimitBot = eBIDxy.y + eBID.clientHeight + 18;
					xLimitLef = eBIDxy.x - 100; xLimitRig = eBIDxy.x + eBID.clientWidth  + 18;
					if (e.pageX < (xLimitLef + 100)) {
						yLimitTop = yLimitTop + (eBID.clientHeight / 2) - 55; yLimitBot = yLimitTop + 129;
					}
					if ( ( e.pageY < yLimitTop ) || ( e.pageY > yLimitBot ) || ( e.pageX < xLimitLef ) || ( e.pageX > xLimitRig ) ) bookHide();
				} // popupPos == "R"
			
				if (popupPos == "L") {
					yLimitTop = eBIDxy.y; yLimitBot = eBIDxy.y + eBID.clientHeight +  18;
					xLimitLef = eBIDxy.x; xLimitRig = eBIDxy.x + eBID.clientWidth  + 120;
					if (e.pageX > (xLimitRig - 120)) {
						yLimitTop = yLimitTop + (eBID.clientHeight / 2) - 55; yLimitBot = yLimitTop + 129;
					}
					if ( ( e.pageY < yLimitTop ) || ( e.pageY > yLimitBot ) || ( e.pageX < xLimitLef ) || ( e.pageX > xLimitRig ) ) bookHide();
				} // popupPos == "R"
			
				if (popupPos == "T") {
					yLimitTop = eBIDxy.y; yLimitBot = eBIDxy.y + eBID.clientHeight + 45;
					xLimitLef = eBIDxy.x; xLimitRig = eBIDxy.x + eBID.clientWidth + 18;
					if ( ( e.pageY < yLimitTop ) || ( e.pageY > yLimitBot ) || ( e.pageX < xLimitLef ) || ( e.pageX > xLimitRig ) ) bookHide();
				} // popupPos == "R"
			
				if (popupPos == "B") {
					yLimitTop = eBIDxy.y - 25; yLimitBot = eBIDxy.y + eBID.clientHeight + 18;
					xLimitLef = eBIDxy.x; xLimitRig = eBIDxy.x + eBID.clientWidth + 18;
					if ( ( e.pageY < yLimitTop ) || ( e.pageY > yLimitBot ) || ( e.pageX < xLimitLef ) || ( e.pageX > xLimitRig ) ) bookHide();
				} // popupPos == "R"
	  } // showing Popup
   }); 
})

function __parseBorderWidth(width) {
    var res = 0;
    if (typeof(width) == "string" && width != null 
                && width != "" ) {
        var p = width.indexOf("px");
        if (p >= 0) {
            res = parseInt(width.substring(0, p));
        }
        else {
             //do not know how to calculate other 
             //values (such as 0.5em or 0.1cm) correctly now
             //so just set the width to 1 pixel
            res = 1; 
        }
    }
    return res;
}

//returns border width for some element
function __getBorderWidth(element) {
    var res = new Object();
    res.left = 0; res.top = 0; res.right = 0; res.bottom = 0;
    if (window.getComputedStyle) {
        //for Firefox
        var elStyle = window.getComputedStyle(element, null);
        res.left = parseInt(elStyle.borderLeftWidth.slice(0, -2));  
        res.top = parseInt(elStyle.borderTopWidth.slice(0, -2));  
        res.right = parseInt(elStyle.borderRightWidth.slice(0, -2));  
        res.bottom = parseInt(elStyle.borderBottomWidth.slice(0, -2));  
    }
    else {
        //for other browsers
        res.left = __parseBorderWidth(element.style.borderLeftWidth);
        res.top = __parseBorderWidth(element.style.borderTopWidth);
        res.right = __parseBorderWidth(element.style.borderRightWidth);
        res.bottom = __parseBorderWidth(element.style.borderBottomWidth);
    }
   
    return res;
}

//	getElementAbsolutePos:
// 		returns absolute position of some element within document
//		Example Use:
// 			var pos = getElementAbsolutePos(myElement);
// 			window.alert("Element's left: " + pos.x + " and top: " + pos.y);

function getElementAbsolutePos(element) {
    var res = new Object();
    res.x = 0; res.y = 0;
    if (element !== null) {
        res.x = element.offsetLeft;
        res.y = element.offsetTop;
        
        var offsetParent = element.offsetParent;
        var parentNode = element.parentNode;
        var borderWidth = null;

        while (offsetParent != null) {
            res.x += offsetParent.offsetLeft;
            res.y += offsetParent.offsetTop;
            
            var parentTagName = offsetParent.tagName.toLowerCase();    

            if ((__isIE && parentTagName != "table") || 
                (__isFireFoxNew && parentTagName == "td")) {            
                borderWidth = __getBorderWidth(offsetParent);
                res.x += borderWidth.left;
                res.y += borderWidth.top;
            }
            
            if (offsetParent != document.body && 
                offsetParent != document.documentElement) {
                res.x -= offsetParent.scrollLeft;
                res.y -= offsetParent.scrollTop;
            }

            //next lines are necessary to support FireFox problem with offsetParent
               if (!__isIE) {
                while (offsetParent != parentNode && parentNode !== null) {
                    res.x -= parentNode.scrollLeft;
                    res.y -= parentNode.scrollTop;
                    
                    if (__isFireFoxOld) {
                        borderWidth = __getBorderWidth(parentNode);
                        res.x += borderWidth.left;
                        res.y += borderWidth.top;
                    }
                    parentNode = parentNode.parentNode;
                }    
            }

            parentNode = offsetParent.parentNode;
            offsetParent = offsetParent.offsetParent;
        }
    }
    return res;
}

function setXYonRight (xp, yp) { // Popup to the right centered
	popupPos = "R";
  e = document.getElementById("bookInfoDiv");
	xAdj = -2;
	yAdj = -10;
  e.style.left = (xp + 12 + xAdj) + 'px';

  theH = e.clientHeight;
  if ( (theH % 2) != 0) theH++;
  yOff = theH / 2;
  e.style.top  = (yp - yOff + yAdj) + 'px'; // yp - 1/2 height of the div

  eA = document.getElementById("bookInfoArrowLEFT");
  eA.style.left = (xp + xAdj) + 'px';
  eA.style.top  = (yp + 4 + yAdj) + 'px'; // yp - 1/2 height of the div
}
		
function setXYonLeft (xp, yp) { // Popup to the left centered
	popupPos = "L";
  e = document.getElementById("bookInfoDiv");
	xAdj = -665;
	yAdj = -10;
  e.style.left = (xp + 12 + xAdj) + 'px';

  theH = e.clientHeight;
  if ( (theH % 2) != 0) theH++;
  yOff = theH / 2;
  e.style.top  = (yp - yOff + yAdj) + 'px'; // yp - 1/2 height of the div

  eA = document.getElementById("bookInfoArrowRIGHT");
  eA.style.left = (xp + 12 + xAdj + 548) + 'px';
  eA.style.top  = (yp + 4 + yAdj) + 'px'; // yp - 1/2 height of the div
}

function setXYonTop (xp, yp) { // Popup above left aligned
	popupPos = "T";
  e = document.getElementById("bookInfoDiv");
	xAdj = 0;
	yAdj = -24;
  e.style.left = (xp + xAdj - 20) + 'px';

  theH = e.clientHeight;
  // if ( (theH % 2) != 0) theH++;
  yOff = -theH;
  
  e.style.top  = (yp + yOff + yAdj) + 'px'; // yp - height of the div

  eA = document.getElementById("bookInfoArrowBOTTOM");
  eA.style.left = (xp + xAdj) + 'px';
  eA.style.top  = (yp + yAdj + 16) + 'px';
}

function setXYonBtm (xp, yp) { // Popup below left aligned
	popupPos = "B";
  e = document.getElementById("bookInfoDiv");
	xAdj = 0;
	yAdj = 23;
  e.style.left = (xp + xAdj - 20) + 'px';

  yOff = 0;
  
  e.style.top  = (yp + yOff + yAdj) + 'px'; // yp - height of the div

  eA = document.getElementById("bookInfoArrowTOP");
  eA.style.left = (xp + xAdj) + 'px';
  eA.style.top  = (yp + yAdj - 12) + 'px';
}

function bookShow(bookID, popupType) {
	// alert ("bookShow(), bookID / popupType = " + bookID + " / " + popupType);
	bookHide();
	theBookID = bookID;
	thePopupType = popupType;
	loadBook(bookID);
}

function revealBook(bookID) {
		// alert ("revealBook(), entry..., bookID= "+bookID+", thePopupType= "+thePopupType);
		var elid = "img_"+bookID;
		eM = document.getElementById(elid);

 		var scrYTop  = $(window).scrollTop();
		var scrXLeft = $(window).scrollLeft();
				
	  var scrXSize = $(window).width();
	  var scrYSize = $(window).height();

 		var pos = getElementAbsolutePos(eM);
		var theXp = pos.x;
		var theYp = pos.y; // absolute upper/left position of the element "img_###"
		theMW = eM.clientWidth;
		theMH = eM.clientHeight;

		var midScrX = scrXSize / 2;
		var midScrY = scrYSize / 2;

		var imgXPos = theXp - scrXLeft;
		var imgYPos = theYp - scrYTop;
		// alert ("midScrX / midScrY="+midScrX+" / " +midScrY+ ", imgXPos / imgYPos= " +imgXPos+ " / " +imgYPos);

		showingPopup = true;
		if ( (thePopupType == "V") || (thePopupType == "A") ) {
				// alert ("revealBook(), showing div: bookInfoDiv");
				MM_showHideLayers('bookInfoDiv','','show');
				if (imgYPos > midScrY) {
					setXYonTop (theXp, theYp);
					MM_showHideLayers('bookInfoArrowBOTTOM','','show');
				}
				else {
					setXYonBtm (theXp, theYp);
					MM_showHideLayers('bookInfoArrowTOP','','show');
				}
		}
		else {
			theXp = theXp + theMW; // theXP right side of element "img_###"
	
			if ( (theMH % 2) != 0) theMH++;
			theYp = theYp + (theMH / 2); // theYP middle vertically element "img_###"
						
			MM_showHideLayers('bookInfoDiv','','show');
		
			if (imgXPos < midScrX) {
				setXYonRight (theXp, theYp);
				MM_showHideLayers('bookInfoArrowLEFT','','show');
			} else {
				setXYonLeft (theXp, theYp);
				MM_showHideLayers('bookInfoArrowRIGHT','','show');
			}
		}

}

function bookHide() {
		MM_showHideLayers('bookInfoDiv','','hide');
		MM_showHideLayers('bookInfoArrowLEFT','','hide');
		MM_showHideLayers('bookInfoArrowRIGHT','','hide');
		MM_showHideLayers('bookInfoArrowBOTTOM','','hide');
		MM_showHideLayers('bookInfoArrowTOP','','hide');
		showingPopup = false;
		// MM_showHideLayers('bookInfoBEHIND','','hide');
		loadBook("empty");
}
var xmlHttp;
function loadBook(str) {
	// alert ("loadBook(), entry..., str= "+str);
	if (str == "empty") {
  		theCode='<span class="navPri">[loading book info...]</span>';
  		document.getElementById("bookInfoDiv").innerHTML=theCode;
	} else {
	  doRequest(str);
	}
}
//
function doRequest (str) {
		xmlHttp = GetXmlHttpObject()
		if (xmlHttp == null) {
			alert ("Browser does not support HTTP Request");
			return;
		}
		var url="includes/bookPopup.php";
		if (thePopupType == "V") var url="includes/bookPopup2.php";
		if (thePopupType == "A") var url="includes/bookPopupAW.php";
		url=url+"?bookID="+str;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
//
function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("bookInfoDiv").innerHTML=xmlHttp.responseText;
		e=document.getElementById("bookInfoDiv");
		//alert ("stateChanged, bookInfoDiv height="+e.clientHeight);
		revealBook(theBookID);
	} 
}
//
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)	{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
  return xmlHttp;
}
