addEvent(window, "load", initializePage);

var bodyTag;

function initializePage(event)
	{
	initializeDragable();
	initializeRoundedCorners();
	addEvent(window, "resize", resetBrowserDimensions);
	initializeTagCloud();
	//initialiseJeremy();
	initialiseHamish();
	var bodyTag = document.body;
	if(window.location && window.location.hash)
		{
		hashData = window.location.hash;
		hashData = hashData.substring(1).replace(/[^0-9a-z-.]/ig, "");
		backgroundImagePath = "/random-images/" + hashData;
		bodyTag.style.background = "url('" + backgroundImagePath + "')";
		}
	}

var aerofallosovInterval = false;
var aerofallosovs = Array()
var aerofallosovPhysics = Array()

function initializeAerofallosovAttack()
	{
	if(!bodyTag) bodyTag = getTags("body")[0];
	for(var index = 0; index < 10; index++)
		{
		var aerofallosov = document.createElement("div");
		classAttribute = document.createAttribute("class")
		classAttribute.nodeValue = "aerofallosov";
		aerofallosov.setAttributeNode(classAttribute);
		idAttribute = document.createAttribute("id")
		idAttribute.nodeValue = "aerofallosov" + (Math.random() * 9999);
		aerofallosov.setAttributeNode(idAttribute);
		bodyTag.appendChild(aerofallosov);
		aerofallosovs.push(aerofallosov);
		}
	}

function aerofallosovAttack()
	{
	if(aerofallosovInterval) return;
	initializeAerofallosovAttack();
	aerofallosovInterval = setInterval("animateAerofallosov();", 50);
	}

function animateAerofallosov()
	{
	var negativeBorder = 30;
	for(var aerofallosovIndex=0; aerofallosovIndex < aerofallosovs.length; aerofallosovIndex++)
		{
		var aerofallosov = aerofallosovs[aerofallosovIndex];
		if(!aerofallosovPhysics[aerofallosovIndex])
			{
			aerofallosovPhysics[aerofallosovIndex] = new Object()
			var direction = parseInt(Math.random() * 4) + 1;
			switch(direction)
				{
				case 1:
					aerofallosovPhysics[aerofallosovIndex].left = Math.random() * getBrowserWidth();
					aerofallosovPhysics[aerofallosovIndex].top = -negativeBorder;
					break;
				case 2:
					aerofallosovPhysics[aerofallosovIndex].left = getBrowserWidth() + negativeBorder;
					aerofallosovPhysics[aerofallosovIndex].top = Math.random() * getBrowserHeight();
					break;
				case 3:
					aerofallosovPhysics[aerofallosovIndex].left = Math.random() * getBrowserWidth();
					aerofallosovPhysics[aerofallosovIndex].top = negativeBorder;
					break;
				case 4:
					aerofallosovPhysics[aerofallosovIndex].left = -negativeBorder;
					aerofallosovPhysics[aerofallosovIndex].top = Math.random() * getBrowserHeight();
					break;
				}
			aerofallosovPhysics[aerofallosovIndex].leftAccel = 0;
			aerofallosovPhysics[aerofallosovIndex].topAccel = 0;
			}

		aerofallosovPhysics[aerofallosovIndex].leftAccel += Math.random() - 0.5;
		if(aerofallosovPhysics[aerofallosovIndex].leftAccel < -5)
			{
			aerofallosovPhysics[aerofallosovIndex].leftAccel = -5
			}
		else if(aerofallosovPhysics[aerofallosovIndex].leftAccel > 5)
			{
			aerofallosovPhysics[aerofallosovIndex].leftAccel = 5
			}
		aerofallosovPhysics[aerofallosovIndex].left += aerofallosovPhysics[aerofallosovIndex].leftAccel;

		aerofallosovPhysics[aerofallosovIndex].topAccel += Math.random() - 0.5;
		if(aerofallosovPhysics[aerofallosovIndex].topAccel < -5)
			{
			aerofallosovPhysics[aerofallosovIndex].topAccel = -5
			}
		else if(aerofallosovPhysics[aerofallosovIndex].topAccel > 5)
			{
			aerofallosovPhysics[aerofallosovIndex].topAccel = 5
			}
		aerofallosovPhysics[aerofallosovIndex].top += aerofallosovPhysics[aerofallosovIndex].topAccel;

		if(aerofallosovPhysics[aerofallosovIndex].left < -negativeBorder)
			{
			aerofallosovPhysics[aerofallosovIndex].left += getBrowserWidth();
			}
		else if(aerofallosovPhysics[aerofallosovIndex].left > getBrowserWidth() + negativeBorder)
			{
			aerofallosovPhysics[aerofallosovIndex].left -= getBrowserWidth() - negativeBorder;
			}

		if(aerofallosovPhysics[aerofallosovIndex].top < -negativeBorder)
			{
			aerofallosovPhysics[aerofallosovIndex].top += getBrowserHeight();
			}
		else if(aerofallosovPhysics[aerofallosovIndex].top > getBrowserHeight() + negativeBorder)
			{
			aerofallosovPhysics[aerofallosovIndex].top -= getBrowserHeight();
			}

		aerofallosov.style.left = aerofallosovPhysics[aerofallosovIndex].left + "px"
		aerofallosov.style.top = aerofallosovPhysics[aerofallosovIndex].top + "px"
		}
	}

