var _CHARS = {'65':'a','66':'b','67':'c','68':'d','69':'e','70':'f','71':'g','72':'h','73':'i','74':'j','75':'k','76':'l','77':'m','78':'n','79':'o','80':'p','81':'q','82':'r','83':'s','84':'t','85':'u','86':'v','87':'w','88':'x','89':'y','90':'z','96':'`','192':'`','222':'\''};
var _KANAS = {'a':'a','ba':'ba','be':'be','bi':'bi','bo':'bo','bu':'bu','bya':'bya','byo':'byo','byu':'byu','cha':'cha','chi':'chi','cho':'cho','chu':'chu','da':'da','de':'de','do':'do','e':'e','fe':'fe','fu':'fu','ga':'ga','ge':'ge','gi':'gi','go':'go','gu':'gu','gya':'gya','gyo':'gyo','gyu':'gyu','ha':'ha','he':'he','hi':'hi','ho':'ho','hu':'fu','hya':'hya','hyo':'hyo','hyu':'hyu','i':'i','ja':'ja','ji':'ji','jo':'jo','ju':'ju','ka':'ka','ke':'ke','ki':'ki','ko':'ko','ku':'ku','kya':'kya','kyo':'kyo','kyu':'kyu','ma':'ma','me':'me','mi':'mi','mo':'mo','mu':'mu','mya':'mya','myo':'myo','myu':'myu','n':'n','na':'na','ne':'ne','ni':'ni','no':'no','nu':'nu','nya':'nya','nyo':'nyo','nyu':'nyu','o':'o','pa':'pa','pe':'pe','pi':'pi','po':'po','pu':'pu','pya':'pya','pyo':'pyo','pyu':'pyu','ra':'ra','re':'re','ri':'ri','ro':'ro','ru':'ru','rya':'rya','ryo':'ryo','ryu':'ryu','sa':'sa','se':'se','sha':'sha','shi':'shi','sho':'sho','shu':'shu','si':'shi','so':'so','su':'su','sya':'sha','syo':'sho','syu':'shu','ta':'ta','te':'te','ti':'chi','to':'to','tsu':'tsu','tu':'tsu','tya':'cha','tyo':'cho','tyu':'chu','u':'u','wa':'wa','wo':'wo','ya':'ya','yo':'yo','yu':'yu','za':'za','ze':'ze','zi':'ji','zo':'zo','zu':'zu','zya':'ja','zyu':'ju','zyo':'jo'};

var _t = { };

function __ans() {
	var _a, _i, _z;

	if ( _t.x[ 'pp' ] ) {
		_t.typing = false;
		_z = _t.chars;
		_t.hiraganaE = _t.answerE;
		}
	else
		_t.a = this._answer;

	if ( _t.clear )
		__cle( _t.hiraganaE );

	_t.chars = '';

	for ( _i = 0; _i < _t.a.length; _i++ ) {
		_a = _t.a[ _i ].replace( /(nn[aiueo])/, "n$1");

		for ( _j = 0; _j < _a.length; _j++ ) {
			__typ( _a.charAt( _j ) );
			}

		if ( _a.match( /n$/ ) )
			__typ( 'n' );

		if ( _i < ( _t.a.length - 1 ) ) {
			__dis( ',', 'comma', '(comma)' );
			}
		}

	if ( _t.x[ 'pp' ] ) {
		_t.a = _t.a.join( '-' );

		_t.hiraganaE = _t.guessE;
		_t.chars = _z;
		_t.typing = true;
		}
	}

function __cle( _e ) {
	while ( _e.lastChild ) {
		__pur( _e.lastChild );
		_e.removeChild( _e.lastChild );
		}
	}

function __dis( _char, _src, _alt, _chars_new ) {
	var _e;

	if ( ( ! _t.typing ) || ( _t.width < 500 ) ) {
		_e = document.createElement( 'img' );

		_e.alt = _alt ? _alt : _char;
		_e.height = 20;
		_e.src = '/i/h/1/' + ( _src ? _src : _char ) + '.gif';
		_e.width = __dou( _char ) ? 38 : 20;

		_t.hiraganaE.appendChild( _e );

		_t.chars = _chars_new ? _chars_new : '';

		if ( _t.typing ) {
			_t.g[ _t.g.length ] = _char;
			_t.width += _e.width;
			}
		}
	else
		_t.chars = '';

	}

