/*
Author   : bieler batiste
Company  : doSimple : http://www.dosimple.ch
send me a mail for more informations : faden@PASDEPOURRIELaltern.org - remove ( PASDEPOURRIEL )

Short javascript function to create a rich text form for Mozilla and Internet Explorer

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

function onFormSubmit(textarea)
{
        var currentNode = textarea.parentNode;
        while( currentNode.tagName!="FORM" )
        {
                currentNode = currentNode.parentNode;
        }
        currentNode.onsubmit=checkIframe;
}

function checkIframe()
{
        var iframes = document.getElementsByTagName('iframe');
        while( iframes.length>0 )
        {
                eval(iframes[0].id+".textareaEditMode();");
                iframes = document.getElementsByTagName('iframe');
        }
}

function Rte(id,css,charset)
{
        // variables
        this.textarea = document.getElementById(id);
        
        if( !document.designMode )
                return false;

        onFormSubmit(this.textarea);
        this.id = id;
        this.css = css;
        this.charset = charset;
        
        this.frame = document.createElement("iframe");
        this.frame.id = id;
        this.frame.frameBorder=0;
        this.frame.frameMargin=0;
        this.frame.framePadding=0;
        this.frame.className = "richTextFrame";
        
        this.toolbar = document.createElement("div");
        this.toolbar.innerHTML=toolbarHTML(id);
        //this.toolbar.onmouseover = function(){alert('over')};
        
        onFormSubmit(this.textarea);
        this.container = this.textarea.parentNode;
        this.content = this.textarea.value;
        this.previousVersion = this.content;
        this.currentEditMode = "textarea";
        
        this.container.parentNode.insertBefore(this.toolbar,this.container);
        
        // fonctions
        this.loadCanevas = loadCanevas;
        this.designMode = designMode;
        this.reloadContent = reloadContent;
        this.textareaEditMode = textareaEditMode;
        this.rteEditMode = rteEditMode;
        this.formatText = formatText;
        this.select = select;
        this.directInsertImage = directInsertImage;
        this.creatLink = creatLink;
        
}

function getSelectionElement(rte)
{
        /* IE selections */
        if (rte.frame.contentWindow.document.selection)
        {
                selection = rte.frame.contentWindow.document.selection;
                range = selection.createRange();
                try
                {
                        node = range.parentElement();
                }
                catch (e)
                {
                        return false;
                }
        }
        /* Mozilla selections */
        else
        {
                try
                {
                        selection = rte.frame.contentWindow.getSelection();
                }
                catch (e)
                {
                        return false;
                }
                range = selection.getRangeAt(0);
                node = range.commonAncestorContainer;
        }
        return node;
}

function setSelectedType(rte,node)
{
        selectToolBar = document.getElementById('toolBarSelect'+rte.id);
        while(node.parentNode)
        {
                nName = node.nodeName.toLowerCase();
                for(var i=0;i<selectToolBar.options.length;i++)
                {
                        if(nName==selectToolBar.options[i].value)
                        {
                                selectToolBar.selectedIndex=i;
                                return true;
                        }
                }
                
                node = node.parentNode;
        }

        selectToolBar.selectedIndex=0;
        return true;
}

function loadCanevas(content)
{
    if( document.all )
    {
        var css="<link rel=\"stylesheet\" href=\""+this.css+"\"/>";
    }
    else
    {
        // to correct a little Mozilla bug
        var css="<link rel=\"stylesheet\"/>";
    }
        var doc = "\
        <html>\
                <head>\
                        <meta http-equiv=\"Content-Type\" content=\"text/html; charset="+this.charset+"\" />\
                        "+css+"\
                </head>\
                <body class=\"frameBody\">"+content+"</body>\
        </html>";
        this.frame.contentWindow.document.open();
        this.frame.contentWindow.document.write(doc);
        this.frame.contentWindow.document.close();
}

function designMode() 
{
        if (document.contentEditable)
        {
                this.frame.contentWindow.document.designMode = "On";
                this.currentEditMode = "richText";
                return true;
        }
        else if (document.designMode != null)
        {
                try
                {
                        this.frame.contentWindow.document.designMode = "on";
                        this.currentEditMode = "richText";
                        return true;
                }
                catch (error)
                {
                        setTimeout(function(){this.designMode()}, 250);
                        return false;
                }
        }
        setTimeout(function(){this.designMode()}, 250);
        return false;
}

function textareaEditMode()
{
        if( this.currentEditMode=="richText" )
        {
                this.currentEditMode = "textarea";
                this.content = this.frame.contentWindow.document.getElementsByTagName("body")[0].innerHTML;
                this.frame.parentNode.replaceChild(this.textarea, this.frame);
                this.textarea.value= this.content;
        }
}

function rteEditMode()
{
        if( this.currentEditMode=="textarea" )
        {
               try{
                        this.currentEditMode="richText";
                        this.content = this.textarea.value;
                        this.textarea.parentNode.replaceChild(this.frame,this.textarea);
                        this.loadCanevas(this.content);
                        this.frame.contentWindow.document.getElementsByTagName("link")[0].href=this.css;
                        this.designMode();
                        var self = this;
			if (typeof document.addEventListener == "function")
                        {

                                this.frame.contentWindow.document.addEventListener("mouseup", function(){setSelectedType(self,getSelectionElement(self));return true;}, false);
                                this.frame.contentWindow.document.addEventListener("keyup", function(){setSelectedType(self,getSelectionElement(self));return true;}, false);
                        }
                        else
                        {
                                this.frame.contentWindow.document.attachEvent("onmouseup", function(){setSelectedType(self,getSelectionElement(self));return true;});
                                this.frame.contentWindow.document.attachEvent("onkeyup", function(){setSelectedType(self,getSelectionElement(self));return true;});
                        }
                        //this.frame.contentWindow.document.execCommand("useCSS", false, true);
                }
                catch(error)
                {
			this.currentEditMode="textarea";
                        setTimeout(function(){this.rteEditMode()}, 250);
                }
        }
}

function formatText(command, option) 
{
        if( this.currentEditMode=="richText" )
        {
                this.frame.contentWindow.focus();
                this.frame.contentWindow.document.execCommand(command, false, option);
                this.frame.contentWindow.focus();
        }
}

function select(select){
        var selectIndex = select.selectedIndex;
        if( selectIndex!=0 )
        {
                var selected = select.options[selectIndex].value;
                selected = '<'+selected+'>';
                this.formatText( "formatblock", selected );
        }
}

function directInsertImage(imagePath)
{
        this.frame.contentWindow.focus()
        this.frame.contentWindow.document.execCommand('InsertImage', false, imagePath);
}

function creatLink(url)
{
        this.frame.contentWindow.focus()
        this.frame.contentWindow.document.execCommand('CreateLink', false, url);
}
                
function reloadContent(textConfirm)
{
        var result = confirm(textConfirm);
        if(result=="1")
        {
                this.content = this.previousVersion;
                this.loadContent();
        }
}

