// Mech City (c) 2008 by Christer Kaitila of www.orangeviewgames.com
// An action game designed for use on the Nintendo Wii

var frame_delay = 1; //25; // 25=40fps
var fps = 0;
var fps_seconds = 0;
var fps_framecount = 0;
var fps_last_count = 0;
var fps_div = null;
function fps_tick()
{
	fps_seconds++;
	fps = Math.round(fps_framecount / fps_seconds); 
	// fps = fps_framecount - fps_last_count; fps_last_count = fps_framecount;
	if (!fps_div) fps_div = document.getElementById('fps');
	if (fps_div) fps_div.innerHTML = resolutionx+'x'+resolutiony+' '+fps+' fps';
	// window resize only detected when we ask
	resolutionx = getClientWidth();
	resolutiony = getClientHeight();
}

var wiibg = null;
var viewport = null;
var wiibgcount = 0;
function scrollwiibg()
{
	var wobbley = Math.round(800*(Math.sin(wiibgcount/100)/Math.PI))+800;
	var wobblex;// = Math.round(800*(Math.sin(wiibgcount/200)/Math.PI))+800;
	//var 
	wobblex = wiibgcount; 

	wiibgcount++;

	// Nintendo Wii only redraws screen every once in a while unless we change something
	if (fps_div) fps_div.innerHTML = resolutionx+'x'+resolutiony+' '+fps+' fps ('+wiibgcount+')';
	
	/*
	// move a sprite
	if (!wiibg) wiibg = document.getElementById('wiibg');
	if (wiibg)
	{
		wiibg.style.top = '-'+wobbley+'px';
		wiibg.style.left = '-'+wobblex+'px';
	}
	*/

	// scroll background tile WORKS 
	// 19FPS on Wii
	
	/*
	if (!viewport) viewport = document.getElementById('viewport');
	if (viewport)
	{
		viewport.style.backgroundPosition = '-'+wobblex+'px -'+wobbley+'px';
	}
	*/

	fps_framecount++;

	setTimeout(scrollwiibg,frame_delay);
}

// init flash
function startmusic()
{
	document.getElementById('tunes').innerHTML = 
	"<object class='flash' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'" +
	" codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'" +
	" width='64' height='24' id='CoffeeCup' align='middle'>" +
	"<param name='movie' value='tunes.swf'/><param name='quality' value='high' />" +
	"<param name='scale' value='noscale' /><param name='salign' value='lt' />" +
	"<param name='bgcolor' value='#A79FAE' />" +
	"<embed class='flash' src='tunes.swf' quality='high' bgcolor='#A79FAE' scale='noscale' salign='lt'" +
	" type='application/x-shockwave-flash' width='64' height='24' name='CoffeeCup' align='middle'" +
	" pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>";
}

function getClientWidth()
{ // browser resolution
	if( typeof( window.innerWidth ) == 'number' ) {
	// most browsers
	myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	}
	if (myWidth>0) return myWidth;
	//if (game.logger) game.logger('ERROR - Unable to determine the width of the window.');
	return 800; // failure
}

function getClientHeight()
{ // browser resolution
	if( typeof( window.innerWidth ) == 'number' ) {
	// most browsers
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientHeight ) ) {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
	}
	if (myHeight>0) return myHeight;
	//if (game.logger) game.logger('ERROR - Unable to determine the height of the window.');
	return 480; // failure
}

//this part monitors the browsing Wii Remote, and stores it button states
var mainButtonState = 
{ 
	pressedA: false, 
	pressedMinus: false, 
	pressedB: false, 
	pressed1: false, 
	pressed2: false, 
	pressedPlus: false, 
	pressedUp: false, 
	pressedDown: false, 
	pressedRight: false, 
	pressedLeft: false, 
	pressedZ: false, 
	pressedC: false 
};

function storeButtonState(e) {
	//store the new state when a button state changes - Z and C buttons cannot be detected
	var newState = ( e.type == 'mousedown' || e.type == 'keydown' ) ? true : false;
	if( e.type == 'mousedown' || e.type == 'mouseup' || e.keyCode == 13 ) {
		mainButtonState.pressedA = newState;
	} else if( e.keyCode == 170 ) {
		mainButtonState.pressedMinus = newState;
	} else if( e.keyCode == 171 ) {
		mainButtonState.pressedB = newState;
	} else if( e.keyCode == 172 ) {
		mainButtonState.pressed1 = newState;
	} else if( e.keyCode == 173 ) {
		mainButtonState.pressed2 = newState;
	} else if( e.keyCode == 174 ) {
		mainButtonState.pressedPlus = newState;
	} else if( e.keyCode == 175 ) {
		mainButtonState.pressedUp = newState;
	} else if( e.keyCode == 176 ) {
		mainButtonState.pressedDown = newState;
	} else if( e.keyCode == 177 ) {
		mainButtonState.pressedRight = newState;
	} else if( e.keyCode == 178 ) {
		mainButtonState.pressedLeft = newState;
	}
}

