/* ***********************************************
Author	: Derek Jewell
Class	: BannerView Popup
Version	: 0.00.002

Popup Box set - Do not touch any functions contained below.

Requirements:

bvPopUp() in onLoad or within a script called on load of page.

An anchor tag formatted as such:

<a href="x" rel="[dynamic/static][closePos][w]x[h]" [title="content"]>link text</a>

dunamic/static: [required]
----------------
dynamic means that the page in the href attribute will be pulled into
the popup.

static means that the content within the title attribute will be pulled
into the popup.  title tag can contain html, just remember to manage quotes
properly.  ie: title="\"Whatup!\"" for "Whatup!"

closePos: [optional: default - top]
----------------
indicates where the close div will be injected.  top and bottom are the only
valid options - positioning in other cases can be managed via css

width and height: [optional: default - 600x516]
----------------
formatted as wwwxhhh - both width and height are required if used.  otherwise
a default of 600x516 will be used.

w - width
h - height
************************************************ */

// The "executable" class
function bvPopUp () {
	var anchors = document.getElementsByTagName('a');	// Acquire all anchor tags
	var curA;	// Working tag
	for (i = 0; i < anchors.length; i++) {
		curA = anchors[i];
		bvModifyLink(curA,i);
	}
	
}

// The following function updates links as necessary
function bvModifyLink () {
	var matches = [];
	var regex = new RegExp("(dynamic|static)(top|bottom)([0-9]*)([x]*)([0-9]*)");
	var url;
	
	matches = regex.exec(arguments[0].rel);
	if (matches !== null) {
		url = arguments[0].href;
		arguments[0].href = "javascript:bvShowPopup(" + arguments[1] + ",'" + matches[1] + "'";
		if (matches[2]) {
			arguments[0].href += ",'" + matches[2] + "'";
		}
		if (matches[3] && matches[5]) {
			arguments[0].href += "," + matches[3] + "," + matches[5];
		}
		if (matches[1] == "dynamic") {
			arguments[0].href += ",'" + url + "&compact=1'";
		}
		arguments[0].href += ");";
		arguments[0].target = "_self";
		arguments[0].id = arguments[1];
	}
}

// Destroys popup - restores selects (form field - if any)
function bvClosePopup () {
	var selects = document.getElementsByTagName('select');

	// Destroy popup and overlay
	document.body.removeChild(document.getElementById(arguments[0]));
	document.body.removeChild(document.getElementById('overlay'));

	for (i = 0; i < selects.length; i++)
	{
		var thisSelect = selects[i];
		thisSelect.style.display = 'block';
	}
}

// Creates popup and overlay
function bvShowPopup() {
	var height = 516;	// Popup height
	var overlay;		// Holder variable for overlay
	var overlayId;		// Same reason as popupId
	var popup;			// Holder variable for popup box
	var popupId;		// Control does not seem to apply after appending
	var regex = new RegExp("([0-9]{1,})"); 	// To verify dimensions are numeric.
	var selects = document.getElementsByTagName('select');	 // Gather selects in a camp.
	var width = 600;	// Popup width

	// Append the overlay element.
	document.body.innerHTML += '<div class="overlay" id="overlay"></div>';

	// Append the popup element
	document.body.innerHTML += '<div class="popBox" id="' + arguments[1] + '"><div id="load"></div></div>';

	// Additional Processing
	if (regex.exec(arguments[3])) {
		width = arguments[3];
	}
	if (regex.exec(arguments[4])) {
		height = arguments[4];
	}
	
	// Hide undesireable elements
	window.scrollTo(0,0);
	for (i = 0; i < selects.length; i++)
	{
		var thisSelect = selects[i];
		thisSelect.style.display = 'none';
	}

	// Styling up
	overlayId = document.getElementById('overlay');
	overlayId.style.height = document.body.clientHeight + 'px';
	
	popupId = document.getElementById(arguments[1]);
	popupId.style.top = "50%";
	popupId.style.left = "50%";
	popupId.style.width = width + 'px';
	popupId.style.height = height + 'px';
	popupId.style.marginLeft = '-' + Math.round((width / 2)) + 'px';
	popupId.style.marginTop = '-' + Math.round((height / 2)) + 'px';
	
	// Hop over to the data loader function
	bvLoadData(arguments);
}

// Loads data into div, be it from title or url
function bvLoadData() {
	var anchor = document.getElementById(arguments[0][0]);
	var elementId = document.getElementById(arguments[0][1]);
	var location = arguments[0][2];
	var targetId = arguments[0][1];
	var xmlHttp;

	if (anchor.title !== "") {
		if (arguments[0][2] == "bottom") {
			elementId.innerHTML = anchor.title + '<div id="close" class="close" onClick="bvClosePopup(\'' + arguments[0][1] + '\');"></div>';
		} else {
			elementId.innerHTML = '<div id="close" class="close" onClick="bvClosePopup(\'' + arguments[0][1] + '\');"></div>' + anchor.title;
		}
	} else {
		try	{
			xmlHttp = new XMLHttpRequest();
		} catch (errA)	{
			try	{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (errB)	{
				try	{
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (errC)	{
					return false;
				}
			}
		}
		xmlHttp.onreadystatechange = function()	{
			if (xmlHttp.readyState == 4) {
				if (location == "bottom") {
					elementId.innerHTML = xmlHttp.responseText + '<div id="close" class="close" onClick="bvClosePopup(\'' + targetId + '\');"></div>';
				} else {
					elementId.innerHTML = '<div id="close" class="close" onClick="bvClosePopup(\'' + targetId + '\');"></div>' + xmlHttp.responseText;
				}
			}
		};
		xmlHttp.open("GET", arguments[0][arguments[0].length - 1], true);
		xmlHttp.send(null);
	}
}