
window.onresize = resize;
window.onload = loaded;


function welcome() {
	var pictures = new Array();
	var description = new Array();
	
	pictures[0] = "/images/rotatingpics/birdseye.png";
	description[0] = "Bird's Eye View, CMU Campus";
	pictures[1] = "/images/rotatingpics/building1.png";
	description[1] = "Park Library, CMU";
	pictures[2] = "/images/rotatingpics/building2.png";
	description[2] = "Foster Hall, CMU";
	pictures[3] = "/images/rotatingpics/theatre.png";
	description[3] = "Bush Theatre, CMU";
	pictures[4] = "/images/rotatingpics/building4.png";
	description[4] = "Warriner Hall, CMU";
	pictures[5] = "/images/rotatingpics/building5.png";
	description[5] = "Sloan Hall, CMU";
	pictures[6] = "/images/rotatingpics/building6.png";
	description[6] = "Troy Center, MI";
	pictures[7] = "/images/rotatingpics/building7.png";
	description[7] = "Dekalb Center, GA";
	pictures[8] = "/images/rotatingpics/building8.png";
	description[8] = "Grand Rapids Center, MI";
	pictures[9] = "/images/rotatingpics/insidelibrary.png";
	description[9] = "Park Library, CMU";
	
	var randNo = Math.floor(10 * Math.random()); //Total # of pictures
	var welcomePic = document.getElementById('content').getElementsByTagName('img')[0];

	welcomePic.src = pictures[randNo];
	welcomePic.title = description[randNo];
	welcomePic.alt = description[randNo];
}

/*function evenNews() {
	var news = document.getElementById('news').getElementsByTagName('div');
	var useful = document.getElementById('usefullinks').getElementsByTagName('div');
	
	for(var i = 0; i < news.length; i++)
		if(news[i].className == "bottomleft")
			break;
	for(var k = 0; k < useful.length; k++)
		if(useful[k].className == "bottomleft")
			break;
			
	if(news[i].offsetHeight > useful[k].offsetHeight)
		useful[k].style.height = news[i].offsetHeight - 29 + "px";
	else
		news[i].style.height = useful[k].offsetHeight - 29 + "px";
}*/


var arrow = "url(/images/arrow.gif) no-repeat top right"; //image for the subsubmenu arrow
var closetimer	= 0;
var menuitem	= 0;
var submenuitem = 0;
var timeout = 500;
var xOff = -10;
var yOff = 0;

//document.onclick = closeBoth; //Disabling this due to IE styling problem

function initMenu(id) {
	var liList = document.getElementById(id).getElementsByTagName('li');
	var ulCurrent;
	var tempList;
	var k;
	var i;
	
	for(i = 0; i < liList.length; i++) { //This adds an arrow for the subsubmenu
		if(temp = liList[i].getElementsByTagName('ul')[0]) {
			if(temp.className == "subsubmenu") {
				liList[i].getElementsByTagName('a')[0].style.paddingRight = 20 + "px";
				liList[i].getElementsByTagName('a')[0].style.background = arrow;
			}
		}
	}
	
	for(i = 0; i < liList.length; i++)
		liList[i].onmouseover = function(){handleTop()};
		
	for(i = 0; i < liList.length; i++) {
		ulCurrent = liList[i].getElementsByTagName('ul');
		
		if(ulCurrent.length > 0) {
			if(ulCurrent[0].className == "submenu") {
				liList[i].onmouseover = function(){openSub(this)};
				liList[i].onmouseout = function(){closer()};
				liList[i].getElementsByTagName('a')[0].onmouseover = function (){closeSubSub()};
				tempList = ulCurrent[0].getElementsByTagName('li');
				
				for(k = 0; k < tempList.length; k++)
					tempList[k].onmouseover = function(){handleMiddle()};
			}
			else if(ulCurrent[0].className == "subsubmenu") {
				liList[i].onmouseover = function(){openSubSub(this)};
				tempList = ulCurrent[0].getElementsByTagName('li');
				
				for(k = 0; k < tempList.length; k++)
					tempList[k].onmouseover = function(){handleBottom()};
			}
		}
	}
}

function handleTop() {
	closeBoth();
}

function handleMiddle() {
	closeSubSub();
}

function handleBottom() {
}

function openSub(obj) {
	cancelClose();
	
	if(menuitem) 
		menuitem.style.visibility = "hidden";
		
	menuitem = obj.getElementsByTagName('ul')[0];
	menuitem.style.left = (findPosX(obj) + xOff) + "px";
	menuitem.style.top = (findPosY(obj) + obj.offsetHeight + yOff) + "px";
	menuitem.style.visibility = "visible";
}

function openSubSub(obj) {
	cancelClose();
	
	if(submenuitem)
		submenuitem.style.visibility = "hidden";
		
	submenuitem = obj.getElementsByTagName('ul')[0];
	submenuitem.style.left = (obj.offsetWidth + xOff) + "px";
	submenuitem.style.top = (findPosY(obj) - 119 + yOff) + "px";
	submenuitem.style.visibility = "visible";
}

function closer() {
	closetimer = window.setTimeout(closeBoth, timeout);
}

function closeBoth() {
	if(menuitem) menuitem.style.visibility = "hidden";
	menuitem = null;
	if(submenuitem) submenuitem.style.visibility = "hidden";
	submenuitem = null;
}

function closeSubSub() {
	if(submenuitem) submenuitem.style.visibility = "hidden";
	submenuitem = null;
}

function cancelClose() {
	if(closetimer) {
		window.clearTimeout(closetimer); 
		closetimer = null;
	}
}

function findPosX(obj) {
	var curleft = 0;
	
	if(obj.offsetParent)
		while(1) 
		{
			curleft += obj.offsetLeft;
			
			if(!obj.offsetParent)
				break;
				
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
		
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	
	if(obj.offsetParent)
		while(1) 
		{
			curtop += obj.offsetTop;
			
			if(!obj.offsetParent)
				break;
				
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
		
	return curtop;
}


function resize() {

}

function loaded() {

}
