/*
 * Плагин jQuery.BBCode
 * Версия 0.1 Бета
 * Разработка
 * http://www.kamaikinproject.ru
 *
 * Исправлено и дополнено DimaChe
 * Специально для http://www.afishagoroda.ru
 *
 */

/*
 * Добавляем ограничение на количество введёных символов в поле.
 * От отправки мегадлинной мессаги напрямую не спасёт, незабываем
 * резать самостоятельно при записи в БД
 */
 function limitLength(o,max)
 {
         if (o.value.length >= max)
         {
                 alert("Длина данных в этом поле ограничена " + max + " символами.");
                 o.value=o.value.substring(0,max-1);
                 return false;
         }
         return true;
 }

/*
 * Проверка панели смайлов
 */
 function check_smilepanel()
 {
   if(document.getElementById("smilepanel").style.display=="none")
     document.getElementById("smilepanel").style.display="block";
   else
     document.getElementById("smilepanel").style.display="none";
 }

/*
 * Проверка цветовой панели
 */
 function check_colorpanel()
 {
   if(document.getElementById("colorpanel").style.display=="none")
     document.getElementById("colorpanel").style.display="block";
   else
     document.getElementById("colorpanel").style.display="none";
 }

(function($){
  $.fn.bbcode = function(options){
                // настройки по умолчанию
    var options = $.extend({
      teg_bold: true,
      teg_italic: true,
      teg_underline: true,
      teg_strike: true,
      teg_linck: true,
      teg_image: false,
      button_image: true,
      //
      teg_right: true,
      teg_center: true,
      teg_left: true,
      //
      teg_smile: false,
      teg_smilewink: false,
      teg_smileevil: false,
      teg_smilepanel: true,
      //
      teg_colorpanel: true,
      //
      image_url: '/src/bbcode/bbimage/'
    },options||{});
    //  Формируем панель инструментов
    var text = '<div id="bbcode_bb_bar"><nobr>'
    if(options.teg_bold){
      text = text + '<a href="#" alt="b" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'bold.png" />';
      }else{
        text = text + 'Жирный';
      }
      text = text + '</a>';
    }
    if(options.teg_italic){
      text = text + '<a href="#" alt="i" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'italic.png" />';
      }else{
        text = text + 'Курсив';
      }
      text = text + '</a>';
    }
    if(options.teg_underline){
      text = text + '<a href="#" alt="u" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'underline.png" />';
      }else{
        text = text + 'Подчеркнутый';
      }
      text = text + '</a>';
    }
    if(options.teg_strike){
      text = text + '<a href="#" alt="s" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'strike.png" />';
      }else{
        text = text + 'Зачеркнутый';
      }
      text = text + '</a>';
    }
    if(options.teg_left){
      text = text + '<a href="#" alt="left" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'left.png" />';
      }else{
        text = text + 'По левому краю';
      }
      text = text + '</a>';
    }
    if(options.teg_center){
      text = text + '<a href="#" alt="center" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'center.png" />';
      }else{
        text = text + 'По центру';
      }
      text = text + '</a>';
    }
    if(options.teg_right){
      text = text + '<a href="#" alt="right" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'right.png" />';
      }else{
        text = text + 'По правому краю';
      }
      text = text + '</a>';
    }
    if(options.teg_linck){
      text = text + '<a href="#" alt="url[href=]" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'linck.png" />';
      }else{
        text = text + 'Ссылка';
      }
      text = text + '</a>';
    }
    if(options.teg_image){
      text = text + '<a href="#" alt="img" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'image.png" />';
      }else{
        text = text + 'Изображение';
      }
      text = text + '</a>';
    }
    if(options.teg_smile){
      text = text + '<a href="#" alt=":)" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'icon_biggrin.gif" />';
      }else{
        text = text + 'Смайл улыбающийся';
      }
      text = text + '</a>';
    }
    if(options.teg_smilewink){
      text = text + '<a href="#" alt=":wink:" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'icon_wink.gif" />';
      }else{
        text = text + 'Смайл подмигивающий';
      }
      text = text + '</a>';
    }
    if(options.teg_smileevil){
      text = text + '<a href="#" alt=":evil:" title="">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'icon_evil.gif" />';
      }else{
        text = text + 'Смайл злой';
      }
      text = text + '</a>';
    }
    if(options.teg_smilepanel){
      text = text + '<a href="#" alt="" title="" onClick="check_smilepanel(); return false;">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'icon_biggrin.gif" />';
      }else{
        text = text + 'Панель смайлов';
      }
      text = text + '</a>';
      text = text + '<div id="smilepanel" style="z-index:9999; display: none; position: absolute; width:126px; height: 120px; overflow: auto; border: 1px solid #BBB; background:#E9E8F2;filter: alpha(opacity=95, enabled=1) progid:DXImageTransform.Microsoft.Shadow(color=#CACACA,direction=135,strength=3);">';
      text = text + '<table cellpadding="0" cellspacing="0" border="0" width="120">';
      text = text + '<tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":smile:">';
      text = text + '<img style="border: none;" alt="smile" src="' + options.image_url + 'smileys/smile.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":angel:">';
      text = text + '<img style="border: none;" alt="angel" src="' + options.image_url + 'smileys/angel.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":tongue:">';
      text = text + '<img style="border: none;" alt="tongue" src="' + options.image_url + 'smileys/tongue.gif" /></a></td>';
      text = text + '</tr><tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":sad:">';
      text = text + '<img style="border: none;" alt="sad" src="' + options.image_url + 'smileys/sad.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":cry:">';
      text = text + '<img style="border: none;" alt="cry" src="' + options.image_url + 'smileys/cry.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":angry:">';
      text = text + '<img style="border: none;" alt="angry" src="' + options.image_url + 'smileys/angry.gif" /></a></td>';
      text = text + '</tr><tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":cool:">';
      text = text + '<img style="border: none;" alt="cool" src="' + options.image_url + 'smileys/cool.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":embarassed:">';
      text = text + '<img style="border: none;" alt="embarassed" src="' + options.image_url + 'smileys/embarassed.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":rolleyes:">';
      text = text + '<img style="border: none;" alt="rolleyes" src="' + options.image_url + 'smileys/rolleyes.gif" /></a></td>';
      text = text + '</tr><tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":wink:">';
      text = text + '<img style="border: none;" alt="wink" src="' + options.image_url + 'smileys/wink.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":evil:">';
      text = text + '<img style="border: none;" alt="evil" src="' + options.image_url + 'smileys/evil.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":sealedlips:">';
      text = text + '<img style="border: none;" alt="sealedlips" src="' + options.image_url + 'smileys/sealedlips.gif" /></a></td>';
      text = text + '</tr><tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":love:">';
      text = text + '<img style="border: none;" alt="love" src="' + options.image_url + 'smileys/love.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":love2:">';
      text = text + '<img style="border: none;" alt="love2" src="' + options.image_url + 'smileys/love2.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":love3:">';
      text = text + '<img style="border: none;" alt="love3" src="' + options.image_url + 'smileys/love3.gif" /></a></td>';
      text = text + '</tr><tr>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":shocked:">';
      text = text + '<img style="border: none;" alt="shocked" src="' + options.image_url + 'smileys/shocked.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":undecided:">';
      text = text + '<img style="border: none;" alt="undecided" src="' + options.image_url + 'smileys/undecided.gif" /></a></td>';
      text = text + '<td style="padding:2px;" align="center">';
      text = text + '<a href="#" alt=":grin:">';
      text = text + '<img style="border: none;" alt="grin" src="' + options.image_url + 'smileys/grin.gif" /></a></td>';
      text = text + '</tr>';
      text = text + '</table>';
      text = text + '</div>';

    }
    if(options.teg_colorpanel){
      text = text + '<a href="#" alt="" title="" onClick="check_colorpanel(); return false;">';
      if(options.button_image){
        text = text + '<img src="' + options.image_url + 'color.gif" />';
      }else{
        text = text + 'Панель цвета';
      }
      text = text + '</a>';
      text = text + '<div id="colorpanel" style="z-index:9999; display: none; position: absolute; width:153px; height: 102px; overflow: auto; border: 1px solid #BBB; background:#E9E8F2;filter: alpha(opacity=95, enabled=1) progid:DXImageTransform.Microsoft.Shadow(color=#CACACA,direction=135,strength=3);">';
text = text + '<table width="150" height="100" cellpadding="0" cellspacing="0" border="1" align="center">               ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#FFFFFF" bgcolor="#FFFFFF" width="10" height="10"><a href="#" alt="font[color=#FFFFFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCCCC" bgcolor="#FFCCCC" width="10" height="10"><a href="#" alt="font[color=#FFCCCC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCC99" bgcolor="#FFCC99" width="10" height="10"><a href="#" alt="font[color=#FFCC99]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFFF99" bgcolor="#FFFF99" width="10" height="10"><a href="#" alt="font[color=#FFFF99]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFFFCC" bgcolor="#FFFFCC" width="10" height="10"><a href="#" alt="font[color=#FFFFCC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#99FF99" bgcolor="#99FF99" width="10" height="10"><a href="#" alt="font[color=#99FF99]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#99FFFF" bgcolor="#99FFFF" width="10" height="10"><a href="#" alt="font[color=#99FFFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CCFFFF" bgcolor="#CCFFFF" width="10" height="10"><a href="#" alt="font[color=#CCFFFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CCCCFF" bgcolor="#CCCCFF" width="10" height="10"><a href="#" alt="font[color=#CCCCFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCCFF" bgcolor="#FFCCFF" width="10" height="10"><a href="#" alt="font[color=#FFCCFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#CCCCCC" bgcolor="#CCCCCC" width="10" height="10"><a href="#" alt="font[color=#CCCCCC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF6666" bgcolor="#FF6666" width="10" height="10"><a href="#" alt="font[color=#FF6666]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF9966" bgcolor="#FF9966" width="10" height="10"><a href="#" alt="font[color=#FF9966]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFFF66" bgcolor="#FFFF66" width="10" height="10"><a href="#" alt="font[color=#FFFF66]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFFF33" bgcolor="#FFFF33" width="10" height="10"><a href="#" alt="font[color=#FFFF33]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#66FF99" bgcolor="#66FF99" width="10" height="10"><a href="#" alt="font[color=#66FF99]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#33FFFF" bgcolor="#33FFFF" width="10" height="10"><a href="#" alt="font[color=#33FFFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#66FFFF" bgcolor="#66FFFF" width="10" height="10"><a href="#" alt="font[color=#66FFFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#9999FF" bgcolor="#9999FF" width="10" height="10"><a href="#" alt="font[color=#9999FF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF99FF" bgcolor="#FF99FF" width="10" height="10"><a href="#" alt="font[color=#FF99FF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#C0C0C0" bgcolor="#C0C0C0" width="10" height="10"><a href="#" alt="font[color=#C0C0C0]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF0000" bgcolor="#FF0000" width="10" height="10"><a href="#" alt="font[color=#FF0000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF9900" bgcolor="#FF9900" width="10" height="10"><a href="#" alt="font[color=#FF9900]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCC66" bgcolor="#FFCC66" width="10" height="10"><a href="#" alt="font[color=#FFCC66]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFFF00" bgcolor="#FFFF00" width="10" height="10"><a href="#" alt="font[color=#FFFF00]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#33FF33" bgcolor="#33FF33" width="10" height="10"><a href="#" alt="font[color=#33FF33]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#66CCCC" bgcolor="#66CCCC" width="10" height="10"><a href="#" alt="font[color=#66CCCC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#33CCFF" bgcolor="#33CCFF" width="10" height="10"><a href="#" alt="font[color=#33CCFF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#6666CC" bgcolor="#6666CC" width="10" height="10"><a href="#" alt="font[color=#6666CC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CC66CC" bgcolor="#CC66CC" width="10" height="10"><a href="#" alt="font[color=#CC66CC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#999999" bgcolor="#999999" width="10" height="10"><a href="#" alt="font[color=#999999]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CC0000" bgcolor="#CC0000" width="10" height="10"><a href="#" alt="font[color=#CC0000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FF6600" bgcolor="#FF6600" width="10" height="10"><a href="#" alt="font[color=#FF6600]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCC33" bgcolor="#FFCC33" width="10" height="10"><a href="#" alt="font[color=#FFCC33]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#FFCC00" bgcolor="#FFCC00" width="10" height="10"><a href="#" alt="font[color=#FFCC00]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#33CC00" bgcolor="#33CC00" width="10" height="10"><a href="#" alt="font[color=#33CC00]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#00CCCC" bgcolor="#00CCCC" width="10" height="10"><a href="#" alt="font[color=#00CCCC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#3366FF" bgcolor="#3366FF" width="10" height="10"><a href="#" alt="font[color=#3366FF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#6633FF" bgcolor="#6633FF" width="10" height="10"><a href="#" alt="font[color=#6633FF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CC33CC" bgcolor="#CC33CC" width="10" height="10"><a href="#" alt="font[color=#CC33CC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#666666" bgcolor="#666666" width="10" height="10"><a href="#" alt="font[color=#666666]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#990000" bgcolor="#990000" width="10" height="10"><a href="#" alt="font[color=#990000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CC6600" bgcolor="#CC6600" width="10" height="10"><a href="#" alt="font[color=#CC6600]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#CC9933" bgcolor="#CC9933" width="10" height="10"><a href="#" alt="font[color=#CC9933]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#999900" bgcolor="#999900" width="10" height="10"><a href="#" alt="font[color=#999900]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#009900" bgcolor="#009900" width="10" height="10"><a href="#" alt="font[color=#009900]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#339999" bgcolor="#339999" width="10" height="10"><a href="#" alt="font[color=#339999]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#3333FF" bgcolor="#3333FF" width="10" height="10"><a href="#" alt="font[color=#3333FF]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#6600CC" bgcolor="#6600CC" width="10" height="10"><a href="#" alt="font[color=#6600CC]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#993399" bgcolor="#993399" width="10" height="10"><a href="#" alt="font[color=#993399]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#333333" bgcolor="#333333" width="10" height="10"><a href="#" alt="font[color=#333333]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#660000" bgcolor="#660000" width="10" height="10"><a href="#" alt="font[color=#660000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#993300" bgcolor="#993300" width="10" height="10"><a href="#" alt="font[color=#993300]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#996633" bgcolor="#996633" width="10" height="10"><a href="#" alt="font[color=#996633]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#666600" bgcolor="#666600" width="10" height="10"><a href="#" alt="font[color=#666600]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#006600" bgcolor="#006600" width="10" height="10"><a href="#" alt="font[color=#006600]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#336666" bgcolor="#336666" width="10" height="10"><a href="#" alt="font[color=#336666]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#000099" bgcolor="#000099" width="10" height="10"><a href="#" alt="font[color=#000099]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#333399" bgcolor="#333399" width="10" height="10"><a href="#" alt="font[color=#333399]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#663366" bgcolor="#663366" width="10" height="10"><a href="#" alt="font[color=#663366]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '        <tr>                                                                                             ';
text = text + '                <td id="#000000" bgcolor="#000000" width="10" height="10"><a href="#" alt="font[color=#000000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#330000" bgcolor="#330000" width="10" height="10"><a href="#" alt="font[color=#330000]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#663300" bgcolor="#663300" width="10" height="10"><a href="#" alt="font[color=#663300]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#663333" bgcolor="#663333" width="10" height="10"><a href="#" alt="font[color=#663333]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#333300" bgcolor="#333300" width="10" height="10"><a href="#" alt="font[color=#333300]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#003300" bgcolor="#003300" width="10" height="10"><a href="#" alt="font[color=#003300]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#003333" bgcolor="#003333" width="10" height="10"><a href="#" alt="font[color=#003333]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#000066" bgcolor="#000066" width="10" height="10"><a href="#" alt="font[color=#000066]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#330099" bgcolor="#330099" width="10" height="10"><a href="#" alt="font[color=#330099]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '                <td id="#330033" bgcolor="#330033" width="10" height="10"><a href="#" alt="font[color=#330033]"><img width="10" height="10" style="border:0"></a></td>';
text = text + '        </tr>                                                                                            ';
text = text + '</table>';
      text = text + '</div>';

    }
    text = text + '</nobr></div>';

    $(this).wrap('<div id="bbcode_container"></div>');
    $("#bbcode_container").prepend(text);
    $("#bbcode_bb_bar a img").css("border", "none");
    var id = '#' + $(this).attr("id");
    var e = $(id).get(0);

    $('#bbcode_bb_bar a').click(function() {
      var button_id = attribs = $(this).attr("alt");
      if(button_id=="") return false;
      button_id = button_id.replace(/\[.*\]/, '');
      if (/\[.*\]/.test(attribs)) { attribs = attribs.replace(/.*\[(.*)\]/, ' $1'); } else attribs = '';
      if(/^:.+:$/.test(button_id))
      {
        var start = ' '+button_id+' ';
        var end = '';
        document.getElementById("smilepanel").style.display="none";
      }
      else
      {
        var start ='['+button_id+attribs+']';
        var end = '[/'+button_id+']';
        if(button_id=="font") document.getElementById("colorpanel").style.display="none";
      }
      insert(start, end, e);
      return false;
    });
        }
  function insert(start, end, element) {
    if (document.selection) {
       element.focus();
       sel = document.selection.createRange();
       sel.text = start + sel.text + end;
    } else if (element.selectionStart || element.selectionStart == '0') {
       element.focus();
       var startPos = element.selectionStart;
       var endPos = element.selectionEnd;
       element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + end + element.value.substring(endPos, element.value.length);
    } else {
      element.value += start + end;
    }
  }
})(jQuery)
