
//Change this to true for a stretchy canvas!
//
var RESIZEABLE_CANVAS=false;

//Start us up!
//
window.onload=function( e ){

	if( RESIZEABLE_CANVAS ){
		window.onresize=function( e ){
			var canvas=document.getElementById( "GameCanvas" );

			//This vs window.innerWidth, which apparently doesn't account for scrollbar?
			var width=document.body.clientWidth;
			
			//This vs document.body.clientHeight, which does weird things - document seems to 'grow'...perhaps canvas resize pushing page down?
			var height=window.innerHeight;			

			canvas.width=width;
			canvas.height=height;
		}
		window.onresize( null );
	}
	
	game_canvas=document.getElementById( "GameCanvas" );
	game_console=document.getElementById( "GameConsole" );
	
	
	try{
		bbInit();
		bbMain();
	}catch( ex ){
		if( ex ) alert( ex );
		return;
	}
	
	if( game_runner!=null ){
		game_runner();
	}
}

//Globals
var game_canvas;
var game_console;
var game_runner;

//${METADATA_BEGIN}
var META_DATA="[background.png];type=image/png;width=939;height=358;\n[backwater.png];type=image/png;width=332;height=356;\n[f1.png];type=image/png;width=556;height=35;\n[f2.png];type=image/png;width=516;height=36;\n[f3.png];type=image/png;width=335;height=36;\n[f4.png];type=image/png;width=338;height=21;\n[f5.png];type=image/png;width=383;height=34;\n[f6.png];type=image/png;width=657;height=36;\n[f7.png];type=image/png;width=470;height=35;\n[f8.png];type=image/png;width=679;height=36;\n[logo.png];type=image/png;width=569;height=197;\n[mojo_font.png];type=image/png;width=864;height=13;\n";

//${METADATA_END}
function getMetaData( path,key ){	
	var i=META_DATA.indexOf( "["+path+"]" );
	if( i==-1 ) return "";
	i+=path.length+2;

	var e=META_DATA.indexOf( "\n",i );
	if( e==-1 ) e=META_DATA.length;

	i=META_DATA.indexOf( ";"+key+"=",i )
	if( i==-1 || i>=e ) return "";
	i+=key.length+2;

	e=META_DATA.indexOf( ";",i );
	if( e==-1 ) return "";

	return META_DATA.slice( i,e );
}

function loadString( path ){
	if( path=="" ) return "";
//${TEXTFILES_BEGIN}
		return "";

//${TEXTFILES_END}
}

function loadImage( path,onloadfun ){
	var ty=getMetaData( path,"type" );
	if( ty.indexOf( "image/" )!=0 ) return null;

	var image=new Image();
	
	image.meta_width=parseInt( getMetaData( path,"width" ) );
	image.meta_height=parseInt( getMetaData( path,"height" ) );
	image.onload=onloadfun;
	image.src="/backwaterteaser/data/"+path;
	
	return image;
}

function loadAudio( path ){
	var audio=new Audio( "/backwaterteaser/data/"+path );
	return audio;
}

//${TRANSCODE_BEGIN}

// Javascript Monkey runtime.
//
// Placed into the public domain 24/02/2011.
// No warranty implied; use at your own risk.

//***** JavaScript Runtime *****

var obj_id=0;
var err_info="";
var err_stack=[];

var D2R=0.017453292519943295;
var R2D=57.29577951308232;

function push_err(){
	err_stack.push( err_info );
}

function pop_err(){
	err_info=err_stack.pop();
}

function stackTrace(){
	var str="";
	push_err();
	err_stack.reverse();
	for( var i=0;i<err_stack.length;++i ){
		str+=err_stack[i]+"\n";
	}
	err_stack.reverse();
	pop_err();
	return str;
}

function print( str ){
	if( game_console ){
		game_console.value+=str+"\n";
	}
	if( window.console!=undefined ){
		window.console.log( str );
	}
}

function error( err ){
	throw err;
}

function dbg_object( obj ){
	if( obj ) return obj;
	error( "Null object access" );
}

function dbg_array( arr,index ){
	if( index>=0 && index<arr.length ) return arr;
	error( "Array index out of range" );
}

function new_bool_array( len ){
	var arr=Array( len );
	for( var i=0;i<len;++i ) arr[i]=false;
	return arr;
}

function new_number_array( len ){
	var arr=Array( len );
	for( var i=0;i<len;++i ) arr[i]=0;
	return arr;
}

function new_string_array( len ){
	var arr=Array( len );
	for( var i=0;i<len;++i ) arr[i]='';
	return arr;
}

function new_array_array( len ){
	var arr=Array( len );
	for( var i=0;i<len;++i ) arr[i]=[];
	return arr;
}

function new_object_array( len ){
	var arr=Array( len );
	for( var i=0;i<len;++i ) arr[i]=null;
	return arr;
}

function resize_bool_array( arr,len ){
	var i=arr.length;
	arr=arr.slice(0,len);
	if( len<=i ) return arr;
	arr.length=len;
	while( i<len ) arr[i++]=false;
	return arr;
}

function resize_number_array( arr,len ){
	var i=arr.length;
	arr=arr.slice(0,len);
	if( len<=i ) return arr;
	arr.length=len;
	while( i<len ) arr[i++]=0;
	return arr;
}

function resize_string_array( arr,len ){
	var i=arr.length;
	arr=arr.slice(0,len);
	if( len<=i ) return arr;
	arr.length=len;
	while( i<len ) arr[i++]="";
	return arr;
}

function resize_array_array( arr,len ){
	var i=arr.length;
	arr=arr.slice(0,len);
	if( len<=i ) return arr;
	arr.length=len;
	while( i<len ) arr[i++]=[];
	return arr;
}

function resize_object_array( arr,len ){
	var i=arr.length;
	arr=arr.slice(0,len);
	if( len<=i ) return arr;
	arr.length=len;
	while( i<len ) arr[i++]=null;
	return arr;
}

function string_compare( lhs,rhs ){
	var n=Math.min( lhs.length,rhs.length ),i,t;
	for( i=0;i<n;++i ){
		t=lhs.charCodeAt(i)-rhs.charCodeAt(i);
		if( t ) return t;
	}
	return lhs.length-rhs.length;
}

function string_replace( str,find,rep ){	//no unregex replace all?!?
	var i=0;
	for(;;){
		i=str.indexOf( find,i );
		if( i==-1 ) return str;
		str=str.substring( 0,i )+rep+str.substring( i+find.length );
		i+=rep.length;
	}
}

function string_trim( str ){
	var i=0,i2=str.length;
	while( i<i2 && str.charCodeAt(i)<=32 ) i+=1;
	while( i2>i && str.charCodeAt(i2-1)<=32 ) i2-=1;
	return str.slice( i,i2 );
}

function string_starts_with( str,substr ){
	return substr.length<=str.length && str.slice(0,substr.length)==substr;
}

function string_ends_with( str,substr ){
	return substr.length<=str.length && str.slice(str.length-substr.length,str.length)==substr;
}

function object_downcast( obj,clas ){
	if( obj instanceof clas ) return obj;
	return null;
}

function object_implements( obj,iface ){
	if( obj && obj.implments && obj.implments[iface] ) return obj;
	return null;
}

function extend_class( clas ){
	var tmp=function(){};
	tmp.prototype=clas.prototype;
	return new tmp;
}

// HTML5 mojo runtime.
//
// Copyright 2011 Mark Sibly, all rights reserved.
// No warranty implied; use at your own risk.

var dead=false;

var KEY_LMB=1;
var KEY_RMB=2;
var KEY_MMB=3;
var KEY_TOUCH0=0x180;

function eatEvent( e ){
	if( e.stopPropagation ){
		e.stopPropagation();
		e.preventDefault();
	}else{
		e.cancelBubble=true;
		e.returnValue=false;
	}
}

function keyToChar( key ){
	switch( key ){
	case 8:
	case 9:
	case 13:
	case 27:
	case 32:
		return key;
	case 33:
	case 34:
	case 35:
	case 36:
	case 37:
	case 38:
	case 39:
	case 40:
	case 45:
		return key | 0x10000;
	case 46:
		return 127;
	}
	return 0;
}

//***** gxtkApp class *****

function gxtkApp(){

	this.graphics=new gxtkGraphics( this,game_canvas );
	this.input=new gxtkInput( this );
	this.audio=new gxtkAudio( this );

	this.loading=0;
	this.maxloading=0;

	this.updateRate=0;
	
	this.startMillis=(new Date).getTime();
	
	this.suspended=false;
	
	var app=this;
	var canvas=game_canvas;
	
	function gxtkMain(){
		canvas.onkeydown=function( e ){
			app.input.OnKeyDown( e.keyCode );
			var chr=keyToChar( e.keyCode );
			if( chr ) app.input.PutChar( chr );
			if( e.keyCode<48 || (e.keyCode>111 && e.keyCode<124) ) eatEvent( e );
		}

		canvas.onkeyup=function( e ){
			app.input.OnKeyUp( e.keyCode );
		}

		canvas.onkeypress=function( e ){
			if( e.charCode ){
				app.input.PutChar( e.charCode );
			}else if( e.which ){
				app.input.PutChar( e.which );
			}
		}

		canvas.onmousedown=function( e ){
			app.input.OnKeyDown( KEY_LMB );
			eatEvent( e );
		}
		
		canvas.onmouseup=function( e ){
			app.input.OnKeyUp( KEY_LMB );
			eatEvent( e );
		}
		
		canvas.onmouseout=function( e ){
			app.input.OnKeyUp( KEY_LMB );
			eatEvent( e );
		}

		canvas.onmousemove=function( e ){
			var x=e.clientX+document.body.scrollLeft;
			var y=e.clientY+document.body.scrollTop;
			var c=canvas;
			while( c ){
				x-=c.offsetLeft;
				y-=c.offsetTop;
				c=c.offsetParent;
			}
			app.input.OnMouseMove( x,y );
			eatEvent( e );
		}

		canvas.onfocus=function( e ){
			//app.InvokeOnResume();
		}
		
		canvas.onblur=function( e ){
			//app.InvokeOnSuspend();
		}

		canvas.focus();

		app.InvokeOnCreate();
		app.InvokeOnRender();
	}
	
	game_runner=gxtkMain;
}

