function resetNav(sectionId) {
	killFlash();
	
	prevSectionBg = document.getElementById('contentBg' + uSectionId);
	if (currentSection != sectionId) {
		if (currentSection) {
			var prevSectionEl = document.getElementById('section' + currentSection);
			prevSectionEl.className = "section";
			
			var prevLinkHolder = document.getElementById('sLinkHolder' + currentSection);
			prevLinkHolder.className = "sLinkHolder";
			
			var prevSectionLinkEl = document.getElementById('sectionLink' + currentSection);
			prevSectionLinkEl.className = "navLink";
		}
	}
	
	var sectionEl = document.getElementById('section' + sectionId);
	sectionEl.className = "sectionSel";
	
	var linkHolder = document.getElementById('sLinkHolder' + sectionId);
	linkHolder.className = "sLinkHolderSel";
			
	var sectionLinkEl = document.getElementById('sectionLink' + sectionId);
	sectionLinkEl.className = "navLinkSel";
	
	currentSection = sectionId;
}

function swfGetFALW() {
	resetNav(10);
	currentSection = 10;
	filterFacultyList(4);
}

function swfGetFEWEB() {
	resetNav(10);
	currentSection = 10;
	filterFacultyList(1);
}

function swfGetFSW() {
	resetNav(10);
	currentSection = 10;
	filterFacultyList(3);
}

function swfGetFL() {
	resetNav(10);
	currentSection = 10;
	filterFacultyList(2);
}


var xmlHttp

function getIndex() {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var qid = Math.random();
	var url="php/home.php";
	url=url+"?qid="+qid;
	
	xmlHttp.onreadystatechange=stateChangedGetPages
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChangedGetPagesTest() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		id = idCount;		
		content = xmlHttp.responseText;
		document.getElementById('testContent').innerHTML = xmlHttp.responseText;
	}
}


function search(q) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var qid = Math.random();
	var url="php/search.php";
	url=url+"?q=" + q;
	url=url+"&qid="+qid;
	
	xmlHttp.onreadystatechange=stateChangedGetPages
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function getPages(id, order, filterOn, filter) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var qid = Math.random();
	var url="php/getContent.php";
	url=url+"?id=" + id;
	url=url+"&order=" + order;
	if ((filterOn != '') && (filter != '')) {
		url=url+"&filterOn=" + filterOn;
		url=url+"&filter=" + filter;
		
	}
	url=url+"&qid="+qid;
	
	xmlHttp.onreadystatechange=stateChangedGetPages
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}



var contentIds = new Array();
var idCount = 0;
var currentDiv;
function stateChangedGetPages() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		id = idCount;		
		content = xmlHttp.responseText;
		contentIds.push(idCount);
		
		contentEl = document.getElementById('contentHolder');
		
		if (contentIds.length > 5) {
			dropId = contentIds.shift();
			dropDiv = document.getElementById('content' + dropId);
			contentEl.removeChild(dropDiv);
		}
		
		contentDivId = "content" + id;
		contentDiv = document.createElement("div");
		contentDiv.setAttribute("id", contentDivId);
		contentEl.appendChild(contentDiv);
		
		currentDiv = contentDiv
				
		cDiv = document.createElement("div");
		cDiv.setAttribute("class", "content");
		contentDiv.appendChild(cDiv);
		if ((prevSectionBg) && (prevSectionBg.parentNode)) {
			prevSectionBg.parentNode.removeChild(prevSectionBg);
		}
		cDiv.innerHTML = content;
		
		idCount++;
		
		var allDivs = contentDiv.getElementsByTagName("div");

		for (var i=0; i<allDivs.length; i++) {
			if (allDivs[i].className == "titleHolder") {
				var headerEls = allDivs[i].getElementsByTagName("h1");
				for (var j=0; j<headerEls.length; j++) {
					if (headerEls[j].className == "day") dayItem = headerEls[j];
					if (headerEls[j].className == "h1Temp3") titleItem = headerEls[j];
					if (headerEls[j].className == "h1Date3") dateItem = headerEls[j];
				}
				
				if ((dayItem) && (titleItem) && (dateItem)) {
					var dayHeight = dayItem.clientHeight;

					var titleHeight = titleItem.clientHeight;
					var dateHeight = dateItem.clientHeight;
					var totalHeight = titleHeight + dateHeight;
					
					var dayMargin = totalHeight - dayHeight;
					dayItem.style.marginTop = dayMargin + "px";
				}
				
			}
		
		}
		
		if ($("#"+contentDivId+" .pageContent").length == 1) {
			showContent($("#"+contentDivId+" .pageContent:first").attr("id").replace("page", ""));
		}
		
		$("#"+contentDivId+" .pageContent").hover(function() {
			$(this).css("border-top", "1px solid #000");
			$(this).css("border-bottom", "1px solid #000");
			
		}, function() {
			$(this).css("border-top", "1px solid transparent");		
			$(this).css("border-bottom", "1px solid transparent");
		});
		
		
		if (autoLoadPage != undefined) {
			showContent($("#"+contentDivId+" .pageContent"+autoLoadPage).attr("id").replace("page", ""));
			//showContent($("#"+contentDivId+" .pageContent:first").attr("id").replace("page", ""));
			autoLoadPage = undefined;
		}
		
		
		if ($("#"+contentDivId+" .content").hasClass("home")) {
			$("#"+contentDivId+" .home").append('<div class="newscol homecolumn"></div>');
			$("#"+contentDivId+" .home").append('<div class="agendacol homecolumn"></div>');
			
			$("#"+contentDivId+" .home .newscol").append('<div class="coltitle">News</div>');
			$("#"+contentDivId+" .home .agendacol").append('<div class="coltitle">Events</div>');
			
			
			$("#"+contentDivId+" .home .newscol").append($("#"+contentDivId+" .news"));
			$("#"+contentDivId+" .home .agendacol").append($("#"+contentDivId+" .agenda"));
			$("#"+contentDivId+" .home .titleTemp3").css("width", "240px");
			$("#"+contentDivId+" .home .pageContent").css("width", "240px");
			$("#"+contentDivId+" .home .description").css("width", "250px");
			$("#"+contentDivId+" .home .description p").css("width", "250px");
			$("#"+contentDivId+" .home .objImage").css("width", "250px");

			$("#"+contentDivId+" .home .dayTemp3").hide();
			$("#"+contentDivId+" .home .h1Date3").css("font-weight", "normal").css("margin-bottom", "5px");
			
			$("#"+contentDivId+" .home .pageContent").css("margin-bottom", "30px");

		}
	}
	
	//getPages
}

