window.onload = initAll;
var currentLocation = null;

function loadflashgallery(albumid) {
	var fo = new SWFObject("http://students.washington.edu/sacnas/flash/viewer.swf", "viewer", "100%", "600", "8", "#ffffff");

	// SIMPLEVIEWER CONFIGURATION OPTIONS
	// To use an option, uncomment it by removing the "//" at the start of the line
	// For a description of config options, go to:
	// http://www.airtightinteractive.com/simpleviewer/options.html

	fo.addVariable("xmlDataPath", "http://students.washington.edu/sacnas/gallery/query.php?albumid=" + albumid);
	//fo.addVariable("firstImageIndex", "5");
	fo.addVariable("langOpenImage", "Open Image in New Window");
	fo.addVariable("langAbout", "(C) UW SACNAS 2009");

	fo.write("flash_gallery");
}

function loadflashmp3player() {
	var fo = new SWFObject("http://students.washington.edu/sacnas/flash/mp3player.swf", "player", "150", "200", "8", "#ffffff");

	// SIMPLEVIEWER CONFIGURATION OPTIONS
	// To use an option, uncomment it by removing the "//" at the start of the line
	// For a description of config options, go to:
	// http://www.airtightinteractive.com/simpleviewer/options.html

	fo.addVariable("configxml", "includes/control/mp3player.php");
	fo.write("flash_mp3player");
}



function checkAnchor() {
	if(parent.location.hash != currentLocation) {
		if(parent.location.hash != '') {
			changeContent(parent.location.hash.substring(1));
		}
		currentLocation = parent.location.hash;
	}
	setTimeout("checkAnchor()", 300);
}

function initNav() {
	try {
/*		var allLinks = document.getElementsByTagName("div");

		for(var i=0; i < allLinks.length; i++) {
			if(allLinks[i].className.indexOf("main-navigation-link") > -1) {
				var thisMenuName = allLinks[i].id + '-subnav';
				var thisMenu = document.getElementById(thisMenuName).style;
				
				if(thisMenu.display == "none") {
					allLinks[i].onmouseover = toggleMenu;
					allLinks[i].onmouseout = toggleMenu;
				}
			}
		}
*/
		checkAnchor();
	} catch(error) {
	}
}

function initAll() {
	try {
		initNav();
		currentLocation = parent.location.substring(30);
	} catch(err) {
	}
}

function onClickWrapper(id) {
	try {
		parent.location.hash = $(id).href.substring(30);
		checkAnchor();
		return false;
	} catch(err) {
		return true;
	}
}

function changeContent(e) {
	try {
		if(!e) {
			e = window.event;
		}

		var url= null;
		if(e && e.target)  {
			url = e.target;
		} else {
			url = e;
		}

		if(url.lastIndexOf('?') > 0) {
			url = url + '&v=ajax';
		} else {
			url = url + '?v=ajax';
		}

		url = url + '&rand=' + Math.random();
			
		var ajax = new Ajax.Request(url, {method: 'get', onComplete: update_content});
		return false;
	} catch(err) {
		return true;
	}
}

function update_content(request) {
	try {
		var data = new Object();
		if(window.ActiveXObject) {
			var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			var x = '';
			xmlDoc.async="false";
			xmlDoc.loadXML(request.responseText);
	
			var page = xmlDoc.childNodes[1];

			for(i = 0; i < page.childNodes.length; i++) {
				if(page.childNodes[i].tagName == 'title') {
					data.title = page.childNodes[i].text;
				} else if(page.childNodes[i].tagName == 'home_path') {
					data.home_path = page.childNodes[i].text;
				} else if(page.childNodes[i].tagName == 'dir') {
					data.dir = page.childNodes[i].text;
				} else if(page.childNodes[i].tagName == 'content') {
					data.content = page.childNodes[i].text;
				} else if(page.childNodes[i].tagName == 'id') {
					data.id = page.childNodes[i].text;
				}
			}
		} else if(request && request.responseXML && request.responseXML.getElementsByTagName) {
			var rXML = request.responseXML;
			var page = rXML.getElementsByTagName('webpage')[0];
			data.title = page.getElementsByTagName('title')[0].textContent;
			data.home_path = page.getElementsByTagName('home_path')[0].textContent;
			data.dir = page.getElementsByTagName('dir')[0].textContent;
			data.content = page.getElementsByTagName('content')[0].textContent;
			data.id = page.getElementsByTagName('id')[0].textContent;
		}

		$('content').innerHTML = data.content;
		document.title = data.title;
	
		var url = '/sacnas/includes/control/navigation.php?home_path=' + data.home_path +
			'&dir=' + data.dir + '&pageid=' + data.id + '&rand=' + Math.random();

		var ajax = new Ajax.Updater({success: 'side-bar-nav-content'},
					url, {method: 'get', onComplete: initNav});

		return false;
	} catch(err) {
		return true;
	}
}

function toggleMenu() {
	try {
		var thisMenuName = this.id + '-subnav';
		var thisMenu = document.getElementById(thisMenuName).style;
		thisMenu.display = (thisMenu.display == "block") ? "none" : "block";
		return true;
	} catch(err) {
		return false;
	}
}
