// Modified WiiCade JavaScript API
// http://harrigan.org/pikadudeno1/geniuses/ModifiedWiiCade.html

var WiiRemotes=new Array();var PrimaryRemote=0;for(var i=0;i<4;i++){window.WiiRemotes[i]=new WiiRemote(i)}var Wii={isWii:function(){return(navigator.platform=="Nintendo Wii")},getRemote:function(a){WiiRemotes[a].update();return WiiRemotes[a]},getPrimaryRemote:function(){WiiRemotes[PrimaryRemote].update();return WiiRemotes[PrimaryRemote]}};function WiiRemote(a){this.id=a;this.rotated=false;this.allowRotate=(navigator.platform=="Nintendo Wii");this.active=(a==0);this.facing=true;this.x=0;this.y=0;this.dist=0;this.twist=0;this.listeners=new Object()}var proto=WiiRemote.prototype;proto.update=function(){if(!window.opera||!opera.wiiremote){return}var e=opera.wiiremote.update(this.id);var b;var a=1;var c;b=this.active;this.active=e.isEnabled;if(this.active!=b){if(this.active){this.dispatchEvent({type:"connected",identity:this.id})}else{this.dispatchEvent({type:"disconnected",identity:this.id})}}if(!e.isBrowsing){b=this.bitmap^e.hold;this.bitmap=e.hold;if(b>0){for(var d=0;d<15;d++){c=a;if(this.rotated&&this.allowRotate){if(c==WiiRemote.BUTTON_UP){c=WiiRemote.BUTTON_LEFT}else{if(c==WiiRemote.BUTTON_RIGHT){c=WiiRemote.BUTTON_UP}else{if(c==WiiRemote.BUTTON_DOWN){c=WiiRemote.BUTTON_RIGHT}else{if(c==WiiRemote.BUTTON_LEFT){c=WiiRemote.BUTTON_DOWN}}}}}if((a&b)>0){if((e.hold&a)>0){this.dispatchEvent(({type:"buttondown",buttonCode:c,identity:this.id}))}else{this.dispatchEvent(({type:"buttonup",buttonCode:c,identity:this.id}))}}a<<=1}}}if(e.dpdScreenX){this.facing=true;this.x=e.dpdScreenX;this.y=e.dpdScreenY}else{this.facing=false}if(e.dpdDistance){this.dist=Math.floor(e.dpdDistance*100)}if(e.dpdRollX){this.twist=Math.floor((Math.atan2(e.dpdRollY,e.dpdRollX)*180/Math.PI))}if(this.twist<0){this.twist+=360}if(e.isBrowsing){PrimaryRemote=this.id}};proto.isDown=function(a){if(this.rotated&&this.allowRotate){if(a==WiiRemote.BUTTON_UP){a=WiiRemote.BUTTON_RIGHT}else{if(a==WiiRemote.BUTTON_RIGHT){a=WiiRemote.BUTTON_DOWN}else{if(a==WiiRemote.BUTTON_DOWN){a=WiiRemote.BUTTON_LEFT}else{if(a==WiiRemote.BUTTON_LEFT){a=WiiRemote.BUTTON_UP}}}}}return(this.bitmap&a)};proto.addEventListener=function(b,c){var a=this.listeners[b];if(!a){a=new Array();this.listeners[b]=a}a[a.length]=c};proto.removeEventListener=function(e,f){var d=this.listeners[e];var a=d;var b=-1;if(!d){return}for(var c=0;c<d.length&&b<0;c++){if(d[c]==f){b=c}}if(b<0){return}d=new Array();for(var c=0;c<a.length;c++){if(c!=b){d[d.length]=a[c]}}this.listeners[e]=d};proto.dispatchEvent=function(c){var b=this.listeners[c.type];if(!b){return}for(var a=0;a<b.length;a++){b[a](c)}};proto.getIdentity=function(){return this.id};proto.isPrimary=function(){return(this.id==PrimaryRemote)};proto.isConnected=function(){return this.active};proto.isRotated=function(){return this.rotated};proto.setRotated=function(a){this.rotated=a};proto.getTwistAngle=function(){return this.twist};proto.getDistance=function(){return this.dist};proto.getCursorX=function(){return this.x};proto.getCursorY=function(){return this.y};proto.isFacingScreen=function(){return this.facing};WiiRemote.BUTTON_UP=8;WiiRemote.BUTTON_DOWN=4;WiiRemote.BUTTON_RIGHT=2;WiiRemote.BUTTON_LEFT=1;WiiRemote.BUTTON_1=512;WiiRemote.BUTTON_2=256;WiiRemote.BUTTON_A=2048;WiiRemote.BUTTON_B=1024;WiiRemote.BUTTON_PLUS=16;WiiRemote.BUTTON_MINUS=4096;function Nunchuk(a){this.parent=a;this.stickx=0;this.sticky=0}proto=Nunchuk.prototype;proto.isDown=function(a){return(this.parent.bitmap&a)};proto.getStickX=function(){return this.stickx};proto.getStickY=function(){return this.sticky};Nunchuk.BUTTON_C=16384;Nunchuk.BUTTON_Z=8192;if(window.proto){delete (proto)}var KeyboardController={keyMappings:{},buttonMappings:{},wiiMappings:{},getButtonMappingForKey:function(a){return this.keyMappings[a]},getKeyMappingForButton:function(a){return this.buttonMappings[a]},setKeyMapping:function(b,d){var c=this.buttonMappings[b];var a=this.keyMappings[d];if(c){this.keyMappings[c]=null}if(a){this.buttonMappings[a]=null}this.keyMappings[d]=b;this.buttonMappings[b]=d}};var kc=KeyboardController;var wr=WiiRemote;function tr(b,a){if(a.rotated&&a.allowRotate){if(b==WiiRemote.BUTTON_UP){b=WiiRemote.BUTTON_LEFT}else{if(b==WiiRemote.BUTTON_RIGHT){b=WiiRemote.BUTTON_UP}else{if(b==WiiRemote.BUTTON_DOWN){b=WiiRemote.BUTTON_RIGHT}else{if(b==WiiRemote.BUTTON_LEFT){b=WiiRemote.BUTTON_DOWN}}}}}return b}function kd(b){var a=WiiRemotes[PrimaryRemote];var c;if(Wii.isWii()){c=KeyboardController.wiiMappings[b.keyCode]}else{c=KeyboardController.keyMappings[b.keyCode]}a.bitmap|=c;a.dispatchEvent(({type:"buttondown",buttonCode:tr(c,a),identity:PrimaryRemote}));if(Browser.disabledKeys[c]==true){b.preventDefault();b.stopPropagation()}}function ku(b){var a=WiiRemotes[PrimaryRemote];var c;if(Wii.isWii()){c=KeyboardController.wiiMappings[b.keyCode]}else{c=KeyboardController.keyMappings[b.keyCode]}a.bitmap&=~c;a.dispatchEvent(({type:"buttonup",buttonCode:tr(c,a),identity:PrimaryRemote}))}function kp(a){var b;if(Wii.isWii()){b=KeyboardController.wiiMappings[a.keyCode]}else{b=KeyboardController.keyMappings[a.keyCode]}if(Browser.disabledKeys[b]==true){a.preventDefault();a.stopPropagation()}}if(document.addEventListener){document.addEventListener("keydown",kd,false);document.addEventListener("keyup",ku,false);document.addEventListener("keypress",kp,false)}else{document.attachEvent("onkeydown",kd);document.attachEvent("onkeyup",ku);document.attachEvent("onkeypress",kp)}kc.keyMappings[37]=wr.BUTTON_LEFT;kc.keyMappings[38]=wr.BUTTON_UP;kc.keyMappings[39]=wr.BUTTON_RIGHT;kc.keyMappings[40]=wr.BUTTON_DOWN;kc.keyMappings[65]=wr.BUTTON_1;kc.keyMappings[83]=wr.BUTTON_2;kc.keyMappings[68]=wr.BUTTON_A;kc.keyMappings[32]=wr.BUTTON_B;kc.keyMappings[187]=wr.BUTTON_PLUS;kc.keyMappings[189]=wr.BUTTON_MINUS;kc.buttonMappings[wr.BUTTON_LEFT]=37;kc.buttonMappings[wr.BUTTON_UP]=38;kc.buttonMappings[wr.BUTTON_RIGHT]=39;kc.buttonMappings[wr.BUTTON_DOWN]=40;kc.buttonMappings[wr.BUTTON_1]=65;kc.buttonMappings[wr.BUTTON_2]=83;kc.buttonMappings[wr.BUTTON_A]=68;kc.buttonMappings[wr.BUTTON_B]=32;kc.buttonMappings[wr.BUTTON_PLUS]=187;kc.buttonMappings[wr.BUTTON_MINUS]=189;kc.wiiMappings[178]=wr.BUTTON_LEFT;kc.wiiMappings[177]=wr.BUTTON_RIGHT;kc.wiiMappings[175]=wr.BUTTON_UP;kc.wiiMappings[176]=wr.BUTTON_DOWN;kc.wiiMappings[13]=wr.BUTTON_A;kc.wiiMappings[171]=wr.BUTTON_B;kc.wiiMappings[172]=wr.BUTTON_1;kc.wiiMappings[173]=wr.BUTTON_2;kc.wiiMappings[174]=wr.BUTTON_PLUS;kc.wiiMappings[170]=wr.BUTTON_MINUS;delete kc;delete wr;delete kd;delete ku;delete kp;var Browser={disabledKeys:{},isDefaultEnabled:function(a){return(this.disabledKeys[a]==false)},setDefaultEnabled:function(b,a){this.disabledKeys[b]=!a},enableZooming:function(){this.setDefaultEnabled(WiiRemote.BUTTON_PLUS,true);this.setDefaultEnabled(WiiRemote.BUTTON_MINUS,true)},disableZooming:function(){this.setDefaultEnabled(WiiRemote.BUTTON_PLUS,false);this.setDefaultEnabled(WiiRemote.BUTTON_MINUS,false)},enableScrolling:function(){this.setDefaultEnabled(WiiRemote.BUTTON_LEFT,true);this.setDefaultEnabled(WiiRemote.BUTTON_UP,true);this.setDefaultEnabled(WiiRemote.BUTTON_RIGHT,true);this.setDefaultEnabled(WiiRemote.BUTTON_DOWN,true);this.setDefaultEnabled(WiiRemote.BUTTON_B,true)},disableScrolling:function(){this.setDefaultEnabled(WiiRemote.BUTTON_LEFT,false);this.setDefaultEnabled(WiiRemote.BUTTON_UP,false);this.setDefaultEnabled(WiiRemote.BUTTON_RIGHT,false);this.setDefaultEnabled(WiiRemote.BUTTON_DOWN,false);this.setDefaultEnabled(WiiRemote.BUTTON_B,false)}};