var autoLoadPage;
function getResearchItem(section, page) {
	showSections(12);
	getContent(section);
	autoLoadPage = page;
}

function insertEmail(address) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="php/addEmail.php";
	url=url+"?email=" + address;
	url=url+"&qid="+Math.random();
		
	xmlHttp.onreadystatechange=stateChangedAddEmail
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	
}

function stateChangedAddEmail() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	}
}

function deleteEmail(address) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="php/removeEmail.php";
	url=url+"?email=" + address;
	url=url+"&qid="+Math.random();
		
	xmlHttp.onreadystatechange=stateChangedRemoveEmail
	
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChangedRemoveEmail() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
	}
}

var uSectionId
function setSectionId(secId) {
	uSectionId = secId;
}

function showSortOptions(sortOpts) {
	if (sortOpts == '') {
		document.getElementById('sortHolder').style.display = "none";
	} else {
		var sortOptions = sortOpts.split(",");
		document.getElementById('sort_title').style.display = "none";
		document.getElementById('sort_date').style.display = "none";
		document.getElementById('sort_faculty').style.display = "none";
		
		document.getElementById('sortHolder').style.display = "block";		
		for(i=0; i<sortOptions.length; i++) {
			document.getElementById('sort_' + sortOptions[i]).style.display = "block";
		}
		
	}
}
function sortList(sortOn) {
	if (currentSection) {
		getPages(currentSection, sortOn, '', '');
		prevSectionBg = document.getElementById('contentBg' + uSectionId);
	}
}

function filterFacultyList(facId) {
	if (currentSection) {
		getPages(currentSection, 'rank', 'faculty', facId);
		prevSectionBg = document.getElementById('contentBg' + uSectionId);
	}
}

function filterThemeList(themeId) {
	if (currentSection) {
		getPages(currentSection, 'rank', 'theme', themeId);
		prevSectionBg = document.getElementById('contentBg' + uSectionId);
	}
}




function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}  

function pageLinkOver(linkEl, id) {
	if (id != lastId) linkEl.style.color = "#000000";
}

function pageLinkOut(linkEl, id) {
	if (id != lastId) linkEl.style.color = "#666666";

}


