$(function()
 {
    var selsection = $('#selsection');
    var selrubric  = $('#selrubric');
    var form       = $('#postfilter');
    var content    = $('#content');
    //--------------------------------------------------------------------------
    $.extend({
    ssel:'',
    srubr:selrubric.val(),
    url:{set:false}
    });
    //--------------------------------------------------------------------------
    function parseurl(url)
    {
     var reg = /http:\/\/((?:[-.\w]+\/)+)([-\w]+)\.([\w]+)/i;
     var arr=reg.exec(url);
     return (null == arr)?{}:{set:true,base:arr[1],file:arr[2],fext:arr[3]};
    }
    //--------------------------------------------------------------------------
    selrubric.change(function()
      {
        var therubr = $(this).val();
        var obj = selsection.get(0);
        obj.options.length = 0;

        var k=0;
        var seld=0;
        obj.options[k++] = new Option('все',0);
        $.each(sarr, function(i, n)
        {
            if(n.r == therubr)
              {
               if(ssel == n.v && $.srubr == n.r)
                  seld = k;
               obj.options[k++] = new Option(n.t,n.v);
              }
        });
        obj.options[seld].selected = true;

        if(!$.url.set)
           $.url = parseurl(form.get(0).action);

        var file = $('option[value='+therubr+']',this).attr('f');
        var sfile = selsection.val();
        if(0!=sfile) file +='-'+sfile;
        form.get(0).action = 'http://'+$.url.base + file + '.' + $.url.fext;
    }).change();
    //--------------------------------------------------------------------------
    selsection.change(function()
      {
        var file = $('option[value='+selrubric.val()+']',selrubric).attr('f');
        var sfile = $(this).val();

        if(!$.url.set)
           $.url = parseurl(form.get(0).action);

        if(0!=sfile)
          file +='-'+sfile;

        form.get(0).action = 'http://'+$.url.base + file + '.' + $.url.fext;
    });
    //--------------------------------------------------------------------------
    $('#postfilter').appendTo('#divfilter').show();
    //--------------------------------------------------------------------------
    var replObj = {'а':'a','е':'e','о':'o','р':'p','с':'c','х':'x','Т':'T','В':'B','К':'K','Е':'E','Н':'H','М':'M','А':'A','Е':'E','О':'O','Р':'P','С':'C','Х':'X'};
    $('.posttitle,.posttext').each(function(i){$(this).html($(this).html().replace(/[аеорсхКЕНМАЕОРСХ]/g, function(x){return replObj[x];}));});
    //--------------------------------------------------------------------------
    $('.pboximg').click(function(clk){
      var targ = clk.target, imgs=$('img',this);
      if(!$(targ).is('img'))
         return false;
      showimgbox(imgs.index( targ ), imgs.eq(0).attr('src'), $(this).attr('imgbox').split(','));
      return false;
    });

    $('.foto').hover(function(){
         var img = $('.foto0',this), src = img.attr('src');
         if(!src || ''==src)
            img.load( function(){ $(this).show(); }).attr('src',img.attr('url'));
         else
            img.show();
    },function(){
         $('.foto0',this).fadeOut();
    });
    $('.foto').click(function(){
      showimgbox(0, $('.foto0',this).attr('url'), $(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">'+boxcont+'</div>').bind('click', clbk).appendTo(this);
        //$('<div class="'+clname+'box">'+boxcont+'</div>').appendTo(this);
        $('<div class="'+clname+'box"></div>').append(boxcont).appendTo(this);
     return true;
    },function(){
     //$(this).find('.'+clname+'box').fadeOut('fast');
     $(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')),id=$(this).attr('id'),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='+id.substr(1,id.length-1)+'&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='+id.substr(1,id.length-1)+'&rank=ok" target="_blank" title="Увеличить ранг объявления">Поднять объявление</a>').appendTo(todiv);
    todiv.appendTo(this);
  });
/*
    $('.posttitle').append('<div class="todiv"><div class="tosel" cl="tosel"></div><div class="torang" cl="torang"></div><div class="toban" cl="toban"></div></div>');
    //--------------------------------------------------------------------------
    $('.tosel,.torang,.toban').hover(function()
    {
     if(!this.cl) this.cl = '.'+$(this).attr('cl');
     if($(this).is('.tosel'))
       {
        if(0==$(this).find('.toselbox').show().size())
          $(this).append('<div class="toselbox">toselbox</div>');
        return true;
       }
     if($(this).is('.torang'))
       {
        if(0==$(this).find('.torangbox').show().size())
          $(this).append('<div class="torangbox">torangbox</div>');
        return true;
       }
     if($(this).is('.toban'))
       {
        if(0==$(this).find('.tobanbox').show().size())
          $(this).append('<div class="tobanbox">tobanbox</div>');
        return true;
       }
    },function(){
     $(this).find(this.cl+'box').fadeOut('fast');
    });
*/







/*
    function hidetobox(elem,nm)
    {
     if(elem.is(nm))
       {
        elem.find(nm+'box').fadeOut();
        return true;
       }
     if(elem.is(nm+'box'))
       {
        elem.fadeOut();
        return true;
       }
     return false;

    }
    //--------------------------------------------------------------------------
    $('.todiv').mousemove(function(e)
    {
     var elem = $(e.target);
     if(elem.is('.tosel'))
       {
        if(0==elem.find('.toselbox').show().size())
          elem.append('<div class="toselbox">toselbox</div>');
        return true;
       }
     if(elem.is('.torang'))
       {
        if(0==elem.find('.torangbox').show().size())
          elem.append('<div class="torangbox">torangbox</div>');
        return true;
       }
     if(elem.is('.toban'))
       {
        if(0==elem.find('.tobanbox').show().size())
          elem.append('<div class="tobanbox">tobanbox</div>');
        return true;
       }
    })
    .mouseout( function(e){
     var elem = $(e.target);
     if(hidetobox(elem,'.tosel')) return true;
     if(hidetobox(elem,'.torang')) return true;
     if(hidetobox(elem,'.toban')) return true;
    });

    $('.todiv').hover(function(e)
    {
     var elem = $(e.target);
     if(elem.is('.tosel'))
       {
        if(0==elem.find('.toselbox').show().size())
          elem.append('<div class="toselbox">toselbox</div>');
        return true;
       }
     if(elem.is('.torang'))
       {
        if(0==elem.find('.torangbox').show().size())
          elem.append('<div class="torangbox">torangbox</div>');
        return true;
       }
     if(elem.is('.toban'))
       {
        if(0==elem.find('.tobanbox').show().size())
          elem.append('<div class="tobanbox">tobanbox</div>');
        return true;
       }
    },function(e){
     var elem = $(e.target);
     if(hidetobox(elem,'.tosel')) return true;
     if(hidetobox(elem,'.torang')) return true;
     if(hidetobox(elem,'.toban')) return true;
    });
*/
    //--------------------------------------------------------------------------
  /*---------------------------------------------------------------------------------------*/
 });