var SELECT_ST = 0
var SELECT_EN = 0

function editor_init(){
    $("#editor").bind('keydown', function (e){
            tabbed = false
            if(e.keyCode==9) e.preventDefault();
    })
    $("#editor").bind('keyup', function (e){var TIME = getTime()
            if(tabbed) {updateOverlay();return false}
            if(!checkAuto(e,this)) {
                    autoIndent(e)
                    if([9,37,38,39,40,16,17,18,224].indexOf(e.keyCode) == -1) {
                            if(e.keyCode == 8 || e.keyCode == 46) updateTabLocation(1,e.keyCode == 8)
                            else updateTabLocation(1)
                    }
            }
            LAST_EDIT = new Date().getTime()
            newEdit()
            ERRORS[CURRENT_FILE] = []
            if(e.keyCode==9){ //tab
                    if(TABBING.length > 0) tabNext(this)
                    else KEYS.tabBlock(this,e.shiftKey);
                    e.preventDefault();
            }
            updateOverlay()
            //if(COLLAB) collabChange()
            //alert(getTime()-TIME)
    })
    $("#editor").bind('keypress', function (e){
            switch(e.which){
                    case 40: matchChar(this,e,"(",")"); break;
                    case 91: matchChar(this,e,"[","]"); break;
                    case 123: matchChar(this,e,"{","}"); break;
                    case 34: matchChar(this,e,"\"","\""); break;
                    case 39: matchChar(this,e,"'","'"); break;
            }
    })
    $("#editor").bind('cut paste', function (e){setTimeout(updateOverlay,1)})
    $("#editor").bind('touchend mousemove click keypress keyup', function (e){
            var edit = e.target
            if(SELECT_ST!=edit.selectionStart || SELECT_EN!=edit.selectionEnd){
                            SELECT_ST=edit.selectionStart
                            SELECT_EN=edit.selectionEnd
                            if(e.type == 'click' || e.type == 'touchend') DETECT.lastLoc = null
                            KEYS.update(SELECT_ST!=SELECT_EN)
                            var txt = document.getElementById('editor').value
                            var left = txt.substr(SELECT_ST-1,1) == "{"
                            var right = txt.substr(SELECT_ST-1,1) == "}"
                            var tmp
                            if(left)  tmp = txt.substring(SELECT_ST-1)
                            else if(right) tmp = txt.substring(0,SELECT_ST).reverse()
                            if(left || right){
                                var count = 0
                                var total = 0
                                for(var i = 0; i < tmp.length; i++){
                                       count++
                                       if(tmp.charAt(i) == "{") total++
                                       else if(tmp.charAt(i) == "}") total--
                                       if(total == 0) break
                                }
                                if(total == 0){
                                    var a = {st:SELECT_ST + (left?count-2:-count),len:1,str:'{',a:'<font style="background-color:rgba(255,255,10,0.8); border: 1px solid rgba(50,50,255,1);margin-top:-1px;margin-right:-2px">',b:'</font>'}
                                    var b = {st:SELECT_ST-1,len:1,str:'{',a:'<font style="background-color:rgba(255,255,10,0.8); border: 1px solid rgba(50,50,255,1);margin-top:-1px;margin-right:-2px">',b:'</font>'}
                                    updateOverlay([a,b])
                                }
                                
                            }
            }
            if(COLLAB) collabChange()
    })
}