// contents of js/index.js
var currentPage; // set in onLoad() 
var test = false; // will alert errors when a page isn't found

function onLoad(page)
{
	with($('event_div')) { style.display = (page == 'event') ? '' : 'none'; style.height = ''; style.overflow = 'auto'; }
//	$('event_div').appendChild(loadFile('calendar.php'));
	preloadLinks();
	currentPage = page; // set the default page here
	
	window.setInterval(run, 500); // handles the back button, doesn't work in Opera, IE7 jumps past all the hash changes
	function run()
	{
		var page = window.location.hash.substr(1);
		if(page && page != currentPage)
			show(page);
//		alert(page);
	}
}

function preloadLinks()
{
	var div = $('links_div');
	var children = div.childNodes;
	for(var n = 0; n < children.length; n++)
		if(children[n].nodeName && children[n].nodeName.toLowerCase() == 'ul')
			children = children[n].childNodes;
	for(var n = 0; n < children.length; n++)
	{
		if(children[n].nodeName && children[n].nodeName.toLowerCase() == 'li')
		{
			var img = children[n].firstChild.firstChild;
			var img2 = new Image();
			img2.src = img.src.match(/_(\.\w+)$/) ? img.src.replace(/_(\.\w+)$/, '_$1') : img.src.replace(/(\.\w+)$/, '_$1');
		}
	}
}
function show(page, link, force)
{
	var id, div, img;

	if(link)
		link.blur();

	if(page != currentPage || force)
	{
		if(div = $(id = currentPage+'_div'))
			div.style.display = 'none';
		else if(test)
			alert('Failed to hide div: '+id);
		
		if(div = $(id = page+'_div'))
			div.style.display = '';
		else if(test)
			alert('Failed to show div: '+id);

		if(img = $(id = currentPage+'_img'))
			img.src = img.src.replace(/\_(\.\w+)$/, '$1');
		else if(test)
			alert('Failed to switch image: '+id);
	
		if(img = $(id = page+'_img'))
			img.src = img.src.replace(/(\.\w+)$/, '_$1');
		else if(test)
			alert('Failed to switch image: '+id);
	}
	
	if(div && img)
	{
		currentPage = page;
		window.location.hash = currentPage;
	}
	else show(currentPage, null, true);
	
	return false;
}

function loadFile(url)
{
	var div = divEM({});
	var loading = div.ac(loadingEM({}));
	httpRequest(url, onLoad);
	return div;
	
	function onLoad(html)
	{
		loading.rs();
		div.innerHTML = html;
	}
}

function httpRequest(url, handler)
{
	if(window.XMLHttpRequest || window.ActiveXObject)
	{
		var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		request.onreadystatechange = onStateChange;
		request.open('GET', url, true);
		request.send(null);
	}
	else alert('XML request not supported, get a new browser!!!');
	
	function onStateChange()
	{
		if(request.readyState == 4) // 4 means done loading
		{
			if(request.status == 200)
				handler(request.responseText);
			else alert('Load Failed: status: '+request.status+": "+request.statusText);
		}
	}
}
function $(id)
{
	return document.getElementById(id);
}
// contents of ../_/js/em/em.js
//var googleChrome = navigator.userAgent.match(/Chrome/i) ? true : false;
var firefox = navigator.userAgent.match(/Firefox/i) ? true : false;

function $(id)
{
	return document.getElementById(id);
}

