
var AC_TAB = 9;
var AC_UP_ARROW = 38;    // move the drop down list selection up by one
var AC_DOWN_ARROW = 40;  // move the drop down list selection down by one
var AC_ESC = 27;         // removed list of suggestions, no change to input or hilight
var AC_ENTER = 13;       // make the selection, end auto-insert, move cursor to end of text (eat the key)


var _IE6 = /msie|MSIE 6/.test(navigator.userAgent);

var _idx;

$(document).ready( function(){
 	if( typeof main == "function" ){
 		main();
 	}
});

function toggle_display( id ){
 	var itm = document.getElementById(id);
 	if( itm.style.display == 'none' ){
 		itm.style.display = 'inline';
 	}
 	else{
 		itm.style.display = 'none';
 	}
}

function new_word(){

	_idx = Math.floor( Math.random() * ( words.length ) );

	document.getElementById('span_english').innerHTML = words[_idx][0];
	document.getElementById("cheat").innerHTML = "\n<br />";
	
	if( words[_idx][2] == 1 ){
	  	//Bunkei
	  	document.getElementById('word_guess').style.display = 'none';
	  	document.getElementById('bunkei_guess').style.display = 'inline';
	}
	else{
	 	//Word, so display the guess box
	  	document.getElementById('word_guess').style.display = 'inline';
	  	document.getElementById('bunkei_guess').style.display = 'none';
	  	document.getElementById("guess").focus();
	}
}

function guess(){
 	var guess = sanitise( document.getElementById( 'guess_base' ).value );
 	//alert( "'" + guess + "' : '" + sanitise( words[_idx][1] ) );
 	if( guess == sanitise( words[_idx][1] ) ){
 	 	new_word();
	 	document.getElementById('span_result').innerHTML = '';
	 	document.getElementById( 'guess' ).value = '';
	 	document.getElementById( 'guess_base' ).value = '';
 	}
 	else{
	 	document.getElementById('span_result').innerHTML = 'Wrong';
	 	document.getElementById( 'guess_base' ).value = '';
	 	document.getElementById( 'guess' ).value = '';
 	}
 	
}


function toggle_div( div_name){
    if( $("#" + div_name ).get(0).offsetHeight > 0 ){
        $("#" + div_name ).animate({ height: 0 }, {duration: 1000, easeMethod: "easeout"});
        if(!_IE6){
            $("#" + div_name + "Container").animate({ height: 25 }, {duration: 1000, easeMethod: "easeout"});
        }
    }
    else{
        $("#" + div_name ).animate({ height: $("#" + div_name + "Contents").get(0).offsetHeight }, {duration: 1000, easeMethod: "easeout"});
        if(!_IE6){
            $("#" + div_name + "Container").animate({ height: $("#" + div_name + "Contents").get(0).offsetHeight + 25 }, {duration: 1000, easeMethod: "easeout"});
        }
    }
}

function sanitise( word ){
 	word = word.replace( /\./g, "" );
 	word = word.replace( /,/g, "" );
 	word = word.replace( /-/g, "" );
 	word = word.replace( / /g, "" );
 	word = word.replace( /\(/g, "" );
 	word = word.replace( /\)/g, "" );
 	return word.toLowerCase();
}

