imgKoi = new Image();
imgKoi.src = 'gfx/koi_on.gif';
imgAnkeny = new Image();
imgAnkeny.src = 'gfx/ankeny_on.gif';
imgWGE = new Image();
imgWGE.src = 'gfx/wge_on.gif';
imgCAC = new Image();
imgCAC.src = 'gfx/cac_on.gif';
imgLeftArrow = new Image();
imgLeftArrow.src = 'gfx/left_arrow_on.gif';
imgRightArrow = new Image();
imgRightArrow.src = 'gfx/right_arrow_on.gif';

var index = 0;
var max = -1;
var seminarBulletin = null;
var newsArray = new Array();
var imgInfoArray = new Array();
var gStatus = false;
function imgInfo()
{
	this.src = '';
	this.title = '';
	this.date = '';
	this.desc = '';
	this.link = '';
}
function news()
{
	this.date = '';
	this.body = '';
	this.link = '';
}
function seminar()
{
	this.date = '';
	this.time = '';
	this.location = '';
	this.topic = '';
}
function getSafeElements(xhrObj,rootTag)
{
	var elements = xhrObj.responseXML.getElementsByTagName(rootTag);
	if((elements == null) || (elements.length == 0))
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //backup code for IE
		xmlDoc.loadXML(xhrObj.responseText);
		elements = xmlDoc.getElementsByTagName(rootTag);
		if((elements == null) || (elements.length == 0))
			return null;
	}
	return elements;
}
function loadGallery(xhrObject)
{
	if(xhrObject.status == 404)
	{
		gStatus = false;
		return;
	}
	var elements = getSafeElements(xhrObject,"img_info");
	if(elements == null)
		return;
	var imgSrcs = elements[0].getElementsByTagName("img_src");
	if((imgSrcs != null) && (imgSrcs.length > 0))
	{
		max = max + 1;
		imgInfoArray[max] = new imgInfo();
		imgInfoArray[max].src = imgSrcs[0].firstChild.nodeValue;
		imgInfoArray[max].title = elements[0].getElementsByTagName("img_title")[0].firstChild.nodeValue;
		imgInfoArray[max].date = elements[0].getElementsByTagName("img_date")[0].firstChild.nodeValue;
		imgInfoArray[max].desc = elements[0].getElementsByTagName("img_desc")[0].firstChild.nodeValue;
		imgInfoArray[max].link = elements[0].getElementsByTagName("img_link")[0].firstChild.nodeValue;
	}
}
function loadNews(xhrObject)
{
	var elements = getSafeElements(xhrObject,"news");
	if(elements == null) return;
	var Seminars = elements[0].getElementsByTagName("seminar");
	if((Seminars != null) && (Seminars.length > 0))
	{
		seminarBulletin = new seminar();
		seminarBulletin.date = Seminars[0].getElementsByTagName("date")[0].firstChild.nodeValue;
		seminarBulletin.time = Seminars[0].getElementsByTagName("time")[0].firstChild.nodeValue;
		seminarBulletin.location = Seminars[0].getElementsByTagName("location")[0].firstChild.nodeValue;
		seminarBulletin.topic = Seminars[0].getElementsByTagName("topic")[0].firstChild.nodeValue;
	}

	//Then load the news posts
	var Posts = elements[0].getElementsByTagName("post");
	if((Posts == null) || (Posts.length == 0)) return;
	for(var i = 0; i < Posts.length; i++)
	{
		newsArray[i] = new news();
		newsArray[i].date = Posts[i].getElementsByTagName("date")[0].firstChild.nodeValue;
		newsArray[i].body = Posts[i].getElementsByTagName("body")[0].firstChild.nodeValue;
		newsArray[i].link = Posts[i].getElementsByTagName("link")[0].firstChild.nodeValue;
	}
}
var myGlobalHandlers = {
	onFailure:function() { gStatus = false; },
	onException:function() { gStatus = false; } //this catches missing files!
	};
Ajax.Responders.register(myGlobalHandlers);
function getGalleryXML()
{
	gStatus = true;
	var dummy = "dummy=" + new Date().getTime();
	for(var i = 0; ((gStatus == true) /*&& (i < 10)*/); i++)
		var ajaxRequest = new Ajax.Request('gallery/'+i+'.xml',{method: 'get',parameters: dummy,asynchronous: false,onComplete: loadGallery});
}
function getNewsXML()
{
	var dummy = "dummy=" + new Date().getTime();
	var ajaxRequest = new Ajax.Request('news.xml',{method: 'get',parameters: dummy,asynchronous: false,onComplete: loadNews});
}
function navNext()
{
	index = index + 1;
	if(index > max) { index = 0; }
	update();
}
function navBack()
{
	index = index - 1;
	if(index < 0) { index = max; }
	update();
}
function update()
{
	if(imgInfoArray.length > 0)
	{
		document.gallery.src = "gallery/"+imgInfoArray[index].src;
		$("gallery_title").innerHTML = imgInfoArray[index].title;
		var details = imgInfoArray[index].date;
		if(details == "none")
			details = "";
		else
			details = "<i>Created in "+imgInfoArray[index].date+"</i>.  ";
		details += imgInfoArray[index].desc+"<br><br>";
		details += "<a href=\""+imgInfoArray[index].link+"\">\>Click here for more details</a>";
		$("gallery_desc").innerHTML = details;
	}
}
function updateNews()
{
	$("news").innerHTML = "";
	for(var i = 0; i < newsArray.length; i++)
	{
		var oldNews = $("news").innerHTML;
		var newPost = "<b>"+newsArray[i].date+":</b> ";
		newPost += newsArray[i].body+"<br><a href=\"news.html#"+newsArray[i].link+"\">\>Click here to read more</a><br><br>";
		$("news").innerHTML = oldNews + newPost;
	}
	
	if(seminarBulletin == null)
		$("seminars").innerHTML = "Our last seminar was a great success!  We do not yet have a date for the next one, but will post it here when we do.";
	else
		$("seminars").innerHTML = "<a href=\"seminars.html\">Our next seminar is on <b>"+seminarBulletin.date+"</b> at "+seminarBulletin.location+" at "+seminarBulletin.time+", covering <b>"+seminarBulletin.topic+"</b></a>.";
}
function initContent()
{
	doIEFix(); //NOTE: Requires the include "iefix.js" in the parent HTML file!
	getGalleryXML();
	getNewsXML();
	index = max;
	update();
	updateNews();
}

