$(function()
{
  /*-------------------------------------------------------------------*/
  /*-------------------------------------------------------------------*/
  // Аварийно опасные участки РБ
 $.fn.extend({
   sshow: function() {
   var t_width  = $(this).data('dim').width;
   var t_height = $(this).data('dim').height;
   var t_left   = $(this).data('dim').left;
   var t_top    = $(this).data('dim').top;
   $(this).width(0);
   $(this).height(0);

   return $(this).animate( { height: t_height,width: t_width, opacity: 0.9, left:Math.round(t_left-t_width/2),top:Math.round(t_top-t_height/2)}, "slow");
  },
  shide: function(speed,callback) {
   var t_left   = $(this).data('dim').left;
   var t_top    = $(this).data('dim').top;
   return $(this).animate( { height: 0,width: 0, opacity: 0, left:t_left, top:t_top }, speed, callback);
  }
});

 var bashmap = $('#bashmap');
 var bashimg = $('#bashimg');

 bashmap.click( function(e)
  {
   if(!$(e.target).is('area'))
      $('.tipimg').remove();
   return false;
  });

 bashimg.click( function()
  {
   $('.tipimg').remove();
   return false;
  });
 bashimg.mouseover(function()
  {
   if($('.tipimg').length>0)
      $('.tipimg').shide('slow',function(){ $(this).remove(); });
   return false;
  });
 bashmap.mouseover(function(e)
  {
   var clicked = $(e.target);
   if(!clicked.is('area')) return false;
   var coords = clicked.attr('coords').split(',');
   var turl   = clicked.attr('href');
   var url    = clicked.attr('shref');
   var alt    = clicked.attr('alt');
   var title  = clicked.attr('title');
   var w      = clicked.attr('w');
   var h      = clicked.attr('h');

   $('<img class="tipimg" src="'+url+'" alt="'+alt+'" title="'+title+'" border="0">').insertAfter(bashimg)
   .css({ position: 'absolute',cursor:'url(images/curp.cur),pointer',opacity:0,left:coords[0]+'px',top:coords[1]+'px'})
   .data('dim', { width: w, height: h, left:coords[0],top:coords[1] })
   .click(function()
      {
       $('<div class="extmapbox"><img src="'+turl+'" alt="'+alt+'" border="0" style="cursor: url(images/curm.cur),auto;"><div class="embniz">'+title+'</div><div class="embclose">закрыть</div></div>')
       .insertAfter(bashimg)
       .css({ left:377,top:0, width:0})
       .animate( { width: 800, left:-21 }, 1000)
       .click(function()
          {
           $(this).find('.embniz').remove()
           .end().animate( { width: 0, left:377 }, 1000, function(){ $(this).remove(); })
          })
       .find('.embniz').click(function(){return false;});

       $(this).shide(1000, function(){ $(this).remove(); })
      })
   .sshow()
   .mouseout( function() { $(this).shide('slow',function(){ $(this).remove(); });  } );
   return false;
  });
  /*-------------------------------------------------------------------*/
  // эффект для меню
/*
  $('.trextmenu').hover(function(){
    $(this).append( '<span>'+$(this).text()+'</span>' )
    .css('color','#EC8800')
    .css('padding-left', (parseInt($(this).css('padding-left'))+1)+'px');
  },function(){
    $(this).css('color','#EBEBEB')
    .css('padding-left', (parseInt($(this).css('padding-left'))-1)+'px')
    .find('span').remove();
  })
*/
 $('.trextmenusel').each( function(n)
  {
   var optbox = '';
   if( $(this).is('a[optbox]') )
      optbox = 'optbox="'+$(this).attr('optbox')+'"';

   $(this).html( '<div '+optbox+' class="extselbox">'+$(this).text()+'</div>' ).css('padding', '0px 0px 0px 15px');
  });
  /*------------разворачивающееся меню------------*/
  $('#scont').click( function(e)
  {
   var ckliced = $(e.target);
   if(ckliced.is('a[optbox],div[optbox]'))
      {
       $('#for'+ckliced.attr('optbox'),this).slideToggle('fast');
       ckliced.blur();
       return false;
      }
  });
  /*-------------------------------------------------------------------*/
  // печать документа
  //$('#icoprnt').attr('href',  $('#thisurl').val() + ((searcharrstr.length>0)?( '?print&search='+encodeURIComponent(searcharrstr) ):'?print' ) );
  $('#icoprnt').attr('href',  window.location.href + ( (-1==window.location.href.indexOf('?'))?'?print':'&print' )  );
  /*-------------------------------------------------------------------*/
  // Подразделения ГИБДД по районам
  $('#podrbox').click( function(e)
  {
   var ckliced = $(e.target);
   if(ckliced.is('a') && 'podr' == ckliced.attr('id').substr(0,4) )
      {
       var ckliced_id = ckliced.attr('id');
       var load_url = ckliced.attr('href');
       var title = ckliced.attr('title');
       var text = ckliced.text();
       var box =$('#box'+ckliced_id,this);

       if(0 == box.length)
         {
          $('<div id="box'+ckliced_id+'" style="border: 1px solid #033669;padding: 5px 5px 15px 5px; position: relative;"><center>'+title+'</center><div class="podrload">Подождите. Идет загрузка.</div></div>').insertAfter(ckliced)
          .load(load_url+'?justcontent', function() {
                 $(this).append('<a href="'+load_url+'?print" title="распечатать '+text+'" style="position: absolute; right: 5px; bottom: 5px;color:#808080;">распечатать</a>');
                });
          ckliced.addClass('podrasel').removeClass('podra');
         }
       else
         {
          if(ckliced.is('.podrasel'))
            $('#box'+ckliced_id).slideUp("slow",function(){ ckliced.addClass('podravis').removeClass('podrasel'); });
          else
            {
             ckliced.addClass('podrasel').removeClass('podravis');
             $('#box'+ckliced_id).slideDown("slow");
             //,function(){$(this).load(load_url+'?justcontent');});
            }
         }
       ckliced.blur();
       return false;
      }
  });
  /*-------------------------------------------------------------------*/

});