//
// WYZZ v0.1 Copyright (c) 2007 The Mouse Whisperer
//
// Contains code Copyright (c) 2006 openWebWare.com
// This copyright notice MUST stay intact for use.
//
// An open source WYSIWYG editor for use in web based applications.
// For full source code and docs, visit http://wyzz.bpweb.net
//
// This library is free software; you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation; either version 2.1 of the License, or 
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along 
// with this library; if not, write to the Free Software Foundation, Inc., 59 
// Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// smartly edited by Roman Robel, visit eCodeLab | http://www.ecodelab.de
// added default font and default font size
// replaced icons
// added footer information
// edited styles

// Editor width and Height
var wyzzW = 423;
var wyzzH = 250;

var defaultFontFamily = "Arial";
var defaultFontSize = "2";
var youTubeUrl = "http://www.youtube-nocookie.com";
var youTubeTemplate = new Array('<object width="500" height="315" style="z-index:3;"><param name="movie" value="',
    '&hl=de&fs=1&color1=0x5d1719&color2=0xcd311b&border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="',
    '&hl=de&fs=1&color1=0x5d1719&color2=0xcd311b&border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="315"></embed></object>');

// Order of available commands in toolbar
var buttonName = new Array("bold","italic","underline",
    "justifyleft","justifycenter","justifyright",
    "insertunorderedlist","insertorderedlist","upsize",
    "downsize","link","unlink");

// link titles
var titleName = new Array("Fett", "Kursiv", "Unterstrichen", 
    "Links ausrichten", "Zentriert ausrichten", "Rechts ausrichten",
    "Ungeordnete Liste", "Geordnete Liste", "Gr&ouml;&szlig;er",
    "Kleiner", "Link einf&uuml;gen", "Link entfernen",
    "Bild einf&uuml;gen", "Video einf&uuml;gen");
/* 
Emulates insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement() three functions 
so they work with Netscape 6/Mozilla - By Thor Larholm me@jscript.dk
*/
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
    HTMLElement.prototype.insertAdjacentElement = function
        (where,parsedNode)
        {
            switch (where){
                case 'beforeBegin':
                    this.parentNode.insertBefore(parsedNode,this)
                    break;
                case 'afterBegin':
                    this.insertBefore(parsedNode,this.firstChild);
                    break;
                case 'beforeEnd':
                    this.appendChild(parsedNode);
                    break;
                case 'afterEnd':
                    if (this.nextSibling)
                        this.parentNode.insertBefore(parsedNode,this.nextSibling);
                    else this.parentNode.appendChild(parsedNode);
                    break;
            }
        }

    HTMLElement.prototype.insertAdjacentHTML = function
        (where,htmlStr)
        {
            var r = this.ownerDocument.createRange();
            r.setStartBefore(this);
            var parsedHTML = r.createContextualFragment(htmlStr);
            this.insertAdjacentElement(where,parsedHTML)
        }


    HTMLElement.prototype.insertAdjacentText = function
        (where,txtStr)
        {
            var parsedText = document.createTextNode(txtStr)
            this.insertAdjacentElement(where,parsedText)
        }
};

function make_wyzz(textareaID) {
 
    // Hide the textarea
    document.getElementById(textareaID).style.display = 'none';
	
    // get textareaID
    var n = textareaID;
	
    // Toolbars width is 2 pixels wider than the editor
    toolbarWidth = parseFloat(wyzzW) + 2;
	
    // Generate WYSIWYG toolbar
    var toolbar;
    toolbar =  '<table cellpadding="0" cellspacing="0" border="0" class="toolbar" style="width:' + toolbarWidth + 'px;"><tr>';
  
    // Output buttons for toolbar
    for (btn in buttonName) {
        toolbar += '<td style="width: 22px;"><img src="../_wyzz/wyzzicons/' +buttonName[btn]+ '.png" border=0 unselectable="on" title="' +titleName[btn]+ '" id="' +buttonName[btn]+ '" class="button" onClick="formatText(this.id,\'' + n + '\');" onmouseover="if(className==\'button\'){className=\'buttonOver\'};" onmouseout="if(className==\'buttonOver\'){className=\'button\'};" unselectable="on" width="20" height="20"></td>';
    }

    toolbar += '<td align="right"></td></tr></table>';

    // Create iframe for editor
    var iframe = '<table cellpadding="0" cellspacing="0" border="0" style="width:' + wyzzW + 'px; height:' + wyzzH + 'px;border: 1px solid #ccc; background-color:#fff;" id="wyzzcontentarea"><tr><td valign="top">\n'
    + '<iframe frameborder="0" style="border:0px; background-color:#fff;" id="wysiwyg' + n + '"" class="editableContent"></iframe>\n'
    + '</td></tr><tr><td class="commandline"><div class="commands"><input type="button" value="Eintrag speichern" id="save_content"/></div><div class="processor" id="processor">&nbsp;</div></td></tr>\n'
    + '</table>\n';

    // Insert toolbar after the textArea
    document.getElementById(n).insertAdjacentHTML("afterEnd", toolbar + iframe);
	
    // Give the iframe the required height and width
    document.getElementById("wysiwyg" + n).style.height = wyzzH + "px";
    document.getElementById("wysiwyg" + n).style.width = wyzzW + "px";
	
    // Pass the textarea's existing text into the editor
    var content = document.getElementById(n).value;
    var doc = document.getElementById("wysiwyg" + n).contentWindow.document;
	
    // Write the textarea's content into the iframe
    doc.open();
    doc.write(content);
    doc.close();
	
    // Make the iframe editable in both Mozilla and IE
    doc.body.contentEditable = true;
    doc.body.id = "textcontent";
    doc.designMode = "on";

    // Set Default Font
    doc.execCommand("FontName", false, defaultFontFamily);
    doc.execCommand("FontSize", false, defaultFontSize);
	
    // Update the textarea with content in WYSIWYG when user submits form
    var browserName = navigator.appName;
    if (browserName == "Microsoft Internet Explorer") {
        for (var idx=0; idx < document.forms.length; idx++) {
            document.forms[idx].attachEvent('onsubmit', function() {
                updateTextArea(n);
            });
        }
    }
    else {
        for (var idx=0; idx < document.forms.length; idx++) {
            document.forms[idx].addEventListener('submit',function OnSumbmit() {
                updateTextArea(n);
            }, true);
        }
    }
};

