var getViewSize = function() {
	var offsetX, offsetY;
	if ( typeof(self.pageYOffset) == 'number' ) {
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	} else if ( document.documentElement && document.documentElement.scrollTop ) {
		offsetX = document.documentElement.scrollLeft;
		offsetY = document.documentElement.scrollTop;
	} else if ( document.body ) {
		offsetX = document.body.scrollLeft;
		offsetY = document.body.scrollTop;
	}

	var windowWidth, windowHeight;
	if ( self.innerHeight ) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if ( document.documentElement && document.documentElement.clientHeight ) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if ( document.body ) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	return {x:offsetX, y:offsetY, w:windowWidth, h:windowHeight};
};

var getObjCoords = function(o) {
	var oX = 0;
	var oY = 0;
	if (o.offsetParent) {
		while (1) {
			oX += o.offsetLeft;
			oY += o.offsetTop;
				if (!o.offsetParent) {
					break;
				}
			o=o.offsetParent;
		}
	} else if (o.x) {
		oX += o.x;
		oY += o.y;
	}

	return {x:oX, y:oY};
};
var resizeGame = function(){
	var page = $('gu-game');
	var wrap = $('gu-game-wrapper');
	var content = $('gu-game-space');
	var wrapW = bbcjs.dom.getStyle(wrap, 'width');
	var wrapH = bbcjs.dom.getStyle(wrap, 'height');
	var ratio = 1000 / 552;
	var win = getViewSize();
	if(win.w < 1180 && win.w > 780){
		bbcjs.dom.setStyle(wrap, {width: win.w-200, height: (win.w-200)/ratio});
		bbcjs.dom.setStyle(content, {width: win.w - 200, height: (((win.w-200)/ratio)-40)});
		bbcjs.dom.setStyle(page, {width: win.w-20});
	} else if (win.w > 1180) { 
		bbcjs.dom.setStyle(wrap, {width: 1000, height: 552});
		bbcjs.dom.setStyle(content, {width: 1000, height: 512});
		bbcjs.dom.setStyle(page, {width: 1180});
	} else {
		bbcjs.dom.setStyle(wrap, {width: 600, height: 330});
		bbcjs.dom.setStyle(content, {width: 600, height: 290});
		bbcjs.dom.setStyle(page, {width: 780});
	}
}
var gameInit = function() {
	resizeGame();
	var help = new FlashHelp(GAME_URL, 'helpbox-content', GAME_HELP);
}
bbcjs.addOnLoadItem( gameInit );
bbcjs.dom.addEventListener( window, 'resize', resizeGame );
		
function FlashHelp(swfUrl, outputElement, helpData)
{
	this.id = FlashHelp.generateIdFromUrl(swfUrl);
	if ( typeof(outputElement) == 'string' )
		outputElement = $(outputElement);
	this.outputElement = outputElement;
	this.helpData = helpData || {};
	FlashHelp.instances[this.id] = this;
	return this;
}

FlashHelp.instances = {};

FlashHelp.show = function(swfUrl, helpId)
{
	var id = this.generateIdFromUrl(swfUrl);
	this.instances[id].show(helpId);
}

FlashHelp.generateIdFromUrl = function(url)
{
	if ( url.indexOf('/') > -1 )
		url = url.substring(url.lastIndexOf('/')+1);
	var id = url.split('.').join('_');
	return id;
}

FlashHelp.prototype =
{
	add: function(id, content)
	{
		this.helpData[id] = content;
	},
	
	setDefault: function(id)
	{
		if ( this.helpData[id] )
		{
			this.defaultId = id;
			return true;
		}
		return false;
	},
	
	show: function(id)
	{
		if ( this.helpData[id] && this.outputElement )
		{
			bbcjs.dom.empty(this.outputElement);
			bbcjs.dom.append(this.outputElement, this.helpData[id]);
			return true;
		}
		return false;
	},
	
	showDefault: function()
	{
		return this.show(this.defaultId)
	}
}

if ( !String.prototype.lastIndexOf )
{
	String.prototype.lastIndexOf = function(chr)
	{
		for ( var i=this.length-1; i>=0; i-- )
		{
			if ( this.charAt(i) == chr ) return i;
		}
		return -1;
	};
}
