var balloons = ["balloon1", "balloon2", "balloon3", "balloon4", "balloon5", "balloon6"];
var balloonColours = ["yellow-balloon", "orange-balloon", "red-balloon", "pink-balloon", "blue-balloon", "green-balloon"];
var velocity = [1,1,1,1,1,1];
var counter = [0,0,0,0,0,0];
var startPos = [0,0,0,0,0,0];
var multiplier = [0,0,0,0,0,0];
var loopTimer = 0, changeLoopTimer = 0, multiplierTimer = 0;
var hPos = 0, vPos = 0, vel = 0;
var posx = 0, posy = 0;

/*	SETUP VARIABLES FOR IMAGE ENHANCEMENTS */

var smileys = ["tl-smileys1","tl-smileys2","tr-smileys1","tr-smileys2","br-smileys1","br-smileys2","bl-smileys1","bl-smileys2"];
var widths = [100,100,100,100,65,65,84,84];
var heights = [59,59,102,102,120,120,74,74];
var vertPos = ["top","top","top","top","bottom","bottom","bottom","bottom"];
var horizPos = ["left","left","right","right","right","right","left","left"];

function pngFix()
{
	/*	IE VERSION CHECK */

	if (navigator.appVersion.search("MSIE") != null)
	{
		var arrVersion = navigator.appVersion.split("MSIE");
		var version = parseFloat(arrVersion[1]);
	
		if ((version >= 5.5) && (document.body.filters)) 
		{
		   for(var i=0; i<document.images.length; i++)
		   {
			  var img = document.images[i];
			  var imgName = img.src.toUpperCase();
			  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			  {
				 var imgID = (img.id) ? "id='" + img.id + "' " : "";
				 var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				 var imgStyle = "display:inline-block; z-index: 100;" + img.style.cssText;
				 if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				 if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
				 var strNewHTML = "<span " + imgID + imgClass + imgTitle
				 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
				 img.outerHTML = strNewHTML;
				 i = i-1;
			  }
		   }
		}
	}
	else
	{
		alert('Not IE thank god!!');
	}
}


function reset(val)
{
	var obj = document.getElementById(balloons[val]);
	obj.style.left = (Math.round(Math.random()*-350)) + "px";
	startPos[val] = (Math.round(Math.random()*200))+15;
	obj.style.top = startPos[val] + "px";
	velocity[val] = Math.round(Math.random()*3)+1;
	changeMultiplier(val);
}

function move(val, vel)
{
	var obj = document.getElementById(balloons[val]);
	hPos = obj.style.left;
	hPos = (hPos.substring(0,hPos.length - 2))*1;

	vPos = obj.style.top;
	vPos = (vPos.substring(0,vPos.length - 2))*1;

	if (hPos < document.body.offsetWidth)
	{
		hPos += velocity[val];
		obj.style.left = hPos + "px";

		vPos = startPos[val] + Math.sin(counter[val])*multiplier[val];
		obj.style.top = vPos + "px";

		counter[val] += 0.1;
	}
	else
	{
		reset(val);
	}
}

function changeMultiplier(val)
{
	multiplier[val] =  Math.round(Math.random()*20)+1;
	multiplierTimer = setTimeout('changeMultiplier()',1000)
}

function loop()
{
	clearTimeout(loopTimer);
	for (var count = 0; count < balloons.length; count++)
	{
		move(count,velocity[count]);
	}
	loopTimer = setTimeout('loop()', 20);
}

function subNavShow(val)
{
	var obj = document.getElementById("subNavBox");
	if (val == 1)
	{
		obj.style.display = "block";
	}
	else
	{
		obj.style.display = "none";
	}
}

