$(function()
 {
   var msgmail = $('input[name=msgmail]');
  /*---------------------------------------------------------------------------------------*/
  $('#sendpm').click( function() {
      if($('#sendpmbox').toggle().is(':visible'))
         msgmail.focus();
      return false;
    });
  $('#closesendpm').click( function() {
      $('#sendpmbox').fadeOut('slow');
      return false;
    });
  /*---------------------------------------------------------------------------------------*/
  $('#sendmsg').submit(function()
    {
     $('#sendprivmsg').get(0).disabled=true;
    });
  var alertmsg = $('#alertmsg');
  msgmail.blur(function()
    {
     var mail = $(this).val();
     if(""==mail) return true;

     r1 = /(@.*@)|(\.\.)|(@\.)|(^\.)/;
     r2 = /^.+\@[a-z0-9\-\.]+\.([a-z]{2,4})$/i;

     if(r1.test(mail) || !r2.test(mail))
        alertmsg.html('Вы ошиблись при вводе e-mail').show();
     else
        alertmsg.hide();
    });

   var msglen   = $('#msgtextlen');
   var msgtext  = $('textarea[name=msgtext]');
   var max    = parseInt(msglen.attr('max'));
   function calctalen()
     {
      var plen=$(this).val().length;

      msglen.val(max-plen);

      if(plen>max)
         {
          alertmsg.html('Вы привысили допустимую длину в '+max+' символов').show();
          $(this).val( $(this).val().substr(0,max+1) );
          msglen.val(max-$(this).val().length);
         }
      else
         alertmsg.hide();
     }
   msgtext.bind('keydown', calctalen).bind('keyup', calctalen).bind('change', calctalen);
   msgtext.blur(function()
     {
      var MsgText ='';
      var ThisText = $(this).val();
      var MsgFlag = false;

      ThisText = ThisText.replace(/ +/g,' ');
      ThisText = $.trim(ThisText);
      $(this).val(ThisText);

      var text1 = ThisText;
      ThisText = ' '+ThisText+' ';

      ThisText = ThisText.replace(/http.*?\s/ig,'');
      ThisText = ThisText.replace(/www.*?\s/ig,'');
      ThisText = ThisText.replace(/[\s,;][-\.0-9a-zA-Z]+\.(ru|com|net|biz|su|info|org)\S*[\s\.,;:]/ig,'');
      ThisText =  $.trim(  ThisText );

      if( text1!=ThisText)
        {
         MsgFlag = true;
         MsgText += '<li>Указывать веб сайт запрещено</li>';
         $(this).val(ThisText);
        }
      if( '' == ThisText)
        {
         MsgFlag = true;
         MsgText += '<li>Поле не заполнено</li>';
        }
      else
      if( ThisText == ThisText.toUpperCase() )
        {
         MsgFlag = true;
         MsgText += '<li>Нельзя набирать текст полностью в верхнем регистре (БОЛЬШИМИ буквами)</li>';
         $(this).val( ThisText.toLowerCase() )
        }
      if( ThisText.length > max )
        {
         lenbox.show();
         MsgFlag = true;
         MsgText += '<li>Превышен лимит длины сообщения '+max+'</li>';
        }
      if( ''==MsgText)
         alertmsg.hide();
      else
         MsgText = '<ul>'+MsgText+'</ul>';
      if( MsgFlag && ''!=MsgText)
        {
         alertmsg.html( MsgText );
         alertmsg.fadeIn("slow");
        }
     });
  /*---------------------------------------------------------------------------------------*/
   msglen.focus(function(){msgtext.focus();});
  /*---------------------------------------------------------------------------------------*/
  var content = document.getElementById('content'), posttitle = $('.posttitle',content), posttext = $('.posttext',content);
  var replObj = {'а':'a','е':'e','о':'o','р':'p','с':'c','х':'x','К':'K','Е':'E','Н':'H','М':'M','А':'A','Е':'E','О':'O','Р':'P','С':'C','Х':'X'};
  posttitle.html(posttitle.html().replace(/[аеорсхКЕНМАЕОРСХ]/g, function(x){return replObj[x];}));
  posttext.html(posttext.html().replace(/[аеорсхКЕНМАЕОРСХ]/g, function(x){return replObj[x];}));
  /*---------------------------------------------------------------------------------------*/
    $('.pboximg',content).click(function(clk){
      var targ = clk.target,imgs=$('img',this),targInd = imgs.index( targ );
      if(!$(targ).is('img'))
         return false;
      showimgbox(targInd, imgs.eq(0).attr('src'), $(this).attr('imgbox').split(','));
      return false;
    });
  /*---------------------------------------------------------------------------------------*/
 $.fn.extend({
  rbox: function(clname,boxcont) {
    return $(this).hover(function()
    {
     if(0==$(this).find('.'+clname+'box').show().size())
        $('<div class="'+clname+'box"></div>').append(boxcont).appendTo(this);
     return true;
    },function(){
     $(this).find('.'+clname+'box').hide();
    });
   }
  });
  var gorod = $('#gorod',content).val(),urltarg = 'http://1gs.ru/sel/';
  $('.posttitle',content).each(function(i){
    var sel = parseInt($(this).attr('sel')), rank = $(this).attr('rank'), ac = (1==$(this).attr('ac')),post=$(this).attr('post'),boxcont,k;
    if(!ac) return true;
    var todiv = $('<div class="todiv"></div>');
    var toselclname = ((sel>0)?'toselac':'tosel'), torankclname = ((rank>0)?'torangac':'torang');
    if(sel<4)
      {
       boxcont = '';
       if(sel>0)
         boxcont = 'Объявление выделено способом <b>'+sel+'</b>';
       for(k=sel+1;k<=4;k++)
           boxcont += '<a href="'+urltarg+'?gorod='+gorod+'&p='+post+'&sel='+k+'" sel="'+k+'" target="_blank">Выделить способом '+k+'</a>'; //  class="sel'+k+'"
       boxcont = $('<span>'+boxcont+'</span>');
       var the = this;
       boxcont.find('a').hover(function()
           {
            if(!this.oldcl) this.oldcl = the.className;
            if(!this.newcl) this.newcl = 'posttitle sel'+$(this).attr('sel');
            the.className = this.newcl;
           },function(){
            the.className = this.oldcl;
           });
       $('<div class="'+toselclname+'"></div>').rbox('tosel',boxcont).appendTo(todiv);
      }
    else
      $('<div class="toselac" title="Объявление максимально выделено"></div>').appendTo(todiv);

    $('<div class="'+torankclname+'"></div>').rbox('torang','Ранг этого объявления:<b>'+rank+'</b><br><a href="'+urltarg+'?gorod='+gorod+'&p='+post+'&rank=ok" target="_blank" title="Увеличить ранг объявления">Поднять объявление</a>').appendTo(todiv);
    todiv.appendTo(this);
  });
  /*---------------------------------------------------------------------------------------*/
 });