/* jQuery javascript functions for pop-up abstract box */
var gHoverAbsMetrics = {
	x: -9999, y: 10,
	citW: 0, citH: 0,
	defaultW: 405
};

$(document).ready(function() {

	/* set up abs rollover (after short delay) */
	if (callbackToken && allowsCookies()) {
		setTimeout("addAbsPops()", 25);
	}
	
});

var gFloatAbs = {
	deferRef: null, deferFor: null,
	citHovRef: null, absHovRef: null, dispAbsRef: null
};
var gAbsDims = {
	cursX: 20, cursY: -20,
	pad: { top:15, left: 20, right: 30, bottom: 35 }
};

//var popupViews=['abstract','extract','excerpt'];

function addAbsPops() {
	var popupViews;
	if ((gSiteOptions == undefined) || (gSiteOptions.popupViews == undefined)) {
		popupViews = ['abstract'];
	}
	else {
		popupViews = gSiteOptions.popupViews;
	}
	
	var absCits = null;
	var matchExpr;
	for (var i = 0; i < popupViews.length; i++) {
		matchExpr = ".cit:has(div.cit-extra a[rel='" + popupViews[i] + "'])";
		if ((absCits == null) || !(absCits.length)) {
			/* debugOut("defining absCits using matchExpr: " + matchExpr); */
			absCits = $(matchExpr);
		}
		else {
			/* debugOut("adding to absCits using matchExpr: " + matchExpr); */
			absCits = absCits.add(matchExpr);
		}
		/* debugOut("absCits now has length: " + absCits.length); */
	}
	if (absCits.length) {
		absCits.each(
			function() {
				var $this = $(this);
				var thisW = this.offsetWidth;
				var thisH = this.offsetHeight;
				var absLink = null;
				var matchExpr;
				for (var i = 0; i < popupViews.length; i++) {
					if ((absLink == null) || !(absLink.length)) {
						matchExpr = "div.cit-extra a[rel='" + popupViews[i] + "']";
						absLink = $this.find(matchExpr);
					}
				}
				if (absLink.length) {
					var newCitHover = absLink.attr("href");
					$this.hover(
						function() {
							/* if we're displaying abs for different citation, remove it */
							if (gFloatAbs.dispAbsRef && (gFloatAbs.dispAbsRef != newCitHover)) {
								hideAbs(true);
							}
							/* if we're already displaying an abstract it must be for this cit */
							if (gFloatAbs.dispAbsRef != null) {
								gFloatAbs.citHovRef = newCitHover;
							}
							else {
								if (gFloatAbs.deferRef != null) {
									clearTimeout(gFloatAbs.deferRef);
									gFloatAbs.deferRef = null;
									gFloatAbs.deferFor = null;
								}
								var citOffset = $this.offset(); /* top, left */
								gFloatAbs.deferFor = newCitHover;
								gHoverAbsMetrics.x = citOffset.left + thisW;
								gHoverAbsMetrics.y = citOffset.top;
								gHoverAbsMetrics.citW = thisW;
								gHoverAbsMetrics.citH = thisH;
								var cmd = "showAbs('" + newCitHover + "')";
								gFloatAbs.deferRef = setTimeout(cmd, 500); /*1000);*/
								gFloatAbs.citHovRef = newCitHover;
							}
						},
						function() {
							gFloatAbs.citHovRef = null;
							setTimeout("hideAbs()", 20);
						}
					);
				}
			}
		);
	}
}

function rollOffCit(href) {
	hideAbs();
}