window.onload = function()
{
	if (document.getElementById("navigation"))
	{
		var obj = document.getElementById("navigation");
		var primaryLinks = obj.getElementsByTagName("a");

		for (count=0; count < primaryLinks.length; count++)
		{
			if (primaryLinks[count].className != "")
			{
				if (count == 2)
				{
					primaryLinks[count].onmouseover = function() {subNavShow(1);};
				}
				else
				{
					primaryLinks[count].onmouseover = function() {subNavShow(0);};
				}
			}
		}
	}

	var obj = document.getElementById("balloons");
	var output = ""
	for (var count = 0; count < balloons.length; count++)
	{
		output += "<img src=\"/images/layout/" + balloonColours[count] + ".gif\" width=\"150\" height=\"230\" id=\"" + balloons[count] + "\" style=\"position: absolute; top: 50px; left: 0px; margin: 0; padding: 0;\" />";
	}
	obj.innerHTML = output;

	for (var count = 0; count < balloons.length; count++)
	{
		var obj = document.getElementById(balloons[count]);
		obj.style.left = (Math.round(Math.random()*-350)) + "px";
		startPos[count] = (Math.round(Math.random()*200))+15;
		obj.style.top = startPos[count] + "px";
		velocity[count] =  Math.round(Math.random()*3)+1;
		multiplier[count] =  Math.round(Math.random()*9)+1;
	}
	loop();
	createFrames();
	pngFix();
}

function balloonContainer()
{
	document.write('<div id="balloons"><div id="offset">&nbsp;</div></div>');
}

function getElementsByClassName(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function createFrames()
{
	var frames = getElementsByClassName(document, "div", "pictureBox");
	
	for (var count=0; count<frames.length; count++)
	{
		var picture = frames[count].getElementsByTagName("img")[0];
		var html = frames[count].innerHTML;
		var picWidth = picture.width;
		var picHeight = picture.height;

		var num = Math.round(Math.random()*7);		//Generate random number from 0-7
		var smiley = smileys[num];					//Choose which animal to display
		var width = widths[num];					//Get image width
		var height = heights[num];					//Get image height
		var imageVPos = vertPos[num];				//Set position to top or bottom
		var imageHPos = horizPos[num];				//Set position to left or right

		frames[count].innerHTML = html + '<div class=\"frameTL\" style=\"width: '+picWidth+'px; height: '+picHeight+'px;\"><div class=\"frameBR\" style=\"width: '+picWidth+'px; height: '+picHeight+'px;\"><div class=\"frameTR\" style=\"width: '+picWidth+'px; height: '+picHeight+'px;\"><div class=\"frameBL\" style=\"width: '+picWidth+'px; height: '+picHeight+'px;\">&nbsp;</div></div></div></div><img src="/images/layout/' + smiley + '.png\" width=\"' + width + '\" height=\"' + height + '\" alt=\"smiley\" class=\"animal\" style=\"position: absolute; ' + imageVPos + ': 0px; ' + imageHPos + ': 0px;\" />';
	}
}


function createCookie(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=" + expires + "; path=/";
}

/*	CHANGE FONT SIZE AND SET A COOKIE TO STORE THE SETTING	*/
function fontSizer(size)
{
	createCookie("actionKidsFontSize", size, 365);
	window.location.reload();
}

/*	CHANGE FONT FAMILY AND SET A COOKIE TO STORE THE SETTING	*/
function fontStyler(family)
{
	createCookie("actionKidsFontFamily", family, 365);
	window.location.reload();
}

/*	MAKE IMAGE FULL SIZE FOR GALLERY	*/
function mouse_pos(e)
{
	posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	posy = document.body.scrollTop + document.documentElement.scrollTop + 20;
}


function fullSize(imageLink)
{
	var fullImage = document.getElementById("maskImage");
	fullImage.src = imageLink.href;
	document.getElementById("galleryMask").style.top = posy+"px";
	document.getElementById("galleryMask").style.display = "block";
}

function closeImage()
{
	document.getElementById("galleryMask").style.display = "none";
	var fullImage = document.getElementById("maskImage");
	fullImage.src = "/";
}