var PopBox = {
	resultUls: false,
	html: false, body: false, cont: false,
	optionBox: false, optionClose: false, optionNav: false, optionText: false,
	inOptionBox: false,
	optionBoxInt: false, optionBoxInc: 50, optionBoxTime: 20, optionBoxLimH: 200, optionBoxLimW: 320,
	curX: 0, curY: 0, debug: 0,
	
	init: function() {
		PopBox.aElements = document.getElementsByTagName('a');
		PopBox.optionBox = document.getElementById('optionBox');
		PopBox.optionClose = document.getElementById('optionClose');
		PopBox.optionNav = document.getElementById('optionNav');
		PopBox.resultUls = Array();
		
		for(var i = 0; i < PopBox.aElements.length; i++) {
			a = PopBox.aElements[i];
		
			if((typeof a.className) == 'undefined' || !a.className || a.nodeName.toLowerCase() != 'a') {
				continue;
			}
			
			if(Lib.matchClass(a, 'popBox')) {
				Lib.addEvent(a, 'click', PopBox.enableUi);
			}
		}
		
		// init some more objects...
		PopBox.html = document.getElementsByTagName('html')[0];
		PopBox.body = document.getElementsByTagName('body')[0];
		PopBox.cont = document.getElementById('optionBoxContainer');
		PopBox.optionText = document.getElementById('optionText');
		
		// now add some events to the option box...
		Lib.addEvent(PopBox.optionClose, 'click', PopBox.disableUi);
		PopBox.optionClose.childNodes[0].style.display = 'none';

		// keep track of mouse position
		Lib.addEvent(PopBox.body, 'mousemove', PopBox.trackMouse);

		// adjust height for gecko
		if(!document.all) {
			PopBox.optionBoxLimH -= 30;
		}
		
		else {
			PopBox.optionBoxLimW += 20;
		}
	},

	trackMouse: function(e) {
		// get our x,y offsets...
		xOff = (typeof window.pageXOffset) != 'undefined' ? window.pageXOffset : (PopBox.body.scrollLeft + 10);
		yOff = (typeof window.pageYOffset) != 'undefined' ? window.pageYOffset : (PopBox.body.scrollTop - 5);

		PopBox.curX = e.clientX + xOff - PopBox.optionBoxLimW;
		PopBox.curY = e.clientY + yOff - 10;
		//PopBox.debug = "ClientY: " + e.clientY + "\n\nOffsetY: " + window.scrollY;
		
		if(BrowserDetect.browser == 'Safari') {
			PopBox.curX = e.pageX - PopBox.optionBoxLimW;
			PopBox.curY = e.pageY;
		}
		
		// whoa.. no negs please
		if(PopBox.curX < 0) {
			PopBox.curX = 1;
		}
	},
	
	enableUi: function(e) {
		if(PopBox.inOptionBox || !(el = Lib.getTarget(e))) {
			return;
		}
		
		if(!(el = Lib.ascendDOM(el, 'a'))) {
			return;
		}
		
		PopBox.enableOptionBox(el);
	},
	
	disableUi: function(e) {
		if(!(el = Lib.getTarget(e))) {
			return;
		}
		
		PopBox.disableOptionBox();
	},
	
	enableOptionBox: function(a) {		
		// insert links...
		links = PopBox.optionNav.getElementsByTagName('a');
		myTitle = a.getAttribute('linkTitle');
		myMajor = a.getAttribute('major');
		myAssoc = a.getAttribute('associateName');
		isMinor = a.getAttribute('minor');
		
		if(!myMajor || myMajor == '' || myMajor == myTitle) {
			myHTML = '<span style="display: block; margin-bottom: 10px;"><strong>' + myTitle + '</strong> is a major';
			
			if(isMinor == 'yes') {
				myHTML += ', and is also a minor';
			}

			myHTML += '.</span><span class="display: block;"><a href="http://www.umass.edu/nre/majors/index.php/' + myTitle.replace(/\s+.*/, '') + '">Facts, figures, salary info and what students say about ' + myMajor + '</a></span>';
			PopBox.optionText.innerHTML = myHTML;

			links[0].innerHTML = myTitle + ' website';
			links[1].innerHTML = 'Description of the ' + myTitle + ' major in the UMass Amherst guide to undergraduate programs';
		}

		else {
			PopBox.optionText.innerHTML = '<span style="display: block; margin-bottom: 10px;"><strong>' + myTitle + '</strong> is a concentration within <strong>' + myMajor + '</strong>.</span>';

			PopBox.optionText.innerHTML += '<span class="display: block;"><a href="http://www.umass.edu/nre/majors/index.php/' + myMajor.replace(/\s+.*/, '') + '">Facts, figures, salary info and what students say about ' + myMajor + '</a></span>';

			links[0].innerHTML = myMajor + ' website';
			links[1].innerHTML = 'Description of the ' + myTitle + ' major in the UMass Amherst guide to undergraduate programs';
		}

		if(a.getAttribute('link3') != '') {
			links[2].parentNode.innerHTML = '<div>&nbsp;</div><p style="margin-top: 7px;">A similar course of study is also available as a two year program that leads to an Associate of Science degree. <div style="margin-top: 4px;"></div><a href="' + a.getAttribute('link3') + '">' + myAssoc + '</a> is available through the Stockbridge School.</p>';
			links[2].parentNode.style.display = 'inline';
			PopBox.optionBoxLimH = 300;
		}

		else {
			links[2].parentNode.style.display = 'none';
			PopBox.optionBoxLimH = 250;
		}

		links[0].href = a.getAttribute('link1');
		links[1].href = a.getAttribute('link2');

		PopBox.optionBox.uptoWidth = PopBox.optionBoxLimW;
		PopBox.optionBox.uptoHeight = PopBox.optionBoxLimH;
		PopBox.optionBox.style.width = PopBox.optionBox.curWidth = 0;
		PopBox.optionBox.style.height = PopBox.optionBox.curHeight = 0;
		Lib.addClass(PopBox.optionBox, 'enable');
		PopBox.inOptionBox = true;

		/*for(var i = 0, c = 1; i < links.length; i++, c++) {
			if(a.getAttribute('link' + c) == '') {
				links[i].style.display = 'none';
				continue;
			}

			else {
				links[i].style.display = 'inline';
			}

			links[i].href = a.getAttribute('link' + c);
			

			if(c == 1) {
				links[i].innerHTML = 'More info on ' + myTitle + ' in the NRE department website';
			}

			else if(c == 2) {
				links[i].innerHTML = 'More info on ' + myTitle + ' in the UMass Guide to Undergraduate Programs';
			}

			else if(c == 3) {
				links[i].innerHTML = 'More info on ' + myTitle + ' in the 2-year Associate\'s degree program';
			}
		}*/

		// fancy display...
		newTop = document.all ? Lib.offsetTop(a) : Lib.offsetTop(a) - 25;
		PopBox.optionBox.style.top = PopBox.curY + 'px';
		PopBox.optionBox.style.left = PopBox.curX + 'px';
		PopBox.optionBoxInt = window.setInterval(PopBox.showOptionBox, PopBox.optionBoxTime);
		
		// nothing should happen on the outside world...
		PopBox.disableWorld();
	},
	
		showOptionBox: function() {
			// quick access for limits...
			limW = PopBox.optionBox.uptoWidth;
			limH = PopBox.optionBox.uptoHeight;
			
			incW = 0;
			incH = 0;
			
			// calculate ratio...
			if(limW == limH) {
				incW = incH = PopBox.optionBoxInc;
			}
			
			else if(limW > limH) {
				incW = PopBox.optionBoxInc;
				incH = limH / (limW / incW);
			}
			
			else {
				incH = PopBox.optionBoxInc;
				incW = limW / (limH / incH);
			}
			
			PopBox.optionBox.curWidth += incW;
			PopBox.optionBox.curHeight += incH;
			
			// stop!
			if(PopBox.optionBox.curWidth >= limW || PopBox.optionBox.curHeight >= limH) {
				PopBox.optionBox.style.width = limW + 'px';
				PopBox.optionBox.style.height = limH + 'px';
				window.clearInterval(PopBox.optionBoxInt);
				PopBox.optionClose.childNodes[0].style.display = 'block';
				return;
			}
			
			PopBox.optionBox.style.width = PopBox.optionBox.curWidth + 'px';
			PopBox.optionBox.style.height = PopBox.optionBox.curHeight + 'px';
		},
	
	disableOptionBox: function() {
		Lib.removeClass(PopBox.optionBox, 'enable');
		PopBox.inOptionBox = false;
		PopBox.optionClose.childNodes[0].style.display = 'none';
		
		// back to reality...
		PopBox.enableWorld();
	},
	
	disableWorld: function() {
		if(PopBox.cont) Lib.addClass(PopBox.cont, 'opaque');
	},
	
	enableWorld: function() {
		if(PopBox.cont) Lib.removeClass(PopBox.cont, 'opaque');
	}
}

Lib.addEvent(window, 'load', PopBox.init);