(function () {
if( window.opera && opera.wiiremote ) { return; }
var rotation = 0, distance = 20, toggle = false, pageX = 0, pageY = 0, midScreenX = -1, midScreenY = -1, validdata = 2, canv, ctx, mousewheelonce, lastkey;
var reversedwheel = window.opera && opera.version && parseFloat( opera.version() ) < 9.2;
var whatwidth = ( document.compatMode != 'CSS1Compat' || ( window.opera && opera.version && parseFloat( opera.version() ) < 9.5 ) ) ? 'body' : 'documentElement';
var brokenmouseover = window.opera && opera.version;
var opacitycrash = !window.devicePixelRatio;
function swapEvents(e) {
var newCode, evObj, curCode = e.keyCode;
if( e.type == 'keydown' ) { lastkey = curCode;
} else if( e.type == 'keypress' && !curCode ) { curCode = lastkey; }
if( curCode == 77 || curCode == 109 ) { newCode = 170;
} else if( curCode == 66 || curCode == 98 ) { newCode = 171;
} else if( curCode == 49 ) { newCode = 172;
} else if( curCode == 50 ) { newCode = 173;
} else if( curCode == 80 || curCode == 112 ) { newCode = 174;
} else if( curCode == 38 ) { newCode = 175;
} else if( curCode == 40 ) { newCode = 176;
} else if( curCode == 39 ) { newCode = 177;
} else if( curCode == 37 ) { newCode = 178;
} else if( e.type == 'keyup' && curCode == 84 ) {
e.stopPropagation();
toggle = !toggle;
updateCanv();
return;
} else { return; }
e.stopPropagation();
if( window.KeyEvent ) {
evObj = document.createEvent('KeyEvents');
evObj.initKeyEvent( e.type, e.bubbles, e.cancelable, window, false, false, false, false, newCode, 0 );
} else {
evObj = document.createEvent('UIEvents');
evObj.initUIEvent( e.type, e.bubbles, e.cancelable, window, e.detail );
evObj.keyCode = newCode;
}
if( !e.target.dispatchEvent(evObj) ) {
e.preventDefault();
}
}
function mousescroll(e) {
var wheeldir = e.wheelDelta;
if( !mousewheelonce ) {
if( e.type == 'mousewheel' ) {
window.removeEventListener('DOMMouseScroll',arguments.callee,false);
} else {
window.removeEventListener('mousewheel',arguments.callee,false);
}
mousewheelonce = true;
}
if( !wheeldir ) { wheeldir = -1 * e.detail;
} else if( reversedwheel ) { wheeldir = -1 * wheeldir; }
if( !wheeldir ) { return; }
if( toggle ) {
distance += ( wheeldir < 0 ) ? 1 : -1;
if( distance < 0 ) { distance = 0;
} else if( distance > 25 ) { distance = 25; }
} else {
rotation += ( wheeldir > 0 ) ? 1 : -1;
if( rotation < -39 ) { rotation = 40;
} else if( rotation > 40 ) { rotation = -39; }
}
e.preventDefault();
e.stopPropagation();
updateCanv();
}
function updateCanv() {
if( !ctx ) { return; }
var oHeight = ( ( 25 - distance ) / 25 ) * 40;
var comproll = ( rotation / 40 ) * Math.PI;
ctx.clearRect(0,0,40,40);
if( !toggle ) {
ctx.fillStyle = '#aaf';
ctx.fillRect( 0, 40 - oHeight, 40, oHeight );
}
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo( 20, 0 );
ctx.lineTo( 20, 40 );
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo( 0, 20 );
ctx.lineTo( 40, 20 );
ctx.closePath();
ctx.stroke();
ctx.fillStyle = toggle ? '#7d7' : '#070';
ctx.beginPath();
ctx.arc( 20, 20, 20, rotation/(4*Math.PI), Math.PI + ( rotation/(4*Math.PI) ), false );
ctx.closePath();
ctx.fill();
ctx.fillStyle = ctx.strokeStyle = toggle ? '#bbb' : 'black';
ctx.beginPath();
ctx.moveTo( 20 - Math.cos(comproll), 20 - Math.sin(comproll) );
ctx.lineTo( 20 + 20 * Math.sin(comproll), 20 - 20 * Math.cos(comproll) );
ctx.lineTo( 20 + Math.cos(comproll), 20 + Math.sin(comproll) );
ctx.closePath();
ctx.fill();
ctx.stroke();
if( toggle ) {
ctx.fillStyle = 'rgba(0,0,255,0.6)';
ctx.fillRect( 0, 40 - oHeight, 40, oHeight );
}
}
function prepcanvas() {
window.removeEventListener('DOMContentLoaded',arguments.callee,false);
window.removeEventListener('load',arguments.callee,false);
canv = document.createElement('canvas');
canv.setAttribute('height','40');
canv.setAttribute('width','40');
document.documentElement.appendChild(canv);
canv.sidestate = true;
canv.cssTextRight = 'position: fixed; top: 0; right: 0; z-index: 999999; opacity: 0.4; '+(opacitycrash?'-khtml-opacity: 1; ':'')+'border: solid #000; border-width: 0 0 1px 1px';
canv.cssTextLeft = 'position: fixed; top: 0; left: 0; z-index: 999999; opacity: 0.4; '+(opacitycrash?'-khtml-opacity: 1; ':'')+'border: solid #000; border-width: 0 1px 1px 0';
canv.style.cssText = canv.cssTextRight;
canv.onclick = function () {
this.sidestate = !this.sidestate;
this.style.cssText = this.sidestate ? this.cssTextRight : this.cssTextLeft;
};
if( canv.getContext ) { ctx = canv.getContext('2d'); }
updateCanv();
}
if( !window.opera ) {
window.opera = {};
}
opera.wiiremote = {};
opera.wiiremote.update = function (n) {
if( parseInt(n) != n || isNaN(n) || n < 0 || n > 3 ) { return {}; }
if( n ) { return { isEnabled: 0 }; }
var datacopy = { isEnabled: 1, isDataValid: 1, isBrowsing: 1, dpdScreenX: pageX, dpdScreenY: pageY, dpdX: midScreenX,
dpdY: midScreenY, hold: 0, dpdRollX: null, dpdRollY: null, dpdDistance: null, dpdValidity: validdata };
var comproll = ( rotation / 40 ) * Math.PI;
datacopy.dpdRollX = Math.cos(comproll);
datacopy.dpdRollY = Math.sin(comproll);
datacopy.dpdDistance = 0.55 + ( ( 3 - 0.55 ) * ( distance / 25 ) );
if( !validdata ) {
delete datacopy.dpdScreenX;
delete datacopy.dpdScreenY;
}
if( Math.abs( datacopy.dpdRollX ) < 0.0001 ) { datacopy.dpdRollX = 0;
} else if( Math.abs( datacopy.dpdRollY ) < 0.0001 ) { datacopy.dpdRollY = 0; }
return datacopy;
};
if( window.addEventListener ) {
window.addEventListener('DOMContentLoaded',prepcanvas,false);
window.addEventListener('load',prepcanvas,false);
window.addEventListener('keydown',swapEvents,true);
window.addEventListener('keyup',swapEvents,true);
window.addEventListener('keypress',swapEvents,true);
window.addEventListener('mousewheel',mousescroll,true);
window.addEventListener('DOMMouseScroll',mousescroll,true);
window.addEventListener('mousemove',function (e) {
var oWidth = window.innerWidth, oHeight = window.innerHeight;
pageX = e.pageX;
pageY = e.pageY;
midScreenX = ( pageX - ( oWidth / 2 ) ) / ( oWidth / 2 );
midScreenY = ( pageY - ( oHeight / 2 ) ) / ( oHeight / 2 );
},true);
document.addEventListener('mouseout',function () { validdata = 0; },false);
document.addEventListener('mouseover',function (e) {
if( brokenmouseover && e.offsetX == 0 && e.offsetY == 0 && document.body && e.target == document.body ) {
if( e.clientX < 1 || e.clientX >= document[whatwidth].clientWidth || e.clientY < 1 || e.clientY >= document[whatwidth].clientHeight ) { return; }
}
validdata = 2;
},false);
}
})();