
/*
Editor Plugin is GPL  Copyright (C) 2007 by Nicolas Alves
Web site = http://www.guppytop.com/
This file is based on :
---------------------------------------------------------------	  
KOIVI TTW WYSIWYG Editor Copyright (C) 2005 Justin Koivisto
Version 3.2.2
Last Modified: 4/27/2005
    Justin Koivisto
    justin.koivisto@gmail.com
    http://www.koivi.com
*/
/**
*   WYSIWYG_Editor
*
*   Class constructor. Configures and displays the editor object according to values passed
*   This is an implementation of OO-Javascript based on my previous editor.
*
*   @param  instance_name   string  the name of the variable you assigned to this instance ( example: myEdt = new WYSIWYG_Editor('myEdt'); )
*also used as the basis for the name and id attributes for this editor instance in the HTML (hidden input and iframe)
*   @param  content  string  a string of the content to display in the editor.
*   @param  path     string  the URI path to this directory to use for editor components (pallete, iamges, etc.)
*   @param  fwidth   int     the width in pixels of the editor interface on the screen
*   @param  fheight  int     the height in pixels of the editor interface on the screen
*   @param  styleHref       string  the URI of the stylesheet to use for the editor's content window
*   @param  pj      pour afficher ou non le boutton pièces jointes
*   @param  ajoutimg      pour afficher ou non le boutton ajout d'images
**/
function WYSIWYG_Editor(instance_name, content, path, fwidth, fheight, styleHref,pj,ajoutimg){
    // the name used to create the object - used to define the name of the field that contains the HTML on submission
    if(typeof(instance_name)=='undefined'){
 alert("ERROR: No instance name was passed for the editor.");
 return false;
    }else{
 this.instance_name=instance_name;
    }

    // the initial HTML source content for the editor
if(typeof(content)=='undefined'){
 this.content='';
    }else{
 this.content=content;
    }

    // define the path to use for the editor components like img
    if(typeof(path)=='undefined'){
 this.wysiwyg_path='.'; // default value
    }else{
 path.replace(/[\/\\]$/,''); // remove trailing slashes
 this.wysiwyg_path=path;
    }
    
     // define the stylesheet to use for the editor components like images
    if (typeof(styleHref) == 'undefined') {
        this.stylesheet = ''; // default value
    } else {
        this.stylesheet = styleHref;
    }
    if (typeof(pj) == 'undefined') {
        this.pj = ''; // default value
    } else {
        this.pj = pj;
    }
		if (typeof(ajoutimg) == 'undefined') {
        this.ajoutimg = ''; // default value
    } else {
        this.ajoutimg = ajoutimg;
    }
    // define the pixel dimensions of the editor
    if(typeof(fwidth)=='number' && Math.round(fwidth) > 50){
 this.frame_width=Math.round(fwidth); // default value
    }else{
 this.frame_width=548; // default width
    }
    if(typeof(fheight)=='number' && Math.round(fheight) > 50){
 this.frame_height=Math.round(fheight);
    }else{
 this.frame_height=250; // default height
    }
    // properties that depended on the validated values above
    this.wysiwyg_content=this.instance_name+'_WYSIWYG_Editor';  // the editor IFRMAE element id
    this.wysiwyg_hidden=this.instance_name+'_content';   // the editor's hidden field to store the HTML in for the post
    this.ta_rows=Math.round(this.frame_height/15);// number of rows for textarea for unsupported browsers
    this.ta_cols=Math.round(this.frame_width/8);  // number of cols for textarea for unsupported browsers
    // other property defaults
    this._X = this._Y = 0;      // these are used to determine mouse position when clicked

this.lang = new Array();		
}