function showAbs(absHref) {
	/** timer went off **/
	gFloatAbs.deferRef = null;
	gFloatAbs.deferFor = null;

	gFloatAbs.dispAbsRef = absHref;
	var newDiv = '<div id="hovering-abs"></div><div id="hovering-abs-ptr"></div>';
	$("body").append(newDiv);
	$("div#hovering-abs").css("left", "-9999px").css("top", "10px").css("position", "absolute");
	$("div#hovering-abs-ptr").css("left", "-9999px").css("top", "10px").css("position", "absolute");

	var ajaxUrl = document.location.protocol + "//" + document.location.host + absHref;
	if ((typeof(gSiteOptions.popabsParam) != "undefined") && (gSiteOptions.popabsParam.length > 0)) {
		// add on param
		if (ajaxUrl.indexOf('?') >= 0) {
			ajaxUrl = (ajaxUrl + "&" + gSiteOptions.popabsParam);
		}
		else {
			ajaxUrl = (ajaxUrl + "?" + gSiteOptions.popabsParam);
		}
	}
	$.ajax({
		url: ajaxUrl,
		dataType: "html",
		type: "GET",
		beforeSend: addAbsDirHeaders,
		error: ajaxErr,
		success: addHoverAbs,
		complete: ajaxComplete
	});

}
function addAbsDirHeaders(req) {
	addCommonHeaders(req);
	addPartHeaders(req);
}
function ajaxErr(req, msg, e) {
}
function ajaxComplete(req, msg) {
}
function hideAbs() {
	hideAbs(false);
}
function hideAbs(force) {
	if ((gFloatAbs.deferRef != null) && ((gFloatAbs.citHovRef == null) || (gFloatAbs.deferFor != gFloatAbs.citHovRef))) {
		clearTimeout(gFloatAbs.deferRef);
		gFloatAbs.deferRef = null;
		gFloatAbs.deferFor = null;
	}
	if (((gFloatAbs.citHovRef == null) && (gFloatAbs.absHovRef == null)) || force) {
		var hoverAbs = $("div#hovering-abs");
		var hoverAbsPtr = $("div#hovering-abs-ptr");
		if (hoverAbs.length) {
			hoverAbs.remove();
			if (hoverAbsPtr) {
				hoverAbsPtr.remove();
			}
		}
		gFloatAbs.dispAbsRef = null;
	}
}
/* ajax callback- show abs */
function addHoverAbs(xhtmlData) {
	// if we get back a whole html page instead of a fragment, don't display
	if (xhtmlData && !(xhtmlData.indexOf('<html') >= 0)) {
		var pageDim = getViewportDim();
		var hoverAbs = $("div#hovering-abs");
		var hoverAbsPtr = $("div#hovering-abs-ptr");
		if (hoverAbs.length) {
			hoverAbs.empty().append('<div id="hovering-abs-close"><a href="#">[x] <span>close</span></a></div>').append(xhtmlData);
			$("div#hovering-abs-close a").click(
				function(e) {
					hideAbs(true);
					e.preventDefault();
				}
			);
			var pageOffset = getPageOffset();
			var height = getObjHeight(hoverAbs); //height();
			var width = Math.min(gHoverAbsMetrics.defaultW, pageDim.x - (gAbsDims.pad.left + gAbsDims.pad.right));
			var forceWidth = false;
			/* x,y is upper left of abs */
			/* ptrX, ptrY is upper left of ptr */
			var x = gHoverAbsMetrics.x;
			if ((x + width + gAbsDims.pad.right) > (pageDim.x + pageOffset.x)) {
				x = ((pageDim.x + pageOffset.x) - (width + gAbsDims.pad.right));
				if ((gHoverAbsMetrics.x - x) > 200) {
					forceWidth = true;
					width = Math.max(width - (gHoverAbsMetrics.x - x) + 200, 200);
					x = gHoverAbsMetrics.x - 200;
				}
			}
			var minY = pageOffset.y + gAbsDims.pad.top;
			var y = Math.max(Math.min(Math.max((gHoverAbsMetrics.y + (gHoverAbsMetrics.citH / 2) - (height / 2) + gAbsDims.pad.top), minY), ((pageDim.y + pageOffset.y) - (height + gAbsDims.pad.bottom))), minY);
			if ((height + gAbsDims.pad.top + gAbsDims.pad.bottom) > pageDim.y) {
				height = pageDim.y - (gAbsDims.pad.top + gAbsDims.pad.bottom);
			}
			var ptrX = x - 25;
			var ptrY = Math.min(Math.max(gHoverAbsMetrics.y + (gHoverAbsMetrics.citH / 2) - 16, y+16), (y + height)-32);
					
			hoverAbsPtr.hide().css("height", "32px").css("width", "32px").css("left", "" + ptrX + "px").css("top", "" + ptrY + "px");
			hoverAbs.hide().css("height", "" + height + "px").css("left", "" + x + "px").css("top", "" + y + "px");
			if (forceWidth) {
				hoverAbs.css("width", "" + width + "px");
			}
			hoverAbs.fadeIn(200);
			hoverAbsPtr.fadeIn(200);
			hoverAbs.hover(rollOverAbs, rollOffAbs);
			hoverAbsPtr.hover(rollOverAbs, rollOffAbs);
		}
	}
}
function rollOverAbs(e) {
	gFloatAbs.absHovRef = gFloatAbs.dispAbsRef;
}
function rollOffAbs(e) {
	gFloatAbs.absHovRef = null;
	setTimeout("hideAbs()", 20);
}