// contents of ../_/js/element/element.js
// copy of em/em.js

function $(id)
{
//	alert(arguments); // the arguments to the function
	var element = document.getElementById(id);
	if(element == null)
		alert('element not found: '+id);
	return element;
}

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(element.protoBase == undefined)
	{
		var match = element.toString().match(/\s[^\]]+/)
		var proto = (match) ? eval(match[0]).prototype : element;
		proto.proto = proto;
		proto.protoBase = true;
		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 = proto.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 = 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(isUnd(value))
					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.size(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(element.protoAbs == undefined)
	{
		var proto = element.proto;
		proto.protoAbs = true;
		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(element.protoIframe == undefined)
	{
		var proto = element.proto;
		proto.protoIframe = true;
		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(element.protoLink == undefined)
	{
		var proto = element.proto;
		proto.protoLink = true;
		proto.onclick = function()
		{ 
			if(prop.oc)
				prop.oc(this);
			return (prop.allow || prop.allow == undefined) ? true : false;
		};
		
	//	element.set  = function(property, value) { properties[property] = value;	};
	//	element.setC = function(current)         { element.className = current ? 'current' : ''; };
	//	element.setOC = function(func) { element.onclick = function() { func(element, properties.data); return (properties.allow) ? true : false; }; };
	//	element.setU = function(newURL)          { element.href = url = newURL; };
		element.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(element.protoUL == undefined)
	{
		var proto = element.proto;
		proto.protoUL = true;
		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(element.protoIMG == undefined)
	{
		var proto = element.proto;
		proto.protoIMG = true;
		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/element/slide_show.js
window.runShow = runShow;

function runShow(id, props)
{
	var fadeTime = props.fadeTime;
	var delay    = props.delay;
	var minFade  = props.minFade;
	var repeat   = props.repeat;

	var smoothness = 25;
	var fadeAmount = (100 - minFade) / (fadeTime / smoothness);

	var index    = 0;
	var element  = $(id);
	var elements = element.childNodes;
	var len      = elements.length;
	var current  = elements[0];
	var fade     = 100;
	var width    = parseInt(props.width);
	var height   = parseInt(props.height);
	var timerId;
	var setOpacity = document.all && window.opera == null ? setOpacityIE : setOpacityOther;
	next();
	
	function next()
	{
		var temp = elements[(index + 1) % len]; // -1 to skip the script/controls element
		if(temp.src && temp._fetched == null)
		{
			var img = new Image();
			if(width || height)
			{
				img.onload = function()
				{
					var scaleX = width  ? img.width / width   : 0;
					var scaleY = height ? img.height / height : 0;
					var scale = scaleX > scaleY ? scaleX : scaleY;
					
					var w = Math.round(img.width / scale);
					var h = Math.round(img.height / scale);
					temp.style.width  = w+'px';
					temp.style.height = h+'px';

					if(width)
						temp.style.marginLeft = temp.style.marginRight = ((width - w) / 2)+'px';
					if(height)
						temp.style.marginTop = temp.style.marginBottom = ((height - h) / 2)+'px';

					setTimer();
				};
			}
			else setTimer();
			
			img.src = temp.src;
			temp._fetched = true;
		}
		else setTimer();
		
		function setTimer()
		{
			setTimeout(function() { timerId = setInterval(fadeOut, smoothness); }, delay - fadeTime);
		}
	}
	
	function fadeOut()
	{
		fade -= fadeAmount;
		if(fade < minFade)
		{
			clearInterval(timerId);
			current.style.display = 'none';
			
			if(repeat == false && index + 1 == len)
				return;
			index = ++index % len;
			current = elements[index];
			
			fade -= fadeAmount; 
			fadeIn();
			timerId = setInterval(fadeIn, smoothness);
			current.style.display = '';
		}
		else setOpacity();
	}
	
	function fadeIn()
	{
		fade += fadeAmount;
		if(fade > 100)
		{
			clearInterval(timerId);
			next();
		}
		else setOpacity();
	}
	
	function setOpacityIE()
	{
		current.style.filter = 'alpha(opacity='+fade+');';
	}
	function setOpacityOther()
	{
		current.style.opacity = fade / 100;
	}
}
// contents of js/index.js
var currentPage; // set in onLoad() 
var test = false; // will alert errors when a page isn't found

function init(current)
{
	currentPage = current; // set the default page here
	show(window.location.hash ? window.location.hash.substr(1) : currentPage, null, true);
	
	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 != currentPage)
			show(page);
//		alert(page);
	}
}

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 $(id)
{
	return document.getElementById(id);
}