/**
*   Use this in the onSubmit event for the form that the editor is displayed inside.
*   Puts the HTML content into a hidden form field for the submission
**/
WYSIWYG_Editor.prototype.prepareSubmit = function (){
    var htmlCode=document.getElementById(this.wysiwyg_content).contentWindow.document.body.innerHTML;
    htmlCode = htmlCode.replace(/<sup>/ig, "<br /><div><span style='padding: 2px; position:relative; top:4px; left:10px; background-color: #FEFEFE;'><i>Dixit...</i></span><div style='padding: 4px; background-color: #FEFEFE; border: 1px solid #000099'>");
    htmlCode = htmlCode.replace(/<\/sup>/ig, "</div></div><br />");
    htmlCode = htmlCode.replace(/<P>/ig, "<p>");
    htmlCode = htmlCode.replace(/<IMG/ig, "<img");
    htmlCode = htmlCode.replace(/<br>/gi,"<br />");
    htmlCode = htmlCode.replace(/<hr>/gi,"<hr />");
    htmlCode = htmlCode.replace(/<hr>/gi,"<hr />");
    htmlCode = htmlCode.replace(/&nbsp;/gi,"");
    
    document.getElementById(this.wysiwyg_hidden).value=htmlCode;
    return true;
}

WYSIWYG_Editor.prototype.display = function (){
if(this.isSupported()){
 this._display_editor();
 this._load_content();
 var thedoc = document.getElementById(this.wysiwyg_content).contentWindow.document;
 thedoc.designMode='On';
 thedoc.open();
 thedoc.write('<html><head>');
 if(this.stylesheet) {
			//must be done after the document has been opened
thedoc.write('<link rel="stylesheet" type="text/css" media="screen" href="' + this.stylesheet + '.css" />');
			//Patch CSS for IE browsers
thedoc.write('<!--[if lte IE 7]>');
thedoc.write('<link rel="stylesheet" type="text/css" media="screen" href="' + this.stylesheet + '-ie.css" />');
thedoc.write('<![endif]-->');
		}
 thedoc.write('</head><body>');
 thedoc.write(this.content);
 thedoc.write('</body></html>');
 thedoc.close();
    }else{
 this._display_textarea();
 this._load_content();
    }
}

/**
*   WYSIWYG_Editor::isMSIE
*   Checks if browser is MSIE by testing the document.all property that is only supported by MSIE and AOL
*/
WYSIWYG_Editor.prototype.isMSIE = function ()
{
    if (typeof(document.all) == 'object' && typeof(window.opera) != 'object') {
        return true;
    } else {
        return false;
    }
}

/** WYSIWYG_Editor::isOPERA9  Checks if browser is OPERA 9+ */
WYSIWYG_Editor.prototype.isOPERA = function (){
    if (typeof(window.opera) == 'object') {
		return true;
    } else {
        return false;
    }
}

/**
*   WYSIWYG_Editor::isGECKO
*   Checks if browser is Gecko browsers
*/
WYSIWYG_Editor.prototype.isGECKO = function ()
{
    if (typeof(window.sidebar) == 'object' && typeof(window.opera) != 'object') {
        return true;
    } else {
        return false;
    }
}

WYSIWYG_Editor.prototype._display_textarea = function (){
    document.write('<p style="background-color: yellow; color: red; padding: 3px;"><b>WARNING:</b> Your browser does not support the WYSIWYG_Editor class. The script you are posting to may expect HTML code.</p>');
    document.write('<textarea name="'+this.wysiwyg_hidden+'" id="'+this.wysiwyg_hidden+'" rows="'+this.ta_rows+'" cols="'+this.ta_cols+'"></textarea><br />');
}

