//Copyright (c) 2004 Jan-Klaas Kollhof

Module("textInput", "0.0.1", function(thisMod){
    var KeyDelete=127; 
    var KeyBackSpace=8;
    var KeyUp = 38;
    var KeyDown = 40;
    var KeyRight = 39;
    var KeyLeft = 37;
    var KeyReturn = 13;                
    var KeyNewLine = 10;
    
    thisMod.TextField = Class("TextField", function(thisClass, Super){
        
        thisClass.prototype.init = function(txNode, carretNode, execfn){
            this.txNode = txNode;
            this.carretNode = carretNode;
            this.execfn = execfn;
            this.text  = "";
            this.selStart = this.text.length;
            this.selLength = 0;
            this.redraw();
        }
        
        thisClass.prototype.handleEvent=function(evt){
            if(this[evt.type]){
                this[evt.type](evt);
            }
        }
        
        thisClass.prototype.keypress =  function(evt){
                switch(evt.charCode){
                case KeyBackSpace: 
                    this.deleteL();
                    break;
                case KeyDelete:
                    break;
                case KeyNewLine:
                case KeyReturn:
                    this.exec()
                    break;
                default:
                    this.textInput(String.fromCharCode(evt.charCode));
            }
        }
        
        thisClass.prototype.keydown =  function(evt){
            switch(evt.keyCode){
                case KeyBackSpace:
                    //this.deleteL();
                    break;
                case KeyDelete:
                    this.deleteR();
                    break;
                default:
                    break;
            }
        }
        
        thisClass.prototype.keyup =  function(evt){
            switch(evt.keyCode){
                case KeyUp: 
                    break;
                case KeyDown:
                    break;
                case KeyRight:
                    this.onRight();
                    break;
                case KeyLeft:
                    this.onLeft();
                    break;
                default:
            }
        }
        
        thisClass.prototype.textInput = function(data){
            this.replaceSelection(data);
        }
        
        thisClass.prototype.deleteR = function(evt){
            if(this.selLength == 0){
                this.selLength = 1;
            }
            this.replaceSelection("");
        }
    
        thisClass.prototype.deleteL = function(evt){
            if((this.selStart > 0) && (this.selLength == 0)){
                this.selLength = -1;
            }
            this.replaceSelection("");
        }
        
        thisClass.prototype.onLeft = function(){
            this.selStart += this.selLength -1 ;
            this.selLength = 0;
            if(this.selStart<0){
                this.selStart = 0;
            }
            this.selectionChanged();
        }
        
        thisClass.prototype.onRight = function(){
            this.selStart += this.selLength + 1 ;
            this.selLength = 0;
            if(this.selStart>this.text.length){
                this.selStart = this.text.length;
            }
            this.selectionChanged();
        }
        
        thisClass.prototype.replaceSelection = function(str){
            this.text = this.getPreSelection() + str + this.getPostSelection();
            if(this.selLength<0){
                this.selStart += this.selLength + str.length;
            }else{
                this.selStart += str.length ;
            }
            this.selLength = 0;
            this.selectionChanged();
        }
        
        thisClass.prototype.setText = function(text){
            this.text = text;
            this.selStart = text.length;
            this.selLength = 0;
            this.selectionChanged();
        }
        
        thisClass.prototype.getSelection = function(){
            if(this.selLength<0){
                return this.text.slice(this.selStart + this.selLength,  this.selStart);
            }else{
                return this.text.slice(this.selStart, this.selStart + this.selLength);
            }
        }
        thisClass.prototype.getPreSelection = function(){
            if(this.selLength<0){
                return this.text.slice(0, this.selStart + this.selLength);
            }else{
                return this.text.slice(0, this.selStart);
            }
        }
        thisClass.prototype.getPostSelection =  function(){
            if(this.selLength<0){
                return this.text.slice(this.selStart, this.text.length);
            }else{
                return this.text.slice(this.selStart + this.selLength, this.text.length);
            }
        }
        
        thisClass.prototype.selectionChanged = function(){
            this.redraw();
        }        
        
        thisClass.prototype.redraw =  function(){
            var txn = this.txNode;
            while(txn.hasChildNodes()){
                txn.removeChild(txn.firstChild);
            }
            txn.appendChild(document.createTextNode(this.text));
            var ci = this.selStart + this.selLength-1;
            try{
                var x = txn.getEndPositionOfChar(ci).x;
            }catch(e){
                var x =0;
            }
            this.carretNode.setAttribute("x", x);
        }
            
        thisClass.prototype.unfocus=function(){
            this.carretNode.setAttribute('fill','none');
            document.documentElement.removeEventListener("keypress", this);
            document.documentElement.removeEventListener("keydown", this);
            document.documentElement.removeEventListener("keyup",this);
        }
        
        thisClass.prototype.focus=function(){
            this.carretNode.setAttribute('fill','black');
            document.documentElement.addEventListener("keypress", this, false);
            document.documentElement.addEventListener("keydown", this, false);
            document.documentElement.addEventListener("keyup",this, false);
        }
        
        //return was hit
        thisClass.prototype.exec=function(){
            var tx = this.text;
            this.setText("");
            this.execfn(tx);
        }       
    })
})
