var prevContent;
var animInterval;
var alphaInterval;
var menuLeft = 200;
var content, moveTo;
var menuDx;
var alphaDx;
var alphaContent;
var friction;
var currentAlpha;

var bgInterval;
var bgOverInterval;
var bgHiddenTop;
var bgTop;
var bgDx;

function keyHasBeenPressed(keyCode)
{
	GetElement("mariogame").contentWindow.keyboardPressed(keyCode);
}

function keyHasBeenReleased(keyCode)
{
	GetElement("mariogame").contentWindow.keyboardReleased(keyCode);
}

function init()
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		var floor = GetElement("holder").offsetHeight - 15;
		GetElement("mariogame").style.top = floor + "px";
		GetElement("mariogame").style.display = "inline";
		//alert(GetElement("mariogame").style.top);
	}
	else
	{
		//for FireFox because it's too stupid to reload an iFrame if you click reload :/
		GetElement("mariogame").src = "gameteaser.html";
	
		var floor = GetElement("holder").offsetHeight - 250;
		GetElement("mariogame").style.width = GetElement("holder").offsetWidth + "px";
		GetElement("mariogame").style.height = "250px";
		GetElement("mariogame").style.top = floor + "px";
		GetElement("mariogame").style.left = "0px";
		GetElement("mariogame").style.display = "inline";
	}
}

function SetInitialBGImage()
{
	var wp = getCookie("spr_wallpaper");
	/*var wpPos = getCookie("spr_wallpaper_pos2")
	if(wpPos == null || wp == "")
	{	
		wpPos = -1;
		setCookie("spr_wallpaper_pos2", wpPos, 365);
	}*/
	
	if(wp != null && wp != "")
	{
		SwitchBGImage(wp, 0);
	}
	else
	{
		setCookie("spr_wallpaper", 1, 365);
	}
	
	//GetElement("bgselector").style.top = "-" + (GetElement("bgselector").offsetHeight + "px";
	//BGSelectOut();
}

function SwitchBGImage(w, position)
{
	var wallpapers = new Array();
	wallpapers[0] = "images/wallpapers/abstract_0094.jpg";
	wallpapers[1] = "images/wallpapers/abstract_0093.jpg";
	wallpapers[2] = "images/wallpapers/abstract_0058.jpg";
	wallpapers[3] = "images/wallpapers/animal_0001.jpg";
	wallpapers[4] = "images/wallpapers/animal_0026.jpg";
	wallpapers[5] = "images/wallpapers/animal_0058.jpg";
	wallpapers[6] = "images/wallpapers/animal_0120.jpg";
	wallpapers[7] = "images/wallpapers/animal_0123.jpg";
	wallpapers[8] = "images/wallpapers/animal_0125.jpg";
	wallpapers[9] = "images/wallpapers/abstract_0097.jpg";
	wallpapers[10] = "images/wallpapers/abstract_0063.jpg";
	
	wallpapers[11] = "images/wallpapers/nature_0001.jpg";
	wallpapers[12] = "images/wallpapers/nature_0007.jpg";
	wallpapers[13] = "images/wallpapers/nature_0009.jpg";
	wallpapers[14] = "images/wallpapers/nature_0030.jpg";
	wallpapers[15] = "images/wallpapers/nature_0031.jpg";
	wallpapers[16] = "images/wallpapers/nature_0036.jpg";
	wallpapers[17] = "images/wallpapers/nature_0046.jpg";
	wallpapers[18] = "images/wallpapers/nature_0052.jpg";
	wallpapers[19] = "images/wallpapers/nature_0053.jpg";
	wallpapers[20] = "images/wallpapers/xmas_0020.jpg";
	
	
	/*
	var positions = new Array();
	positions[0] = "0% 0%";		//"left top"
	positions[1] = "0% 100%";	//"right top"
	positions[2] = "100% 0%";	//"left bottom"
	positions[3] = "100% 100%";	//"right bottom"
	*/
	
	document.body.style.backgroundImage = 'url("' + wallpapers[w] + '")';
	/*if(pos >= 0)
		document.body.style.backgroundPosition = positions[position];*/
	setCookie("spr_wallpaper", w, 365);
	//setCookie("spr_wallpaper_pos2", position, 365);
}