WYSIWYG_Editor.prototype._display_editor = function (){
DisplayEditor=('<textarea name="'+this.wysiwyg_hidden+'" id="'+this.wysiwyg_hidden+'" style="visibility:hidden;display:none;"></textarea>');
DisplayEditor+=('<table cellpadding="5" cellspacing="0" border="0" id="'+this.instance_name+'_table" class="menuediteur" style="width:'+(this.frame_width+2)+'px;">');
DisplayEditor+=('<tr><td valign="top" align="center" width="95%" id="instance_table">');
DisplayEditor+=('<img alt="'+ this.lang[5]+ '" title="'+ this.lang[5]+'" class="cursor" src="'+this.wysiwyg_path+'/img/bold.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/bold_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/bold.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'bold\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[4]+'" title="'+ this.lang[4]+'" class="cursor" src="'+this.wysiwyg_path+'/img/italic.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/italic_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/italic.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'italic\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[6]+'" title="'+ this.lang[6]+'" class="cursor" src="'+this.wysiwyg_path+'/img/underline.gif"  onmouseover="this.src=\''+this.wysiwyg_path+'/img/underline_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/underline.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'underline\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[1]+'" title="'+ this.lang[1]+'" class="cursor" src="'+this.wysiwyg_path+'/img/left.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/left_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/left.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'justifyleft\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[2]+'" title="'+ this.lang[2]+'" class="cursor" src="'+this.wysiwyg_path+'/img/center.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/center_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/center.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'justifycenter\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[3]+'" title="'+ this.lang[3]+'" class="cursor" src="'+this.wysiwyg_path+'/img/right.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/right_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/right.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'justifyright\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[14]+'" title="'+ this.lang[14]+'" class="cursor" src="'+this.wysiwyg_path+'/img/full.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/full_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/full.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'justifyfull\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[10]+'" title="'+ this.lang[10]+'" class="cursor" src="'+this.wysiwyg_path+'/img/undo.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/undo_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/undo.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'undo\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[11]+'" title="'+ this.lang[11]+'" class="cursor" src="'+this.wysiwyg_path+'/img/redo.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/redo_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/redo.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'redo\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[7]+'" title="'+ this.lang[7]+'" class="cursor" src="'+this.wysiwyg_path+'/img/forecolor.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/forecolor_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/forecolor.gif\';" onClick="return hs.htmlExpand(this,{contentId: \'forecolor\'});">');
DisplayEditor+=('<img alt="'+ this.lang[8]+'" title="'+ this.lang[8]+'" class="cursor" src="'+this.wysiwyg_path+'/img/backcolor.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/backcolor_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/backcolor.gif\';" onClick="return hs.htmlExpand(this,{contentId: \'hilitecolor\' } );">');
DisplayEditor+=('<img alt="'+ this.lang[9]+'" title="'+ this.lang[9]+'" class="cursor" src="'+this.wysiwyg_path+'/img/link.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/link_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/link.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'createlink\',\'\')">');
DisplayEditor+=('<img alt="'+ this.lang[13]+'" title="'+ this.lang[13]+'" class="cursor" src="'+this.wysiwyg_path+'/img/smiley.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/smiley_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/smiley.gif\';" onClick="return hs.htmlExpand(this, { contentId: \'smileys\' } )">');
if(this.ajoutimg){
DisplayEditor+=('<img alt="'+ this.lang[12]+'" title="'+ this.lang[12]+'" class="cursor" src="'+this.wysiwyg_path+'/img/img.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/img_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/img.gif\';" onClick="return hs.htmlExpand(this, { contentId: \'img\' } )">');
}
DisplayEditor+=('<img alt="'+ this.lang[15]+'" title="'+ this.lang[15]+'" class="cursor" src="'+this.wysiwyg_path+'/img/say.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/say_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/say.gif\';" onClick="'+this.instance_name+'.doTextFormat(\'superscript\',\'\')">');
if(this.pj){
DisplayEditor+=('<img alt="'+ this.lang[16]+'" title="'+ this.lang[16]+'" class="cursor" src="'+this.wysiwyg_path+'/img/pj.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/pj_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/pj.gif\';" onClick="PopupWindow(\'../../plugnico/editor/pj.php?lng=<?php echo $lng; ?>\',\'page\',520,500,\'n\',\'yes\')">');
}
DisplayEditor+=('</td><td width="5%"><img alt="'+ this.lang[17]+'" title="'+ this.lang[17]+'" class="cursor" src="'+this.wysiwyg_path+'/img/help.gif" onmouseover="this.src=\''+this.wysiwyg_path+'/img/help_on.gif\';" onmouseout="this.src=\''+this.wysiwyg_path+'/img/help.gif\';" onClick="return hs.htmlExpand(this,{contentId: \'help\' } );">');
DisplayEditor+=('</td></tr></table>');
DisplayEditor+=('<iframe id="'+this.wysiwyg_content+'" style="margin-left: 3px;margin-right: 3px;margin-bottom: 3px; border: 1px solid #666666;background-color: white; color: black; width:'+this.frame_width+'px; height:'+this.frame_height+'px;"></iframe>');
DisplayEditor+=('<br />');
document.write(DisplayEditor);
}