document.doc  = function()
{
	return (document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
};
document.size = function()
{
	var height = (document.documentElement) ? document.documentElement.clientHeight : document.body.clientHeight;
	var width  = (document.documentElement) ? document.documentElement.clientWidth  : document.body.clientWidth;
	var bodyW = document.style('width', null), match;
	var offset = (bodyW && (match = bodyW.match(/(\d+)px/i))) ? Math.round((width - match[1]) / 2) : 0;
	width -= offset * 2;
	return {w:width, h:height, o:offset};
};
document.style = function(key, def, numeric) 
{ 
	var doc = document.doc();
	var style = (doc.currentStyle) ? doc.currentStyle : getComputedStyle(doc, null);
	if(key)
	{
		var value = style[key];
		if(value == undefined)
			return def;
		else return (numeric) ? parseInt(value) : value;
	}
	else return style;
};
document.ge = function(id, def) 
{ 
	var element = document.getElementById(id); 
	if(element)
		return element;
	else if(def)
		return def;
	else alert('element not found: '+id);
};
document.cl = function()      { while(document.body.hasChildNodes()) document.rc(document.body.firstChild); };
document.ac = function(child) { document.body.appendChild(typeof(child) != 'object' ? child = document.createTextNode(child) : child); return child; };
document.rc = function(child) { document.body.removeChild(child); return child; };
document.sw = function(newChild, oldChild) { document.body.replaceChild(newChild, oldChild); };

function baseEM(prop, content, element, type) // type is tag or element
{
	element = element ? (typeof(element) == 'string' ? document.ge(element) : element) : document.createElement(type);
	element.prop = prop;

	if(firefox == false || element.protoBase == null)
	{
		var proto, match;
		if(firefox && (match = element.toString().match(/\s[^\]]+/)) && (proto = eval(match[0]).prototype))
		{
			proto.proto = proto;
			proto.protoBase = true;
		}
		else proto = element;

		proto.ac = function(child)
		{
			if(typeof(child) == 'object')
			{
				if(child instanceof Array)
				{
					for(var n = 0; n < child.length; n++)
					{
						if(typeof(child[n]) == 'object')
							this.appendChild(child[n]);
						else this.appendChild(child[n] = document.createTextNode(child[n]));
					}
				}
				else this.appendChild(child);
			}
			else this.appendChild(child = document.createTextNode(child));
			
			return child;
		};
	
		proto.cl = function(startAfter, stopBefore)
		{
			if(startAfter != null)
			{
				var current = this.firstChild;
				while(current != null)
				{
					if(current == startAfter)
					{
						current = current.nextSibling;
						while(current != null && current != stopBefore)
						{
							var temp = current;
							current = current.nextSibling;
							this.removeChild(temp);
						}
						break;
					}
					else current = current.nextSibling;
				}
			}
			else if(stopBefore != null)
			{
				while(this.hasChildNodes() && this.firstChild != stopBefore)
					this.removeChild(this.firstChild);
			}
			else
			{
				while(this.hasChildNodes())
					this.removeChild(this.firstChild);
			}
		};
		
		proto.cn = function(index)
		{
			return this.childNodes[index];
		};
		
		proto.ih = function(html)
		{
			this.innerHTML = html;
		};

		proto.rs = function()
		{
			var parent = this.parentNode
			if(parent != null && parent != '#document-fragment')
				parent.removeChild(this);
		};

		proto.ia = function(newElement, oldElement)
		{
			if(typeof(newElement) != 'object')
				newElement = document.createTextNode(newElement);
			if(oldElement.nextSibling)
				this.insertBefore(newElement, oldElement.nextSibling);
			else this.appendChild(newElement);
			return newElement;
		}
		proto.ib = function(newElement, oldElement)
		{
			if(typeof(newElement) != 'object')
				newElement = document.createTextNode(newElement);
			this.insertBefore(newElement, oldElement);
			return newElement;
		};
		
		proto.rc = element.removeChild;
		
		proto.rw = function(newElement) 
		{ 
			if(typeof(newElement) != 'object')
				newElement = document.createTextNode(newElement);
			this.parentNode.replaceChild(newElement, this);
			return newElement;
		};
		
		proto.sw = function(newElement, oldElement)
		{
			if(typeof(newElement) != 'object')
				newElement = document.createTextNode(newElement);
			this.replaceChild(newElement, oldElement);
			return newElement;
		};
		
		proto.hide = function() { this.style.display = 'none'; };
		proto.show = function() { this.style.display = ''; };

		// proto.size not allowed for form elements
		proto.setSize = function(size)
		{
			if(typeof(size) == 'object')
			{
				if(size.w)
					this.style.width = size.w+'px';
				if(size.h)
					this.style.height = size.h+'px';
			}
			else if(typeof(size) == 'number')
				this.style.width = size+'px';
		};
		
		proto.gs = function(key, def, numeric) 
		{ 
			var style = this.currentStyle || getComputedStyle(this, null) || this.style;
			if(key)
			{
				var value = style[key];
				if(value !== undefined)
					return def;
				else return (numeric) ? parseInt(value) : value;
			}
			else return style;
		};

	}

	if(prop.id)
		element.id = prop.id;
	if(prop.c)
		element.className = prop.c;
	if(prop.s)
		element.setSize(prop.s);
	if(prop.t)
		element.title = prop.t;
	if(content != undefined)
	{
		if(prop.html)
		{
			if(content)
				element.innerHTML = content;
		}
		else element.ac(content);
	}
	return element;
}

function absEM(prop, content, element)
{
	element = baseEM(prop, content, element, 'div');
	
	if(firefox == false || element.protoAbs == null)
	{
		var proto;
		if(firefox && element.proto)
		{
			proto = element.proto;
			proto.protoAbs = true;
		}
		else proto = element;

		proto.position = function(position)
		{
			if(typeof(position.l) == 'number')
				this.style.left = position.l+'px';
			if(typeof(position.r) == 'number')
				this.style.right = position.r+'px';
			if(typeof(position.t) == 'number')
				this.style.top = position.t+'px';
			if(typeof(position.b) == 'number')
				this.style.bottom = position.b+'px';
		};
		
		proto.level = function(level) { this.style.zIndex = level; };
	}
	element.style.position = 'absolute';
	if(prop.p)
		element.position(prop.p);
	return element;
}

function brEM()
{
	return document.createElement('br');
}

function divEM(prop, content, element)
{
	element = baseEM(prop, content, element, 'div');
	return element;
}

function iframeEM(prop, url, element)
{
	element = baseEM(prop, null, element, 'iframe');
	if(firefox == false || element.protoIframe == null)
	{
		var proto;
		if(firefox && element.proto)
		{
			proto = element.proto;
			proto.protoIframe = true;
		}
		else proto = element;

		proto.init = function(url) { this.src = url; };
	}
	
	if(url)
		element.init(url);
	return element;
}