var timerSeq=0;

gxtkApp.prototype.SetFrameRate=function( fps ){

	var seq=++timerSeq;
	
	if( !fps ) return;
	
	var app=this;
	var updatePeriod=1000.0/fps;
	var nextUpdate=(new Date).getTime()+updatePeriod;
	
	function timeElapsed(){
		if( seq!=timerSeq ) return;

		var time;		
		var updates=0;

		for(;;){
			nextUpdate+=updatePeriod;

			app.InvokeOnUpdate();
			if( seq!=timerSeq ) return;
			
			if( nextUpdate>(new Date).getTime() ) break;
			
			if( ++updates==7 ){
				nextUpdate=(new Date).getTime();
				break;
			}
		}
		app.InvokeOnRender();
		if( seq!=timerSeq ) return;
			
		var delay=nextUpdate-(new Date).getTime();
		setTimeout( timeElapsed,delay>0 ? delay : 0 );
	}
	
	setTimeout( timeElapsed,updatePeriod );
}

gxtkApp.prototype.IncLoading=function(){
	++this.loading;
	if( this.loading>this.maxloading ) this.maxloading=this.loading;
	if( this.loading==1 ) this.SetFrameRate( 0 );
}

gxtkApp.prototype.DecLoading=function(){
	--this.loading;
	if( this.loading!=0 ) return;
	this.maxloading=0;
	this.SetFrameRate( this.updateRate );
}

gxtkApp.prototype.GetMetaData=function( path,key ){
	return getMetaData( path,key );
}

gxtkApp.prototype.Die=function( ex ){
	dead=true;
	this.audio.OnSuspend();
	if( ex ) alert( ex+"\n"+stackTrace() );
	throw ex;
}

gxtkApp.prototype.InvokeOnCreate=function(){
	if( dead ) return;
	
	try{
		this.OnCreate();
	}catch( ex ){
		this.Die( ex );
	}
}

gxtkApp.prototype.InvokeOnUpdate=function(){
	if( dead || this.suspended || !this.updateRate || this.loading ) return;
	
	try{
		this.input.BeginUpdate();
		this.OnUpdate();		
		this.input.EndUpdate();
	}catch( ex ){
		this.Die( ex );
	}
}

gxtkApp.prototype.InvokeOnSuspend=function(){
	if( dead || this.suspended ) return;
	
	try{
		this.suspended=true;
		this.OnSuspend();
		this.audio.OnSuspend();
	}catch( ex ){
		this.Die( ex );
	}
}

gxtkApp.prototype.InvokeOnResume=function(){
	if( dead || !this.suspended ) return;
	
	try{
		this.audio.OnResume();
		this.OnResume();
		this.suspended=false;
	}catch( ex ){
		this.Die( ex );
	}
}

gxtkApp.prototype.InvokeOnRender=function(){
	if( dead || this.suspended ) return;
	
	try{
		this.graphics.BeginRender();
		if( this.loading ){
			this.OnLoading();
		}else{
			this.OnRender();
		}
		this.graphics.EndRender();
	}catch( ex ){
		this.Die( ex );
	}
}

//***** GXTK API *****

gxtkApp.prototype.GraphicsDevice=function(){
	return this.graphics;
}

gxtkApp.prototype.InputDevice=function(){
	return this.input;
}

gxtkApp.prototype.AudioDevice=function(){
	return this.audio;
}

gxtkApp.prototype.AppTitle=function(){
	return document.URL;
}

gxtkApp.prototype.LoadState=function(){
	//use cookies for file:// URLS in FF and IE...
	if( document.URL.toLowerCase().substr(0,7)=="file://" &&
			(navigator.userAgent.indexOf( "Firefox" )!=-1 || navigator.userAgent.indexOf( "MSIE" )!=-1) ){
		var bits=document.cookie.split( ";" )
		if( bits.length!=1 ) return "";
		bits=bits[0].split( "=" );
		if( bits.length!=2 || bits[0]!=".mojostate" ) return "";
		return unescape( bits[1] );
	}else{
		var state=localStorage.getItem( ".mojostate@"+document.URL );
		if( state ) return state;
	}
	return "";
}

gxtkApp.prototype.SaveState=function( state ){
	//use cookies for file:// URLS in FF and IE...
	if( document.URL.toLowerCase().substr(0,7)=="file://" &&
			(navigator.userAgent.indexOf( "Firefox" )!=-1 || navigator.userAgent.indexOf( "MSIE" )!=-1) ){
		var exdate=new Date();
		exdate.setDate( exdate.getDate()+3650 );
		document.cookie=".mojostate="+escape( state )+"; expires="+exdate.toUTCString()
	}else{
		localStorage.setItem( ".mojostate@"+document.URL,state );
	}
}

gxtkApp.prototype.LoadString=function( path ){
	return loadString( path );
}

gxtkApp.prototype.SetUpdateRate=function( fps ){
	this.updateRate=fps;
	
	if( !this.loading ) this.SetFrameRate( fps );
}

gxtkApp.prototype.MilliSecs=function(){
	return ((new Date).getTime()-this.startMillis)|0;
}

gxtkApp.prototype.Loading=function(){
	return this.loading;
}

gxtkApp.prototype.OnCreate=function(){
}

gxtkApp.prototype.OnUpdate=function(){
}

gxtkApp.prototype.OnSuspend=function(){
}

gxtkApp.prototype.OnResume=function(){
}

gxtkApp.prototype.OnRender=function(){
}

gxtkApp.prototype.OnLoading=function(){
}

//***** gxtkGraphics class *****

function gxtkGraphics( app,canvas ){
	this.app=app;
	this.canvas=canvas;
	this.gc=canvas.getContext( '2d' );
	this.tmpCanvas=null;
	this.r=255;
	this.b=255;
	this.g=255;
	this.white=true;
	this.color="rgb(255,255,255)"
	this.alpha=1.0;
	this.blend="source-over";
	this.ix=1;this.iy=0;
	this.jx=0;this.jy=1;
	this.tx=0;this.ty=0;
	this.tformed=false;
	this.scissorX=0;
	this.scissorY=0;
	this.scissorWidth=0;
	this.scissorHeight=0;
	this.clipped=false;
}

gxtkGraphics.prototype.BeginRender=function(){
	this.gc.save();
}

gxtkGraphics.prototype.EndRender=function(){
	this.gc.restore();
}

gxtkGraphics.prototype.Width=function(){
	return this.canvas.width;
}

gxtkGraphics.prototype.Height=function(){
	return this.canvas.height;
}

gxtkGraphics.prototype.LoadSurface=function( path ){
	
	var app=this.app;
	
	function onloadfun(){
		app.DecLoading();
	}

	app.IncLoading();

	var image=loadImage( path,onloadfun );
	if( image ) return new gxtkSurface( image,this );

	app.DecLoading();
	return null;
}

gxtkGraphics.prototype.SetAlpha=function( alpha ){
	this.alpha=alpha;
	this.gc.globalAlpha=alpha;
}

gxtkGraphics.prototype.SetColor=function( r,g,b ){
	this.r=r;
	this.g=g;
	this.b=b;
	this.white=(r==255 && g==255 && b==255);
	this.color="rgb("+(r|0)+","+(g|0)+","+(b|0)+")";
	this.gc.fillStyle=this.color;
	this.gc.strokeStyle=this.color;
}

gxtkGraphics.prototype.SetBlend=function( blend ){
	switch( blend ){
	case 1:
		this.blend="lighter";
		break;
	default:
		this.blend="source-over";
	}
	this.gc.globalCompositeOperation=this.blend;
}

gxtkGraphics.prototype.SetScissor=function( x,y,w,h ){
	this.scissorX=x;
	this.scissorY=y;
	this.scissorWidth=w;
	this.scissorHeight=h;
	this.clipped=(x!=0 || y!=0 || w!=this.canvas.width || h!=this.canvas.height);
	this.gc.restore();
	this.gc.save();
	if( this.clipped ){
		this.gc.beginPath();
		this.gc.rect( x,y,w,h );
		this.gc.clip();
		this.gc.closePath();
	}
	this.gc.fillStyle=this.color;
	this.gc.strokeStyle=this.color;
	if( this.tformed ) this.gc.setTransform( this.ix,this.iy,this.jx,this.jy,this.tx,this.ty );
}

gxtkGraphics.prototype.SetMatrix=function( ix,iy,jx,jy,tx,ty ){
	this.ix=ix;this.iy=iy;
	this.jx=jx;this.jy=jy;
	this.tx=tx;this.ty=ty;
	this.gc.setTransform( ix,iy,jx,jy,tx,ty );
	this.tformed=(ix!=1 || iy!=0 || jx!=0 || jy!=1 || tx!=0 || ty!=0);
}

