/**********************************************************************************
 * Classes definition section
 **********************************************************************************/

 // Class for symbol replacement
function ExpRep(regExp, strRep)
{
  this.m_regExp = regExp;   // Regular expression to be replaced
  this.m_strRep = strRep;   // Replacement string
}

// Class to define an Icon in a list above comment edit field
function CommentIcon(urlIcon, strBeg, strEnd)
{
  this.m_urlIcon = urlIcon; // url of the Icon to display
  this.m_strBeg  = strBeg;  // String that is inserted before the selection (or at the cursor position)
  this.m_strEnd  = strEnd;  // String that is inserted after the selection
}

/**********************************************************************************
 * Data definition section
 **********************************************************************************/

// Definition of symbols (defined by regular expression) to be replaced by HTML tags
var i = 0;
expRepArray = new Array();
expRepArray[i++]  = new ExpRep("(:\\))|(:-\\))|(:o\\))",
                             "<img alt=\":)\" src=\"http://storage.canalblog.com/81/12/101627/32704947.png\" />");
expRepArray[i++]  = new ExpRep("(:\\'\\()|(:\\'-\\()|(:\\'o\\()",
                             "<img alt=\":'(\" src=\"http://storage.canalblog.com/77/59/101627/32704945.gif\" />");
expRepArray[i++]  = new ExpRep("(:D)|(:-D)|(:oD)",
                             "<img alt=\":D\" src=\"http://storage.canalblog.com/35/58/101627/32704938.gif\" />");
expRepArray[i++]  = new ExpRep("(:P)|(:-P)|(:oP)",
                             "<img alt=\":P\" src=\"http://storage.canalblog.com/22/12/101627/32704932.gif\" />");
expRepArray[i++]  = new ExpRep("(:love:)|(<3)",
                             "<img alt=\":love:\" src=\"http://storage.canalblog.com/62/84/101627/32704917.gif\" />");
expRepArray[i++]  = new ExpRep("(:S)|(:-S)|(:oS)",
                             "<img alt=\":S\" src=\"http://storage.canalblog.com/82/73/101627/32704901.png\" />");
expRepArray[i++]  = new ExpRep("(:\\()|(:-\\()|(:o\\()",
                             "<img alt=\":(\" src=\"http://storage.canalblog.com/41/36/101627/32704891.gif\" />");
expRepArray[i++]  = new ExpRep("(:\\|)|(:-\\|)|(:o\\|)|(:-/)|(:o/)",
                             "<img alt=\":|\" src=\"http://storage.canalblog.com/02/14/101627/32704886.gif\" />");
expRepArray[i++]  = new ExpRep("(;\\))|(;-\\))|(;o\\))|(;D)|(;-D)|(;oD)",
                             "<img alt=\";)\" src=\"http://storage.canalblog.com/41/38/101627/32704879.gif\" />");
expRepArray[i++]  = new ExpRep(":cat:",
                             "<img alt=\":cat:\" src=\"http://storage.canalblog.com/87/57/101627/32704867.png\" />");



// Definition of icons : the image and the symbol text that is inserted around the selection
var i = 0;
iconsArray = new Array();
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/81/12/101627/32704947.png",
                                   ":)", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/77/59/101627/32704945.gif",
                                   ":'(", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/35/58/101627/32704938.gif",
                                   ":D", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/22/12/101627/32704932.gif",
                                   ":P", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/62/84/101627/32704917.gif",
                                   ":love:", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/82/73/101627/32704901.png",
                                   ":S", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/41/36/101627/32704891.gif",
                                   ":(", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/02/14/101627/32704886.gif",
                                   ":|", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/41/38/101627/32704879.gif",
                                   ";)", "");
iconsArray[i++]  = new CommentIcon("http://storage.canalblog.com/87/57/101627/32704867.png",
                                   ":cat:", "");


/**********************************************************************************
 * Function definition section
 * REGULAR FUNCTIONS
 **********************************************************************************/


/**********************************************************************************
 * FUNCTION : formatComment()
 **********************************************************************************
 * Function called to display a comment body by replacing symbols defined in
 * expRepArray by their corresponding HTML tags.
 * INPUT :  p_str   String containing the comment body
 **********************************************************************************/
function formatComment(p_str)
{
  var v_str = p_str;
  for(i in expRepArray)
  {
    var v_exp = new RegExp(expRepArray[i].m_regExp, "gi");
    v_str = v_str.replace(v_exp, expRepArray[i].m_strRep);
    delete v_exp;
  }
  document.write(v_str);
}

/**********************************************************************************
 * FUNCTION : displayCommentIcons()
 **********************************************************************************
 * Display the list of icons defined in iconsArray.
 **********************************************************************************/
function displayCommentIcons()
{
  for(i in iconsArray)
  {
    document.write("<img class=\"comIconImg\" alt=\"" + iconsArray[i].m_strBeg + iconsArray[i].m_strEnd +
                   "\" src=\"" + iconsArray[i].m_urlIcon +
                   "\" onclick=\"insertIconTag(" + i + ")\">");
  }
}

/**********************************************************************************
 * FUNCTION : insertTextBegEnd()
 **********************************************************************************
 * Insert in the textarea commentBody of the comment form of the blog a couple
 * of strings around the selection.
 **********************************************************************************/
function insertTextBegEnd(p_strBeg, p_strEnd)
{
  var input = document.forms['frmComment'].elements['commentBody'];
  input.focus();
  // For IE
  if(typeof document.selection != 'undefined') {
    // Format code insertion
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = p_strBeg + insText + p_strEnd;
    // Adjust cursor position
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -p_strEnd.length);
    } else {
      range.moveStart('character', p_strBeg.length + insText.length + p_strEnd.length);
    }
    range.select();
  }
  // For Gecko based browsers
  else if(typeof input.selectionStart != 'undefined')
  {
    // Format code insertion
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + p_strBeg + insText + p_strEnd + input.value.substr(end);
    // Adjust cursor position
    var pos;
    if (insText.length == 0) {
      pos = start + p_strBeg.length;
    } else {
      pos = start + p_strBeg.length + insText.length + p_strEnd.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  // For the other browsers
  else
  {
    var pos = input.value.length;
    // Format code insertion at the end
    input.value = input.value + p_strBeg + p_strEnd;
  }
}

/**********************************************************************************
 * FUNCTION : insertIconTag()
 **********************************************************************************
 * Insert a comment tag in the comment edit field.
 * INPUT :  p_i    Index of icon tag to insert in the comment field.
 **********************************************************************************/
function insertIconTag(p_i)
{
  insertTextBegEnd(iconsArray[p_i].m_strBeg, iconsArray[p_i].m_strEnd);
}
