/*
	Global variables - Common

*/

var rStates = [
	"Alabama", "Alaska", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "Delaware", "Distict of Columbia"
	, "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana"
	, "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana"
	, "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina"
	, "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina"
	, "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia"
	, "Wisconsin", "Wyoming"
];

/*
	Global functions
*/

function getMousePosition(e) {
	
  return e.pageX ? {'x':e.pageX, 'y':e.pageY} : {'x':e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 'y':e.clientY + document.documentElement.scrollTop + document.body.scrollTop};

};

function showToolTip(oEvent) {
	
	var toolTipText = arguments[0].data.tip;
	
	var mouse = getMousePosition(oEvent);
	var fullText = toolTipText || "";
						
	var toolTip = $("#toolTip")[0];
	toolTip.style.left = (mouse.x + 30) + "px";
		
	var tipWidth = arguments[0].data.tipWidth + "px";
	
	toolTip.style.width = tipWidth;
	toolTip.style.textAlign = "left";	
	
	toolTip.innerHTML = fullText;
	
	var tipPos = arguments[0].data.tipPos;
	
	if (tipPos == "top") {
		toolTip.style.top = (mouse.y - $(toolTip).height() - 20) + "px";	
	} else {
		toolTip.style.top = (mouse.y + 10) + "px";	
	}
	
	if (fullText.length > 0) {
	
		$("#toolTip").show();
	
	} else {
		
		$("#toolTip").hide();
			
	}
	
};

function hideToolTip() {
	
	var toolTip = $("#toolTip");
	
	toolTip.html("");
	
	toolTip.hide();
		
};

function reloadMethods() {  
	
	common.handleMask("on");
	
	common.expandCollapse.build("one");
	
	common.assignToolTips();
	
};

var Common_Class = function() {
	
	this.container = $(document.body);
	
	this.browsers = {
		mozilla: $.browser.mozilla
		, msie: $.browser.msie
		, opera: $.browser.opera
		, safari: $.browser.safari
	};
	
	this.version = $.browser.version;	
		
	var sType;
	
	for (sType in this.browsers) {
	
		if (this.browsers[sType]) {
			
			this.browser = sType;	
		}
		
	}
	
	var loc = String(location.href);
	var rLoc = loc.split("\/");
	
	var _page = rLoc[rLoc.length-1] || "index.asp";
	
	this.page = _page;
	
	this.activeAdminFunction = ""; 
	
	this.init();
};

Common_Class.prototype.init = function() {
	
	this.handleMask("off");	
	
	document.oncontextmenu = this.contextMenu;
	
	this._buildExpandCollapse();
	
	this._buildToolTip();
	
	this._handleNavigation();
	
	this._checkMessageOnLoad();
	
};

Common_Class.prototype._checkMessageOnLoad = function() {

	var messageOnLoad = $("input#messageOnLoad");
	
	if (messageOnLoad.length > 0) {
	
		var mss = messageOnLoad[0].value;
		
		if (mss == "SessionExpired") {
		
			var oEl = oElement.create("div", {className: "offset5"}, "Sorry, but your session has expired. Please log in again.");
		
			this.showDefaultMsg(oEl, "Session Expired");
				
		} else if (mss == "SuccessfulLogout") {
		
			var oEl = oElement.create("div", {className: "offset5"}, "You have been successfully logged out.");
			
			this.showDefaultMsg(oEl, "Logout Successful");
			
		}
		
	}	
};

Common_Class.prototype._handleNavigation = function() {

	/* Control Links */
	
	var linkHome = $("a#linkHome");
	var linkPrint = $("a#linkPrint");
	var linkClose = $("a#linkClose");

	linkHome.bind("click", function() {
		location.href = "index.asp";	
		return false;
	});
	
	linkPrint.bind("click", function() {
		window.print(); // will build preview later
		return false;
	});
	
	linkClose.bind("click", function() {
		
		msgBox.display({
			title: "Are your sure"
			, displayTitle: true
			, content: oElement.create("div", {className: "offsetTop10"}, "Are you sure you want to close the Interactive Transfer Guide?")
			, buttons: {
			   cancel: {
			       label: "No"
			       , id: "cancelButton"
			       , className: "buttonCancel"
			       , oData: {ref: "cancelButton"}
			       , actionFunc: function() {
					msgBox.close();
			       }
			   }
			   , okay: {
			       label: "Yes"
			       , id: "okayButton"
			       , className: "buttonSave"
			       , oData: {ref: "okayButton"}
			       , actionFunc: function() {
					window.close();
			       }
			   }			   
			}
		});
		return false;
	});


	var navType = $("input#navigationType")[0];
	
	var navSelect = $("select#navigationSelect");
	
	if (navType) {
	
		navSelect.bind("change", {navType: navType.value}, function() {
			
			var nType = arguments[0].data.navType;
			
			var sPath = "index.asp";
			
			if (nType == "sideBySide") {
				
				sPath = "sidebyside.asp";
			
			} else if (nType = "interactive") {
			
				sPath = "interactive.asp";
				
			}
			
			if (this.value != "") {
			
				location.href = sPath + "?major=" + this.value;
			
			}
			
		});
		
	}
	
};

