// JavaScript Document

window.onresize = function() {
	var size = getBrowserContentSize();
	resizeBlanket(size[0], size[1]);
}
window.onload = function() {
	var size = getBrowserContentSize();
	
	showNavigation();
	//rewritePopupLinks();
	resizeBlanket(size[0], size[1]);
}

_primaryPopup = null;

//////////////////////////////////
//			SETUP				//
//////////////////////////////////

function rewritePopupLinks() {
/*	var links = document.getElementsByTagName("a");
	var i;
	var url;
	var clone;
	
	for (i = 0; i < links.length; i++) {
		if (links[i].className == "eBrochurePopup") {
			url = links[i].getAttribute("href");
			links[i].removeAttribute("target");
			links[i].removeAttribute("class");
			links[i].setAttribute("href", "javascript: admission_popup(this, '" + url + "', '" + links[i].innerHTML + " E-Brochure', 830, '-40');");
		} else if (links[i].className == "informationPopup") {
			url = links[i].getAttribute("href");
			links[i].removeAttribute("target");
			links[i].removeAttribute("class");
			links[i].setAttribute("href", "javascript: admission_popup(this, '" + url + "', '" + links[i].getAttribute("value") + "', 830, '-40');");
		}
	} */
}

function showNavigation() {
	/*var level1 = document.getElementById("Level1");
	var firstLink;
	var lvl2 = document.getElementById("Level2");
	
	if ((level1 != null) && (lvl2 != null)) {
		firstLink = level1.getElementsByTagName("a")[0];
		if (firstLink.className.indexOf("selected") == -1) firstLink.className += " selected";
		
		lvl2.innerHTML = '<div class="level2"><a class="external" href="http://besmart.com/apply.honorcode.php" target="_blank">Apply Online</a><a class="external" href="http://www.byu.edu" target="_blank">BYU Home Page</a><a class="external" href="http://www.besmart.com/" target="_blank">BeSmart.com</a></div>';
		lvl2.style.visibility = "visible";
		lvl2.style.display = "block";
		
		//document.getElementById("Navigation").innerHTML = '<div id="Level1"><a class="highlight selected" href="javascript:byu.none();">Admissions Services</a></div><div style="visibility: visible; display: block;" id="Level2"><div class="level2"><a class="external" href="http://besmart.com/apply.honorcode.php" target="_blank">Apply Online</a><a class="external" href="http://www.byu.edu" target="_blank">BYU Home Page</a><a class="external" href="http://www.besmart.com/" target="_blank">BeSmart.com</a></div></div>';
	} else {
		setTimeout("showNavigation()", 500);
	}*/
}

//////////////////////////////////
//		BLANKET CONTROL			//
//////////////////////////////////
function resizeBlanket(w, h) {
	var b = document.getElementById("Blanket");
	if (b != null) {
		b.style.width = w + "px";
		b.style.height = h + "px";
	}
}

//////////////////////////////////
//		COMMON EXT.JS USE		//
//////////////////////////////////
function admission_popup(obj, url, title, width, height) {
	_primaryPopup = loadPrimaryPopupWindow(obj, url, title, width, height);
}

function loadPrimaryPopupWindow(linkObj, url, title, width, height) {
	
	if (_primaryPopup == null) {
	
		//calc width and height if they are percentages
		if ((typeof(width) == "string") || (typeof(height) == "string")) {
			var size = getBrowserContentSize();
			if (typeof(width) == "string") {
				if (width.indexOf("%") == width.length - 1) {
					width = Math.round(size[0] * parseInt(width.substr(0, width.length - 1)) / 100);
				} else if (width.indexOf("-") == 0) {
					width = size[0] - parseInt(width.substr(1));
				}
			}
			if (typeof(height) == "string") {
				if (height.indexOf("%") == height.length - 1) {
					height = Math.round(size[1] * parseInt(height.substr(0, height.length - 1)) / 100);
				} else if (height.indexOf("-") == 0) {
					height = size[1] - parseInt(height.substr(1));
				}
			}
		}
	
		var win = new Ext.Window({
			title: title,
			width: width,
			height: height,
			maximizable: true,
			animateTarget: linkObj,
			resizable: true,
			html: "<iframe id='" + this.id + "_iframe' src='" + url + "' width='100%' frameborder='0'></iframe>",
			listeners: {
				resize: function(panel, w, h) {
					Ext.get(panel.id).dom.getElementsByTagName("iframe")[0].style.height = (h - 32) + "px";
				},
				render: function() {
					var obj = Ext.get("Blanket");
					obj.fadeIn({
						endOpacity: .3,
						duration: .3
					});
				},
				close: function(panel) {
					document.getElementById("Blanket").style.display = "none";
					_primaryPopup = null;
				}
			}
		});
		win.show();
		return win;
	} else {
		return _primaryPopup;	
	}
}

//////////////////////////////////
//			POSITIONING			//
//////////////////////////////////
function getDocumentScrollPosition() {
	body=document.body
	d=document.documentElement
	if (body && body.scrollTop) return body.scrollTop
	if (d && d.scrollTop) return d.scrollTop
	if (window.pageYOffset) return window.pageYOffset
	return 0
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

//////////////////////////////////
//				AJAX			//
//////////////////////////////////
function setupAJAXRequest() { //setup an AJAX request and return the xmlHTTP object
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	return xmlHttp;
}

/*
 * Parameter options:
 * 1. AJAX_post("pageToCall.php");
 * 2. AJAX_post("pageToCall.php", "var1=foo&var2=bar");
 * 2. AJAX_post("pageToCall.php", "var1=foo&var2=bar", functionToCallOnComplete);  -- Note: Response is passed in as a parameter for the funtion called on complete
*/
function AJAX_post(pageToCall) {
	var xmlHttp = setupAJAXRequest();
	var params = "";
	var func = null;
	
	if (arguments.length > 1) params = arguments[1];
	if (arguments.length > 2) func = arguments[2];
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var response = xmlHttp.responseText;
			if (func != null) func.call(this, response);
		}
	}
	
	xmlHttp.open("POST", pageToCall, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//////////////////////////////////
//		STRING PROTOTYPE		//
//////////////////////////////////
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}