var S1 = new String();
var ipaDialog;
var ipaXPos;
var ipaYPos;
var currField;
var enabled = true;
var detached = false;
var msgBox;

Ext.onReady(	
	function(){
		createIPA();
	}
);

function createIPA(){

	if(ipaXPos){
		ipaDialog = new Ext.BasicDialog(Ext.id(), {
	        		autoCreate: true,
	        		title: 'IPA &amp; UTF-8 Characters',
	        		modal:false,
	        		autoTabs:false,
	        		width:200,
	        		height:400,
	        		shadow:true,
			draggable:false,
	        		x: ipaXPos,
	        		y: ipaYPos
		});
	}
	else{
		ipaDialog = new Ext.BasicDialog(Ext.id(), {
	        		autoCreate: true,
	        		title: 'IPA &amp; UTF-8 Characters',
	        		modal:false,
	        		autoTabs:false,
	        		width:200,
	        		height:350,
			draggable:false,
	        		shadow:true
		});
	}

	msgBox = document.createElement("div");
	document.body.appendChild(msgBox);
	msgBox = Ext.get(msgBox);
	msgBox.anchorTo(ipaDialog.el.id,'tl-bl');	
	msgBox.setWidth(200);
	var box = Ext.getDom(msgBox);

	box.innerHTML = "<div style='width:200px;'>\
    			<div class='x-box-tl'><div class='x-box-tr'><div class='x-box-tc'></div></div></div>\
    			<div class='x-box-ml'><div class='x-box-mr'><div class='x-box-mc'>\
        				<h3 style='margin-bottom:5px;'>press ctrl+shift+u keys to disable/enable.</h3>\
        				<div id='form-ct'>\
        				</div>\
    			</div></div></div>\
    			<div class='x-box-bl'><div class='x-box-br'><div class='x-box-bc'></div></div></div> \
			</div>";

	document.getElementById(ipaDialog.body.id).innerHTML = "Please type a character to replace it with a corresponding Unicode symbol.";

	ipaDialog.on('move',function(){msgBox.anchorTo(ipaDialog.el.id,'tl-bl');});
	ipaDialog.on('hide',function(){enabled=false;msgBox.hide();});
	ipaDialog.on('show',function(){msgBox.show();});

	var map = new Ext.KeyMap(document.body,{
		key: 'u',
		shift: true,
		ctrl: true,
		fn: function(){toggleIPA();}
	});

}

function toggleIPA(){
	enabled = !enabled;
	if(!enabled){
		ipaDialog.hide();
	}
	else{
		ipaDialog.show();
	}
}

function detachIP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ǒ,ǫ/r;ŕ,ŗ,ř/s;ß,ś,ŝ,ş,š/t;þ,ţ,ť,ŧ/u;ù,ú,û,ü,ũ,ū,ŭ,ů,ű,ų,ǔ,ǖ,ǘ,ǚ,ǜ/w;ŵ/y;ý,ÿ,ŷ,ȳ,ỹ/z;ź,ż,ž";