gxtkGraphics.prototype.Cls=function( r,g,b ){
	if( this.tformed ) this.gc.setTransform( 1,0,0,1,0,0 );
	this.gc.fillStyle="rgb("+(r|0)+","+(g|0)+","+(b|0)+")";
	this.gc.globalAlpha=1;
	this.gc.globalCompositeOperation="source-over";
	this.gc.fillRect( 0,0,this.canvas.width,this.canvas.height );
	this.gc.fillStyle=this.color;
	this.gc.globalAlpha=this.alpha;
	this.gc.globalCompositeOperation=this.blend;
	if( this.tformed ) this.gc.setTransform( this.ix,this.iy,this.jx,this.jy,this.tx,this.ty );
}

gxtkGraphics.prototype.DrawRect=function( x,y,w,h ){
	if( w<0 ){ x+=w;w=-w; }
	if( h<0 ){ y+=h;h=-h; }
	if( w<=0 || h<=0 ) return;
	//
	this.gc.fillRect( x,y,w,h );
}

gxtkGraphics.prototype.DrawLine=function( x1,y1,x2,y2 ){
	if( this.tformed ){
		var x1_t=x1 * this.ix + y1 * this.jx + this.tx;
		var y1_t=x1 * this.iy + y1 * this.jy + this.ty;
		var x2_t=x2 * this.ix + y2 * this.jx + this.tx;
		var y2_t=x2 * this.iy + y2 * this.jy + this.ty;
		this.gc.setTransform( 1,0,0,1,0,0 );
	  	this.gc.beginPath();
	  	this.gc.moveTo( x1_t,y1_t );
	  	this.gc.lineTo( x2_t,y2_t );
	  	this.gc.stroke();
	  	this.gc.closePath();
		this.gc.setTransform( this.ix,this.iy,this.jx,this.jy,this.tx,this.ty );
	}else{
	  	this.gc.beginPath();
	  	this.gc.moveTo( x1,y1 );
	  	this.gc.lineTo( x2,y2 );
	  	this.gc.stroke();
	  	this.gc.closePath();
	}
}

gxtkGraphics.prototype.DrawOval=function( x,y,w,h ){
	if( w<0 ){ x+=w;w=-w; }
	if( h<0 ){ y+=h;h=-h; }
	if( w<=0 || h<=0 ) return;
	//
  	var w2=w/2,h2=h/2;
	this.gc.save();
	this.gc.translate( x+w2,y+h2 );
	this.gc.scale( w2,h2 );
  	this.gc.beginPath();
	this.gc.arc( 0,0,1,0,Math.PI*2,false );
	this.gc.fill();
  	this.gc.closePath();
	this.gc.restore();
}

gxtkGraphics.prototype.DrawPoly=function( verts ){
	if( verts.length<6 ) return;
	this.gc.beginPath();
	this.gc.moveTo( verts[0],verts[1] );
	for( var i=2;i<verts.length;i+=2 ){
		this.gc.lineTo( verts[i],verts[i+1] );
	}
	this.gc.fill();
	this.gc.closePath();
}

gxtkGraphics.prototype.DrawSurface=function( surface,x,y ){
	if( !surface.image.complete ) return;
	
	if( this.white ){
		this.gc.drawImage( surface.image,x,y );
		return;
	}
	
	this.DrawImageTinted( surface.image,x,y,0,0,surface.swidth,surface.sheight );
}

gxtkGraphics.prototype.DrawSurface2=function( surface,x,y,srcx,srcy,srcw,srch ){
	if( !surface.image.complete ) return;

	if( srcw<0 ){ srcx+=srcw;srcw=-srcw; }
	if( srch<0 ){ srcy+=srch;srch=-srch; }
	if( srcw<=0 || srch<=0 ) return;

	if( this.white ){
		this.gc.drawImage( surface.image,srcx,srcy,srcw,srch,x,y,srcw,srch );
		return;
	}
	
	this.DrawImageTinted( surface.image,x,y,srcx,srcy,srcw,srch  );
}

gxtkGraphics.prototype.DrawImageTinted=function( image,dx,dy,sx,sy,sw,sh ){

	if( !this.tmpCanvas ){
		this.tmpCanvas=document.createElement( "canvas" );
	}

	if( sw>this.tmpCanvas.width || sh>this.tmpCanvas.height ){
		this.tmpCanvas.width=Math.max( sw,this.tmpCanvas.width );
		this.tmpCanvas.height=Math.max( sh,this.tmpCanvas.height );
	}
	
	var tgc=this.tmpCanvas.getContext( "2d" );
	
	tgc.globalCompositeOperation="copy";

	tgc.drawImage( image,sx,sy,sw,sh,0,0,sw,sh );
	
	var imgData=tgc.getImageData( 0,0,sw,sh );
	
	var p=imgData.data,sz=sw*sh*4,i;
	
	for( i=0;i<sz;i+=4 ){
		p[i]=p[i]*this.r/255;
		p[i+1]=p[i+1]*this.g/255;
		p[i+2]=p[i+2]*this.b/255;
	}
	
	tgc.putImageData( imgData,0,0 );
	
	this.gc.drawImage( this.tmpCanvas,0,0,sw,sh,dx,dy,sw,sh );
}

//***** gxtkSurface class *****

function gxtkSurface( image,graphics ){
	this.image=image;
	this.graphics=graphics;
	this.swidth=image.meta_width;
	this.sheight=image.meta_height;
}

//***** GXTK API *****

gxtkSurface.prototype.Discard=function(){
	if( this.image ){
		this.image=null;
	}
}

gxtkSurface.prototype.Width=function(){
	return this.swidth;
}

gxtkSurface.prototype.Height=function(){
	return this.sheight;
}

gxtkSurface.prototype.Loaded=function(){
	return this.image.complete;
}

//***** Class gxtkInput *****

function gxtkInput( app ){
	this.app=app;
	this.keyStates=new Array( 512 );
	this.charQueue=new Array( 32 );
	this.charPut=0;
	this.charGet=0;
	this.mouseX=0;
	this.mouseY=0;
	this.joyX=0;
	this.joyY=0;
	this.joyZ=0;
	this.accelX=0;
	this.accelY=0;
	this.accelZ=0;
	for( var i=0;i<512;++i ){
		this.keyStates[i]=0;
	}
}

gxtkInput.prototype.BeginUpdate=function(){
}

gxtkInput.prototype.EndUpdate=function(){
	for( var i=0;i<512;++i ){
		this.keyStates[i]&=0x100;
	}
	this.charGet=0;
	this.charPut=0;
}

gxtkInput.prototype.OnKeyDown=function( key ){
	if( (this.keyStates[key]&0x100)==0 ){
		this.keyStates[key]|=0x100;
		++this.keyStates[key];	
	}
}

gxtkInput.prototype.OnKeyUp=function( key ){
	this.keyStates[key]&=0xff;
}

gxtkInput.prototype.PutChar=function( char ){
	if( this.charPut-this.charGet<32 ){
		this.charQueue[this.charPut & 31]=char;
		this.charPut+=1;
	}
}

gxtkInput.prototype.OnMouseMove=function( x,y ){
	this.mouseX=x;
	this.mouseY=y;
}

//***** GXTK API *****

gxtkInput.prototype.KeyDown=function( key ){
	if( key>0 && key<512 ){
		if( key==KEY_TOUCH0 ) key=KEY_LMB;
		return this.keyStates[key] >> 8;
	}
	return 0;
}

gxtkInput.prototype.KeyHit=function( key ){
	if( key>0 && key<512 ){
		if( key==KEY_TOUCH0 ) key=KEY_LMB;
		return this.keyStates[key] & 0xff;
	}
	return 0;
}

gxtkInput.prototype.GetChar=function(){
	if( this.charPut!=this.charGet ){
		var char=this.charQueue[this.charGet & 31];
		this.charGet+=1;
		return char;
	}
	return 0;
}

gxtkInput.prototype.MouseX=function(){
	return this.mouseX;
}

gxtkInput.prototype.MouseY=function(){
	return this.mouseY;
}

gxtkInput.prototype.JoyX=function( index ){
	return this.joyX;
}

gxtkInput.prototype.JoyY=function( index ){
	return this.joyY;
}

gxtkInput.prototype.JoyZ=function( index ){
	return this.joyZ;
}

gxtkInput.prototype.TouchX=function( index ){
	return this.mouseX;
}

gxtkInput.prototype.TouchY=function( index ){
	return this.mouseY;
}

gxtkInput.prototype.AccelX=function(){
	return 0;
}

gxtkInput.prototype.AccelY=function(){
	return 0;
}

gxtkInput.prototype.AccelZ=function(){
	return 0;
}


//***** gxtkChannel class *****
function gxtkChannel(){
	this.audio=null;
	this.sample=null;
	this.volume=1;
	this.pan=0;
	this.rate=1;
}

//***** gxtkAudio class *****
function gxtkAudio( app ){
	this.app=app;
	this.okay=typeof(Audio)!="undefined";
	this.nextchan=0;
	this.music=null;
	this.channels=new Array(33);
	for( var i=0;i<33;++i ){
		this.channels[i]=new gxtkChannel();
	}
}

gxtkAudio.prototype.OnSuspend=function(){
	var i;
	for( i=0;i<33;++i ){
		var chan=this.channels[i];
		if( chan.audio ) chan.audio.pause();
	}
}

gxtkAudio.prototype.OnResume=function(){
	var i;
	for( i=0;i<33;++i ){
		var chan=this.channels[i];
		if( chan.audio ) chan.audio.play();
	}
}