function SetAlpha(id, alpha)
{
	var img = GetElement(id);
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		img.filters.alpha.opacity = alpha * 100;
		//GetElement("bgselector").filters.alpha.opacity = alpha * 100;
	}
	else
	{
		img.style.opacity = alpha;
		//GetElement("bgselector").style.opacity = alpha;
	}
}

function ImageOver(id)
{
	var img = GetElement(id);
	img.style.width = "25px";
	img.style.height = "20px";
}

function ImageOut(id)
{
	var img = GetElement(id);
	img.style.width = "20px";
	img.style.height = "15px";
}

function BGSelectOut()
{
	clearInterval(bgOverInterval);
	//GetElement("bgselector").style.left = "-10px";
	bgHiddenTop = (-GetElement("bgselector").offsetHeight);//-((18*20) + 5);
	//alert(bgHiddenTop);
	bgTop = -45;
	bgDx = 5;
	friction = .11;
	bgInterval = setInterval("BGAnimOut()", 33);
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		GetElement("bgcon").filters.alpha.opacity = 50;
		bgHiddenTop += 25;
	}
	else
		GetElement("bgcon").style.opacity = .5;
}

function BGAnimOut()
{
	bgDx -= friction;
	if(bgDx < 1)
		bgDx = 1;
	bgTop -= (bgDx*bgDx);
	GetElement("bgselector").style.top = parseInt(bgTop) + "px";
	if(bgTop < bgHiddenTop)
	{
		GetElement("bgselector").style.top = bgHiddenTop + "px";
		clearInterval(bgInterval);
	}
}

function BGSelectOver()
{
	clearInterval(bgInterval);
	bgDx = 4.89;
	friction = .11;
	if(GetElement("bgselector").offsetParent != null)
	{
		bgTop = GetElement("bgselector").offsetTop;//-((18*20) + 5);
		bgOverInterval = setInterval("BGAnimOver()", 33);
	}
	else
		GetElement("bgselector").style.top = "5px";
		
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
		GetElement("bgcon").filters.alpha.opacity = 100;
	else
		GetElement("bgcon").style.opacity = 1;
}

function BGAnimOver()
{
	bgDx -= friction;
	if(bgDx < 1)
		bgDx = 1;
	bgTop += (bgDx*bgDx);
	GetElement("bgselector").style.top = parseInt(bgTop) + "px";
	if(bgTop > -45)
	{
		GetElement("bgselector").style.top = "-45px";
		clearInterval(bgOverInterval);
	}
}

function ReadAddress()
{
	var add = window.location.href.toString();
	var addr = add.split("#")
	if(addr.length > 1)
	{
		if(addr[1] != "home" && addr[1] != "")
			GoContent(addr[1], 1);
		
	}
}

function GetElement(id)
{
	var obj = document.getElementById(id);
	return obj;
}

