var output;
var editor_root;
var editor_popup;
var editor;
var expected_words;
var xlit_items=[];
var number_xlit_items;
var dragging=false;
function update_ui_state(){
parent.update_ui_state();
}
function keycode_name(_1){
var _2=null;
if(_1==37){
_2="left";
}else{
if(_1==39){
_2="right";
}else{
if(_1==38){
_2="up";
}else{
if(_1==40){
_2="down";
}else{
if(_1==36){
_2="home";
}else{
if(_1==35){
_2="end";
}else{
if(_1==33){
_2="pageup";
}else{
if(_1==34){
_2="pagedown";
}else{
if(_1==46){
_2="delete";
}else{
if(_1==8){
_2="backspace";
}else{
if(_1==13){
_2="enter";
}else{
if(_1==9){
_2="tab";
}
}
}
}
}
}
}
}
}
}
}
}
return _2;
}
function handle_key_code(_3,_4,_5,_6,_7){
if(_5||_6||_7){
return false;
}
var _8=keycode_name(_3);
if(_8==null){
return false;
}
switch(_8){
case "left":
editor.move_left(_4);
break;
case "right":
editor.move_right(_4);
break;
case "home":
editor.move_home(_4);
break;
case "end":
editor.move_end(_4);
break;
case "up":
editor.move_up(_4);
break;
case "down":
editor.move_down(_4);
break;
case "pageup":
for(var i=0;i<10;++i){
editor.move_up(_4);
}
break;
case "pagedown":
for(var i=0;i<10;++i){
editor.move_down(_4);
}
break;
case "delete":
editor.key_delete();
break;
case "backspace":
editor.key_backspace();
break;
case "enter":
editor.key_enter();
break;
case "tab":
editor.key_tab();
break;
default:
assert(false);
}
is_xlit_correct();
return true;
}
function handle_key_char(_a,_b,_c,_d,_e){
var _f={"z":editor.undo,"y":editor.redo,"v":editor.paste,"x":editor.internal_cut,"c":editor.internal_copy,"a":editor.select_all};
var ch=String.fromCharCode(_a);
var lch=ch.toLowerCase();
var _12=_f[lch];
if((_c||_e)&&(!_d)&&(_12)){
_12.call(editor);
if((lch=="z")||(lch=="y")||(lch=="a")){
return true;
}
}
if(_c||_d||_e){
return false;
}
if(_a==32){
editor.key_space(ch);
}else{
if((_a>=65)&&(_a<65+26)){
editor.key_xlit_char(ch);
is_xlit_correct();
}else{
if((_a>=97)&&(_a<97+26)){
editor.key_xlit_char(ch);
}else{
editor.key_simple_char(ch);
}
}
}
return true;
}
function on_popup_choice(){
editor.grab_focus();
editor.move_right();
}
function set_expected_input(_13){
expected_words=_13;
}
function get_all_xlit_items(){
var _14=editor.ctx_.doc.lines_[0].items_;
var _15=-1;
xlit_items=[];
for(var i=0;i<_14.length;i++){
var t=_14[i];
var _18;
if(t.english_){
_15++;
_18={"index":_15,"eng":t.english_};
}else{
_18={"index":"","eng":t.text_};
}
xlit_items.push(_18);
}
}
function is_xlit_correct(){
get_all_xlit_items();
xlit_index=editor.caret_.v;
var _19=xlit_items[xlit_index];
if(!expected_words){
return;
}
var _1a=expected_words[xlit_items[xlit_index].index];
if(_1a){
if(_19.eng!=_1a.substring(0,_19.eng.length)){
if(editor_popup.visible_){
editor_popup.demo=true;
editor_popup.update(editor.on_which_word_.item());
}
}else{
editor_popup.demo=false;
editor_popup.update(editor.on_which_word_.item());
}
}
}
function do_init(_1b,_1c,_1d,_1e){
output=parent.output;
var _1f=_1c?parent.document:document;
editor_popup=new QuillPopup(_1f,on_popup_choice);
if(_1c){
editor_popup.set_offset(_1d,_1e);
}
editor_root=document.body;
editor=new QuillEditor(document,editor_root,editor_popup,_1b);
window.setInterval("editor.toggle_caret_blink()",500);
$(editor_root).focus(function(e){
editor.set_has_focus(true);
});
$(editor_root).blur(function(e){
editor.set_has_focus(false);
});
$(window).resize(function(e){
editor.update_on_resize();
});
$(editor_root).mousedown(function(e){
editor.grab_focus();
if(dragging){
return;
}
dragging=true;
$(editor_root).mousemove(function(e){
editor.move_caret_to_xy(e.pageX,e.pageY,true);
update_ui_state();
});
editor.move_caret_to_xy(e.pageX,e.pageY,false);
update_ui_state();
is_xlit_correct();
});
$().mouseup(function(e){
dragging=false;
$(editor_root).unbind("mousemove");
});
function print_key(_26,e){
}
$(editor_root).keypress(function(e){
print_key("keypress",e);
var _29=false;
if(e.which>=32){
if((!window.event)||(!e.ctrlKey)){
_29=handle_key_char(e.which,e.shiftKey,e.ctrlKey,e.altKey,e.metaKey);
}
}else{
if((!window.event)&&((e.which==0)||(e.which==8)||(e.which==13))){
_29=handle_key_code(e.keyCode,e.shiftKey,e.ctrlKey,e.altKey,e.metaKey);
}
}
if(_29){
e.preventDefault();
update_ui_state();
}
});
$(editor_root).keydown(function(e){
print_key("keydown",e);
var _2b;
if(!window.event){
return;
}
_2b=handle_key_code(e.keyCode,e.shiftKey,e.ctrlKey,e.altKey,e.metaKey);
if((!_2b)&&e.ctrlKey&&(e.keyCode>=32)){
_2b=handle_key_char(e.keyCode,e.shiftKey,e.ctrlKey,e.altKey,e.metaKey);
}
if(_2b){
e.preventDefault();
update_ui_state();
}
});
if($.browser.msie){
$().bind("selectstart",function(e){
var src=e.target.id;
if(src!=="paste"){
e.preventDefault();
}
});
}else{
$().mousedown(function(e){
e.preventDefault();
});
}
}