Common_Class.prototype.handleMask = function(state) {

	var mask = $("#mask")[0];
	var loader = $("#loader")[0];
	
	if (!mask) {
	
		$(document.body).append("<div id='mask'>&nbsp;</div>");
				
	}
	
	if (!loader) {
		$(document.body).append("<div id='loader'>Loading...</div>");
	}
	
	if (state && state != "off") {
	
		$("#mask").show();
		$("#loader").show();
		
		this._centerLoader("#loader");
		
	} else if (!state || state == "off") {
		
		$("#mask").hide();
		$("#loader").hide();
	
	}
	
	window.scrollTo(0, 0);
	
};

Common_Class.prototype._centerLoader = function(sElement) {
	
	var bodyWidth = $(document.body).width();
	
	$(sElement).css({"left": (bodyWidth/2) - 200});
	
	
};

Common_Class.prototype.contextMenu = function() {
	
	/*Turning off the context menu */
	
	return false;
	
};

Common_Class.prototype.ajax = function(oData, showMask) {
	
	if (!oData) {
	
		this.displayDefault();
		
		return;
		
	}
	if (!showMask) {
		
		this.handleMask("on");
	
	}

	var filePath = oData.filePath || "";
	
	var _data = oData.data || {};
	
	var success = oData.success || function(data, success) {
		alert("default success method");
		common.handleMask("on");
	};
	
	var failure = oData.failure || function(xhr, status, error) {
		alert("default failure method");
		common.handleMask("on");
		
		setTimeout(function() {
			common.handleMask("off");
		}, 5000);
		
	};
	
	if (filePath != "") {
	
		$.ajax({
			url: oData.filePath
			, dataType: "html"
			, type: "POST"
			, data: oData.data
			, success: success
			, error: failure
		});
		
	} else {
		
		this.displayDefault();
	}
	
};

Common_Class.prototype.ajaxSuccess = function(data, success) {
	
	var sReturn = String(data).split("|");
	
	if (sReturn.length != 2) {
		
		msgBox.display({
			title: "Error:"
			, displayTitle: true
			, content: oElement.create("div", {}, data)
			, buttons: {
			   okay: {
			       label: "Okay"
			       , id: "okayButton"
			       , className: "buttonCancel"
			       , oData: {ref: "okayButton"}
			       , actionFunc: function() {
					
					msgBox.close();
					
					common.handleMask("off");
					
			       }
			   }
			}
		});
		
	} else {
		msgBox.display({
			title: sReturn[0] || "Message:"
			, displayTitle: true
			, content: oElement.create("div", {}, sReturn[1])
			, buttons: {
			   okay: {
			       label: "Okay"
			       , id: "okayButton"
			       , className: "buttonCancel"
			       , oData: {ref: "okayButton"}
			       , actionFunc: function() {
					
					msgBox.close();
					
					common.handleMask("off");
		
					admin.hideModal();
					
					if (common.activeAdminFunction != "") {
						
						var rAdmin = String(common.activeAdminFunction).split("_");
						
						var sAction = rAdmin[0];
						var sSection = rAdmin[1];
						var sLabel = rAdmin[2];					
						
						admin.openContent(sAction, sSection, sLabel);	
					}
					
					common.activeAdminFunction = "";
					
			       }
			   }
			}
		});
	}
};

Common_Class.prototype.ajaxFailure = function(xhr, oError, status) {
	
	for (var sType in xhr) {
		$(document.body).append("<div>" + sType + "</div>");	
	}
	
	common.showDefaultMsg(oError, "AJAX Error:");

	common.handleMask("off");

};

