function $() // $() a la prototype.js (http://prototype.conio.net/)
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string') element = document.getElementById(element);
		if (arguments.length == 1) return element;
		elements.push(element);
	}
	return elements;
}
document.getElementsByClassName = function(classname) // who doesn't have an adapted version of getElementsByClassName? - this is a mix of prototype and http://www.snook.ca/archives/000370.php
{
	var a = [];
	var re = new RegExp('(^| )'+classname+'( |$)');
	var els = document.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	{
		if(re.test(els[i].className)) a.push(els[i]);
	}
	return a;
}
var Cookies = { // cookie tools care of PPK - http://www.quirksmode.org/js/cookies.html
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	read: function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();
function fixPng(p)
{
	var imgsrc;
	var trans = "img/site/foo.gif";
	if(typeof p.tested != undefined && !p.tested &&  typeof p.runtimeStyle != 'undefined') {
		imgsrc = p.src;
		if ( /\.png$/.test( imgsrc.toLowerCase() ) ) {
			p.src = trans;
			p.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgsrc + "',sizingMethod='scale')";
			p.tested = true;
		}
	}
}
function leadingZero(n, totalDigits) 
{
	n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length) 
	{ 
		for (i=0; i < (totalDigits-n.length); i++) 
		{ 
			pd += '0'; 
		} 
	}
	return pd + n.toString(); 
}
function ajaxAnImage(url,target,obj) {
    if (window.XMLHttpRequest)
	{
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {showTheImage(url,target,obj);};
        req.open("GET", url, true);
        req.send(null);
    }
	else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {showTheImage(url,target,obj);};
            req.open("GET", url, true);
            req.send();
        }
    }
}   
function showTheImage(url,target,obj)
{
	// while it's loading...
	if (req.readyState == 0 || req.readyState == 1 || req.readyState == 2 || req.readyState == 3)
	{
	}
	if (req.readyState == 4)
	{
		// only if "OK"
		if (req.status == 200)
		{
			try
			{
				var closeMessage = document.createElement('div');
				closeMessage.innerHTML = "Click anywhere to continue browsing.";
				closeMessage.id = 'closeMessage';
				closeMessage.onclick = modal.destroy();
				$('modalContainer').appendChild(closeMessage);
				var imgBox = $('modalPanelDisplay');
				if (document.documentElement)
				{
					closeMessage.style.top = (document.documentElement.scrollTop+30) + 'px';
					imgBox.style.background = "url('" + url + "') center " + (document.documentElement.scrollTop+80) + "px no-repeat";
				}
				else
				{
					closeMessage.style.top = (document.body.scrollTop+30) + 'px';
					imgBox.style.background = "url('" + url + "') center " + (document.body.scrollTop+80) + "px no-repeat";
				}
			}
			catch (err)
			{
			}
		}
		else
		{
		
		}
	}
}
zInt = 0;
var animate =
{
	pause: function(obj,delay,func) // keep event bubbling, but limit how often a given event is fired
	{
		return function()
		{
			if (!delay)
			{
				clearTimeout(obj.wait);
				if (!obj.mode)
				{
					func();
					obj.mode = true;
				}
			}
			else
			{
				obj.wait = setTimeout(function()
				{
					func();
					obj.mode = false;
				},delay);
			}
		}
	},
	ease: function(obj,time,step,actions)
	{
		return function()
		{
			var steps = Math.ceil(time/step);
			if (!time)
			{
				animate.paint(obj,actions,0,1);
				clearInterval(obj.ease);
			}
			else
			{
				var i = 0;
				obj.ease = setInterval(function()
				{
					i++;
					animate.paint(obj,actions,i,steps);
					if (i>=steps)
					{
						clearInterval(obj.ease);
					}
				},step);
			}
		}
	},
	paint: function(obj,actions,i,max)
	{
		for (var n=0; n<actions.length; n++)
		{
			var act = actions[n][0];
			var val = actions[n][1];
			switch(act)
			{
				case 'fadeOut':
					obj.style.opacity = 1-((i/max)*(1-(val/100)));
					obj.style.filter = 'alpha(opacity=' + (100-((i/max)*(100-val))) + ')';
					break;
				case 'fadeIn':
					obj.style.opacity = ((i/max)*(val/100));
					obj.style.filter = 'alpha(opacity=' + ((i/max)*val) + ')';
					break;
				case 'fadeInOut':
					obj.style.opacity = Math.round((Math.sin((i/max)*Math.PI))*100)/100;
					obj.style.filter = 'alpha(opacity=' + Math.round((Math.sin((i/max)*Math.PI))*100) + ')';
					break;
				case 'left':
					obj.style.left = val + 'px';
					break;
				case 'top':
					obj.style.top = val + 'px';
					break;
				case 'marginLeft':
					obj.style.marginLeft = val + 'px';
					break;
				case 'easeLeft':
					obj.style.left = val+(i/max)*10 + 'px';
					break;
				case 'disappear':
					if (i == max) obj.style.display = 'none';
				case 'onComplete':
					if (i == max) val(obj);
					break;
				case 'onStart':
					if (i == 0) val(obj);
					break;
			}
		}
	}
};
var modal =
{
	show: function(obj)
	{
		return function()
		{
			modal.destroy()();
			var container = $('modalContainer');
			var panel = document.createElement('div');
			panel.id = 'modalPanel';
			container.appendChild(panel);
			panel.style.height = document.body.scrollHeight + 'px';
			panel.style.opacity = .60;
			panel.style.filter = "alpha(opacity=60)";
			panel.style.background = 'black';
			panel.onclick = modal.destroy();
			var panelTwo = document.createElement('div');
			container.appendChild(panelTwo);
			panelTwo.style.height = document.body.scrollHeight + 'px';
			panelTwo.id = 'modalPanelDisplay';
			panelTwo.onclick = modal.destroy();
			var bgTopOffset = (document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;
			bgTopOffset += 300;
			panelTwo.style.background = "url('./img/site/loadingAnimation.gif') center " + bgTopOffset + "px no-repeat";
			var img = obj.getElementsByTagName('img')[0];
			var largeImg = img.src.replace(/\/thumb/,"");
			ajaxAnImage(largeImg, panel, obj);
			return false;
		}
	},
	destroy: function()
	{
		return function()
		{
			try
			{
				$('modalContainer').innerHTML = '';
			}
			catch (err)
			{
			}
		}
	}
};
var animsFired = 0;
var checkDomLoad = setInterval(
	function()
	{
		if (document.body.innerHTML.search(/domload/ig))
		{
			clearInterval(checkDomLoad);
			var fixPngs = document.getElementsByClassName('pngfix');
			for (var i=0; i<fixPngs.length; i++)
			{
				fixPng(fixPngs[i]);
				var thing = fixPngs[i].parentNode;
				var off = animate.pause
				(
					thing, 50, animate.ease
					(
						thing, 200, 20, [['fadeOut',40]]
					)
				);
				var on = animate.pause
				(
					thing, false, animate.ease
					(
						thing, false, null, [['fadeOut',100]]
					)
				);
				thing.onmouseout = off;
				thing.onmouseover = on;
				thing.onblur = off;
				thing.onfocus = on;
				/* thing.hideFocus = 'true'; */
			}
			/* if (Cookies.read('littleevergladessteeplechase') != 'animation')
			{
				Cookies.create('littleevergladessteeplechase','animation',1000);
				animateHeader();
			} */
			var subnavs = document.getElementsByClassName('subnav');
			for (var i = 0; i < subnavs.length; i++)
			{
				var subnav = subnavs[i];
				var flyout = subnav.getElementsByTagName('ul')[0];
				var zInt = 1;
				var off = animate.pause
				(
					flyout, 50, animate.ease
					(
						flyout, 100, 20, [['fadeOut',0],['easeLeft',170],['top',subnav.offsetTop],['onComplete',function(obj)
						{
							obj.style.left = -1000 + 'px';
							obj.parentNode.className = obj.parentNode.className.replace(/ hover/ig,"");
						}]]
					)
				);
				var on = animate.pause
				(
					flyout, false, animate.ease
					(
						flyout, false, null, [['fadeOut',100],['left',170],['top',subnav.offsetTop],['onStart',function(obj)
						{
							zInt++;
							obj.style.zIndex = zInt;
							obj.parentNode.className = obj.parentNode.className + ' hover';
						}]]
					)
				);
				subnav.onmouseout = off;
				subnav.onmouseover = on;
				subnav.onblur = off;
				subnav.onfocus = on;
				subnav.ondeactivate = off;
				subnav.onactivate = on;
			}
			var modals = document.getElementsByClassName('modal');
			for (var i = 0; i < modals.length; i++)
			{
				modals[i].onclick = modal.show(modals[i]);
			}
		}
	}
,50);
window.onload = function()
{
	var currentPage = window.location.toString().split("/");
	currentPage = currentPage[currentPage.length-1].substring(3);
	if (currentPage == 'Home')
	{
		animateHeader();
		var sound = document.createElement('div');
		sound.innerHTML = '<noembed><bgsound src="./misc/calltothepost.wav" loop="false"/></noembed><embed src="./misc/calltothepost.wav" height="0" width="0" autostart="true" loop="false"></embed>';
		document.body.appendChild(sound);
	}
	else
	{
		$('hd_anim').style.background = "url('img/site/animate_hd_09_v2.jpg') center center no-repeat";
	}
}
function animateHeader()
{
	var images = [];
	for (var i=0; i<9; i++)
	{
		var img = new Image;
		img.src = "img/site/animate_hd_" + leadingZero(i+1,2) + "_v2.jpg";
		images.push(img);
	}
	for (var i=0; i<images.length; i++)
	{
		var frame = document.createElement('div');
		$('hd_anim').appendChild(frame);
		frame.style.backgroundImage = "url('" + images[i].src + "')";
		animate.pause
		(
			frame, 1000*i, animate.ease
			(
				frame, 500, 40, [['fadeIn',100],['left',0]]
			)
		)();
	}
}