function __dou( _char ) {
	return ( _char.match( /(.y.)|((j|sh|ch)[aou])|(fe)/ ) );
	}

function __ena( _a, _e ) {
	_a.onclick = _e ? __pag : function() { return false; };
	_a.style.color = _e ? '#2f5aae' : '#b6cbf3';
	_a.style.textDecoration = _e ? 'underline' : 'none';
	}

function __hid() {
	_t.hidden = false;
	__met();
	}

function __inp( _name, _value ) {
	var _e = document.createElement( 'input' );

	_e.name = _name;
	_e.type = 'hidden';
	_e.value = _value;

	_t.formE.appendChild( _e );
	}

function __lin() {
	if ( this.href.match( /^https?:\/\/(\w+\.)?realkanji/ ) ) {
		__sst();

		if ( this._practice ) {
			if ( this._char )
				__inp( 'c', this._char );

			__inp( 'i', _t.i );
			__inp( 'r', _t.r );
			__inp( 't', _t.t );
			__inp( 'w', _t.w );
			}
		else if ( this._subset )
			__inp( 'ss', this._subset );

		_t.formE.action = this.href;
		_t.formE.submit();

		return false;
		}
	else {
		if ( ! this.href.match( /^mailto/ ) )
			urchinTracker( this.href.replace( /^https?:\/\/(.*)$/, "$1" ) );
		return true;
		}
	}

function __met() {
	_t.answerE.style.visibility = _t.scoreE.style.visibility = ( _t.hidden ? 'visible' : 'hidden' );

	_t.hidden = ! _t.hidden;
	}

function __onk( _event ) {
	var _key;

	_event = _event ? _event : event;
	_key = _event.keyCode;

	if ( _key == 13 )
		__pra();
	else if ( _key == 32 )
		__met();
	else if ( _CHARS[ _key ] )
		__typ( _CHARS[ _key ] );
	}

