
window.onload = function()
{
    tab.init();
};

var tab =
{
    checkbox:false,
    checkboxText:"Gebruik tab's in de textarea. (shortcut = AltGr + TAB)",
    a_u:true,
    a_uKey:18,
    
    keys:Array(),
    init:function()
    {
        // textarea's
        var oTextArea = document.getElementsByTagName("textarea");
        var y = oTextArea.length;
        for(var x=0;x<y;x++)
        {
            if(oTextArea.item (x).getAttribute("wysiwyg"))
            {
                if(tab.checkbox)
                {
                    tab.setupCheckbox(oTextArea.item(x));    
                }
                oTextArea.item (x).onkeydown = tab.keyDown;
                oTextArea.item(x).onkeyup = tab.keyUp;
            }
        }
        
        // keys
        tab.keys["shift"] = false;
        tab.keys["altGr"] = false;
    },
    setupCheckbox:function(obj)
    {
        var oDiv = document.createElement("DIV");
        var oInput=document.createElement("INPUT");
        var oTextNode = document.createTextNode (tab.checkboxText);
        oInput.type = "checkbox";
        oInput.onclick=function(){a_u = this.checked = !a_u;};
        oDiv.appendChild(oInput);
        oDiv.appendChild(oTextNode);
        input_id = oDiv.firstChild;
        obj.parentNode.insertBefore(oDiv,obj);
        oInput.checked = tab.a_u;
    },
    getKey:function(e)
    {
        return (typeof e != 'undefined' && typeof e.which != 'undefined') ? e.which :
        (typeof e != 'undefined' && typeof e.keyCode != 'undefined') ? e.keyCode :
        (typeof window.event != 'undefined' && typeof event.keyCode != 'undefined') ? event.keyCode :
        null;
    },
    keyDown:function(e)
    {
        var key = tab.getKey(e);
        //document.title = key;
        if (key)
        {
            if (key == tab.a_uKey)
            {
                 tab.keys["altGr"] = true;
                return true;
            }
            if (key == 16)
            {
                tab.keys["shift"] = true;
                return true;
            }
            if (key == 9)
            {
                if(tab.keys["altGr"])
                {
                    (tab.a_u)?tab.a_u=false:tab.a_u=true;
                    tab.keys ["altGr"] = false;
                    if(tab.checkbox)
                    {
                        input_id.checked = tab.a_u;
                    }
                    return false;
                }
                if(tab.keys["shift"])
                {
                    //shift = false;
                    if(tab.checkbox)
                    {
                        caret.shiftTab(this);
                    }
                    return false;
                }
                if(tab.a_u)
                {
                    caret.tab(this);
                    return false;
                }
            }
            if(key == 13)
            {
                return caret.enter(this);
            }
        }
    },
    keyUp:function(e)
    {
        var key = tab.getKey(e);
        if (key)
        {
            if(key == 16)
            {
                tab.keys["shift"] = false;
            }
        }
        return tab.keys["altGr"] = false;
    }
};

var caret =
{
    tab:function(obj)
    {
        //MOZILLA/NETSCAPE support
        if (obj.selectionStart || obj.selectionStart == '0')
        {
            caret.FF_tab(obj);
        }
        // IE support
        else if (document.selection)
        {
            obj.focus();
            sel = document.selection.createRange();
            sel.text = "\t";
            sel.moveStart ('character', 0);
            sel.select();
        }
        // if NOT supported
        else
        {
            obj.value += "\t";
        }    
    },
    FF_tab:function(obj)
    {
        var startPos     = obj.selectionStart;
        var endPos       = obj.selectionEnd;
        var replace    = obj.value.substring(startPos,endPos);
        var scroll     = obj.scrollTop;
        
        if( replace.indexOf("\n") != -1)
        {
            var first     = obj.value.substring(0, startPos);
                replace    = replace.replace(/\n/g,"\n\t");
            
            if(replace.substring(0,1) != "\n")
            {// - FF: soms heb je bij de multipleTabs een lijn teveel mee, als je de volle lijnen selecteert. (multiple times happend, none reproducable)
                if(first.indexOf("\n") == -1)
                    first = "\t"+first
                else
                    first = first.replace(/\n(.*)$/,"\n\t$1")
            }
            obj.value = first+replace+obj.value.substring(endPos, obj.value.length);
            obj.selectionStart = startPos+1;
            obj.selectionEnd = startPos+replace.length+1;
        }
        else
        {
            replace = "\t";    
            obj.value = obj.value.substring(0, startPos)+replace+obj.value.substring(endPos, obj.value.length);
            obj.selectionStart = startPos+replace.length;
            obj.selectionEnd = startPos+replace.length;
        }
        obj.scrollTop = scroll;    
    },
    shiftTab:function(obj)
    {
        if (obj.selectionStart || obj.selectionStart == '0')
        {
            caret.FF_shiftTab(obj);    
        }
    },
    FF_shiftTab:function(obj)
    {
        var startPos     = obj.selectionStart;
        var endPos     = obj.selectionEnd;
        var replace     = obj.value.substring(startPos,endPos);
        var first    = obj.value.substring(0, startPos);
        var scroll     = obj.scrollTop;
        
        replace = replace.replace(/\n\t/g,"\n");
                
        if(first.indexOf("\n") == -1)
        {
            if(first.substring(0,1) == "\t")
                first = first.substr(1);
        }
        else
        {
            if(first.lastIndexOf("\n\t") == first.lastIndexOf("\n"))
                first = first.replace(/\n\t(.*)$/,"\n$1");
            else
            {
                if( replace.substring(0,1) == "\t")
                    replace = replace.substr(1);
            }
        }
        obj.value         = first+replace+obj.value.substring(endPos, obj.value.length);
         obj.selectionStart     = first.length;
        obj.selectionEnd     = first.length+replace.length;
        obj.scrollTop         = scroll;
    },
    enter:function(obj)
    {
        //MOZILLA/NETSCAPE support
        if (obj.selectionStart || obj.selectionStart == '0')
        {
            return caret.FF_enter(obj);
        }
        // IE support
        else if (document.selection)
        {
            return true;
        }
        // if NOT supported
        else
        {
            return true;
        }
        return false;
    },
    FF_enter:function(obj)
    {
        var scroll     = obj.scrollTop;
	    var startPos     = obj.selectionStart ;
        var endPos       = obj.selectionEnd;
        var first    = obj.value.substring(obj.value.lastIndexOf("\n"), startPos);

        var replace = "\n";
        for(var i=1;first.substring(i,i+1) == "\t";i++)
        {
            replace += "\t";
        }
        if(obj.value.substring(startPos-1,startPos) == "{")
            replace += "\t";
        
        obj.value = obj.value.substring (0, startPos)+replace+obj.value.substring(endPos, obj.value.length);
        obj.selectionStart = startPos+replace.length;
        obj.selectionEnd = startPos+replace.length;
        obj.scrollTop = scroll;
        return false;
    }
}
