﻿/// <reference path="jquery-1.3.2-vsdoc.js"/>

/*

Coded by Nguyen Quang Huy
Y!M: nasavietnam, nasa8x
Email: nasavietnam@gmail.com
Website: www.nasa8x.com
        www.xomnet.org

*/

(function() {
    window.BBCode = {      

        wrapSelection: function(input, preString, postString) {
            if (input.setSelectionRange) {
                var selectionStart = input.selectionStart;
                var selectionEnd = input.selectionEnd;
                input.value = input.value.substring(0, selectionStart)
					+ preString
					+ input.value.substring(selectionStart, selectionEnd)
					+ postString
					+ input.value.substring(selectionEnd);
                if (selectionStart != selectionEnd) // has there been a selection
                {
                    this.setSelectionRange(input, selectionStart, preString.length + postString.length + selectionEnd);
                }
                else // set caret
                {
                    this.setCaretToPos(input, selectionStart + (preString + postString).length);
                }
            } else if (document.selection) {
                var sel = document.selection.createRange().text;
                if (sel) {
                    document.selection.createRange().text = preString + sel + postString;
                    input.focus();
                } else {
                    input.value += preString + postString;
                    input.focus();
                }
            } else {
                input.value += preString + postString;
                input.focus();
            }
        },

        getSelection: function(input) {
            if (input.setSelectionRange) {
                return input.selectionStart != input.selectionEnd;
            } else if (document.selection) {
                var range = document.selection.createRange();
                return range.parentElement() == input && range.text != '';
            } else {
                return false;
            }
        },

        replaceSelection: function(input, replaceString) {
            if (input.setSelectionRange) {
                var selectionStart = input.selectionStart;
                var selectionEnd = input.selectionEnd;
                input.value = input.value.substring(0, selectionStart)
					+ replaceString
					+ input.value.substring(selectionEnd);
                if (selectionStart != selectionEnd) // has there been a selection
                    this.setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
                else {
                    this.setCaretToPos(input, selectionStart + replaceString.length);
                }
            }
            else if (document.selection) {
                input.focus();
                document.selection.createRange().text = replaceString;
            }
            else {
                input.value += replaceString;
                input.focus();
            }
        },
        setCaretToPos: function(input, pos) {
            this.setSelectionRange(input, pos, pos);
        },

        storeCaret: function(input) {
            if (input.createTextRange) {
                input.caretPos = document.selection.createRange().duplicate();
            }
        },

        setSelectionRange: function(input, selectionStart, selectionEnd) {
            if (input.setSelectionRange) {
                //input.focus();
                input.setSelectionRange(selectionStart, selectionEnd);
            } else if (input.createTextRange) {
                var range = input.createTextRange();
                range.collapse(true);
                range.moveEnd('character', selectionEnd);
                range.moveStart('character', selectionStart);
                range.select();
            }
        },

        clientEncode: function(_html) {

            _html = _html.replace(/&quot;/g, '&quotx;');
            _html = _html.replace(/"/g, '&quot;');
            _html = _html.replace(/&amp;/g, '&ampx;');
            _html = _html.replace(/&/g, '&amp;');
            _html = _html.replace(/&lt;/g, '&ltx;');
            _html = _html.replace(/</g, '&lt;');
            _html = _html.replace(/&gt;/g, '&gtx;');
            _html = _html.replace(/>/g, '&gt;');
            return _html;
        },

        clientDecode: function(html) {
            html = html.replace(/&gt;/g, '>');
            html = html.replace(/&gtx;/g, '&gt;');
            html = html.replace(/&lt;/g, '<');
            html = html.replace(/&ltx;/g, '&lt;');
            html = html.replace(/&amp;/g, '&');
            html = html.replace(/&ampx;/g, '&amp;');
            html = html.replace(/&quot;/g, '"');
            html = html.replace(/&quotx;/g, '&quot;');

            return html;
        },

        ExecCommand: function(textareaId, command, option) {
        var _textarea = document.getElementById(textareaId);
            var scrollTop = _textarea.scrollTop;
            var scrollLeft = _textarea.scrollLeft;

            switch (command) {
                case "bold":
                    this.wrapSelection(_textarea, "[b]", "[/b]");
                    break;
                case "italic":
                    this.wrapSelection(_textarea, "[i]", "[/i]");
                    break;
                case "underline":
                    this.wrapSelection(_textarea, "[u]", "[/u]");
                    break;
                case "code":
                    this.wrapSelection(_textarea, "[code]", "[/code]");
                    break;

                case "quote":
                    this.wrapSelection(_textarea, "[quote]", "[/quote]");
                    break;
                case "strikethrough":
                    this.wrapSelection(_textarea, "[s]", "[/s]");
                    break;
                case "justifyfull":
                    this.wrapSelection(_textarea, "[justify]", "[/justify]");
                    break;
                case "justifyleft":
                    this.wrapSelection(_textarea, "[left]", "[/left]");
                    break;
                case "justifycenter":
                    this.wrapSelection(_textarea, "[center]", "[/center]");
                    break;
                case "justifyright":
                    this.wrapSelection(_textarea, "[right]", "[/right]");
                    break;

                case "image":

                    var url = prompt('Enter URL:', 'http://');
                    if (url != '' && url != null) {

                        this.wrapSelection(_textarea, '[img]' + url, '[/img]');

                    }

                    break;
                case "link":
                    var url = prompt('Enter URL:', 'http://');
                    if (url != '' && url != null) {
                        if (this.getSelection(_textarea)) {
                            this.wrapSelection(_textarea, '[url=' + url + ']', '[/url]');
                        }
                        else {
                            // ask for the description text...
                            var desc = prompt('Enter URL Description:', '');
                            if (desc != '' && desc != null)
                                this.replaceSelection(_textarea, '[url=' + url + ']' + desc + '[/url]');
                            else
                                this.replaceSelection(_textarea, '[url]' + url + '[/url]');
                        }
                    }
                    break;
                case "color":
                    this.wrapSelection(_textarea, "[color=" + option + "]", "[/color]");
                    break;
                case "fontsize":
                    this.wrapSelection(_textarea, "[size=" + option + "]", "[/size]");
                    break;
                default:
                    // make custom option
                    this.wrapSelection(_textarea, "[" + command + "]", "[/" + command + "]");
                    break;
            }

            _textarea.scrollTop = scrollTop;
            _textarea.scrollLeft = scrollLeft;

        }

    };

})();