function MoveToPosition(type)
{
	/*
	var hi = GetElement("hi");
	var tommy = GetElement("tommy");
	var fb = GetElement("fb");
	var im = GetElement("im");
	var gd = GetElement("gd");
	var menu = GetElement("menu");

	if(type == 1)
	{
		hi.style.top = "10px";
		hi.style.left = "165px";
		tommy.style.top = "72px";
		tommy.style.left = "169";
		fb.style.top = "111px";
		fb.style.left = "250px";
		im.style.top = "196px";
		im.style.left = "250px";
		gd.style.top = "221px";
		gd.style.left = "315px";
		//menu.style.top = "261px";
		//menu.style.left = "200px";
	}
	else if(type == 2)
	{
		hi.style.top = "20px";
		hi.style.left = "64px";
		tommy.style.top = "82px";
		tommy.style.left = "69";
		fb.style.top = "1px";
		fb.style.left = "170px";
		im.style.top = "90px";
		im.style.left = "290px";
		gd.style.top = "115px";
		gd.style.left = "355px";
		//menu.style.top = "161px";
		//menu.style.left = "590px";
	}
	*/
}
function GoContent(c, mt)
{
	if(content == c)
		return;
		
	content = c;
	moveTo = mt;
	
	if(menuLeft == 0)
		currentAlpha = 1;
	else
		currentAlpha = 0;
	
	alphaContent = content;
	ChangeElementAlpha(currentAlpha);
	GetElement(content).style.display = "inline";
	
	if(prevContent == null)
		prevContent = c;
	else
	{
		//currentAlpha = 1;
		GetElement(prevContent).style.display = "none";
		prevContent = c;
	}
	menuDx = -4;
	friction = -.11;
	
	AnimateMenu();
}

function ChangeElementAlpha(alpha)
{
	GetElement(alphaContent).style.filter = "alpha(opacity=" + (alpha*100) + ")";
	GetElement(alphaContent).style.opacity = alpha;
}

function Reset()
{
	if(menuLeft == 200)
		return;
	GetElement("closelink").style.display = "none";
	menuDx = 4;
	friction = .11;
	currentAlpha = 1;
	alphaDx = -.4;
	alphaContent = prevContent;
	AnimateFade();
	
	AnimateMenu();
}

function AnimateMenu()
{
	animInterval = setInterval("MoveMenu()", 20);
}

function AnimateFade()
{
	alphaInterval = setInterval("FadeAnimation()", 33);
}

function FadeAnimation()
{
	currentAlpha += alphaDx;
	var a = currentAlpha;//*currentAlpha;
	
	if(a > 1 || a < 0)
	{
		clearInterval(alphaInterval);
		if(a > 1)
		{
			a = 1;
			currentAlpha = 1;
		}
		else if(a < 0)
		{
			a = 0;
			GetElement(prevContent).style.display = "none";
			prevContent = null;
			currentAlpha = 0;
		}
	}
	
	ChangeElementAlpha(a);

}

function MoveMenu()
{
	var menu = GetElement("menu");
	menuDx -= friction;
	var multiplier;
	if(menuDx < 0)
	{	
		if(menuDx > -1)
			menuDx = -1;
		multiplier = -menuDx
	}
	else
	{
		if(menuDx < 1)
			menuDx = 1;
		multiplier = menuDx;
	}
	
	menuLeft += menuDx*multiplier;
	
	if(menuDx < 0)
	{
		/*if(currentAlpha == 0 && menuLeft < 50)
		{
			alphaContent = content;
			alphaDx = .1;
			AnimateFade();
		}*/
		if(menuLeft > 0)
			menu.style.left = parseInt(menuLeft) + "px";
		else if(menuLeft <= 0)
		{
			menuLeft = 0;
			clearInterval(animInterval);
			animInterval = 0;
			menu.style.left = parseInt(menuLeft)+"px";
			
			MoveToPosition(moveTo);
			var cont = GetElement(content);
			cont.style.display = "inline";
			
			GetElement("closelink").style.display = "inline";
			
			alphaContent = content;
			alphaDx = .3;
			AnimateFade();
		}
	}
	else
	{
		if(menuLeft < 200)
			menu.style.left = parseInt(menuLeft) + "px";
		else if(menuLeft >= 200)
		{
			menuLeft = 200;
			clearInterval(animInterval);
			animInterval = 0;
			menu.style.left = parseInt(menuLeft)+"px";
			
			menuDx = 4;
			friction = .11;
			//alphaDx = -.2;
			
			content = null;
			prevContent = null;
		}
	}
}

function setCookie(c_name, value, expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name + "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1)
		{
			c_start = c_start + c_name.length+1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) 
				c_end = document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	
	return "";
}