Common_Class.prototype.showErrorMessage = function(sMessage, sTitle) {
	this.showDefaultMsg(sMessage, sTitle);	
};

Common_Class.prototype.showDefaultMsg = function(sMessage, sTitle) {
	
	msgBox.display({
		title: sTitle || "Message:"
		, displayTitle: true
		, content: oElement.create("div", {}, sMessage)
		, buttons: {
		   okay: {
		       label: "Okay"
		       , id: "okayButton"
		       , className: "buttonCancel"
		       , oData: {ref: "okayButton"}
		       , actionFunc: function() {
				msgBox.close();
		       }
		   }
		}
	});

};

Common_Class.prototype.displayDefault = function() {
	
	msgBox.display({
		title: "Error:"
		, displayTitle: false
		, content: oElement.create("div", {}, "The information provided does not result in a valid ajax call.")
		, buttons: {
		   okay: {
		       label: "Okay"
		       , id: "okayButton"
		       , className: "buttonCancel"
		       , oData: {ref: "okayButton"}
		       , actionFunc: function() {
				msgBox.close();
		       }
		   }
		}
	});

};

Common_Class.prototype.truncate = function(sString, iLength) {
	
	var sStr = String(sString);
	
	if (sStr.length < iLength) {
		
		return sStr;	
		
	} else {
		
		return sStr.substr(0, iLength) + "...";
		
	}
	
};

Common_Class.prototype._buildExpandCollapse = function() {
	
	this.expandCollapse = new ExpandCollapse_Class("one");
		
};

Common_Class.prototype._buildToolTip = function() {

	var toolTip = $("div#toolTip");
	
	if (toolTip.length == 0) {
		
		toolTip = Element.create("div", {id: "toolTip"}, "&nbsp;");
		
		this.container.append(toolTip);
		
	}
	
	this.assignToolTips();
	
};

Common_Class.prototype.assignToolTips = function() {
	
	// Elements that get the tool tip must have the class name toolTip and an attribute called tip
	
	// This method should get called any time new content is loaded onto the page via an ajax call.
	
	var i, len;
	
	var toolTips = $(".toolTip");
	
	if (toolTips.length > 0) {
	
		toolTips.unbind("mouseover");
		toolTips.unbind("mouseout");
		toolTips.unbind("mousemove");
		
		for (i = 0, len = toolTips.length; i < len; i++) {
		
			var tip = toolTips[i];
		
			var tipText = tip.getAttribute("tip") || "";
			var tipWidth = tip.getAttribute("tipWidth") || 400;
			var tipPos = tip.getAttribute("tipPos") || "default";
		
			$(tip).bind("mouseover", {tip: tipText, tipWidth: tipWidth, tipPos: tipPos}, showToolTip);	
			$(tip).bind("mousemove", {tip: tipText, tipWidth: tipWidth, tipPos: tipPos}, showToolTip);
			$(tip).bind("mouseout", {}, hideToolTip);
	
		}		
	
	}
	this.handleMask("off");
};

Common_Class.prototype.json = function(sString) {

	return json_parse(unescape(sString));
	
};

Common_Class.prototype.sort = function(rArray, sortField, sDirection) {

	var field = sortField;
	var direction = sDirection || "asc";

	rArray.sort(compareSort);

	return rArray;
	
	function compareSort(a, b) {
		
		if (typeof(a[field]) == "string" || typeof(b[field]) == "string") {
			a[field] = String(a[field]);
			b[field] = String(b[field]);
		}
		
		if (direction == "desc") {
			if (a[field] > b[field]) {return -1;}
			if (a[field] < b[field]) {return 1;}
			return 0;
		} else {
			if (a[field] < b[field]) {return -1;}
			if (a[field] > b[field]) {return 1;}
			return 0;
		}
	}

};

Common_Class.prototype.buildCollegeSelect = function(rColleges, iIndex) {
	
	var rOptions = [];
	
	var i, len;
	
	for (i = 0, len = rColleges.length; i < len; i++) {
	
		rOptions.push(oElement.create("option", {value: rColleges[i].name}, rColleges[i].name));
		
	}
	
	
	return oElement.create("select", {size: 3, id: "infoCollege" + iIndex, className: "infoCollege"}, 
		rOptions
	);
	
};

var common = new Common_Class();