function formatText(id, n, selected) {
    // When user clicks button make sure it always targets correct textarea
    document.getElementById("wysiwyg" + n).contentWindow.focus();
  
    if(id=="upsize") {
        var currentFontSize = document.getElementById("wysiwyg"+n).contentWindow.document.queryCommandValue("FontSize");
        if(currentFontSize == '') currentFontSize = 3; // fudge for FF
        if(currentFontSize < 7) {
            var newFontSize = parseInt(currentFontSize) + 1;
        } else {
            var newFontSize = currentFontSize;
        }
        document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("FontSize", false, newFontSize);
    }
    else if(id=="downsize") {
        var currentFontSize = document.getElementById("wysiwyg"+n).contentWindow.document.queryCommandValue("FontSize");
        if(currentFontSize > 1) {
            var newFontSize = currentFontSize - 1;
        } else {
            var newFontSize = currentFontSize;
        }
        document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("FontSize", false, newFontSize);
    }
    else if(id=="link") {
        var szURL = prompt("Bitte geben sie eine URL an:", "http://");
        document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("CreateLink", false, szURL);
    }
    else if(id=="unlink") {
        document.getElementById("wysiwyg" + n).contentWindow.document.execCommand("Unlink", false, null);
    }
    else if(id=="video") {
        var url = prompt("Bitte geben sie die YouTube Url an:", "http://www.youtube.com/watch?v=");

        if (url != "http://www.youtube.com/watch?v=") {
            var div = document.getElementById("wysiwyg" + n).contentWindow.document.createElement("div");
            $(div).attr("style", "display:block; background-color:#c00; height:350px; width:500px; z-index:1;");
            var vid = url.substring(31);
            vid = youTubeUrl+"/v/"+vid;

            var embedded = youTubeTemplate[0]+vid+youTubeTemplate[1]+vid+youTubeTemplate[2];
            div.innerHTML = embedded;
            var node = insertNodeAtSelection(div, n);
        } else
            alert("Die angegebene URL ist ungültig.");
    }
    else {
        document.getElementById("wysiwyg" + n).contentWindow.document.execCommand(id, false, null);
    }
};

function insertHTML(html, n) {
    var browserName = navigator.appName;
    if (browserName == "Microsoft Internet Explorer") {
        document.getElementById('wysiwyg' + n).contentWindow.document.selection.createRange().pasteHTML(html);
    }
	 
    else {
        var div = document.getElementById('wysiwyg' + n).contentWindow.document.createElement("div");
		 
        div.innerHTML = html;
        var node = insertNodeAtSelection(div, n);
    }
}

function insertNodeAtSelection(insertNode, n) {
    // get current selection
    var sel = document.getElementById('wysiwyg' + n).contentWindow.getSelection();

    // get the first range of the selection
    // (there's almost always only one range)
    var range = sel.getRangeAt(0);

    // deselect everything
    sel.removeAllRanges();

    // remove content of current selection from document
    range.deleteContents();

    // get location of current selection
    var container = range.startContainer;
    var pos = range.startOffset;

    // make a new range for the new selection
    range=document.createRange();

    if (container.nodeType==3 && insertNode.nodeType==3) {

        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);
    }
	
    else {
        var afterNode;
    
        if (container.nodeType==3) {
            // when inserting into a textnode
            // we create 2 new textnodes
            // and put the insertNode in between

            var textNode = container;
            container = textNode.parentNode;
            var text = textNode.nodeValue;

            // text before the split
            var textBefore = text.substr(0,pos);
            // text after the split
            var textAfter = text.substr(pos);

            var beforeNode = document.createTextNode(textBefore);
            afterNode = document.createTextNode(textAfter);

            // insert the 3 new nodes before the old one
            container.insertBefore(afterNode, textNode);
            container.insertBefore(insertNode, afterNode);
            container.insertBefore(beforeNode, insertNode);

            // remove the old node
            container.removeChild(textNode);
        }
	
        else {
            // else simply insert the node
            afterNode = container.childNodes[pos];
            container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
    }

    sel.addRange(range);
};


function updateTextArea(n) {
    document.getElementById(n).value = document.getElementById("wysiwyg" + n).contentWindow.document.body.innerHTML;
};


function grabSelectedText(n){ 
    var browserName = navigator.appName;
    var selectedText = '';
    // Grab Selected Text for IE
    if (browserName == "Microsoft Internet Explorer") {
        var theText = document.getElementById("wysiwyg" + n).contentWindow.document.selection;
        if(theText.type =='Text')   {
            var newText = theText.createRange();
            selectedText = newText.text;
        }
    }
    // Grab Selected Text for Mozilla/Netscape
    else {
        var selectedText = document.getElementById("wysiwyg" + n).contentWindow.document.getSelection();
    }
    return selectedText;
} 