var lastTHolder;
var lastId;
function showContent(id) {
	var tHolder = document.getElementById('titleHolder' + id);
	var allTitleEls = new Array();

	if (tHolder != lastTHolder) {
		if (lastTHolder) {
			allTitleEls.push(lastTHolder.getElementsByTagName('h1'));
			allTitleEls.push(lastTHolder.getElementsByTagName('h2'));
			allTitleEls.push(lastTHolder.getElementsByTagName('h3'));
			allTitleEls.push(lastTHolder.getElementsByTagName('h4'));
			allTitleEls.push(lastTHolder.getElementsByTagName('a'));
			
			for (i=0; i<allTitleEls.length; i++) {
				for (j=0; j<allTitleEls[i].length; j++) {
					allTitleEls[i][j].style.color = "#666666";
				}
			}
		}	
	
		var allTitleEls = new Array();
		allTitleEls.push(tHolder.getElementsByTagName('h1'));
		allTitleEls.push(tHolder.getElementsByTagName('h2'));
		allTitleEls.push(tHolder.getElementsByTagName('h3'));
		allTitleEls.push(tHolder.getElementsByTagName('h4'));
		allTitleEls.push(tHolder.getElementsByTagName('a'));
	
		for (i=0; i<allTitleEls.length; i++) {
			for (j=0; j<allTitleEls[i].length; j++) {
				allTitleEls[i][j].style.color = "#000000";
			}
		}
		lastTHolder = tHolder
		lastId = id;
	} else {
		if (lastTHolder) {
		allTitleEls.push(lastTHolder.getElementsByTagName('h1'));
		allTitleEls.push(lastTHolder.getElementsByTagName('h2'));
		allTitleEls.push(lastTHolder.getElementsByTagName('h3'));
		allTitleEls.push(lastTHolder.getElementsByTagName('h4'));
		allTitleEls.push(lastTHolder.getElementsByTagName('a'));
		}
		for (i=0; i<allTitleEls.length; i++) {
			for (j=0; j<allTitleEls[i].length; j++) {
				allTitleEls[i][j].style.color = "#666666";
			}
		}
		
		lastTHolder = "";
		lastId = "";
	
	}
	
	
	
	
	
	
	var abstractEl = document.getElementById('abstract' + id);
	var descriptionEl = document.getElementById('description' + id);
	
	var titleEl = 'titleHolder' + id;
	
	if (abstractEl) {
		if (descriptionEl) {
			if (abstractEl.style.display == "block") {
				abstractEl.style.display = "none";
				descriptionEl.style.display = "block";
				$("#"+titleEl).find("img").hide();
				
				
			} else {
				abstractEl.style.display = "block";
				descriptionEl.style.display = "none";
				$("#"+titleEl).find("img").show();
				
			}
		}
	} else {
		if (descriptionEl) {
			if (descriptionEl.style.display == "none") {
				descriptionEl.style.display = "block";
				$("#"+titleEl).find("img").hide();
			} else {
				descriptionEl.style.display = "none";
				$("#"+titleEl).find("img").show();
			}
		}
	}
}


var currentChapter;

function showSections(chapterId) {
	document.getElementById('preloader').innerHTML = "";
	if (currentChapter != chapterId) {
		
		if (currentChapter) {
			var prevChapterEl = document.getElementById('chapter' + currentChapter);
			prevChapterEl.className = "chapter";
			
			var prevLinkHolder = document.getElementById('cLinkHolder' + currentChapter);
			prevLinkHolder.className = "cLinkHolder";
			
			var prevChapterLinkEl = document.getElementById('chapterLink' + currentChapter);
			prevChapterLinkEl.className = "navLink";

			var prevSectionsEl = document.getElementById("sections" + currentChapter);
			prevSectionsEl.style.display = "none";
		}
		var sectionsEl = document.getElementById("sections" + chapterId);
		sectionsEl.style.display = "block";
		
		var chapterEl = document.getElementById('chapter' + chapterId);
		chapterEl.className = "chapterSel";
		
		var linkHolder = document.getElementById('cLinkHolder' + chapterId);
		linkHolder.className = "cLinkHolderSel";
		
		var chapterLinkEl = document.getElementById('chapterLink' + chapterId);
		chapterLinkEl.className = "navLinkSel";

		currentChapter = chapterId;
	}
}

var currentSection;

function killFlash() {
	var flashEls = document.getElementsByTagName('object');
	for (i=0; i<flashEls.length; i++) {		
		if (flashEls[i].className == "flashEl") {
			var flashId = flashEls[i].id;
			document.getElementById(flashId + "Preview").style.display = "inline";
			document.getElementById(flashId+'Holder').style.display = "none";
		}
	}
}