gxtkAudio.prototype.LoadSample=function( path ){
	var audio=loadAudio( path );
	if( audio ) return new gxtkSample( audio );
	return null;
}

gxtkAudio.prototype.PlaySample=function( sample,channel,flags ){
	if( !this.okay ) return;
	
	var chan=this.channels[channel];
	
	if( chan.sample==sample && chan.audio ){	//&& !chan.audio.paused ){
		chan.audio.loop=(flags&1)!=0;
		chan.audio.volume=chan.volume;
		try{
			chan.audio.currentTime=0;
		}catch(ex){
		}
		chan.audio.play();
		return;
	}

	if( chan.audio ) chan.audio.pause();
	
	var audio=sample.AllocAudio();
	
	if( audio ){
		for( var i=0;i<33;++i ){
			if( this.channels[i].audio==audio ){
				this.channels[i].audio=null;
				break;
			}
		}
		audio.loop=(flags&1)!=0;
		audio.volume=chan.volume;
		audio.play();
	}
	
	chan.audio=audio;
	chan.sample=sample;
}

gxtkAudio.prototype.StopChannel=function( channel ){
	var chan=this.channels[channel];
	if( chan.audio ) chan.audio.pause();
}

gxtkAudio.prototype.ChannelState=function( channel ){
	var chan=this.channels[channel];
	if( chan.audio && !chan.audio.paused && !chan.audio.ended ) return 1;
	return 0;
}

gxtkAudio.prototype.SetVolume=function( channel,volume ){
	var chan=this.channels[channel];
	if( chan.audio ) chan.audio.volume=volume;
	chan.volume=volume;
}

gxtkAudio.prototype.SetPan=function( channel,pan ){
	var chan=this.channels[channel];
	chan.pan=pan;
}

gxtkAudio.prototype.SetRate=function( channel,rate ){
	var chan=this.channels[channel];
	chan.rate=rate;
}

gxtkAudio.prototype.PlayMusic=function( path,flags ){
	this.StopMusic();
	
	this.music=this.LoadSample( path );
	if( !this.music ) return;
	
	this.PlaySample( this.music,32,flags );
}

gxtkAudio.prototype.StopMusic=function(){
	this.StopChannel( 32 );

	if( this.music ){
		this.music.Discard();
		this.music=null;
	}
}

gxtkAudio.prototype.MusicState=function(){

	return this.ChannelState( 32 );
}

gxtkAudio.prototype.SetMusicVolume=function( volume ){

	this.SetVolume( 32,volume );
}

//***** gxtkSample class *****

function gxtkSample( audio ){
	this.audio=audio;
	this.insts=new Array( 8 );
	this.insts[0]=audio;
}

gxtkSample.prototype.Discard=function(){
	if( this.audio ){
		this.audio=null;
		for( var i=0;i<8;++i ){
			this.insts[i]=null;
		}
	}
}

gxtkSample.prototype.AllocAudio=function(){
	for( var i=0;i<8;++i ){
		var audio=this.insts[i];
		if( audio ){
			//Ok, this is ugly but seems to work best...no idea how/why!
			if( audio.paused ){
				if( audio.currentTime==0 ) return audio;
				audio.currentTime=0;
			}else if( audio.ended ){
				audio.pause();
			}
		}else{
			audio=new Audio( this.audio.src );
			this.insts[i]=audio;
			return audio;
		}
	}
	return null;
}
function util() {
}

util.NavigateToUrl = function(url) {
	document.location.href = url;
};

