var ACT = 
{
	getClientSize : function()
	{
		var dimensions = {width: 0, height: 0};
	     if (document.documentElement) {
	         dimensions.width = document.documentElement.offsetWidth;
	         dimensions.height = document.documentElement.offsetHeight;
	     } else if (window.innerWidth && window.innerHeight) {
	         dimensions.width = window.innerWidth;
	         dimensions.height = window.innerHeight;
	     }
	     return dimensions;
	},
	
	getToken : function()
	{
		return parseInt( (new Date()).getTime()/10000 ) + '';
	},
	
	setUTF8 : function()
	{
		var charset = (typeof document.charset == "undefined" || document.charset == null) ? document.characterSet : document.charset;
		if( charset != "UTF-8" && charset != "utf-8" ) 
		{
			try 
			{
				if(typeof document.charset == "undefined" ) 
					document.characterSet = "UTF-8"; //FF, hopefully someday FF allows it
				else
					document.charset = "UTF-8"; //IE
			} catch (e) {}
			window.location.reload();
		}
	},
	
	resizeIframe : function()
	{
		$("#frameContent").height(clientSize.height - 50);
	},
	
	resizeContentHeight : function()
	{
		$("#mainContent").height(ACT.getClientSize().height - 25);
	}
};
GAME = 
{
	listItemClick : function(item, callback)
	{
		GAME.loadGData($(item).attr("href"), "#mainContent", callback);
		GAME.listItemSelect($(item).parent());
	},
	
	listItemSelect : function(liItem)
	{
		GAME.listItemDeselectAll();
		$(liItem).addClass("selected");
	},
	
	listItemDeselectAll : function()
	{
		$("ul.game-list li.selected").removeClass("selected");
	},
	
	loadGData : function(url, container, callback)
	{
		$(container).fadeOut('fast');
		$.get(
			url,
			function(data)
			{
				$(container).html(GAME.getContent(data)).fadeIn('fast');
				
				if(callback)
				{
					callback();
				}
			}
		);
	},
	
	TOKEN_START : '<div class="main"><div class="entry-content">',
	getContent : function(data)
	{
		var idxStart = data.indexOf(GAME.TOKEN_START) + GAME.TOKEN_START.length;
		var idxEnd = data.indexOf('</div></div></body></html>');
		return data.substring(idxStart, idxEnd);
	}
};

$(document).ready(
	function()
	{
		ACT.setUTF8();
		$("iframe#navbar-iframe, script:eq(0), div:eq(0)").remove();
		$("body").show();
		
		GAME.loadGData(
			"2008/11/menu.html?t=" + ACT.getToken(), 
			"#sideBar",
			function()
			{
				$("#sideBar a").click(
					function()
					{
						if ($(this).is(":first-child")) 
						{
							GAME.listItemClick(
								this,
								function()
								{
									ACT.resizeContentHeight();
									$("#mainContent li:not(.soon) a").click(
										function()
										{
											var link = $(this).attr("href");
											GAME.listItemClick($("#sideBar a[href=" + link +"]"));
											return false;
										}
									);
								}
							);
						}
						else
						{
							GAME.listItemClick(this);
						}
						
						return false;
					}
				).eq(0).click();
			}
		);

		$(window).resize(function(){
			ACT.resizeContentHeight();
		});
	}
);