var prevSectionBg;
function getContent(sectionId) {
	killFlash();
	
	var order = "rank";
	if (sectionId == 10) order = "title1";
	getPages(sectionId, order, '', '');
	
	prevSectionBg = document.getElementById('contentBg' + uSectionId);
	if (currentSection != sectionId) {
		if (currentSection) {
			
			var prevSectionEl = document.getElementById('section' + currentSection);
			prevSectionEl.className = "section";
			
			var prevLinkHolder = document.getElementById('sLinkHolder' + currentSection);
			prevLinkHolder.className = "sLinkHolder";
			
			var prevSectionLinkEl = document.getElementById('sectionLink' + currentSection);
			prevSectionLinkEl.className = "navLink";
		}
	}
	
	var sectionEl = document.getElementById('section' + sectionId);
	sectionEl.className = "sectionSel";
	
	var linkHolder = document.getElementById('sLinkHolder' + sectionId);
	linkHolder.className = "sLinkHolderSel";
			
	var sectionLinkEl = document.getElementById('sectionLink' + sectionId);
	sectionLinkEl.className = "navLinkSel";
	
	currentSection = sectionId;
}

function chapterNavOver(chapterId) {	
	//saturateBackground();
	var chapterEl = document.getElementById('chapter' + chapterId);
	chapterEl.className = "chapterSel";
	
	var chapterLinkEl = document.getElementById('chapterLink' + chapterId);
	chapterLinkEl.className = "navLinkSel";
	
	var linkHolder = document.getElementById('cLinkHolder' + chapterId);
	linkHolder.className = "cLinkHolderSel";
}

function chapterNavOut(chapterId) {
	//desaturateBackground();
	if (currentChapter != chapterId) {
		var chapterEl = document.getElementById('chapter' + chapterId);
		chapterEl.className = "chapter";
		
		var chapterLinkEl = document.getElementById('chapterLink' + chapterId);
		chapterLinkEl.className = "navLink";
		
		var linkHolder = document.getElementById('cLinkHolder' + chapterId);
		linkHolder.className = "cLinkHolder";
	} 
}

function sectionNavOver(sectionId) {
	//saturateBackground();
	var sectionEl = document.getElementById('section' + sectionId);
	sectionEl.className = "sectionSel";
	
	var linkHolder = document.getElementById('sLinkHolder' + sectionId);
	linkHolder.className = "sLinkHolderSel";
	
	var sectionLinkEl = document.getElementById('sectionLink' + sectionId);
	sectionLinkEl.className = "navLinkSel";
}

function sectionNavOut(sectionId) {
	//desaturateBackground();
	if (currentSection != sectionId) {
		var sectionEl = document.getElementById('section' + sectionId);
		sectionEl.className = "section";
		
		var linkHolder = document.getElementById('sLinkHolder' + sectionId);
		linkHolder.className = "sLinkHolder";
	
		var sectionLinkEl = document.getElementById('sectionLink' + sectionId);
		sectionLinkEl.className = "navLink";
	}
}
function preloadImg(path) {
	document.getElementById('preloader').innerHTML += '<img src="' + path + '" />';
}

function showColor(imgEl, path) {
	imgEl.src = path;
}

function showBw(imgEl, path) {
	imgEl.src = path;
}

function showFull(imgEl, path) {
	window.open(path);
}

function showSub(dropId) {
	document.getElementById(dropId).style.display = "block";
}

function hideSub(dropId) {
	document.getElementById(dropId).style.display = "none";
}



function addEmail(emailId) {
	emailInput = document.getElementById('emailAddress' + emailId);
	if (emailInput) {
		if (emailInput.value) {
			insertEmail(emailInput.value);
			document.getElementById('description' + emailId).innerHTML = "<p>You have been successfully added to the mailing list</p>";
		}
	}
}

function removeEmail(emailId) {
	emailInput = document.getElementById('emailAddress' + emailId);
	if (emailInput) {
		if (emailInput.value) {
			deleteEmail(emailInput.value);
			document.getElementById('description' + emailId).innerHTML = "<p>You have been successfully removed from the mailing list</p>";
		}
	}
}


function submitSearch() {
	var searchTerm = document.getElementById('q').value;
	if (searchTerm != "") {
		search(searchTerm);
	}
	document.getElementById('q').value = "Search..."
	return false;
}


function saturateBackground() {
	var bodyclass = $("body").attr("class");
	$("body").removeClass(bodyclass);
	$("body").addClass(bodyclass+"_hover");
}

function desaturateBackground() {
	var bodyclass = $("body").attr("class");
	$("body").removeClass(bodyclass);
	$("body").addClass(bodyclass.replace("_hover", ""));	
}

function printContent(id) {
	window.open("print.php?id="+id);
}

function downloadPDF(id) {
	window.open("/pdf/?id="+id);
}

$(document).ready(function() {
	$("#navMenu").hover(function(e) {
		if (e.pageX < 350) saturateBackground();
	}, function(e) {
		desaturateBackground();
	});
});


