// Popup prototype JS - v0.01 - Paul Strapps elab
var offsetY = 25;
if (IE){
	offsetY=offsetY-48;
}

window.onload = function(){
	//register event listener for mouse
	
	// this has been obsolete circa 1995
	//if (!IE){
	//	document.captureEvents(Event.MOUSEMOVE);
	//	document.captureEvents(Event.MOUSEDOWN);
	//}
	document.onmousemove = getMouseXY;
	Position.prepare();
}

/*
	popupId : the div of the whole popup
	contentId : the div inside the popup where the content will go
*/
function showPopup(parent, contentId, popupId, url, params){
	this.popupParent = parent;
	this.popupId = popupId;
	this.contentId = contentId;
	document.body.style.cursor = "progress";	
	if (params!=undefined){
		new Ajax.Updater(contentId, url,{method:'post',onComplete:showPopupCallback, parameters: params});
	} else {
		new Ajax.Updater(contentId, url,{method:'post',onComplete:showPopupCallback});
	}
	hideSelectBoxes();
}

function showPopupCallback(response){
	positionPopupBelow(this.popupParent,this.popupId);
	Element.show(this.popupId);
	document.body.style.cursor = "auto";
	if (setupSubmitDisabling) {
	  setupSubmitDisabling();
	}
}

function hidePopup(id){
	Element.hide(id);
	showSelectBoxes();
}

function hideSelectBoxes() {
	if (!IE)
		return;
	$A(document.getElementsByTagName('select')).each(
		function(select) { 
			select.style.visibility = 'hidden'; 
		}
	);
}

function showSelectBoxes() {
	if (!IE)
		return;
	$A(document.getElementsByTagName('select')).each(
		function(select) { 
			select.style.visibility = 'visible'; 
		}
	);
}



function positionPopupBelow(parent,id) {
	var parentPosition = Position.cumulativeOffset(parent);
	var button = Element.getDimensions(parent);
	var popup =  Element.getDimensions(id);
	$(id).style.left=(parentPosition[0]-popup.width/2) + button.width/2 + "px";
	$(id).style.top= (parentPosition[1] + button.height + 16) + "px";
	popupRemoveArrows(id);
	popupTopArrow(id);
}

function positionPopupAbove(parent,id) {
	var parentPosition = Position.cumulativeOffset(parent);
	var button = Element.getDimensions(parent);
	var popup =  Element.getDimensions(id);	
	$(id).style.left=(parentPosition[0]-popup.width/2) + button.width/2 + "px";
	$(id).style.top= (parentPosition[1] - popup.height - offsetY) + "px";
	
	popupRemoveArrows(id);
	popupBottomArrow(id);
}

function positionPopupRight(parent,id) {
	var parentPosition = Position.cumulativeOffset(parent);
	var button = Element.getDimensions(parent);
	var popup =  Element.getDimensions(id);
		
	$(id).style.left= parentPosition[0] + button.width + "px";
	$(id).style.top = parentPosition[1] - offsetY + (button.height/2) + "px";
	
	popupRemoveArrows(id);
	popupLeftArrow(id);
}


function getElementsByClassName(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function popupRemoveArrows(popup) {
	popup = $(popup);
	
	var topArrow = getElementsByClassName(popup,'topArrow','img')[0];
	var leftArrow = getElementsByClassName(popup,'leftArrow', 'div')[0];
	var rightArrow = getElementsByClassName(popup,'rightArrow','div')[0];
	var bottomArrow = getElementsByClassName(popup,'bottomArrow','img')[0];
	
	setImage( topArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-3.png' );
	setImage( leftArrow, 'http://www2.warwick.ac.uk/static_war/popup/0.gif' );
	setImage( rightArrow, 'http://www2.warwick.ac.uk/static_war/popup/0.gif' );
	setImage( bottomArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-11.png' );
	
}

function popupTopArrow(popup) {
	popup = $(popup);
	var topArrow = getElementsByClassName(popup,'topArrow','img')[0];
	setImage( topArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-4.png' );
}

function popupLeftArrow(popup) {
	popup = $(popup);
	var leftArrow = getElementsByClassName(popup,'leftArrow','div')[0];
	setImage( leftArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-15.png' );
}

function popupRightArrow(popup) {
	popup = $(popup);
	var rightArrow = getElementsByClassName(popup,'rightArrow','div')[0];
	setImage( rightArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-14.png' );
}

function popupBottomArrow(popup) {
	popup = $(popup);
	var bottomArrow = getElementsByClassName(popup,'bottomArrow','img')[0];
	setImage( bottomArrow, 'http://www2.warwick.ac.uk/static_war/popup/box-16.png' );
}