function __onl( _o ) {
	var _eA, _subset;

	_uacct = 'UA-277145-8';

	_t = _o;
	if ( ! _t.x )
		_t.x = { };
	_t.formE = document.forms[ 0 ];

	_eA = document.getElementsByTagName( 'input' );

	for ( _i = 0; _i < _eA.length; _i++ )
		_eA[ _i ].onclick = __set;

	if ( _t.x[ 'pp' ] ) {
		_t.againE = document.getElementById( 'again' ).firstChild;
		_t.answerE = document.getElementById( 'answer' );
		_t.guessE = _t.hiraganaE = document.getElementById( 'guess' );
		_t.questionE = document.getElementById( 'question' );
		_t.scoreE = document.getElementById( 'score' );
		_t.summaryE = document.getElementById( 'summary' );

		_eA = document.getElementsByTagName( 'span' );
		_t.rightE = _eA[ 1 ];
		_t.wrongE = _eA[ 2 ];

		document.onkeydown = __onk;

		_t.busy = _t.clear = false;
		_t.chars = '';
		_t.hidden = true;
		_t.width = 0;

		__que();

		if ( _t.g ) {
			_t.typing = false;

			_t.g = _t.g.replace( /n(n|$)/, "nn$1" );

			for ( _i = 0; _i < _t.g.length; _i++ )
				__typ( _t.g.charAt( _i ) );

			_t.againE.style.visibility = 'visible';
			_t.clear = true;
			}

		_t.g = [ ];
		_t.typing = true;

		__ans();
		_t.rightE.innerHTML = _t.r;
		_t.wrongE.innerHTML = _t.w;
		}
	else if ( _t.x[ 'ssp' ] ) {
		_t.nextE = document.getElementById( 'paging' ).lastChild;
		_t.previousE = document.getElementById( 'paging' ).firstChild;
		_t.setListE = document.getElementById( 'set-list' );

		_t.nextE._next = true;

		__ena( _t.nextE, _t.n > 1 );
		__ena( _t.previousE, _t.n && ( _t.n < 3 ) );
		}
	else if ( _t.x[ 'wsp' ] ) {
		_t.hiraganaE = document.getElementById( 'answer' );
		_t.columnsE = document.getElementById( 'columns' ).firstChild.firstChild;
		_t.nextE = document.getElementById( 'paging' ).lastChild;
		_t.previousE = document.getElementById( 'paging' ).firstChild;
		_t.summaryE = document.getElementById( 'summary' );

		_t.nextE._next = true;

		_t.clear = _t.typing = false;

		__ena( _t.nextE, _t.n > 1 );
		__ena( _t.previousE, _t.n && ( _t.n < 3 ) );

		__wor();
		}

	_eA = document.getElementsByTagName( 'a' );

	if ( _t.x[ 'pt' ] ) {
		for ( _i = 0; _i < _eA.length; _i++ ) {
			if ( _eA[ _i ].href.match( /\/practice\// ) )
				_eA[ _i ]._practice = true;
			if ( ! _eA[ _i ].onclick )
				_eA[ _i ].onclick = __lin;
			}
		}
	else if ( _t.x[ 'sp' ] ) {
		_subset = 1;

		for ( _i = 0; _i < _eA.length; _i++ ) {
			if ( _eA[ _i ].href == document.location.href )
				_eA[ _i ]._subset = _subset++;
			if ( ! _eA[ _i ].onclick )
				_eA[ _i ].onclick = __lin;
			}
		}
	else
		for ( _i = 0; _i < _eA.length; _i++ )
			if ( ! _eA[ _i ].onclick )
				_eA[ _i ].onclick = __lin;

		urchinTracker( _t.x[ 'wsp' ] ? ( _t.c ? '/usage/' : '/words/' ) : document.location.pathname );
	}

function __pag() {
	var _i, _o, _r, _u;

	if ( ! _t.busy ) {
		_t.busy = true;

		_r = __req();

		if ( _r ) {
			if ( _t.c )
				_u = '/practice/json/?c=' + _t.c + '&i=' + _t.i + '&n=' + ( this._next ? '1' : '0' ) + '&p=' + _t.p + '&r=' + _t.r + '&s=' + _t.s + '&sss=' + _t.sss + '&t=' + _t.t + '&ts=' + _t.ts + '&w=' + _t.w;
			else if ( _t.sqo >= 0 )
				_u = '/kanji/json/?n=' + ( this._next ? '1' : '0' ) + '&s=' + _t.s + ( _t.sq ? ( '&sq=' + _t.sq ) : '' ) + '&sqo=' + _t.sqo + '&sss=' + _t.sss + '&ts=' + _t.ts;
			else
				_u = document.location.pathname + 'json/?n=' + ( this._next ? '1' : '0' ) + '&p=' + _t.p + '&s=' + _t.s + '&ss=' + _t.ss + '&sss=' + _t.sss + '&ts=' + _t.ts;

			_r.open( 'GET', _u, true );

			_r.onreadystatechange = function() {
				if ( ( _r.readyState == 4 ) && ( _r.status == 200 ) ) {
					_t.clear = true;

					_o = eval( '(' + _r.responseText + ')' );

					for ( _i in _o )
						_t[ _i ] = _o[ _i ];

					__ena( _t.nextE, _t.n > 1 );
					__ena( _t.previousE, _t.n && ( _t.n < 3 ) );

					if ( _t.p )
						__wor();
					else
						_t.setListE.innerHTML = _t.content;

					_t.busy = false;

					urchinTracker( _t.c ? '/usage/json/' : ( _t.sqo ? '/kanji/json/' : '/words/json/' ) );
					}
				};

			_r.send( null );
			}
		else {
			__sst();

			__inp( 'n', this._next ? '1' : '0' );

			if ( _t.c ) {
				__inp( 'c', _t.c );
				__inp( 'i', _t.i );
				__inp( 'p', _t.p );
				__inp( 'r', _t.r );
				__inp( 't', _t.t );
				__inp( 'w', _t.w );
				}
			else if ( _t.sqo >= 0 ) {
				if ( _t.sq )
					__inp( 'sq', _t.sq );
				__inp( 'sqo', _t.sqo );
				}
			else {
				__inp( 'p', _t.p );
				__inp( 'ss', _t.ss );
				}

			_t.formE.action = this.href;
			_t.formE.submit();
			}
		}

	return false;
	}

function __pra() {
	var _i, _o, _r;

	if ( ! _t.busy ) {
		_t.busy = true;

		if ( _t.chars == 'n' )
			__dis( 'n' );

		_t.againE.style.visibility = 'hidden';
		__hid();

		_t.g = _t.g.join( '-' );
		_t.g = _t.g.replace( /n-( -)?([aiueo])/g, "n'-$1$2" );
		_t.g = _t.g.replace( /-/g, '' );
		_t.g = _t.g.replace( /n( )?([bmp])/g, "m$1$2" );

		_r = __req();

		if ( _r ) {
            _r.open( 'GET', '/practice/json/?a=' + _t.a + '&g=' + _t.g + '&q=' + _t.q + '&r=' + _t.r + '&s=' + _t.s + '&sss=' + _t.sss + '&t=' + _t.t + '&ts=' + _t.ts + '&w=' + _t.w, true );
			_r.onreadystatechange = function() {
				if ( ( _r.readyState == 4 ) && ( _r.status == 200 ) ) {
					_t.chars = '';
					_t.clear = true;
					_t.g = [ ];
					_t.width = 0;

					_o = eval( '(' + _r.responseText + ')' );

					for ( _i in _o )
						_t[ _i ] = _o[ _i ];

					if ( _o.w ) {
						_t.againE.style.visibility = 'visible';
						_t.wrongE.innerHTML = _t.w;
						}
					else {
						__que();
						__cle( _t.guessE );
						__ans();
						_t.rightE.innerHTML = _t.r;
						}

					if ( _t.r )
						_t.summaryE.style.visibility = 'hidden';

					_t.busy = false;

					urchinTracker( '/practice/json/' );
					}
				};

			_r.send( null );
			}
		else {
			__sst();

			__inp( 'a', _t.a );
			__inp( 'g', _t.g );
			__inp( 'i', _t.i );
			__inp( 'q', _t.q );
			__inp( 'r', _t.r );
			__inp( 't', _t.t );
			__inp( 'w', _t.w );

			_t.formE.action = '/practice/';
			_t.formE.submit();
			}
		}
	}

function __pur( _e ) {
	var _a, _i, _l, _n;

	_a  = _e.attributes;

	if ( _a ) {
		_l = _a.length;
		for ( _i = 0; _i < _l; _i += 1 ) {
			_n = _a[ _i ].name;
			if ( typeof _e[ _n ] === 'function' ) {
				_e[ _n ] = null;
				}
			}
		}

	_a = _e.childNodes;

	if ( _a ) {
		_l = _a.length;
		for ( _i = 0; _i < _l; _i += 1 ) {
			__pur( _e.childNodes[ _i ] );
			}
		}
	}

function __que() {
	var _a, _i, _j;

	if ( _t.clear )
		__cle( _t.questionE );

	for ( _j = 0; _j < _t.q.length; _j++ ) {
		var _i = document.createElement( 'img' );

		_i.height = _i.width = 36;
		_i.onmouseover = __sho;
		_i.onmouseout = __hid;
		_i.src = '/i/k/' + _t.t + '/' + _t.q[ _j ] + '.gif';

		if ( _t.q[ _j ].charAt( 0 ) == '3' )
			_t.questionE.appendChild( _i );
		else {
			var _a = document.createElement( 'a' );

			_a.href = document.location.protocol + '//' + document.location.hostname + '/practice/';
			_a.onclick = __lin;
			_a._char = _t.q[ _j ];
			_a._practice = true;

			_a.appendChild( _i );
			_t.questionE.appendChild( _a )
			}
		}

	_t.q = _t.q.join( '-' );
	}

function __req() {
	var _r = false;

	if ( window.XMLHttpRequest )
		_r = new XMLHttpRequest();
	else if ( window.ActiveXObject ) {
		try {
			_r = new ActiveXObject( 'Msxml2.XMLHTTP' );
			}
		catch ( e ) {
			try {
				_r = new ActiveXObject( 'Microsoft.XMLHTTP' );
				}
			catch ( e ) {
				}
			}
		}

	return _r;
	}

function __set( _o ) {
	var _b, _c, _eA, _i, _temp;

	_b = ( this.type == 'button' );

	if ( _b )
		_c = ( this.value == 'Check All' );

	_eA = document.getElementsByTagName( 'input' );
	_temp = 0;

	for ( _i = 0; _i < _eA.length; _i++ )
		if ( _eA[ _i ].type == 'checkbox' ) {
			if ( _b )
				_eA[ _i ].checked = _c;
			if ( _eA[ _i ].checked )
				_temp += Math.pow( 2, _i );
			}

	if ( _t.x[ 'sp' ] )
		_t.sss = _temp;
	else
		_t.ts = _temp;
	}

function __sho() {
	_t.hidden = true;
	__met();
	}

function __sst() {
	__inp( 's', _t.s );
	__inp( 'sss', _t.sss );
	__inp( 'ts', _t.ts );
	}

function __typ( _char ) {
	var _e, _first;

	if ( _t.clear && _t.typing ) {
		_t.againE.style.visibility = 'hidden';
		__cle( _t.hiraganaE );
		_t.clear = false;
		}

	if ( _char == ' ' ) {
		if ( _t.chars.charAt( _t.chars.length - 1 ) == 'n' )
			__dis( 'n' );
		if ( ( ! _t.typing ) || ( _t.g.length && ( _t.g[ _t.g.length - 1 ] != ' ' ) ) )
			__dis( ' ', 'space', '(space)' );
		}
	else if ( _char == '`' ) {
		if ( _t.chars )
			_t.chars = '';
		else if ( _e = _t.hiraganaE.lastChild ) {
			_t.width -= _e.width;
			_t.g.pop();
			_t.hiraganaE.removeChild( _e );
			}
		}
	else {
		_t.chars += _char;

		if ( _char.match( /[aiueo]/ ) ) {
			if ( _KANAS[ _t.chars ] )
				__dis( _KANAS[ _t.chars ] );
			else {
				alert( '"' + _t.chars + '" is not recognized.' );
				_t.chars = '';
				}
			}
		else if ( _t.chars.match( /n[n']/ ) ) {
			__dis( 'n' );
			}
		else if ( ( _t.chars.length == 2 ) && ( ( _char == ( _first = _t.chars.charAt( 0 ) ) ) || ( _first.match( /[mn]/ ) && ( _char != 'y' ) ) || ( _t.chars == 'cc' ) || ( _t.chars == 'tc' ) ) ) {
			if ( _first == 'm' )
				_t.chars[ 0 ] = _first = 'n';
			else if ( _first == 'c' )
				_t.chars[ 0 ] = _first = 't';
			__dis( _first, ( _first == 'n' ) ? 'n' : 'xtsu', '(small tsu)', _t.chars.substring( 1 ) );
			}
		}
	}

function __wor() {
	var _columnE, _i, _j;

	if ( _t.clear )
		__cle( _t.columnsE );

	if ( _t.c )
		_t.summaryE.innerHTML = 'Words ' + _t.pws + '&#8211;' + _t.pwe + ' of ' + _t.cwc;
	else
		_t.summaryE.innerHTML = '<strong>' + _t.ssn + '</strong> &#8212; Words ' + _t.pws + '&#8211;' + _t.pwe + ' of ' + _t.sswc;

	for ( _i = 0; _i < _t.ws.length; _i++ ) {
		if ( ! ( _i % 6 ) ) {
			var _e = document.createElement( 'td' );
				if ( _i == 0 )
					_e.style.paddingLeft = 0;
			_t.columnsE.appendChild( _e );
			_columnE = _e;
			}

		var _e = document.createElement( 'div' );
		_e.onmouseout = function() { __cle( _t.hiraganaE ); };
		_e.onmouseover = __ans;
		_e.style.margin = _t.c ? '4px 0' : '6px 0';
		_e._answer = _t.ws[ _i ][ 1 ];

		_columnE.appendChild( _e );

		for ( _j = 0; _j < _t.ws[ _i ][ 0 ].length; _j++ ) {
			var _e = document.createElement( 'img' );

			_e.height = _e.width = 36;
			_e.src = '/i/k/' + _t.t + '/' + _t.ws[ _i ][ 0 ][ _j ] + '.gif';

			if ( _t.c && ( _t.ws[ _i ][ 0 ][ _j ] == _t.c ) )
				_e.style.borderColor = '#ddd';

			_columnE.lastChild.appendChild( _e );
			}
		}
	}