function linkEM(prop, content, element)
{
	element = baseEM(prop, content, element, 'a');
	element.href = (prop.url) ? prop.url : 'javascript:;';
	
	if(firefox == false || element.protoLink == null)
	{
		var proto;
		if(firefox && element.proto)
		{
			proto = element.proto;
			proto.protoLink = true;
		}
		else proto = element;
		
		proto.onclick = function()
		{ 
			if(prop.oc)
				prop.oc(this);
			return (prop.allow || prop.allow == undefined) ? true : false;
		};
		
		proto.setT = function(title)
		{
			prop.t = title;
			this.sw(title, this.firstChild);
		};
	}
	if(prop.blank)
		element.target = '_blank';
	
	return element;
}

function spanEM(prop, content, element)
{
	return baseEM(prop, content, element, 'span');
}

function strongEM(prop, content, element)
{
	return baseEM(prop, content, element, 'strong');
}

function subEM(prop, content, element)
{
	return baseEM(prop, content, element, 'sub');
}

function supEM(prop, content, element)
{
	return baseEM(prop, content, element, 'sup');
}

function pEM(prop, content, element)
{
	return baseEM(prop, content, element, 'p');
}

function h1EM(prop, content, element)
{
	return baseEM(prop, content, element, 'h1');
}
function h2EM(prop, content, element)
{
	return baseEM(prop, content, element, 'h2');
}
function h3EM(prop, content, element)
{
	return baseEM(prop, content, element, 'h3');
}
function h4EM(prop, content, element)
{
	return baseEM(prop, content, element, 'h4');
}
function h5EM(prop, content, element)
{
	return baseEM(prop, content, element, 'h5');
}

function ulEM(prop, content, element)
{
	element = baseEM(prop, null, element, 'ul');
	if(firefox == false || element.protoUL == null)
	{
		var proto;
		if(firefox && element.proto)
		{
			proto = element.proto;
			proto.protoUL = true;
		}
		else proto = element;

		proto.ac = function(child)
		{
			if(typeof(child) == 'object')
			{
				if(child instanceof Array)
				{
					for(var n = 0; n < child.length; n++)
					{
						if(typeof(child[n]) == 'object')
						{
							if(child[n] instanceof Array)
								this.appendChild(child[n] = ulEM({}, child[n]));
							else this.appendChild(child[n]);
						}
						else this.appendChild(child[n] = liEM({}, child[n]));
					}
				}
				else this.appendChild(child);
			}
			else this.appendChild(child = liEM({}, child));
			return child;
		}
	}
	if(content)
		element.ac(content);
	return element;
}

function liEM(prop, content, element)
{
	return baseEM(prop, content, element, 'li');
}

function imgEM(prop, src, element)
{
	element = baseEM(prop, null, element, 'img');
	if(src)
		element.src = src;
	element.alt = prop.alt || prop.t || src;

	if(firefox == false || element.protoIMG == null)
	{
		var proto;
		if(firefox && element.proto)
		{
			proto = element.proto;
			proto.protoIMG = true;
		}
		else proto = element;

		proto.fit = function(fit)
		{
			var heightRatio = fit.ih / fit.ah;
			var widthRatio  = fit.iw / fit.aw;
			var scaledW, scaledH;
			if(heightRatio > widthRatio)
			{
				scaledW  = Math.floor(fit.iw / heightRatio);
				scaledH = fit.ah;
				if(fit.c)
					element.style.marginRight = element.style.marginLeft = Math.round((fit.aw - scaledW) / 2)+'px';
			}
			else
			{
				scaledW  = fit.aw;
				scaledH = Math.floor(fit.ih / widthRatio);
				if(fit.c)
					element.style.marginBottom = element.style.marginTop = Math.round((fit.ah - scaledH) / 2)+'px';
			}
			this.width  = scaledW;
			this.height = scaledH;
			this.style.width  = scaledW+'px';
			this.style.height = scaledH+'px';
		}
	}
	
	if(prop.f)
		element.fit(prop.f);
	if(prop.oc)
		element.onclick = prop.oc;
	return element;
}
// contents of ../_/js/em/loading.js
function loadingEM(prop, content, element)
{
	prop.c = 'loading_div'+(prop.c ? ' '+prop.c : '');
	var element   = (prop.inline) ? spanEM(prop, content, element) : divEM(prop, content, element);
	element.start = function() { intervalID = window.setInterval(element.onInc, prop.interval); };
	element.stop  = function() { window.clearInterval(intervalID); };

	var intervalID;
	var counter = 0;
	if(prop.interval == null)
		prop.interval = 500;
	if(prop.length == null)
		prop.length = 5;
	prop.timeout = (prop.timeout ? prop.timeout * 1000 : 30000) / prop.interval;
	
	element.onInc = function()
	{
		if(element.parentNode == null || element.parentNode.nodeName == '#document-fragment' || counter > prop.timeout)
			element.stop();
		else if(++counter % prop.length)
			element.ac('.');
		else element.cl(element.firstChild);
	}
	return element;
}
