// reference:
// http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/execcommand.asp
// http://msdn.microsoft.com/workshop/author/dhtml/reference/commandids.asp
// http://www.mozilla.org/editor/ie2midas.html
// http://www.mozilla.org/editor/midas-spec.html
QuickEdit.Commands = {
    'Undo': function() {
        document.tb.activeEditable.getDoc().execCommand('Undo', false, null);
    },
    
    'Redo': function() {
        document.tb.activeEditable.getDoc().execCommand('Redo', false, null);
    },
    
    'Bold': function() {
        document.tb.activeEditable.getDoc().execCommand('Bold', false, null);
    },
    
    'Italic': function() {
        document.tb.activeEditable.getDoc().execCommand('Italic', false, null);
    },
    
    'Underline': function() {
        document.tb.activeEditable.getDoc().execCommand('Underline', false, null);
    },
    
    'StrikeThrough': function() {
        document.tb.activeEditable.getDoc().execCommand('StrikeThrough', false, null);
    },
    
    'JustifyLeft': function() {
        document.tb.activeEditable.getDoc().execCommand('JustifyLeft', false, null);
    },
    
    'JustifyCenter': function() {
        document.tb.activeEditable.getDoc().execCommand('JustifyCenter', false, null);
    },
    
    'JustifyRight': function() {
        document.tb.activeEditable.getDoc().execCommand('JustifyRight', false, null);
    },
    
    'JustifyFull': function() {
        document.tb.activeEditable.getDoc().execCommand('JustifyFull', false, null);
    },
    
    'Copy': function() {
        document.tb.activeEditable.getDoc().execCommand('Copy', false, null);
    },
    
    'Cut': function() {
        document.tb.activeEditable.getDoc().execCommand('Cut', false, null);
    },
    
    'Paste': function() {
        document.tb.activeEditable.getDoc().execCommand('Paste', false, null);
    },
    
    // <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <p>, <pre>, <adress>
    'FormatBlock': function() {
        document.tb.activeEditable.getDoc().execCommand('FormatBlock', false, arguments[0]);
    },
    
    // tiefgestellt
    'Subscript': function() {
        document.tb.activeEditable.getDoc().execCommand('Subscript', false, arguments[0]);
    },
    
    // hochgestellt
    'Superscript': function() {
        document.tb.activeEditable.getDoc().execCommand('Superscript', false, arguments[0]);
    },
    
    'InsertOrderedList': function() {
        document.tb.activeEditable.getDoc().execCommand('InsertOrderedList', false, arguments[0]);
    },
    
    'InsertUnorderedList': function() {
        document.tb.activeEditable.getDoc().execCommand('InsertUnorderedList', false, arguments[0]);
    },
    
    'InsertHorizontalRule': function() {
        document.tb.activeEditable.getDoc().execCommand('InsertHorizontalRule', false, arguments[0]);
    },
    
    // einrücken
    'Indent': function() {
        document.tb.activeEditable.getDoc().execCommand('Indent', false, arguments[0]);
    },
    
    // ausrücken
    'Outdent': function() {
        document.tb.activeEditable.getDoc().execCommand('Outdent', false, arguments[0]);
    },
    
    // text color
    'ForeColor': function() {
        document.tb.activeEditable.getDoc().execCommand('ForeColor', false, arguments[0]);
    },
    
    // background color
    'BackColor': function() {
        if (document.all) {
            document.tb.activeEditable.getDoc().execCommand('BackColor', false, arguments[0]);
        } else {
            document.tb.activeEditable.getDoc().execCommand('useCSS', false, false);
            document.tb.activeEditable.getDoc().execCommand('hilitecolor', false, arguments[0]);
            document.tb.activeEditable.getDoc().execCommand('useCSS', false, true);
        }
    },
    
    // set css class
    'BlockStyle': function() {
        if (document.all) {
            var rng = document.tb.activeEditable.getSel().createRange();
            selectedText = (rng.text && rng.text.length > 0);
        } else {
            selectedText = (document.tb.activeEditable.getSel().toString().length > 0);
        }
        
        QuickEdit.Commands['RemoveFormat']();
        if (value = arguments[0]) {
            document.tb.activeEditable.getDoc().execCommand('fontname', false, '#qe_temp_font#');
            var elementArray = document.tb.activeEditable.getElementsByAttributeValue(document.tb.activeEditable.getDoc().body, 
                'font', 'face', '#qe_temp_font#');
            
            for (var x = 0; x < elementArray.length; x++) {
                elm = elementArray[x];
                if (elm) {
                    var spanElm = document.tb.activeEditable.getDoc().createElement('span');
                    spanElm.className = arguments[0];
                    if (elm.hasChildNodes()) {
                        for (var i = 0; i < elm.childNodes.length; i++)
                            spanElm.appendChild(elm.childNodes[i].cloneNode(true));
                    }
                    
                    elm.parentNode.replaceChild(spanElm, elm);
                }
            }
        }
    },
    
    // remove text formats (bold, underline, class, etc)
    'RemoveFormat': function() {
        if (document.all && false) {
            try {
                document.tb.activeEditable.contentWindow.focus();
                var rng = document.tb.activeEditable.getDoc().selection.createRange();
                rng.execCommand('RemoveFormat', false, null);
                rng.pasteHTML(rng.text);
            } catch (e) {
                // Do nothing
            }
        } else {
            document.tb.activeEditable.getDoc().execCommand('RemoveFormat', false, null);
        }
    },
    
    'InsertImage': function() {
        if (!arguments[0] || typeof(arguments[0]) == 'object') {
            var url = '';
            mywin = window.open(absCorrect + 'icms/content/editTemplates/quickedit_pictures.php?SID=%SID%&pic=' + encodeURI(url), 
                'picwin', 'width=420,height=450,dependent=yes,location=no,menubar=no,resizable=yes,status=no,toolbar=no,scrollbars=yes');
        } else {
            document.tb.activeEditable.getDoc().execCommand('InsertImage', null, arguments[0]);
            document.tb.activeEditable.updateSize();
        }
    },
    
    'CreateLink': function() {
        if (!arguments[0] || typeof(arguments[0]) == 'object') {
            var url = '';
            mywin = window.open(absCorrect + 'icms/content/editTemplates/quickedit_navigation.html?SID=' + SID + '&url=' + encodeURI(url), 
                'navwin', 'width=300,height=450,dependent=yes,location=no,menubar=no,resizable=no,status=no,toolbar=no,scrollbars=yes');
        } else {
            document.tb.activeEditable.getDoc().execCommand('CreateLink', null, arguments[0]);
            document.tb.activeEditable.updateSize();
        }
    },
    
    // remove link
    'UnLink': function() {
        document.tb.activeEditable.getDoc().execCommand('UnLink', null, null);
    },
    
    // toggle source mode
    'SourceMode': function() {
        if (!document.tb.activeEditable.inSourceMode) {
            if (document.all) {
                var iHTML = document.tb.activeEditable.getDoc().body.innerHTML;
                document.tb.activeEditable.getDoc().body.innerText = iHTML;
            } else {
                var html = document.createTextNode(document.tb.activeEditable.getDoc().body.innerHTML);
                document.tb.activeEditable.getDoc().body.innerHTML = "";
                document.tb.activeEditable.getDoc().body.appendChild(html);
            }
        } else {
            if (document.all) {
                var iText = document.tb.activeEditable.getDoc().body.innerText;
                document.tb.activeEditable.getDoc().body.innerHTML = iText;
            } else {
                var html = document.tb.activeEditable.getDoc().body.ownerDocument.createRange();
                html.selectNodeContents(document.tb.activeEditable.getDoc().body);
                document.tb.activeEditable.getDoc().body.innerHTML = html.toString();
            }
        }
        
        document.tb.activeEditable.inSourceMode = !(document.tb.activeEditable.inSourceMode || false);
    }
}

QuickEdit.Command = Class.create();

Object.extend(QuickEdit.Command.prototype, {
    
    initialize: function(name) {
        this.cmdFunc = QuickEdit.Commands[name] || Prototype.emptyFunction;
    },
    
    exec: function() {
        this.cmdFunc.apply(this, arguments);
        document.tb.activeEditable.updateSize();
        if ((this.cmdFunc != QuickEdit.Commands['CreateLink'] && this.cmdFunc != QuickEdit.Commands['InsertImage']) ||
            !(!arguments[0] || typeof(arguments[0]) == 'object')) {
            document.tb.activeEditable.contentWindow.focus();
        }
    }
});