/**
*   WYSIWYG_Editor::doTextFormat
*
*   Apply a text formatting command to the selected text in the editor (or starting at the current cursor position)
*
*   @param  command string  Which of the editor/browser text formatting commands to apply
**/
WYSIWYG_Editor.prototype.doTextFormat = function (command, optn, evnt){
  if(command=='createlink'){
 var szURL=prompt(''+ this.lang[33]+'', 'http://');
 if(document.getElementById(this.wysiwyg_content).contentWindow.document.queryCommandEnabled(command)){
     document.getElementById(this.wysiwyg_content).contentWindow.document.execCommand('CreateLink',false,szURL);
     return true;
 }else return false;
    }else{
 if(document.getElementById(this.wysiwyg_content).contentWindow.document.queryCommandEnabled(command)){
document.getElementById(this.wysiwyg_content).contentWindow.document.execCommand(command, false, optn);
return true;
   }else return false;
    }
    document.getElementById(this.wysiwyg_content).contentWindow.focus();
}

/**
*   Puts the passed content into the editor or textarea (Use this one *after* displaying the editor.)
*   @param  content string  The string containing the properly-formatted HTML source to use
*/
WYSIWYG_Editor.prototype._load_content = function (){
    document.getElementById(this.wysiwyg_hidden).value=this.content;
}

/**   WYSIWYG_Editor::isSupported  Checks that the browser supports this programming by writing an invisible IFRAME and testing its properties */
WYSIWYG_Editor.prototype.isSupported = function () {
    // This is to get rid of the browser UA check that was previously implemented for this class.
    // should be called from somewhere in the body of the document for best results
    document.write('<iframe id="WYSIWYG_Editor_Testing_Browser_Features" style="display: none; visibility: hidden;"></iframe>');
    test = typeof(document.getElementById('WYSIWYG_Editor_Testing_Browser_Features').contentWindow);
    if(test == 'object'){
 return true;
    }else{
 return false;
    }
    return this.supported;
}

/**
*   WYSIWYG_Editor::InserPieceJointe
*   Generates the InserPieceJointe that will be insert in the editor.
*   @param  html string  The html that indicates which text is being set
**/
WYSIWYG_Editor.prototype.InserPieceJointe = function (html){
var thisPJ = '<a href="../../plugnico/tinymsg.php?lng=<?php echo $lng; ?>&zip='+html+'"><img src="../../plugnico/editor/img/pj.gif" alt="Image" border="0" />'+html+'</a>';		
		if (this.isGECKO()) {
		document.getElementById(this.wysiwyg_content).contentWindow.focus();
		document.getElementById(this.wysiwyg_content).contentWindow.document.execCommand('insertHTML', false,thisPJ);
  	}
		if (this.isMSIE()) {
		window.frames[this.wysiwyg_content].focus();
  	window.frames[this.wysiwyg_content].document.selection.createRange();
		window.frames[this.wysiwyg_content].document.selection.createRange().pasteHTML(thisPJ);
		window.frames[this.wysiwyg_content].document.selection.createRange().collapse(true);
		window.frames[this.wysiwyg_content].document.selection.createRange().select();
 	}
		if (this.isOPERA()) {
		document.getElementById(this.wysiwyg_content).contentWindow.focus();
		document.getElementById(this.wysiwyg_content).contentWindow.document.execCommand('insertHTML', false,thisPJ);
 	}
}

