/*
  Patch Piano written by Diabl@ (diabl@nightmarebeforechristmas.net)

  Though this I could have made this cross-browser, using <embed>s,
  I decided to stick with <bgsound>, because it's much less laggy, 
  and much more dynamic... Meaning this only works in Internet Explorer.

  You may use this for personal purposes, however, 
  you may NOT use it on another site without asking me first.
*/

var sx = screen.width / 2 - 250;		// where to show the piano image
if (screen.width < 1024) sx = 1024 / 2 - 264;
var sy = 240;

document.write('<div style="position:absolute; left:'+sx+'px; top:'+sy+'px"><img src="/images/games/piano/piano.gif" alt="" /></div>');

if (is_ie) {

var seq = new Array();				// array of notes
var syl = new Array();				// array of syllables
var sylS = new Array();				// starting note of syllable sequences

var nx = sx;
var nx2 = sx + 16;
var kP = 0;
var notes = new Array();			// array of MIDI notes
var divs = new Array();				// array of DIV keys
var z = 0;
var chosen = false;
var n;

document.write('<bgsound src="/midi/0/sound.jpg" id="playmusic" loop="1" />');

for (x = 48; x <= 84; x++)
	{
	notes[x] = new Image();
	notes[x].src = '/midi/'+x+'/sound.jpg';
	}

var keys = new Array();				// IE keycodes of the piano keys
keys[48] = 90;
keys[49] = 83;
keys[50] = 88;
keys[51] = 68;
keys[52] = 67;
keys[53] = 86;
keys[54] = 71;
keys[55] = 66;
keys[56] = 72;
keys[57] = 78;
keys[58] = 74;
keys[59] = 77;
keys[60] = 188;
keys[61] = 76;
keys[62] = 190;
keys[63] = 186;
keys[64] = 191;
keys[65] = 81;
keys[66] = 50;
keys[67] = 87;
keys[68] = 51;
keys[69] = 69;
keys[70] = 52;
keys[71] = 82;
keys[72] = 84;
keys[73] = 54;
keys[74] = 89;
keys[75] = 55;
keys[76] = 85;
keys[77] = 73;
keys[78] = 57;
keys[79] = 79;
keys[80] = 48;
keys[81] = 80;
keys[82] = 189;
keys[83] = 219;
keys[84] = 221;

for (x = 3; x < 6; x++)
	{
	for (y = 0; y < 7; y++)
		{
		if (y == 0) note = 'c';
		if (y == 1) note = 'd';
		if (y == 2) note = 'e';
		if (y == 3) note = 'f';
		if (y == 4) note = 'g';
		if (y == 5) note = 'a';
		if (y == 6) note = 'b';
		document.write('<div class="pkey" id="'+note+x+'" style="position:absolute; left:'+nx+'px; top:'+sy+'px"><img src="/images/games/piano/'+note+x+'.gif" alt="" /></div>');
		divs.push(note+x);
		if (y != 3 && y != 0) 
			{
			document.write('<div class="pkey" id="'+note+'b'+x+'" style="position:absolute; left:'+nx2+'px; top:'+sy+'px"><img src="/images/games/piano/'+note+'b'+x+'.gif" alt="" /></div>');
			divs.push(note+'b'+x);
			}
		nx += 23;
		if (x > 3 || y > 0) nx2 += 23;
		}
	}
document.write('<div class="pkey" id="c6" style="position:absolute; left:'+nx+'px; top:'+sy+'px"><img src="/images/games/piano/c6.gif" alt="" /></div>');

function SelectSong(id)				// selecting a song
	{
	if (chosen) LightDownKeys();
	else chosen = true;
	document.getElementById('songsjs').src = '/games/piano/'+id;
	}

function StartOver()				// starting over
	{
	z = 0;
	s = 0;
	LightUpKeys();
	}

function NoteConvert(note)			// converting MIDI code to note name
	{
	i = note % 12;
	octave = (note - i - 12) / 12;
	if (i == 0) n = 'c';
	if (i == 1) n = 'db';
	if (i == 2) n = 'd';
	if (i == 3) n = 'eb';
	if (i == 4) n = 'e';
	if (i == 5) n = 'f';
	if (i == 6) n = 'gb';
	if (i == 7) n = 'g';
	if (i == 8) n = 'ab';
	if (i == 9) n = 'a';
	if (i == 10) n = 'bb';
	if (i == 11) n = 'b';
	return n+octave;
	}

function LightUpKeys()				// lighting up the next key
	{
	if (z < seq.length) document.getElementById(NoteConvert(seq[z])).style.display = 'block';
	if (sylS[s] == z || (sylS[s] < z && sylS[s] + syl[s].length > z))
		{
		var lyrics = '';
		for (a = 0; a < syl[s].length; a++) 
			{
			if (a == z - sylS[s]) lyrics += '<span class="emph">'+syl[s][a]+'</span>';
			else lyrics += syl[s][a]; 
			}
		document.getElementById('lyrics').innerHTML = '&raquo; '+lyrics+' &laquo;';
		if (z == sylS[s] + syl[s].length - 1) s++;
		}
	else document.getElementById('lyrics').innerHTML = '';
	}

function LightDownKeys()			// lighting down the current key
	{
	if (z == seq.length - 1) chosen = false;
	document.getElementById(NoteConvert(seq[z])).style.display = 'none';
	}

function CheckKeys()				// checking if pressed key was correct, then lighting up and down
	{
	if (kP == keys[seq[z]]) 
		{ 
		document.all.playmusic.src = notes[seq[z]].src;
		UnregisterKey(keys[seq[z]]); 
		LightDownKeys(); 
		z++; 
		window.setTimeout('LightUpKeys()', 10); }
	}

function RegisterKey(e)				// registering pressed key
	{
	if (e) { var keyCode = e.keyCode; }
	else { var keyCode = window.event.keyCode; }
	kP = keyCode;
	CheckKeys();
	}

function UnregisterKey(e)			// unregistering key
	{
	if (e) { var keyCode = e.keyCode; }
	else { var keyCode = window.event.keyCode; }
	kP = 0;
	}

document.onkeydown = RegisterKey;
document.onKeyUp = UnregisterKey;

}

