var key = [ 0, 0, 0, 0, 0 ];

var Controller = {
	canvas : null,
	context : null,	
	frame : 0,
	current : 0,
	
	width : 320,
	height: 480,
	
	eg : [
		[1,60,45,15,10,10,  59, 10,9,7,5, 1.5,0, 360,0.2, 0.4, 250, 144, 35, 1, 32, 31, 25, 0, 245, 0, 0, 0, 21, 21, 8, 0],
		[1,62,26,87,24,63,8,77,6,22,2,2,157,5,-0.5,4,250,255,255,1,32,31,25,0,0,182,0,0,56,53,50,0],
 		[1,25,57,64,46,40,50,11,7,5,4,1.5,126,54,-0.5,0,0,211,0,0,47,47,42,0,0,0,164,252,0,0,0,0],
		[1,40,69,0,4,0,18,13,6,46,0,1.5,0,4,-0.25,0,0,72,195,1,32,31,25,0,0,0,192,0,246,21,8,0],
		[1,59,31,15,13,21,46,10,9,4,4,1.5,164,193,-1,0,0,0,0,255,255,255,255,0,0,0,0,0,255,255,250,0],
		[1,26,96,57,43,47,61,13,27,46,5,19,34,360,0.25,0,0,91,167,255,40,14,129,0,24,0,215,0,32,21,208,1]
	],

	init : function(){
		this.canvas = document.getElementById( "blobs" );
		if(!this.canvas.getContext){
			return;
		}
		this.setCanvas(true);
		this.context = this.canvas.getContext( "2d" );
		this.context.globalCompositeOperation = "lighter";
		this.pe = new cParticleEmitter();	
		this.setExample( this.pe, this.eg[ 0 ] );
		this.pe.init();

		this.main();
	},
	
	setXY : function(x,y){
		this.pe.position.x  =x;
		this.pe.position.y = y;
	},
	
	setCanvas : function(orient){
		return;
		this.width = orient ? 320 : 480;
		this.height = orient ? 480 : 320;

		this.canvas.style.width = this.width + "px";
		this.canvas.style.height = this.width + "px";

	},
		
	main : function(){
		this.update();
		this.draw();
		setTimeout( function(){ Controller.main(); }, 120 );
	},
	
	update : function(){
		if(this.frame++%80==40){
			this.setExample( this.pe, this.eg[ ++this.current % this.eg.length ] );
		}
		this.pe.update(1);
	},
	
	setExample : function( pe, arr ){
		// Will have to try and itterate over properties
		var index = 1;
		for (var i in pe){
			if( typeof pe[ i ] == "object" ){
				if( pe[ i ] instanceof Array ){
					pe[ i ][ 0 ] = arr[ index++ ];
					pe[ i ][ 1 ] = arr[ index++ ];
					pe[ i ][ 2 ] = arr[ index++ ];
					pe[ i ][ 3 ] = arr[ index++ ];
				} else {
					pe[ i ].x = arr[ index++ ];
					pe[ i ].y = arr[ index++ ];
				}
			} else {
				pe[ i ] = arr[ index++ ];
			}
			if( index >= arr.length ){
				break;
			}
		}		
	},
	
	draw : function(){
		this.context.clearRect( 0, 0, this.width, this.height );
		this.pe.renderParticles( this.context );	 
	}
};