/** addImage Puts the image into the editor iframe (Called from the pop-up window that insertImage creates) **/
WYSIWYG_Editor.prototype.addImage = function (thisimage){
document.getElementById(this.wysiwyg_content).contentWindow.focus();
 var idimg=document.getElementById(this.wysiwyg_content).contentWindow.document;
 idimg.execCommand('insertimage', false, thisimage);
 document.getElementById(this.wysiwyg_content).contentWindow.focus();
}


WYSIWYG_Editor.prototype.setColor = function (color, command){
    // only one difference for MSIE
    if(this.isMSIE() && command == 'hilitecolor') command = 'backcolor';
    //get current selected range
    var sel=document.getElementById(this.wysiwyg_content).contentWindow.document.selection;
    if(sel!=null){
     rng=sel.createRange();
    }
    document.getElementById(this.wysiwyg_content).contentWindow.focus();
    if(document.getElementById(this.wysiwyg_content).contentWindow.document.queryCommandEnabled(command)){
        document.getElementById(this.wysiwyg_content).contentWindow.document.execCommand(command, false, color);
    }else return false;
    document.getElementById(this.wysiwyg_content).contentWindow.focus();
    return true;
}
/** Variables pour construire les palettes de couleurs **/
var nomcouleurs = new Array('#FF0000','#FFFF00','#00FF00','#00FFFF','#0000FF','#FF00FF','#FFFFFF','#F5F5F5','#DCDCDC',
'#D3D3D3','#C0C0C0','#A9A9A9','#808080','#696969','#000000','#2F4F4F','#708090','#778899','#4682B4','#4169E1',
'#6495ED','#B0C4DE','#7B68EE','#6A5ACD','#483D8B','#191970','#000080','#00008B','#0000CD','#1E90FF','#00BFFF',
'#87CEFA','#87CEEB','#ADD8E6','#B0E0E6','#F0FFFF','#E0FFFF','#AFEEEE','#48D1CC','#20B2AA','#008B8B','#008080',
'#5F9EA0','#00CED1','#00FFFF','#40E0D0','#7FFFD4','#66CDAA','#8FBC8F','#3CB371','#2E8B57','#006400','#008000',
'#228B22','#32CD32','#00FF00','#7FFF00','#7CFC00','#ADFF2F','#98FB98','#90EE90','#00FF7F','#00FA9A','#556B2F',
'#6B8E23','#808000','#BDB76B','#B8860B','#DAA520','#FFD700','#F0E68C','#EEE8AA','#FFEBCD','#FFE4B5','#F5DEB3',
'#FFDEAD','#DEB887','#D2B48C','#BC8F8F','#A0522D','#8B4513','#D2691E','#CD853F','#F4A460','#8B0000','#800000',
'#A52A2A','#B22222','#CD5C5C','#F08080','#FA8072','#E9967A','#FFA07A','#FF7F50','#FF6347','#FF8C00','#FFA500',
'#FF4500','#DC143C','#FF0000','#FF1493','#FF00FF','#FF69B4','#FFB6C1','#FFC0CB','#DB7093','#C71585','#800080',
'#8B008B','#9370DB','#8A2BE2','#4B0082','#9400D3','#9932CC','#BA55D3','#DA70D6','#EE82EE','#DDA0DD','#D8BFD8',
'#E6E6FA','#F8F8FF','#F0F8FF','#F5FFFA','#F0FFF0','#FAFAD2','#FFFACD','#FFF8DC','#FFFFE0','#FFFFF0','#FFFAF0',
'#FAF0E6','#FDF5E6','#FAEBD7','#FFE4C4','#FFDAB9','#FFEFD5','#FFF5EE','#FFF0F5','#FFE4E1','#FFFAFA');