//this part displays the states of the wii controller
function updatedisplay() 
{
	
	for( var i = 0, remote, s = '', buttons = {}; i < 4; i++ ) 
	{
		remote = opera.wiiremote.update(i);
		if( !remote.isEnabled ) 
		{
			//s += '<p>Wii remote '+i+' is not enabled.<\/p>';
		} 
		else 
		{
			s += '<div>Resolution: '+resolutionx+'x'+resolutionx+'</div>';
			s += '<div id="list'+i+'">Wii Remote ('+i+')';

			var rolly=remote.dpdRollY;
			var rollx=remote.dpdRollX;
			//get the roll angle in radians
			var rolldeg = Math.atan2(rolly,rollx);
			//convert the roll to degrees
			rolldeg = rolldeg * ( 180 / Math.PI );
			s += '<p>Roll X: '+Math.round(rollx)+' Y:'+Math.round(rolly)+' Degrees:'+Math.round(rolldeg)+'</p>';
			
			if( remote.isBrowsing ) 
			{
				//get a copy of the current status of monitored buttons
				//do not use the mainButtonState variable directly, in case it changes
				for( var oneprop in mainButtonState ) 
				{
					buttons[oneprop] = mainButtonState[oneprop];
				}
			} 
			else 
			{
				//use the hold property
				buttons.pressedLeft = remote.hold & 1;
				buttons.pressedRight = remote.hold & 2;
				buttons.pressedDown = remote.hold & 4;
				buttons.pressedUp = remote.hold & 8;
				buttons.pressedPlus = remote.hold & 16;
				buttons.pressed2 = remote.hold & 256;
				buttons.pressed1 = remote.hold & 512;
				buttons.pressedB = remote.hold & 1024;
				buttons.pressedA = remote.hold & 2048;
				buttons.pressedMinus = remote.hold & 4096;
				buttons.pressedZ = remote.hold & 8192;
				buttons.pressedC = remote.hold & 16384;
			}
			for( var k in buttons ) 
			{
				s += ''+k.replace(/^pressed/,'')+'='+(buttons[k]?'1':'0')+'<br>';
			}
		}
		s += '<\/div>';
	}
	wiimotediv.innerHTML = s;
}

function startwii()
{
	wiimotediv = document.getElementById('wiimote');
	wiimotediv.innerHTML = 'Detecting Wii...';
	if (!window.opera)
	{
		wiimotediv.innerHTML = ''; //'This game is designed for the Nintendo Wii.';
		return;
	}
	if(!opera.wiiremote ) 
	{
		wiimotediv.innerHTML = ''; //'This game is designed for the Nintendo Wii.';
		return;
	}
	wiimotediv.innerHTML = 'Wii detected!';

	if( window.opera && opera.wiiremote ) 
	{
	//remember that the A button can be either a left mouse button or the enter key,
	//depending on if the remote is pointing at the screen or not
	document.addEventListener('mousedown',storeButtonState,false);
	document.addEventListener('mouseup',storeButtonState,false);
	document.addEventListener('keydown',storeButtonState,false);
	document.addEventListener('keyup',storeButtonState,false);
	document.addEventListener('keypress',
		function (e) 
		{
			//allow scrolling key actions, prevent other actions
			if( e.keyCode > 174 || e.keyCode == 171 ) { return; }
			//do not do this unless you know you want to prevent the normal action of that key:
			e.preventDefault();
		}
		,false);
	}

	updatedisplay();
	setInterval(updatedisplay,150);
}


var wiimotediv = null;
var resolutionx = 0;
var resolutiony = 0;
function initgame()
{
	resolutionx = getClientWidth();
	resolutiony = getClientHeight();
	//setInterval(scrollwiibg,1);
	setTimeout(scrollwiibg,frame_delay);
	startmusic();
	//startwii();
	setInterval(fps_tick,1000);
}
