/*
 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 * modified by CH June 10, 2008 
 */

(function() {

        var fieldSelection = {

                getSelection: function() {

                        var e = this.jquery ? this[0] : this;

                        return (

                                /* mozilla / dom 3.0 */
                                ('selectionStart' in e && function() {
                                        var l = e.selectionEnd - e.selectionStart;
                                        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
                                }) ||

                                /* exploder */
                                (document.selection && function() {

                                        e.focus();

                                        var r = document.selection.createRange(); // r is a TextRange object
                                        if (r == null) {
                                                return { start: 0, end: e.value.length, length: 0 }
                                        }
        
                        // get the selected text and replace '\r'
                        var txt = r.text;
                        txt = txt.replace(/\r*/g, '');
                        
                        // count chars before selection
                        var wt = r.duplicate();
                        wt.moveToElementText(e);
                        var charBefore = -1;
                                        while(wt.inRange(r)) { // fix most of the ie bugs with linefeeds...
                                                wt.moveStart('character');
                                                charBefore ++;
                                        }
                                        
                                        // count chars in selection
                                        /*
                                        r.setEndPoint('StartToEnd', r);
                                        r.setEndPoint('EndToEnd', wt);
                                        var charIn = 0;
                                        while(wt.inRange(r)) { // fix most of the ie bugs with linefeeds...
                                                wt.moveStart('character');
                                                charIn ++;
                                        }
                                        */
                                        return { start: charBefore, end: charBefore + txt.length, length: txt.length, text: txt };
                                }) ||

                                /* browser not supported */
                                function() {
                                        return { start: 0, end: e.value.length, length: 0 };
                                }

                        )();

                },

                replaceSelection: function() {

                        var e = this.jquery ? this[0] : this;
                        var text = arguments[0] || '';

                        return (

                                /* mozilla / dom 3.0 */
                                ('selectionStart' in e && function() {
                                    var newPos = e.selectionStart + text.length;
                                        e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
                                        e.setSelectionRange(newPos, newPos);
                                        return this;
                                }) ||

                                /* exploder */
                                (document.selection && function() {
                                        e.focus();
                                        document.selection.createRange().text = text;
                                        return this;
                                }) ||

                                /* browser not supported */
                                function() {
                                        e.value += text;
                                        return this;
                                }

                        )();

                }

        };

        jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();