util.GetTimestamp = function() {
	var ts = Math.round((new Date()).getTime() / 1000);
	return ts;
}
function bb_app_App(){
	Object.call(this);
}
function bb_app_new(){
	bb_app_device=bb_app_new2.call(new bb_app_AppDevice,this);
	return this;
}
bb_app_App.prototype.bbm_OnCreate=function(){
	return 0;
}
bb_app_App.prototype.bbm_OnUpdate=function(){
	return 0;
}
bb_app_App.prototype.bbm_OnSuspend=function(){
	return 0;
}
bb_app_App.prototype.bbm_OnResume=function(){
	return 0;
}
bb_app_App.prototype.bbm_OnRender=function(){
	return 0;
}
bb_app_App.prototype.bbm_OnLoading=function(){
	return 0;
}
function bb_application_Application(){
	bb_app_App.call(this);
	this.bb_width=480.000000;
	this.bb_height=320.000000;
	this.bb_faders=null;
	this.bb_scenes=null;
	this.bb_currentScene=null;
	this.bb_zoomFactorX=.0;
	this.bb_zoomFactorY=.0;
	this.bb_state=0;
	this.bb_loading=false;
	this.bb_nextScene=null;
}
bb_application_Application.prototype=extend_class(bb_app_App);
var bb_application_application;
function bb_application_new(){
	bb_app_new.call(this);
	return this;
}
function bb_application_GetInstance(){
	if(!((bb_application_application)!=null)){
		bb_application_application=bb_application_new.call(new bb_application_Application);
	}
	return bb_application_application;
}
bb_application_Application.prototype.bbm_SetSize=function(bbt_w,bbt_h){
	this.bb_width=bbt_w;
	this.bb_height=bbt_h;
}
bb_application_Application.prototype.bbm_Init=function(){
	this.bb_faders=bb_list_new.call(new bb_list_List);
	this.bb_scenes=bb_map_new2.call(new bb_map_StringMap);
}
bb_application_Application.prototype.bbm_AddScene=function(bbt_sceneName,bbt_scene){
	if(this.bb_scenes.bbm_IsEmpty()){
		this.bb_currentScene=bbt_scene;
	}
	bbt_scene.bb_name=bbt_sceneName;
	this.bb_scenes.bbm_Set((bb_boxes_new3.call(new bb_boxes_StringObject,bbt_sceneName)),bbt_scene);
}
bb_application_Application.prototype.bbm_Run=function(){
	if(!((this.bb_currentScene)!=null)){
		error("No scenes found!");
	}
	this.bb_currentScene.bbm_OnEnter();
	var bbt_=this.bb_faders.bbm_ObjectEnumerator();
	while(bbt_.bbm_HasNext()){
		var bbt_f=bbt_.bbm_NextObject();
		bbt_f.bbm_FadeIn();
	}
}
bb_application_Application.prototype.bbm_OnCreate=function(){
	bb_app_SetUpdateRate(60);
	this.bb_zoomFactorX=(bb_graphics_DeviceWidth())/this.bb_width;
	this.bb_zoomFactorY=(bb_graphics_DeviceHeight())/this.bb_height;
	return 0;
}
bb_application_Application.prototype.bbm_SetState=function(bbt_st){
	this.bb_state=bbt_st;
}
bb_application_Application.prototype.bbm_OnUpdate=function(){
	this.bb_currentScene.bbm_OnUpdate();
	if(this.bb_state!=1){
		var bbt_changeState=true;
		var bbt_=this.bb_faders.bbm_ObjectEnumerator();
		while(bbt_.bbm_HasNext()){
			var bbt_f=bbt_.bbm_NextObject();
			if(bbt_f.bbm_IsFading()){
				bbt_changeState=false;
			}
			bbt_f.bbm_OnUpdate();
		}
		if(bbt_changeState){
			if(this.bb_state==0 && this.bb_loading==false){
				this.bbm_SetState(1);
			}else{
				if(this.bb_state==2){
					this.bbm_SetState(0);
					this.bb_currentScene.bbm_OnLeave();
					this.bb_currentScene=this.bb_nextScene;
					this.bb_currentScene.bbm_OnEnter();
					var bbt_2=this.bb_faders.bbm_ObjectEnumerator();
					while(bbt_2.bbm_HasNext()){
						var bbt_f2=bbt_2.bbm_NextObject();
						bbt_f2.bbm_FadeIn();
					}
				}
			}
		}
	}
	return 0;
}
bb_application_Application.prototype.bbm_OnRender=function(){
	bb_graphics_Scale(this.bb_zoomFactorX,this.bb_zoomFactorY);
	this.bb_loading=false;
	if((this.bb_currentScene)!=null){
		this.bb_currentScene.bbm_OnRender();
	}
	if(this.bb_state!=1){
		var bbt_=this.bb_faders.bbm_ObjectEnumerator();
		while(bbt_.bbm_HasNext()){
			var bbt_f=bbt_.bbm_NextObject();
			bbt_f.bbm_OnRender();
		}
	}
	return 0;
}
bb_application_Application.prototype.bbm_OnLoading=function(){
	this.bb_loading=true;
	var bbt_t="Loading...Please stand by";
	bb_graphics_SetColor(0.00000000,255.000000,255.000000);
	bb_graphics_SetAlpha(0.5);
	bb_graphics_DrawText(bbt_t,this.bb_width/2.00000000,this.bb_height-40.0000000,.5,.5);
	return 0;
}
bb_application_Application.prototype.bbm_OnResume=function(){
	return 0;
}
bb_application_Application.prototype.bbm_OnSuspend=function(){
	return 0;
}
function bb_app_AppDevice(){
	gxtkApp.call(this);
	this.bb_app=null;
	this.bb_updateRate=0;
}
bb_app_AppDevice.prototype=extend_class(gxtkApp);
function bb_app_new2(bbt_app){
	this.bb_app=bbt_app;
	bb_graphics_SetGraphicsContext(bb_graphics_new.call(new bb_graphics_GraphicsContext,this.GraphicsDevice()));
	bb_input_SetInputDevice(this.InputDevice());
	bb_audio_SetAudioDevice(this.AudioDevice());
	return this;
}
function bb_app_new3(){
	return this;
}
bb_app_AppDevice.prototype.OnCreate=function(){
	bb_graphics_SetFont(null,32);
	return this.bb_app.bbm_OnCreate();
}
bb_app_AppDevice.prototype.OnUpdate=function(){
	return this.bb_app.bbm_OnUpdate();
}
bb_app_AppDevice.prototype.OnSuspend=function(){
	return this.bb_app.bbm_OnSuspend();
}
bb_app_AppDevice.prototype.OnResume=function(){
	return this.bb_app.bbm_OnResume();
}
bb_app_AppDevice.prototype.OnRender=function(){
	bb_graphics_BeginRender();
	var bbt_r=this.bb_app.bbm_OnRender();
	bb_graphics_EndRender();
	return bbt_r;
}
bb_app_AppDevice.prototype.OnLoading=function(){
	bb_graphics_BeginRender();
	var bbt_r=this.bb_app.bbm_OnLoading();
	bb_graphics_EndRender();
	return bbt_r;
}
bb_app_AppDevice.prototype.SetUpdateRate=function(bbt_hertz){
	gxtkApp.prototype.SetUpdateRate.call(this,bbt_hertz);
	this.bb_updateRate=bbt_hertz;
	return 0;
}
function bb_graphics_GraphicsContext(){
	Object.call(this);
	this.bb_device=null;
	this.bb_defaultFont=null;
	this.bb_font=null;
	this.bb_firstChar=0;
	this.bb_matrixSp=0;
	this.bb_ix=1.00000000;
	this.bb_iy=.0;
	this.bb_jx=.0;
	this.bb_jy=1.00000000;
	this.bb_tx=.0;
	this.bb_ty=.0;
	this.bb_tformed=0;
	this.bb_matDirty=0;
	this.bb_color_r=.0;
	this.bb_color_g=.0;
	this.bb_color_b=.0;
	this.bb_alpha=.0;
	this.bb_blend=0;
	this.bb_scissor_x=.0;
	this.bb_scissor_y=.0;
	this.bb_scissor_width=.0;
	this.bb_scissor_height=.0;
	this.bb_matrixStack=new_number_array(192);
}
function bb_graphics_new(bbt_device){
	this.bb_device=bbt_device;
	return this;
}
function bb_graphics_new2(){
	return this;
}
var bb_graphics_context;
function bb_graphics_SetGraphicsContext(bbt_gc){
	bb_graphics_context=bbt_gc;
	return 0;
}
var bb_input_device;
function bb_input_SetInputDevice(bbt_dev){
	bb_input_device=bbt_dev;
	return 0;
}
var bb_audio_device;
function bb_audio_SetAudioDevice(bbt_dev){
	bb_audio_device=bbt_dev;
	return 0;
}
var bb_app_device;
function bb_fader_Fader(){
	Object.call(this);
	this.bb_fadeState=0;
	this.bb_speed=.0;
	this.bb_position=.0;
}
bb_fader_Fader.prototype.bbm_FadeIn=function(){
	this.bb_fadeState=2;
}
bb_fader_Fader.prototype.bbm_IsFading=function(){
	return this.bb_fadeState!=1;
}
bb_fader_Fader.prototype.bbm_OnUpdate=function(){
	var bbt_=this.bb_fadeState;
	if(bbt_==0){
		this.bb_position+=this.bb_speed;
		if(this.bb_position>=1.00000000){
			this.bb_position=1.00000000;
			this.bb_fadeState=1;
		}
	}else{
		if(bbt_==2){
			this.bb_position-=this.bb_speed;
			if(this.bb_position<=0.00000000){
				this.bb_position=0.00000000;
				this.bb_fadeState=1;
			}
		}
	}
}
bb_fader_Fader.prototype.bbm_OnRender=function(){
}
function bb_list_List(){
	Object.call(this);
	this.bb__head=bb_list_new4.call(new bb_list_Node);
}
function bb_list_new(){
	return this;
}
bb_list_List.prototype.bbm_ObjectEnumerator=function(){
	return bb_list_new2.call(new bb_list_Enumerator,this);
}
function bb_scene_Scene(){
	Object.call(this);
	this.bb_name="";
}
function bb_scene_new(){
	return this;
}
bb_scene_Scene.prototype.bbm_OnEnter=function(){
}
bb_scene_Scene.prototype.bbm_OnUpdate=function(){
}
bb_scene_Scene.prototype.bbm_OnLeave=function(){
}
bb_scene_Scene.prototype.bbm_OnRender=function(){
}
function bb_boxes_StringObject(){
	Object.call(this);
	this.bb_value="";
}
function bb_boxes_new(bbt_value){
	this.bb_value=String(bbt_value);
	return this;
}
function bb_boxes_new2(bbt_value){
	this.bb_value=String(bbt_value);
	return this;
}
function bb_boxes_new3(bbt_value){
	this.bb_value=bbt_value;
	return this;
}
function bb_boxes_new4(){
	return this;
}
function bb_map_Map(){
	Object.call(this);
	this.bb_root=null;
}
function bb_map_new(){
	return this;
}
bb_map_Map.prototype.bbm_IsEmpty=function(){
	return this.bb_root==null;
}
bb_map_Map.prototype.bbm_Compare=function(bbt_lhs,bbt_rhs){
	return 0;
}
bb_map_Map.prototype.bbm_RotateLeft=function(bbt_node){
	var bbt_child=bbt_node.bb_right;
	bbt_node.bb_right=bbt_child.bb_left;
	if((bbt_child.bb_left)!=null){
		bbt_child.bb_left.bb_parent=bbt_node;
	}
	bbt_child.bb_parent=bbt_node.bb_parent;
	if((bbt_node.bb_parent)!=null){
		if(bbt_node==bbt_node.bb_parent.bb_left){
			bbt_node.bb_parent.bb_left=bbt_child;
		}else{
			bbt_node.bb_parent.bb_right=bbt_child;
		}
	}else{
		this.bb_root=bbt_child;
	}
	bbt_child.bb_left=bbt_node;
	bbt_node.bb_parent=bbt_child;
	return 0;
}
bb_map_Map.prototype.bbm_RotateRight=function(bbt_node){
	var bbt_child=bbt_node.bb_left;
	bbt_node.bb_left=bbt_child.bb_right;
	if((bbt_child.bb_right)!=null){
		bbt_child.bb_right.bb_parent=bbt_node;
	}
	bbt_child.bb_parent=bbt_node.bb_parent;
	if((bbt_node.bb_parent)!=null){
		if(bbt_node==bbt_node.bb_parent.bb_right){
			bbt_node.bb_parent.bb_right=bbt_child;
		}else{
			bbt_node.bb_parent.bb_left=bbt_child;
		}
	}else{
		this.bb_root=bbt_child;
	}
	bbt_child.bb_right=bbt_node;
	bbt_node.bb_parent=bbt_child;
	return 0;
}
bb_map_Map.prototype.bbm_InsertFixup=function(bbt_node){
	while(((bbt_node.bb_parent)!=null) && bbt_node.bb_parent.bb_color==-1 && ((bbt_node.bb_parent.bb_parent)!=null)){
		if(bbt_node.bb_parent==bbt_node.bb_parent.bb_parent.bb_left){
			var bbt_uncle=bbt_node.bb_parent.bb_parent.bb_right;
			if(((bbt_uncle)!=null) && bbt_uncle.bb_color==-1){
				bbt_node.bb_parent.bb_color=1;
				bbt_uncle.bb_color=1;
				bbt_uncle.bb_parent.bb_color=-1;
				bbt_node=bbt_uncle.bb_parent;
			}else{
				if(bbt_node==bbt_node.bb_parent.bb_right){
					bbt_node=bbt_node.bb_parent;
					this.bbm_RotateLeft(bbt_node);
				}
				bbt_node.bb_parent.bb_color=1;
				bbt_node.bb_parent.bb_parent.bb_color=-1;
				this.bbm_RotateRight(bbt_node.bb_parent.bb_parent);
			}
		}else{
			var bbt_uncle2=bbt_node.bb_parent.bb_parent.bb_left;
			if(((bbt_uncle2)!=null) && bbt_uncle2.bb_color==-1){
				bbt_node.bb_parent.bb_color=1;
				bbt_uncle2.bb_color=1;
				bbt_uncle2.bb_parent.bb_color=-1;
				bbt_node=bbt_uncle2.bb_parent;
			}else{
				if(bbt_node==bbt_node.bb_parent.bb_left){
					bbt_node=bbt_node.bb_parent;
					this.bbm_RotateRight(bbt_node);
				}
				bbt_node.bb_parent.bb_color=1;
				bbt_node.bb_parent.bb_parent.bb_color=-1;
				this.bbm_RotateLeft(bbt_node.bb_parent.bb_parent);
			}
		}
	}
	this.bb_root.bb_color=1;
	return 0;
}
bb_map_Map.prototype.bbm_Set=function(bbt_key,bbt_value){
	var bbt_node=this.bb_root;
	var bbt_parent=null;
	var bbt_cmp=0;
	while((bbt_node)!=null){
		bbt_parent=bbt_node;
		bbt_cmp=this.bbm_Compare((bbt_key),(bbt_node.bb_key));
		if(bbt_cmp>0){
			bbt_node=bbt_node.bb_right;
		}else{
			if(bbt_cmp<0){
				bbt_node=bbt_node.bb_left;
			}else{
				bbt_node.bb_value=bbt_value;
				return 0;
			}
		}
	}
	bbt_node=bb_map_new3.call(new bb_map_Node,bbt_key,bbt_value,-1,bbt_parent);
	if(!((bbt_parent)!=null)){
		this.bb_root=bbt_node;
		return 0;
	}
	if(bbt_cmp>0){
		bbt_parent.bb_right=bbt_node;
	}else{
		bbt_parent.bb_left=bbt_node;
	}
	this.bbm_InsertFixup(bbt_node);
	return 0;
}
function bb_map_StringMap(){
	bb_map_Map.call(this);
}
bb_map_StringMap.prototype=extend_class(bb_map_Map);
function bb_map_new2(){
	bb_map_new.call(this);
	return this;
}
bb_map_StringMap.prototype.bbm_Compare=function(bbt_lhs,bbt_rhs){
	var bbt_l=object_downcast((bbt_lhs),bb_boxes_StringObject).bb_value;
	var bbt_r=object_downcast((bbt_rhs),bb_boxes_StringObject).bb_value;
	if(bbt_l<bbt_r){
		return -1;
	}
	return ((bbt_l>bbt_r)?1:0);
}
function bb_teaser_SceneTeaser(){
	bb_scene_Scene.call(this);
	this.bb_bg=null;
	this.bb_backwater=null;
	this.bb_logo=null;
	this.bb_fonts=new_object_array(8);
	this.bb_tw=null;
	this.bb_fff=0;
	this.bb_twLogo=null;
	this.bb_twFont=null;
	this.bb_twFontAlpha=null;
}
bb_teaser_SceneTeaser.prototype=extend_class(bb_scene_Scene);
function bb_teaser_new(){
	bb_scene_new.call(this);
	return this;
}
bb_teaser_SceneTeaser.prototype.bbm_OnEnter=function(){
	this.bb_bg=bb_graphics_LoadImage("background.png",1,bb_graphics_DefaultFlags);
	this.bb_backwater=bb_graphics_LoadImage("backwater.png",1,bb_graphics_DefaultFlags);
	this.bb_logo=bb_graphics_LoadImage("logo.png",1,1);
	this.bb_fonts[0]=bb_graphics_LoadImage("f1.png",1,1);
	this.bb_fonts[1]=bb_graphics_LoadImage("f2.png",1,1);
	this.bb_fonts[2]=bb_graphics_LoadImage("f3.png",1,1);
	this.bb_fonts[3]=bb_graphics_LoadImage("f4.png",1,1);
	this.bb_fonts[4]=bb_graphics_LoadImage("f5.png",1,1);
	this.bb_fonts[5]=bb_graphics_LoadImage("f6.png",1,1);
	this.bb_fonts[6]=bb_graphics_LoadImage("f7.png",1,1);
	this.bb_fonts[7]=bb_graphics_LoadImage("f8.png",1,1);
	this.bb_tw=bb_teaser_new2.call(new bb_teaser_Tween);
	this.bb_tw.bb_p1=1000.00000;
	this.bb_tw.bb_p2=0.00000000;
	this.bb_tw.bb_steps=70.0000000;
	this.bb_fff=0;
}
bb_teaser_SceneTeaser.prototype.bbm_OnLeave=function(){
	this.bb_bg=null;
	this.bb_backwater=null;
	this.bb_logo=null;
	for(var bbt_i=0;bbt_i<=7;bbt_i=bbt_i+1){
		this.bb_fonts[bbt_i]=null;
	}
}
bb_teaser_SceneTeaser.prototype.bbm_OnRender=function(){
	bb_graphics_DrawImage(this.bb_bg,0.00000000,0.00000000,0);
	bb_graphics_DrawImage(this.bb_backwater,this.bb_tw.bb_val,2.00000000,0);
	if((this.bb_twLogo)!=null){
		bb_graphics_SetAlpha(this.bb_twLogo.bb_val);
		var bbt_sc=1.00000000+(1.00000000-this.bb_twLogo.bb_val);
		bb_graphics_DrawImage2(this.bb_logo,640.000000,180.000000+Math.sin(((bb_app_Millisecs())/50.0)*D2R)*10.0000000,0.00000000,bbt_sc,bbt_sc,0);
		bb_graphics_SetAlpha(1.00000000);
		bb_graphics_Scale(1.00000000,1.00000000);
	}
	if((this.bb_twFont)!=null){
		bb_graphics_SetAlpha(Math.sin((this.bb_twFontAlpha.bb_val)*D2R)*0.15);
		bb_graphics_DrawImage2(this.bb_fonts[this.bb_fff],this.bb_twFont.bb_val+Math.sin((this.bb_twFontAlpha.bb_val/2.00000000)*D2R)*30.0000000,320.000000,0.00000000,1.2,1.2,0);
		bb_graphics_SetAlpha(Math.sin((this.bb_twFontAlpha.bb_val)*D2R));
		bb_graphics_DrawImage(this.bb_fonts[this.bb_fff],this.bb_twFont.bb_val,320.000000,0);
		bb_graphics_SetAlpha(1.00000000);
	}
}
bb_teaser_SceneTeaser.prototype.bbm_OnUpdate=function(){
	if((bb_input_MouseHit(0))!=0){
		util.NavigateToUrl("/games/CaptainBackwater");
	}
	this.bb_tw.bbm_Update();
	if((this.bb_tw.bb_stp)>this.bb_tw.bb_steps-30.0000000 && !((this.bb_twLogo)!=null)){
		this.bb_twLogo=bb_teaser_new2.call(new bb_teaser_Tween);
		this.bb_twLogo.bb_p1=0.00000000;
		this.bb_twLogo.bb_p2=1.00000000;
		this.bb_twLogo.bb_steps=60.0000000;
	}
	if((this.bb_twLogo)!=null){
		this.bb_twLogo.bbm_Update();
		if(this.bb_twLogo.bbm_IsFinished() && !((this.bb_twFont)!=null)){
			this.bb_twFont=bb_teaser_new2.call(new bb_teaser_Tween);
			this.bb_twFont.bb_p1=660.000000;
			this.bb_twFont.bb_p2=520.000000;
			this.bb_twFont.bb_steps=300.000000;
			this.bb_twFontAlpha=bb_teaser_new2.call(new bb_teaser_Tween);
			this.bb_twFontAlpha.bb_p1=0.00000000;
			this.bb_twFontAlpha.bb_p2=180.000000;
			this.bb_twFontAlpha.bb_steps=300.000000;
		}
	}
	if((this.bb_twFont)!=null){
		this.bb_twFont.bbm_Update();
		this.bb_twFontAlpha.bbm_Update();
		if(this.bb_twFont.bbm_IsFinished()){
			this.bb_twFont.bb_stp=0;
			this.bb_twFontAlpha.bb_stp=0;
			this.bb_fff+=1;
			if(this.bb_fff>7){
				this.bb_fff=0;
			}
		}
	}
}
function bb_map_Node(){
	Object.call(this);
	this.bb_key=null;
	this.bb_right=null;
	this.bb_left=null;
	this.bb_value=null;
	this.bb_color=0;
	this.bb_parent=null;
}
function bb_map_new3(bbt_key,bbt_value,bbt_color,bbt_parent){
	this.bb_key=bbt_key;
	this.bb_value=bbt_value;
	this.bb_color=bbt_color;
	this.bb_parent=bbt_parent;
	return this;
}
function bb_map_new4(){
	return this;
}
function bb_list_Enumerator(){
	Object.call(this);
	this.bb__list=null;
	this.bb__curr=null;
}
function bb_list_new2(bbt_list){
	this.bb__list=bbt_list;
	this.bb__curr=bbt_list.bb__head.bb__succ;
	return this;
}
function bb_list_new3(){
	return this;
}
bb_list_Enumerator.prototype.bbm_HasNext=function(){
	return this.bb__curr!=this.bb__list.bb__head;
}
bb_list_Enumerator.prototype.bbm_NextObject=function(){
	var bbt_data=this.bb__curr.bb__data;
	this.bb__curr=this.bb__curr.bb__succ;
	return bbt_data;
}
function bb_list_Node(){
	Object.call(this);
	this.bb__succ=null;
	this.bb__pred=null;
	this.bb__data=null;
}
function bb_list_new4(){
	this.bb__succ=this;
	this.bb__pred=this;
	return this;
}
function bb_list_new5(bbt_succ,bbt_pred,bbt_data){
	this.bb__succ=bbt_succ;
	this.bb__pred=bbt_pred;
	this.bb__succ.bb__pred=this;
	this.bb__pred.bb__succ=this;
	this.bb__data=bbt_data;
	return this;
}
function bbMain(){
	var bbt_app=bb_application_GetInstance();
	bbt_app.bbm_SetSize(939.000000,358.000000);
	bbt_app.bbm_Init();
	bbt_app.bbm_AddScene("teaser",(bb_teaser_new.call(new bb_teaser_SceneTeaser)));
	bbt_app.bbm_Run();
	return 0;
}
function bb_graphics_Image(){
	Object.call(this);
	this.bb_surface=null;
	this.bb_width=0;
	this.bb_height=0;
	this.bb_frames=[];
	this.bb_flags=0;
	this.bb_tx=.0;
	this.bb_ty=.0;
	this.bb_source=null;
}
var bb_graphics_DefaultFlags;
function bb_graphics_new3(){
	return this;
}
bb_graphics_Image.prototype.bbm_SetHandle=function(bbt_tx,bbt_ty){
	this.bb_tx=bbt_tx;
	this.bb_ty=bbt_ty;
	this.bb_flags=this.bb_flags&-2;
	return 0;
}
bb_graphics_Image.prototype.bbm_ApplyFlags=function(bbt_iflags){
	this.bb_flags=bbt_iflags;
	if((this.bb_flags&2)!=0){
		var bbt_=this.bb_frames;
		var bbt_2=0;
		while(bbt_2<bbt_.length){
			var bbt_f=bbt_[bbt_2];
			bbt_2=bbt_2+1;
			bbt_f.bb_x+=1;
		}
		this.bb_width-=2;
	}
	if((this.bb_flags&4)!=0){
		var bbt_3=this.bb_frames;
		var bbt_4=0;
		while(bbt_4<bbt_3.length){
			var bbt_f2=bbt_3[bbt_4];
			bbt_4=bbt_4+1;
			bbt_f2.bb_y+=1;
		}
		this.bb_height-=2;
	}
	if((this.bb_flags&1)!=0){
		this.bbm_SetHandle((this.bb_width)/2.0,(this.bb_height)/2.0);
	}
	if(this.bb_frames.length==1 && this.bb_frames[0].bb_x==0 && this.bb_frames[0].bb_y==0 && this.bb_width==this.bb_surface.Width() && this.bb_height==this.bb_surface.Height()){
		this.bb_flags|=65536;
	}
	return 0;
}
bb_graphics_Image.prototype.bbm_Load=function(bbt_path,bbt_nframes,bbt_iflags){
	this.bb_surface=bb_graphics_context.bb_device.LoadSurface(bbt_path);
	if(!((this.bb_surface)!=null)){
		return null;
	}
	this.bb_width=((this.bb_surface.Width()/bbt_nframes)|0);
	this.bb_height=this.bb_surface.Height();
	this.bb_frames=new_object_array(bbt_nframes);
	for(var bbt_i=0;bbt_i<bbt_nframes;bbt_i=bbt_i+1){
		this.bb_frames[bbt_i]=bb_graphics_new4.call(new bb_graphics_Frame,bbt_i*this.bb_width,0);
	}
	this.bbm_ApplyFlags(bbt_iflags);
	return this;
}
bb_graphics_Image.prototype.bbm_Grab=function(bbt_x,bbt_y,bbt_iwidth,bbt_iheight,bbt_nframes,bbt_iflags,bbt_source){
	this.bb_source=bbt_source;
	this.bb_surface=bbt_source.bb_surface;
	this.bb_width=bbt_iwidth;
	this.bb_height=bbt_iheight;
	this.bb_frames=new_object_array(bbt_nframes);
	var bbt_ix=bbt_x+bbt_source.bb_frames[0].bb_x;
	var bbt_iy=bbt_y+bbt_source.bb_frames[0].bb_y;
	for(var bbt_i=0;bbt_i<bbt_nframes;bbt_i=bbt_i+1){
		if(bbt_ix+this.bb_width>bbt_source.bb_width){
			bbt_ix=bbt_source.bb_frames[0].bb_x;
			bbt_iy+=this.bb_height;
		}
		if(bbt_ix+this.bb_width>bbt_source.bb_width || bbt_iy+this.bb_height>bbt_source.bb_height){
			error("Image frame outside surface");
		}
		this.bb_frames[bbt_i]=bb_graphics_new4.call(new bb_graphics_Frame,bbt_ix,bbt_iy);
		bbt_ix+=this.bb_width;
	}
	this.bbm_ApplyFlags(bbt_iflags);
	return this;
}
bb_graphics_Image.prototype.bbm_GrabImage=function(bbt_x,bbt_y,bbt_width,bbt_height,bbt_frames,bbt_flags){
	if(this.bb_frames.length!=1){
		return null;
	}
	return (bb_graphics_new3.call(new bb_graphics_Image)).bbm_Grab(bbt_x,bbt_y,bbt_width,bbt_height,bbt_frames,bbt_flags,this);
}
bb_graphics_Image.prototype.bbm_Width=function(){
	return this.bb_width;
}
bb_graphics_Image.prototype.bbm_Height=function(){
	return this.bb_height;
}
bb_graphics_Image.prototype.bbm_Frames=function(){
	return this.bb_frames.length;
}
function bb_graphics_Frame(){
	Object.call(this);
	this.bb_x=0;
	this.bb_y=0;
}
function bb_graphics_new4(bbt_x,bbt_y){
	this.bb_x=bbt_x;
	this.bb_y=bbt_y;
	return this;
}
function bb_graphics_new5(){
	return this;
}
function bb_graphics_LoadImage(bbt_path,bbt_frameCount,bbt_flags){
	return (bb_graphics_new3.call(new bb_graphics_Image)).bbm_Load(bbt_path,bbt_frameCount,bbt_flags);
}
function bb_graphics_LoadImage2(bbt_path,bbt_frameWidth,bbt_frameHeight,bbt_frameCount,bbt_flags){
	var bbt_atlas=(bb_graphics_new3.call(new bb_graphics_Image)).bbm_Load(bbt_path,1,0);
	if((bbt_atlas)!=null){
		return bbt_atlas.bbm_GrabImage(0,0,bbt_frameWidth,bbt_frameHeight,bbt_frameCount,bbt_flags);
	}
	return null;
}
function bb_graphics_SetFont(bbt_font,bbt_firstChar){
	if(!((bbt_font)!=null)){
		if(!((bb_graphics_context.bb_defaultFont)!=null)){
			bb_graphics_context.bb_defaultFont=bb_graphics_LoadImage("mojo_font.png",96,2);
		}
		bbt_font=bb_graphics_context.bb_defaultFont;
		bbt_firstChar=32;
	}
	bb_graphics_context.bb_font=bbt_font;
	bb_graphics_context.bb_firstChar=bbt_firstChar;
	return 0;
}
var bb_graphics_renderDevice;
function bb_graphics_SetMatrix(bbt_ix,bbt_iy,bbt_jx,bbt_jy,bbt_tx,bbt_ty){
	bb_graphics_context.bb_ix=bbt_ix;
	bb_graphics_context.bb_iy=bbt_iy;
	bb_graphics_context.bb_jx=bbt_jx;
	bb_graphics_context.bb_jy=bbt_jy;
	bb_graphics_context.bb_tx=bbt_tx;
	bb_graphics_context.bb_ty=bbt_ty;
	bb_graphics_context.bb_tformed=((bbt_ix!=1.00000000 || bbt_iy!=0.00000000 || bbt_jx!=0.00000000 || bbt_jy!=1.00000000 || bbt_tx!=0.00000000 || bbt_ty!=0.00000000)?1:0);
	bb_graphics_context.bb_matDirty=1;
	return 0;
}
function bb_graphics_SetMatrix2(bbt_m){
	bb_graphics_SetMatrix(bbt_m[0],bbt_m[1],bbt_m[2],bbt_m[3],bbt_m[4],bbt_m[5]);
	return 0;
}
function bb_graphics_SetColor(bbt_r,bbt_g,bbt_b){
	bb_graphics_context.bb_color_r=bbt_r;
	bb_graphics_context.bb_color_g=bbt_g;
	bb_graphics_context.bb_color_b=bbt_b;
	bb_graphics_context.bb_device.SetColor(bbt_r,bbt_g,bbt_b);
	return 0;
}
function bb_graphics_SetAlpha(bbt_alpha){
	bb_graphics_context.bb_alpha=bbt_alpha;
	bb_graphics_context.bb_device.SetAlpha(bbt_alpha);
	return 0;
}
function bb_graphics_SetBlend(bbt_blend){
	bb_graphics_context.bb_blend=bbt_blend;
	bb_graphics_context.bb_device.SetBlend(bbt_blend);
	return 0;
}
function bb_graphics_DeviceWidth(){
	return bb_graphics_context.bb_device.Width();
}
function bb_graphics_DeviceHeight(){
	return bb_graphics_context.bb_device.Height();
}
function bb_graphics_SetScissor(bbt_x,bbt_y,bbt_width,bbt_height){
	bb_graphics_context.bb_scissor_x=bbt_x;
	bb_graphics_context.bb_scissor_y=bbt_y;
	bb_graphics_context.bb_scissor_width=bbt_width;
	bb_graphics_context.bb_scissor_height=bbt_height;
	bb_graphics_context.bb_device.SetScissor(((bbt_x)|0),((bbt_y)|0),((bbt_width)|0),((bbt_height)|0));
	return 0;
}
function bb_graphics_BeginRender(){
	bb_graphics_renderDevice=bb_graphics_context.bb_device;
	bb_graphics_context.bb_matrixSp=0;
	bb_graphics_SetMatrix(1.00000000,0.00000000,0.00000000,1.00000000,0.00000000,0.00000000);
	bb_graphics_SetColor(255.000000,255.000000,255.000000);
	bb_graphics_SetAlpha(1.00000000);
	bb_graphics_SetBlend(0);
	bb_graphics_SetScissor(0.00000000,0.00000000,(bb_graphics_DeviceWidth()),(bb_graphics_DeviceHeight()));
	return 0;
}
function bb_graphics_EndRender(){
	bb_graphics_renderDevice=null;
	return 0;
}
function bb_teaser_Tween(){
	Object.call(this);
	this.bb_stp=0;
	this.bb_p1=.0;
	this.bb_p2=.0;
	this.bb_steps=100.0;
	this.bb_val=.0;
}
function bb_teaser_new2(){
	this.bb_stp=0;
	this.bb_p1=0.00000000;
	this.bb_p2=100.000000;
	return this;
}
bb_teaser_Tween.prototype.bbm_Update=function(){
	this.bb_val=(this.bb_stp)/this.bb_steps;
	this.bb_val=this.bb_val*this.bb_val*(3.00000000-2.00000000*this.bb_val);
	this.bb_val=this.bb_p2*this.bb_val+this.bb_p1*(1.00000000-this.bb_val);
	this.bb_stp+=1;
	if((this.bb_stp)>this.bb_steps){
		this.bb_stp=((this.bb_steps)|0);
	}
	return 0;
}
bb_teaser_Tween.prototype.bbm_IsFinished=function(){
	return (this.bb_stp)>=this.bb_steps;
}
function bb_app_SetUpdateRate(bbt_hertz){
	return bb_app_device.SetUpdateRate(bbt_hertz);
}
function bb_graphics_Transform(bbt_ix,bbt_iy,bbt_jx,bbt_jy,bbt_tx,bbt_ty){
	var bbt_ix2=bbt_ix*bb_graphics_context.bb_ix+bbt_iy*bb_graphics_context.bb_jx;
	var bbt_iy2=bbt_ix*bb_graphics_context.bb_iy+bbt_iy*bb_graphics_context.bb_jy;
	var bbt_jx2=bbt_jx*bb_graphics_context.bb_ix+bbt_jy*bb_graphics_context.bb_jx;
	var bbt_jy2=bbt_jx*bb_graphics_context.bb_iy+bbt_jy*bb_graphics_context.bb_jy;
	var bbt_tx2=bbt_tx*bb_graphics_context.bb_ix+bbt_ty*bb_graphics_context.bb_jx+bb_graphics_context.bb_tx;
	var bbt_ty2=bbt_tx*bb_graphics_context.bb_iy+bbt_ty*bb_graphics_context.bb_jy+bb_graphics_context.bb_ty;
	bb_graphics_SetMatrix(bbt_ix2,bbt_iy2,bbt_jx2,bbt_jy2,bbt_tx2,bbt_ty2);
	return 0;
}
function bb_graphics_Transform2(bbt_coords){
	var bbt_out=new_number_array(bbt_coords.length);
	for(var bbt_i=0;bbt_i<bbt_coords.length-1;bbt_i=bbt_i+2){
		var bbt_x=bbt_coords[bbt_i];
		var bbt_y=bbt_coords[bbt_i+1];
		bbt_out[bbt_i]=bbt_x*bb_graphics_context.bb_ix+bbt_y*bb_graphics_context.bb_jx+bb_graphics_context.bb_tx;
		bbt_out[bbt_i+1]=bbt_x*bb_graphics_context.bb_iy+bbt_y*bb_graphics_context.bb_jy+bb_graphics_context.bb_ty;
	}
	return bbt_out;
}
function bb_graphics_Scale(bbt_x,bbt_y){
	bb_graphics_Transform(bbt_x,0.00000000,0.00000000,bbt_y,0.00000000,0.00000000);
	return 0;
}
function bb_graphics_PushMatrix(){
	var bbt_sp=bb_graphics_context.bb_matrixSp;
	bb_graphics_context.bb_matrixStack[bbt_sp+0]=bb_graphics_context.bb_ix;
	bb_graphics_context.bb_matrixStack[bbt_sp+1]=bb_graphics_context.bb_iy;
	bb_graphics_context.bb_matrixStack[bbt_sp+2]=bb_graphics_context.bb_jx;
	bb_graphics_context.bb_matrixStack[bbt_sp+3]=bb_graphics_context.bb_jy;
	bb_graphics_context.bb_matrixStack[bbt_sp+4]=bb_graphics_context.bb_tx;
	bb_graphics_context.bb_matrixStack[bbt_sp+5]=bb_graphics_context.bb_ty;
	bb_graphics_context.bb_matrixSp=bbt_sp+6;
	return 0;
}
function bb_graphics_Translate(bbt_x,bbt_y){
	bb_graphics_Transform(1.00000000,0.00000000,0.00000000,1.00000000,bbt_x,bbt_y);
	return 0;
}
function bb_graphics_ValidateMatrix(){
	if((bb_graphics_context.bb_matDirty)!=0){
		bb_graphics_context.bb_device.SetMatrix(bb_graphics_context.bb_ix,bb_graphics_context.bb_iy,bb_graphics_context.bb_jx,bb_graphics_context.bb_jy,bb_graphics_context.bb_tx,bb_graphics_context.bb_ty);
		bb_graphics_context.bb_matDirty=0;
	}
	return 0;
}
function bb_graphics_PopMatrix(){
	var bbt_sp=bb_graphics_context.bb_matrixSp-6;
	bb_graphics_SetMatrix(bb_graphics_context.bb_matrixStack[bbt_sp+0],bb_graphics_context.bb_matrixStack[bbt_sp+1],bb_graphics_context.bb_matrixStack[bbt_sp+2],bb_graphics_context.bb_matrixStack[bbt_sp+3],bb_graphics_context.bb_matrixStack[bbt_sp+4],bb_graphics_context.bb_matrixStack[bbt_sp+5]);
	bb_graphics_context.bb_matrixSp=bbt_sp;
	return 0;
}
function bb_graphics_DrawImage(bbt_image,bbt_x,bbt_y,bbt_frame){
	var bbt_f=bbt_image.bb_frames[bbt_frame];
	if((bb_graphics_context.bb_tformed)!=0){
		bb_graphics_PushMatrix();
		bb_graphics_Translate(bbt_x-bbt_image.bb_tx,bbt_y-bbt_image.bb_ty);
		bb_graphics_ValidateMatrix();
		if((bbt_image.bb_flags&65536)!=0){
			bb_graphics_context.bb_device.DrawSurface(bbt_image.bb_surface,0.00000000,0.00000000);
		}else{
			bb_graphics_context.bb_device.DrawSurface2(bbt_image.bb_surface,0.00000000,0.00000000,bbt_f.bb_x,bbt_f.bb_y,bbt_image.bb_width,bbt_image.bb_height);
		}
		bb_graphics_PopMatrix();
	}else{
		bb_graphics_ValidateMatrix();
		if((bbt_image.bb_flags&65536)!=0){
			bb_graphics_context.bb_device.DrawSurface(bbt_image.bb_surface,bbt_x-bbt_image.bb_tx,bbt_y-bbt_image.bb_ty);
		}else{
			bb_graphics_context.bb_device.DrawSurface2(bbt_image.bb_surface,bbt_x-bbt_image.bb_tx,bbt_y-bbt_image.bb_ty,bbt_f.bb_x,bbt_f.bb_y,bbt_image.bb_width,bbt_image.bb_height);
		}
	}
	return 0;
}
function bb_graphics_Rotate(bbt_angle){
	bb_graphics_Transform(Math.cos((bbt_angle)*D2R),-Math.sin((bbt_angle)*D2R),Math.sin((bbt_angle)*D2R),Math.cos((bbt_angle)*D2R),0.00000000,0.00000000);
	return 0;
}
function bb_graphics_DrawImage2(bbt_image,bbt_x,bbt_y,bbt_rotation,bbt_scaleX,bbt_scaleY,bbt_frame){
	var bbt_f=bbt_image.bb_frames[bbt_frame];
	bb_graphics_PushMatrix();
	bb_graphics_Translate(bbt_x,bbt_y);
	bb_graphics_Rotate(bbt_rotation);
	bb_graphics_Scale(bbt_scaleX,bbt_scaleY);
	bb_graphics_Translate(-bbt_image.bb_tx,-bbt_image.bb_ty);
	bb_graphics_ValidateMatrix();
	if((bbt_image.bb_flags&65536)!=0){
		bb_graphics_context.bb_device.DrawSurface(bbt_image.bb_surface,0.00000000,0.00000000);
	}else{
		bb_graphics_context.bb_device.DrawSurface2(bbt_image.bb_surface,0.00000000,0.00000000,bbt_f.bb_x,bbt_f.bb_y,bbt_image.bb_width,bbt_image.bb_height);
	}
	bb_graphics_PopMatrix();
	return 0;
}
function bb_graphics_DrawText(bbt_text,bbt_x,bbt_y,bbt_xalign,bbt_yalign){
	if(!((bb_graphics_context.bb_font)!=null)){
		return 0;
	}
	var bbt_w=bb_graphics_context.bb_font.bbm_Width();
	var bbt_h=bb_graphics_context.bb_font.bbm_Height();
	bbt_x-=(bbt_w*bbt_text.length)*bbt_xalign;
	bbt_y-=(bbt_h)*bbt_yalign;
	for(var bbt_i=0;bbt_i<bbt_text.length;bbt_i=bbt_i+1){
		var bbt_ch=bbt_text.charCodeAt(bbt_i)-bb_graphics_context.bb_firstChar;
		if(bbt_ch>=0 && bbt_ch<bb_graphics_context.bb_font.bbm_Frames()){
			bb_graphics_DrawImage(bb_graphics_context.bb_font,bbt_x+(bbt_i*bbt_w),bbt_y,bbt_ch);
		}
	}
	return 0;
}
function bb_app_Millisecs(){
	return bb_app_device.MilliSecs();
}
function bb_input_MouseHit(bbt_button){
	return bb_input_device.KeyHit(1+bbt_button);
}
function bbInit(){
	bb_application_application=null;
	bb_graphics_context=null;
	bb_input_device=null;
	bb_audio_device=null;
	bb_app_device=null;
	bb_graphics_DefaultFlags=0;
	bb_graphics_renderDevice=null;
}
//${TRANSCODE_END}