function roma2hira(s)
{
h = s.toLowerCase();
h=h.replace(/kk/g,"っk");
h=h.replace(/ss/g,"っs");
h=h.replace(/tt/g,"っt");
h=h.replace(/hh/g,"っh");
h=h.replace(/mm/g,"っm");
h=h.replace(/rr/g,"っr");
h=h.replace(/gg/g,"っg");
h=h.replace(/zz/g,"っz");
h=h.replace(/dd/g,"っd");
h=h.replace(/pp/g,"っp");
h=h.replace(/bb/g,"っb");
h=h.replace(/kya/g,"きゃ");
h=h.replace(/kyu/g,"きゅ");
h=h.replace(/kyo/g,"きょ");
h=h.replace(/sha/g,"しゃ");
h=h.replace(/shi/g,"し");
h=h.replace(/shu/g,"しゅ");
h=h.replace(/sho/g,"しょ");
h=h.replace(/cha/g,"ちゃ");
h=h.replace(/chu/g,"ちゅ");
h=h.replace(/cho/g,"ちょ");
h=h.replace(/nya/g,"にゃ");
h=h.replace(/nyu/g,"にゅ");
h=h.replace(/nyo/g,"にょ");
h=h.replace(/hya/g,"ひゃ");
h=h.replace(/hyu/g,"ひゅ");
h=h.replace(/hyo/g,"ひょ");
h=h.replace(/mya/g,"みゃ");
h=h.replace(/myu/g,"みゅ");
h=h.replace(/myo/g,"みょ");
h=h.replace(/rya/g,"りゃ");
h=h.replace(/ryu/g,"りゅ");
h=h.replace(/ryo/g,"りょ");
h=h.replace(/gya/g,"ぎゃ");
h=h.replace(/gyu/g,"ぎゅ");
h=h.replace(/gyo/g,"ぎょ");
h=h.replace(/bya/g,"びゃ");
h=h.replace(/byu/g,"びゅ");
h=h.replace(/byo/g,"びょ");
h=h.replace(/pya/g,"ぴゃ");
h=h.replace(/pyu/g,"ぴゅ");
h=h.replace(/pyo/g,"ぴょ");
h=h.replace(/xya/g,"ゃ");
h=h.replace(/xyu/g,"ゅ");
h=h.replace(/xyo/g,"ょ");
h=h.replace(/xyu/g,"ゅ");
h=h.replace(/xtu/g,"っ");
h=h.replace(/chi/g,"ち");
h=h.replace(/tsu/g,"つ");
h=h.replace(/dji/g,"ぢ");
h=h.replace(/dju/g,"づ");
h=h.replace(/ja/g,"じゃ");
h=h.replace(/ju/g,"じゅ");
h=h.replace(/jo/g,"じょ");
h=h.replace(/ka/g,"か");
h=h.replace(/ki/g,"き");
h=h.replace(/ku/g,"く");
h=h.replace(/ke/g,"け");
h=h.replace(/ko/g,"こ");
h=h.replace(/sa/g,"さ");
h=h.replace(/si/g,"し");
h=h.replace(/su/g,"す");
h=h.replace(/se/g,"せ");
h=h.replace(/so/g,"そ");
h=h.replace(/ta/g,"た");
h=h.replace(/ti/g,"ち");
h=h.replace(/tu/g,"つ");
h=h.replace(/te/g,"て");
h=h.replace(/to/g,"と");
h=h.replace(/na/g,"な");
h=h.replace(/ni/g,"に");
h=h.replace(/nu/g,"ぬ");
h=h.replace(/ne/g,"ね");
h=h.replace(/no/g,"の");
h=h.replace(/ha/g,"は");
h=h.replace(/hi/g,"ひ");
h=h.replace(/hu/g,"ふ");
h=h.replace(/fu/g,"ふ");
h=h.replace(/he/g,"へ");
h=h.replace(/ho/g,"ほ");
h=h.replace(/ma/g,"ま");
h=h.replace(/mi/g,"み");
h=h.replace(/mu/g,"む");
h=h.replace(/me/g,"め");
h=h.replace(/mo/g,"も");
h=h.replace(/ya/g,"や");
h=h.replace(/yu/g,"ゆ");
h=h.replace(/yo/g,"よ");
h=h.replace(/ra/g,"ら");
h=h.replace(/ri/g,"り");
h=h.replace(/ru/g,"る");
h=h.replace(/re/g,"れ");
h=h.replace(/ro/g,"ろ");
h=h.replace(/wa/g,"わ");
h=h.replace(/wo/g,"を");
h=h.replace(/ga/g,"が");
h=h.replace(/gi/g,"ぎ");
h=h.replace(/gu/g,"ぐ");
h=h.replace(/ge/g,"げ");
h=h.replace(/go/g,"ご");
h=h.replace(/za/g,"ざ");
h=h.replace(/ji/g,"じ");
h=h.replace(/zi/g,"じ");
h=h.replace(/zu/g,"ず");
h=h.replace(/ze/g,"ぜ");
h=h.replace(/zo/g,"ぞ");
h=h.replace(/da/g,"だ");
h=h.replace(/di/g,"ぢ");
h=h.replace(/du/g,"づ");
h=h.replace(/de/g,"で");
h=h.replace(/do/g,"ど");
h=h.replace(/ba/g,"ば");
h=h.replace(/bi/g,"び");
h=h.replace(/bu/g,"ぶ");
h=h.replace(/be/g,"べ");
h=h.replace(/bo/g,"ぼ");
h=h.replace(/pa/g,"ぱ");
h=h.replace(/pi/g,"ぴ");
h=h.replace(/pu/g,"ぷ");
h=h.replace(/pe/g,"ぺ");
h=h.replace(/po/g,"ぽ");
h=h.replace(/a/g,"あ");
h=h.replace(/i/g,"い");
h=h.replace(/u/g,"う");
h=h.replace(/e/g,"え");
h=h.replace(/o/g,"お");
h=h.replace(/n/g,"ん");
return h;
}