function newBackground()
	{
	getUrl("/random-image.php", randomImageResponse);
	return false;
	}

function randomImageResponse(callbackParams, request, url)
	{
	backgroundImagePath = "/random-images/" + request.responseText;
	//alert(bodyTag + " was set to " + backgroundImagePath);
	bodyTag.style.background = "url('" + backgroundImagePath + "')";
	window.location.hash = request.responseText
	}



function addEvent(sender, eventType, callBackFunction, useCapture)
	{
  	if (sender.addEventListener)
		{
		sender.addEventListener(eventType, callBackFunction, useCapture);
		return true;
		}
	else if (sender.attachEvent)
		{
		var r = sender.attachEvent("on"+eventType, callBackFunction);
		return r;
		}
	}

function getId(byId)
	{
	return document.getElementById(byId);
	}

function getTags(byName)
	{
	return document.getElementsByTagName(byName);
	}

function getSenderByEvent(event)
	{
	var sender = event.target;
	if(!sender) sender = event.srcElement;
	return sender;
	}

function resetBrowserDimensions()
	{
	browserWidth = "";
	browserHeight = "";
	}

var browserWidth = "";
function getBrowserWidth()
	{
	if(browserWidth == "")
		{
		if(window.innerWidth)
			{
			browserWidth = window.innerWidth;
			}
		else if(document.body.clientWidth)
			{
			browserWidth = document.body.clientWidth;
			}
		}
	return browserWidth;
	}

var browserHeight = "";
function getBrowserHeight()
	{
	if(browserHeight == "")
		{
		if(window.innerHeight)
			{
			browserHeight = window.innerHeight;
			}
		else if(document.body.clientHeight)
			{
			browserHeight = document.body.clientHeight;
			}
		}
	return browserHeight;
	}

function ascendToFindNode(sender, nodeName)
	{
	if(sender.parentNode && sender.parentNode.nodeName.toLowerCase() == nodeName)
		{
		return sender.parentNode;
		}
	else if(sender.parentNode)
		{		
		return ascendToFindNode(sender.parentNode, nodeName);
		}
	}


function stopEventPropagation(event)
	{
	event.cancelBubble=true;
	if(event.stopPropagation) event.stopPropagation();
	}


var _xmlHttpRequestInstances = new Array()

function getUrl(url, callback, callbackParams)
	{
	var request = _getXmlHttpRequest()
	request.onreadystatechange = function()
		{
		if(request.readyState == 4)
			{
			request.onreadystatechange = null
			callback(callbackParams, request, url)
			}
		}
	request.open("GET", url)
	request.send(null)
	}

function _getXmlHttpRequest()
	{
	var xmlHttpRequest = null
	var userAgent = navigator.userAgent
	if(window.XMLHttpRequest)
		{
      		xmlHttpRequest = new XMLHttpRequest()
		}
	else if(!/MSIE 4/i.test(userAgent))
		{
		if(/MSIE 5/i.test(userAgent))
			{
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP")
			}
		else
			{
			xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP")
			}
		}
	_xmlHttpRequestInstances.push(xmlHttpRequest)
	return xmlHttpRequest
	}


function hiderevealPanels(sender)
    {
    visible = false;
    if(document.getElementById("box1").style.display != "none")
        {
        visible = true;
        }
    if(visible)
        {
        document.getElementById("box1").style.display = "none";
        document.getElementById("box2").style.display = "none";
        document.getElementById("box3").style.display = "none";
        document.getElementById("box4").style.display = "none";
        sender.innerHTML = "&#x2198;"
        }
    else
        {
        document.getElementById("box1").style.display = "block";
        document.getElementById("box2").style.display = "block";
        document.getElementById("box3").style.display = "block";
        document.getElementById("box4").style.display = "block";
        sender.innerHTML = "&#x2196;"
        }
    return false;
    }

/* So we can unload XmlHttpRequest objects when leaving the page.
 * Helps prevent memory leaks in IE.
 */
function unloadXmlHttpRequestInstances()
	{
	while( _xmlHttpRequestInstances.length > 0 )
		{
		var xmlHttpRequestInstance = _xmlHttpRequestInstances.pop()
		xmlHttpRequestInstance.onReadyStateChange = null
		xmlHttpRequestInstance = null
		}
	}