var ipa = "?;ʔ,ʕ,ʖ,ʡ,ʢ,ˤ,ˀ,ˁ/a;a,ä,æ,ą,ɐ,ɑ,ɒ,ɒ̈,ʌ/b;b,ɓ,ʙ,β/c;c,ç,ɕ,ʗ/d;d,dʱ,ð,ɖ,ɗ,ʣ,ʤ,ʥ/e;e,ẽ,ë,ɘ,ə,ɚ,ɛ,ɜ,ɝ,ɞ,ʚ/f;f,ɸ/g;g,ɠ,ɢ,ɣ,ʛ/h;h,ħ,ɥ,ɦ,ɧ,ʜ,ʰ,ʱ/i;i,ï,ɨ,ɩ,ɪ/j;j,ɟ,ʝ,ʲ/k;k,k’,ʞ/l;l,ł,ɬ,ɭ,ɮ,ʎ,ʟ,ˡ/m;m,m̥,ɱ/n;n,n̥,ń,ŋ,ɲ,ɳ,ɴ,ⁿ/o;o,ö,ø,œ,ɔ,ɔ̈,ɵ,ɶ,̥/p;p,p’/q;q/r;r,ɹ,ɺ,ɻ,ɼ,ɽ,ɾ,ʀ,ʁ,˞,ʳ,ʴ,ʵ,ʶ/s;s,s’,ʂ,ʃ,ʄ,ʆ,ˢ/t;t,tʰ,t̪,t’,ʇ,ʈ,ʧ,ʨ,θ/ts;ʦ/u;u,ü,ʉ,ʊ,ʌ/v;v,ʋ,ʌ/w;w,ẅ̈,ɯ,ɰ,ʍ,ʷ/x;x,χ,ˣ,̽/y;y,ÿ,ɤ,ʏ,ˠ/z;z,ʐ,ʑ,ʒ,ʓ/ǀ;ǀ/ǁ;ǁ/ǂ;ǂ/:;ː,ˑ,̆/ǃ;ǃ/ʘ;ʘ/`;̋,́,̄,̀,̏/';̋,́,̄,̀,̏/|;ː,ˑ,ˈ,ˌ,̆,|,‖/~;̬,ʰ,̹,̜,̟,̱,̈,̽,̣,̯,˞,̤,̰,̼,ʷ,ʲ,ˠ,ˤ,̴,̝,̞,̘,̙,̪,̺,̻,̃,ⁿ,ˡ,̚";

	var unicodeArray = unicode.split("/")
	var tempArray = new Array()
	var unicodeResults = new Array()
	var ipaResults = new Array()

	for (var i = 0; i < unicodeArray.length; i++) {
		tempArray = unicodeArray[i].split(";")
		//alert("left"+tempArray[0]);
		//alert("right"+tempArray[1]);
		unicodeResults[tempArray[0]] = tempArray[1]
	}
	var ipaArray = ipa.split("/")
	for (var i = 0; i < ipaArray.length; i++) {
		tempArray = ipaArray[i].split(";")
		//alert("left"+tempArray[0]);
		//alert("right"+tempArray[1]);
		ipaResults[tempArray[0]] = tempArray[1]
	}

	ipaoutput="";
	var temp = new Array()
	
	if (S1) {
		ID = "stub";
		if ((unicodeResults[S1]) || (ipaResults[S1])){
			echo("<div style='font-family: lucida sans unicode;'><span style='font-weight:bold'>Click the character you require!</span><br /><div style='float:left;text-align:center;width:70px'>IPA<br />");
			if (ipaResults[S1]){
				temp = ipaResults[S1].split(",")
				for (var i = 0; i < temp.length; i++) {
					echo("<a style='text-decoration:none' onclick='Update(\""+temp[i]+"\",\""+ID+"\");setTimeout(\"focusField()\",30);' href=javascript:;>&nbsp;&nbsp;"+temp[i]+"&nbsp;&nbsp;</a><br />");
				}
 			}
			echo("</div><div style='float:left;text-align:center;width:70px'>Latin<br />");
			if (unicodeResults[S1]){
				temp = unicodeResults[S1].split(",")
				for (var i = 0; i < temp.length; i++) {
					echo("<a style='text-decoration:none' onclick='Update(\""+temp[i]+"\",\""+ID+"\");setTimeout(\"focusField()\",30);' href=javascript:;>&nbsp;&nbsp;"+temp[i]+"&nbsp;&nbsp;</a><br />");
				}
	 		}
			echo("</div></div>");
		}
		else{
			echo("<P style='font-family:sans-serif;font-size:16px;'>No character found matching your search character</P><DIV align=center class=unichars> "+S1+" </DIV><BR>");
			echo("<STRONG>Please search again.</STRONG></A></DIV>");
			echo("<BR><BR><BR>");
		}
	}
	else {
		echo("<p class='smallheader'>");
		echo("Please type a character to replace it with a corresponding Unicode symbol.");
	}
	document.getElementById(ipaDialog.body.id).innerHTML = ipaoutput;
}	

var ipaoutput ="";
function echo(str){
	ipaoutput += str;
}
	
function Update(Alphabet,ID){
	var temp1 = currField.value.length-1;
	var temp3 = currField.value.substring(0,temp1);
	currField.value = temp3+Alphabet;
}

function operaFix(obj){


}
