// tracks the most recent popup
var currentPopup = null;
var popCurtain = null;
var reportURL = null;

// hide popup when the window resizes
window.onresize = function() {
	if(currentPopup != null) {
		hidePopover(currentPopup.id);
	}
};
// hide popup when the window scrolls
window.onscroll = function() {
	if(currentPopup != null) {
		hidePopover(currentPopup.id);
	}
};

/**
 * Update the size of the curtain to the current window size.
 */
function sizeCurtain() {
	var winW = 630, winH = 460;
//	if (document.body && document.body.offsetWidth) {
//	 winW = document.body.offsetWidth;
//	 winH = document.body.offsetHeight;
//	}
//	if (document.compatMode=='CSS1Compat' &&
//	    document.documentElement &&
//	    document.documentElement.offsetWidth ) {
//	 winW = document.documentElement.offsetWidth;
//	 winH = document.documentElement.offsetHeight;
//	}
//	if (window.innerWidth && window.innerHeight) {
//	 winW = window.innerWidth;
//	 winH = window.innerHeight;
//	}
	
	winW = f_clientWidth();
	winH = f_clientHeight();
	
	document.getElementById('PopCurtain').style.width = winW + 'px';
	document.getElementById('PopCurtain').style.height = winH + 'px';
}

/**
 * Hide the curtain and the object with the id of the objName.
 * 
 * @param objName - the name of the object to hide
 */
function revealPopover(objName) {
	if(validate(objName)) {
		fullCenterInWindow(document.getElementById(objName));
		sizeCurtain();
		popCurtain.style.visibility = 'visible';
		popCurtain.style.top = f_scrollTop() + "px";
		currentPopup.style.display = 'block';
	}
}

/**
 * Reveal the curtain and the object with the id of the objName.
 * 
 * @param objName - the name of the object to show
 */
function hidePopover(objName) {
	if(validate(objName)) {
		currentPopup.style.display='none';
		currentPopup = null;
		popCurtain.style.visibility = 'hidden';
		popCurtain.style.width = '0px';
		popCurtain.style.height = '0px';
	}
}

/**
 * Reveal the object with the id 'CartAddPopover'.
 * 
 * @param imgsrc - the image url
 * @param title - the book title
 */
function revealCartAddPopover(imgsrc, title) {
	if(imgsrc != '') {
		document.getElementById('image').src = imgsrc;
	} else {
		try {
		document.getElementById('imageTD').style.display = 'none';
		} catch(e) {
			alert('no imageTD');
		}
	}
	try {
		document.getElementById('head').innerHTML = title;
	} catch(e) {
		document.getElementById('head').firstChild.nodeValue = title;
	}
	revealPopover('CartAdd');
}

/**
 * Verify that the popup and the popup curtain both exist and set the global variables currentPopup and popCurtain.

 * @param popupName - name of the popup to validate
 * @returns whether the setup and validation succeeded
 */
function validate(popupName) {
	var success = true;

	var popup = document.getElementById(popupName);
	if(popup != null) {
		currentPopup = popup;
	} else {
		alert('The popup '+popupName+' was not found on the page.');
		success = false;
	}
	
	var curtain = document.getElementById('PopCurtain');
	if (curtain != null) {
		popCurtain = curtain;
	} else {
		alert('The curtain '+PopCurtain+' was not found on the page.');
		success = false;		
	}
	return success;
}

/**
 * horizontally centers the parameter in the viewable area based on the elements
 * style.width property NOTE: If the element does not have the style.width
 * property set a default width of 620 will be assumed.
 * 
 * @param ob - the element to be centered
 * @return void
 */
function centerInWindow(ob) {
//	alert('centerInWindow('+ob+')');
	var body_width = 0;
	
//	if (navigator.appName == "Microsoft Internet Explorer") {
//		body_width = document.body.parentNode.clientWidth;
//	} else {
//		body_width = window.innerWidth;
//	}
	var popup_width = parseInt(ob.style.width);
	if(isNaN(popup_width)) {
		popup_width = 500;
	}
	
	body_width = f_clientWidth();
	
	left_position = parseInt((body_width/2) - (popup_width/2));
	ob.style.position='absolute';
	ob.style.left=left_position+'px';
}

/**
 * attempts to basically vertically center the parameter in the viewable area.
 * Because the style.height is unreliable it simply sets the style.top to 25% of
 * the way down the viewable area.
 * 
 * @param ob - the element to be centered
 * @return void
 */
function vCenterInWindow(ob, evt) {
	var body_height = 0;
	var scroll_dist = 0;

//	if (navigator.appName == "Microsoft Internet Explorer") {
//		body_height = document.body.parentNode.clientHeight;
//		scroll_dist = document.documentElement.scrollTop;
//	} else {
//		body_height = window.innerHeight;
//		scroll_dist = window.pageYOffset;
//	}
	
	body_height = f_clientHeight();
	scroll_dist = f_scrollTop();

	top_position = parseInt(body_height/4) + scroll_dist;
	ob.style.top=top_position+'px';
}

/**
 * an interim method that calls centerInWindow() and vCenterInWindow() on the
 * parameter
 * 
 * @param ob - the element to be centered
 * @return void
 */
function fullCenterInWindow(ob) {
//	alert('fullCenterInWindow('+ob+')');
	centerInWindow(ob);
	vCenterInWindow(ob, null);
}

/**
 * calls fullCenterInWindow() for each of the elements defined by the global
 * popups array
 * 
 * @return void
 */
function fullCenterAllInWindow() {
	for(var i=0; i<popups.length; i++) {
		if(document.getElementById(popups[i])) {
			fullCenterInWindow(document.getElementById(popups[i]));
		}
	}
}

/**
 * Find an object based on the parameters.
 * @param regexp - the regular expression for which to search
 * @param element - the element base from which to search (document is used if this is 'undefined')
 * @param tagName - the type of element searched for
 * @returns the found element
 */
function getElementsByRegExpId(regexp, element, tagName) {
	// input[type="text"]
	var re = new RegExp(regexp);
	element = element === undefined ? document : element;
	tagName = tagName === undefined ? 'input' : tagName;
	p_tagType = 'submit';
	var elements = element.getElementsByTagName(tagName);
	var el = null;
	
	for(i in elements) {
		el = elements[i];
		if(el.type == p_tagType && re.exec(el.id) != null) {
			return el;
		}
	}
	return null;
}

/**
 * Sets the checked flag.
 * <ol>
 * <li>Find the first 'TABLE' above the order parameter</li>
 * <li>Find all 'INPUT' tags of that table</li>
 * <li>Set the checked flag of all the children to the opposite of the checked flag of the order parameter</li>
 * </ol>
 * @param order the base object from which to search
 */
function checkChildren(order) {
	// alert('begin: ' + order);
	var tbl = findParent(order, 'TABLE');
	var childs = tbl.parentNode.getElementsByTagName('INPUT');
	for(var x=1; x<childs.length; x++) {
		// alert(childs.item(x));
		childs.item(x).checked = ! order.checked;
	}
}

/**
 * Find the first parent of the ob parameter that is of type parameter.
 * @param ob - the base object from which to search
 * @param type - the type of parent object for which to search
 * @returns the found parent object
 */
function findParent(ob, type) {
	var parent = ob.parentNode;
	// alert('Object: ' + ob + ' type=' + ob.tagName + '\nParent: ' + parent + '
	// type=' + parent.tagName);
	if(parent == null) {
		return null;
	} else if (parent.tagName == type) {
		return parent;
	} else {
		return findParent(parent, type);
	}
}

/*** Thanks to Knowledge Base (http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html) ***/

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

