/** 
 * NOTE: Les fonctions forum_addTextToMCE et forum_getTinyMCEContent utilisent la finction $$ de prototype.
 */

/**
 * Récupere le text d'un post pour en faire une citation.
 * 
 * idToQuote - id de l'element contenant le post
 */
function forum_quotePost(idToQuote) 
{
    var txt = '';
    
    if (window.getSelection) { txt = window.getSelection().toString(); }
    else if (document.getSelection) { txt = document.getSelection().toString(); }
    else if (document.selection) { txt = document.selection.createRange().text; }
    
    txt = txt.replace(/<\/?([a-zA-Z])[^>]*>/g, '');
    txt = txt.replace(/&nbsp;/g, ' ');
    txt = txt.replace(/\\r\\n\\r\\n/g, '<br />');
    txt = txt.replace(/\\n/g, '<br />');
    txt = txt.replace(/\\r/g, '<br />');
    
    var idToQuoteContent = document.getElementById(idToQuote).innerHTML;
    idToQuoteContent = idToQuoteContent.replace(/<\/?([a-zA-Z])[^>]*>/g, '');
    idToQuoteContent = idToQuoteContent.replace(/&nbsp;/g, ' ');
    idToQuoteContent = idToQuoteContent.replace(/\\n/g, '<br />');
    
    var res = idToQuoteContent.indexOf(txt);
    if(res == -1) { txt = ''; }
    else if(res == 0 && txt == '') { txt = idToQuoteContent; }
    
    return txt;
}

/**
 * Ajoute le texte d'une citation dans le text de tiny mce.
 * 
 * string author - nom de l'auteur du post initial
 * string str - texte de la citation
 * string scrollToElmt - id de l'elemet bers lequel il faut scroller (le champ de creatin d'un post ou la citation est ajouté par exemple)
 */
function forum_addTextToMCE(author, str, scrollToElmt) 
{
    if(str) 
    {
        var containers = $$('.mceIframeContainer');
        var iframes = containers[0].getElementsByTagName('iframe');
        var mceIframe = iframes[0];
        
        var strToAdd = '<p style=\"border-right: 2px #666 solid; border-left: 2px #666 solid; background-color: #FFD; color: #666; padding: 2px; margin-left: 10px;\">';
        strToAdd = strToAdd + '<label style=\"color: #666; font-style: italic;\">- ' + author + ' a dit :</label><br />' + str + '</p><p> <br /></p>';
        var body = mceIframe.contentWindow.document.body.innerHTML + strToAdd;
        mceIframe.contentWindow.document.body.innerHTML = body;
        
        document.getElementById(scrollToElmt).style.display = 'block';
        new Effect.ScrollTo(scrollToElmt, { duration: 0.5 });
    }
    else { alert('Sélectionnez du texte écrit dans ce post.'); }
}

/**
 * Fonction permettant de d'affecter la valeur du widget tiny mce au textarea du formulaire lors d'appel ajax.
 *
 * string textareaClass - class css du textarea pour lequel on utilise tiny mce.
 */
function forum_getTinyMCEContent(textareaClass) 
{
    var containers = $$('.mceIframeContainer');
    var iframes = containers[0].getElementsByTagName('iframe');
    var mceIframe = iframes[0];
    var mceContent = mceIframe.contentWindow.document.body.innerHTML;
    var textearea = $$(textareaClass);
    textearea[0].value = mceContent;
}