function roma2kata(s)
{
h = s.toLowerCase();
h=h.replace(/aa/g,"aー");
h=h.replace(/ee/g,"eー");
h=h.replace(/ii/g,"iー");
h=h.replace(/oo/g,"oー");
h=h.replace(/uu/g,"uー");
h=h.replace(/kk/g,"ッk");
h=h.replace(/ss/g,"ッs");
h=h.replace(/tt/g,"ッt");
h=h.replace(/hh/g,"ッh");
h=h.replace(/mm/g,"ッm");
h=h.replace(/rr/g,"ッr");
h=h.replace(/gg/g,"ッg");
h=h.replace(/zz/g,"ッz");
h=h.replace(/dd/g,"ッd");
h=h.replace(/pp/g,"ッp");
h=h.replace(/bb/g,"ッb");
h=h.replace(/kya/g,"キャ");
h=h.replace(/kyu/g,"キュ");
h=h.replace(/kyo/g,"キョ");
h=h.replace(/sha/g,"シャ");
h=h.replace(/shi/g,"シ");
h=h.replace(/shu/g,"シュ");
h=h.replace(/sho/g,"ショ");
h=h.replace(/cha/g,"チャ");
h=h.replace(/chu/g,"チュ");
h=h.replace(/cho/g,"チョ");
h=h.replace(/nya/g,"ニャ");
h=h.replace(/nyu/g,"ニュ");
h=h.replace(/nyo/g,"ニョ");
h=h.replace(/hya/g,"ヒャ");
h=h.replace(/hyu/g,"ヒュ");
h=h.replace(/hyo/g,"ヒョ");
h=h.replace(/mya/g,"ミャ");
h=h.replace(/myu/g,"ミュ");
h=h.replace(/myo/g,"ミョ");
h=h.replace(/rya/g,"リャ");
h=h.replace(/ryu/g,"リュ");
h=h.replace(/ryo/g,"リョ");
h=h.replace(/gya/g,"ギャ");
h=h.replace(/gyu/g,"ギュ");
h=h.replace(/gyo/g,"ギョ");
h=h.replace(/bya/g,"ビャ");
h=h.replace(/byu/g,"ビュ");
h=h.replace(/byo/g,"ビョ");
h=h.replace(/pya/g,"ピャ");
h=h.replace(/pyu/g,"ピュ");
h=h.replace(/pyo/g,"ピョ");
h=h.replace(/xya/g,"ャ");
h=h.replace(/xyu/g,"ュ");
h=h.replace(/xyo/g,"ョ");
h=h.replace(/xyu/g,"ュ");
h=h.replace(/xtu/g,"ッ");
h=h.replace(/chi/g,"チ");
h=h.replace(/tsu/g,"ツ");
h=h.replace(/dji/g,"ヂ");
h=h.replace(/dju/g,"ヅ");
h=h.replace(/ja/g,"ジャ");
h=h.replace(/ju/g,"ジュ");
h=h.replace(/jo/g,"ジョ");
h=h.replace(/ka/g,"カ");
h=h.replace(/ki/g,"キ");
h=h.replace(/ku/g,"ク");
h=h.replace(/ke/g,"ケ");
h=h.replace(/ko/g,"コ");
h=h.replace(/sa/g,"サ");
h=h.replace(/si/g,"シ");
h=h.replace(/su/g,"ス");
h=h.replace(/se/g,"セ");
h=h.replace(/so/g,"ソ");
h=h.replace(/ta/g,"タ");
h=h.replace(/ti/g,"チ");
h=h.replace(/tu/g,"ツ");
h=h.replace(/te/g,"テ");
h=h.replace(/to/g,"ト");
h=h.replace(/na/g,"ナ");
h=h.replace(/ni/g,"ニ");
h=h.replace(/nu/g,"ヌ");
h=h.replace(/ne/g,"ネ");
h=h.replace(/no/g,"ノ");
h=h.replace(/ha/g,"ハ");
h=h.replace(/hi/g,"ヒ");
h=h.replace(/hu/g,"フ");
h=h.replace(/fu/g,"フ");
h=h.replace(/he/g,"ヘ");
h=h.replace(/ho/g,"ホ");
h=h.replace(/ma/g,"マ");
h=h.replace(/mi/g,"ミ");
h=h.replace(/mu/g,"ム");
h=h.replace(/me/g,"メ");
h=h.replace(/mo/g,"モ");
h=h.replace(/ya/g,"ヤ");
h=h.replace(/yu/g,"ユ");
h=h.replace(/yo/g,"ヨ");
h=h.replace(/ra/g,"ラ");
h=h.replace(/ri/g,"リ");
h=h.replace(/ru/g,"ル");
h=h.replace(/re/g,"レ");
h=h.replace(/ro/g,"ロ");
h=h.replace(/wa/g,"ワ");
h=h.replace(/wo/g,"ヲ");
h=h.replace(/ga/g,"ガ");
h=h.replace(/gi/g,"ギ");
h=h.replace(/gu/g,"グ");
h=h.replace(/ge/g,"ゲ");
h=h.replace(/go/g,"ゴ");
h=h.replace(/za/g,"ザ");
h=h.replace(/ji/g,"ジ");
h=h.replace(/zi/g,"ジ");
h=h.replace(/zu/g,"ズ");
h=h.replace(/ze/g,"ゼ");
h=h.replace(/zo/g,"ゾ");
h=h.replace(/da/g,"ダ");
h=h.replace(/di/g,"ヂ");
h=h.replace(/du/g,"ヅ");
h=h.replace(/de/g,"デ");
h=h.replace(/do/g,"ド");
h=h.replace(/ba/g,"バ");
h=h.replace(/bi/g,"ビ");
h=h.replace(/bu/g,"ブ");
h=h.replace(/be/g,"ベ");
h=h.replace(/bo/g,"ボ");
h=h.replace(/pa/g,"パ");
h=h.replace(/pi/g,"ピ");
h=h.replace(/pu/g,"プ");
h=h.replace(/pe/g,"ペ");
h=h.replace(/po/g,"ポ");
h=h.replace(/a/g,"ア");
h=h.replace(/i/g,"イ");
h=h.replace(/u/g,"ウ");
h=h.replace(/e/g,"エ");
h=h.replace(/o/g,"オ");
h=h.replace(/n/g,"ン");
return h;
}