$(document).ready(function(){
// top menu
  var n=0;
  $('.tm ul:first li:first').addClass('first');
  $('.tm ul').each(function(){
    $(this).find('li:last').addClass('last');
  });
  if(!$.browser.msie)
  $('.tm li').hover(function(){
    var tel=$(this).find('ul:first');
    tel.show(0);
    //alert($(this).find('ul:first li').length);
    //if(!cwid){
    var liwid=0;
    $(this).find('ul:first').children('li').find('a').each(function(){
      var twid=$(this).width()*1;
      //if($.browser.msie)
      if(twid>liwid){ if($.browser.msie)twid=twid+32;
        liwid=twid;
        tel.css('width',liwid);//.find('ul li');
      }
    });
    //var twid=$(this).width();
      $(this).find('ul li ul').css('left',liwid);
    //}
  },function(){
    $(this).find('ul:first').hide(0);
  });
  var n=0; var offl=[];
  $('.tm ul:first').children('li').each(function(){n++; $(this).addClass('top'+n); offl[n]=$(this).offset().left; /*alert(offl[n]);*/});
  // some hacks
  var uloffl=$('.tm ul:first').offset().left;
  n=0; if($.browser.safari || $.browser.opera)
  $('.tm ul:first').children('li').each(function(){n++;$(this).css({left:offl[n]-uloffl,position:'absolute'});});

// page cosmetics
  var sh="#166D0C 1px 1px,#166D0C -1px -1px,#166D0C 0 -1px,#166D0C -1px 0";//  ,fontSize:'16px'   textShadow:sh,
  Cufon.replace('.tm a', {fontFamily:'DINPro',hover:true});
  $('.leftcol').wrap('<div class=cornerg1><div class=cornerb2><div class=cornerb3><div class=cornerb4></div></div></div></div>');
  $('.rightcol').wrap('<div class=cornerb1><div class=cornerg2><div class=cornerb3><div class=cornerb4></div></div></div></div>');
  $('.centercol').wrap('<div class=grabg></div>'); // <div class=cornert1><div class=cornert2><div class=cornert3><div class=cornert4></div></div></div></div>
  if($.browser.safari) $('.tm ul li ul:first').css('top','25px');
  $('.wrap').removeClass('wj');
// click on logo
  $('.underlogo').click(function(){location.href='/';});
  // click on popup
  lboxopened=false;
  $('a.popup').click(function(){
    var ahref=$(this).attr('href');
    //alert($('#lbox').length);
    //if($('#lbox').length==0)
    //$(document).append("<div id='lbox'><div id='ca'></div><div id='lframe' class='blocklistitem'></div></div>");
    showpopup();
    $('.printdata').html('<img src="/img/loading.gif">');
    var html=$.get(ahref,{mode:'print'},function(data){
      popupplace(data);
      $('#lframe *').load(popupcenter);
    });
    return false;
  });

// прикручиваем плееры справа
      //var flashvars = {"uid":"trap","m":"audio","pl":"/swf/pls.php?fix=trap","st":"/swf/audio13-839right.txt"};
      //var params = {id:"trap",allowFullScreen:"true",allowScriptAccess:"always",wmode:"opaque"};
      //if($('div#trap').length==1)
      //new swfobject.embedSWF("/swf/uppod.swf", "trap", "220", "270", "9.0.115", false, flashvars, params);
// прикручиваем плееры справа
      //var flashvars1 = {"uid":"trvp","m":"audio","pl":"/swf/pls.php?fix=trvp","st":"/swf/video13-1634right.txt"};
      //var params1 = {id:"trvp",allowFullScreen:"true",allowScriptAccess:"always",wmode:"opaque"};
      //if($('div#trvp').length==1)
      //new swfobject.embedSWF("/swf/uppod.swf", "trvp", "220", "350", "9.0.115", false, flashvars1, params1);
  pageready('body');
});

function place_data(to,cnt,callback){       //  to,{mode:'print'}
  jQuery.ajax({
    url:to,
    type:'get',
    //dataType: 'xml',
    async:false,
    data:{mode:'print'},
    success:function(data){
      if(cnt=="td.center") data='<div class="centercol">'+data+'</div>'
      $(cnt).html(data);
      if(cnt=="td.center") $('.centercol').wrap('<div class=grabg></div>');
      pageready('td.center');
    }});
}

function pageready(harea){
  $(harea+' a[href]').click(function(){
    var to=$(this).attr('href');
    //if(to!=undefined)
    /*if(to.substring(0,1)=='/'){
//      console.log($(this).text());
      if(to=='') to='/';
      return gotourl(to);
      console.log(to);
    }else return true;*/
  });
  VK.init({apiId: 2437195, onlyWidgets: true});
  var news=$('.newslist');
  if(news.length>0){ //$(".grabg:has(h1.newstitle):first").css('backgroundImage','url(/img/ntbg.jpg)').find('.centercol').css('padding','9px 16px');
    $(".grabg:has(.newstitle):first").hide();
    $('.newslist a:has(img) img').each(function(){
      $(this).load(function(){
        var ih=$(this).height();
        $(this).parent().parent().css('min-height',ih+10);
      });
    });
  }
}



function gotourl(to){
      if(history.pushState){
        history.pushState(null, null, to);
        //set_content(to);
        place_data(to,"td.center");
        return false;
      }else return true;
}

/*function set_content(cpath){
  $.get(cpath,{mode:'print'},function(data){
    $(".centercol").html(data);
    pageready(".centercol");
  });
}*/

fixpage=false;

function textdate(str){
  if(str){
    var parts=str.toString().split(' ');
    var vars=parts[0].split('-');
  	var txt_months =  ["", "Января", "Февраля", "Марта", "Апреля",
  		"Мая", "Июня", "Июля", "Августа", "Сентября", "Октября", "Ноября",
  		"Декабря"];
    var ret=vars[2]*1+' '+txt_months[vars[1]*1];
    if(parts[1]) ret+=' '+parts[1];
    return ret;
  }
}

$(window).keydown(function(e){
//    if (e.keyCode == 37 && !fixpage) if($('.prev').length>0){ $('.prev').click(); location.hash=$('.prev').attr('href'); return false;}else{location.hash=''; popupclose();}
//    if (e.keyCode == 39 && !fixpage) if($('.next').length>0){ $('.next').click(); location.hash=$('.next').attr('href'); return false;}else{location.hash=''; popupclose();}
//    if(e.keyCode == 27) if(lboxopened) { popupclose(); return false;}
});
document.write('<script src="/js/trjp.js"></script>');


