function menuBind(){
  $('.mMenuLevel1 td, .mMenuLevel1 li').hover(function(){
    clearInterval(inter);

    $(this).addClass('hover');
    var secondLevelMenu = $(this).find('.mMenuLevel2');
    if (secondLevelMenu.length > 0){
      hideMenu();
      $('body').append(secondLevelMenu);
      $(this).attr('id','hMenu1');
      secondLevelMenu.attr('id','hMenu12');
      menuPositioning($(this), secondLevelMenu);
      secondLevelMenu.fadeIn();
    }
    var thirdLevelMenu = $(this).children('.mMenuLevel3');
    if (thirdLevelMenu.length > 0){
      hideMenu('hMenu2');
      $('body').append(thirdLevelMenu);
      $(this).attr('id','hMenu2');
      thirdLevelMenu.attr('id','hMenu22');
      menuPositioning($(this), thirdLevelMenu);
      thirdLevelMenu.fadeIn();
    }
    

  }, function(){
    $(this).removeClass('hover');
  });
  $('.mMenuLevel1 td, .mMenuLevel1 li, .mMenuLevel2 li, .mMenuLevel3 li').mouseout(function(){ hideTimet(); });
  $('.mMenuLevel1 a, .mMenuLevel2 a, .mMenuLevel3 a, .mMenuLevel1 i, .mMenuLevel2 i, .mMenuLevel3 i').hover(function(){clearInterval(inter);});
}

var inter = null;
function hideTimet(){
  clearInterval(inter);
  inter = setInterval(function(){ clearInterval(inter); hideMenu(); }, 500);  
}

function hideMenu(parentId){
  if (parentId == null){
    $('#hMenu1').append( $('#hMenu12') ).attr('id','');
    $('#hMenu12').hide().attr('id','');
    $('#hMenu2').append( $('#hMenu22') ).attr('id','');
    $('#hMenu22').hide().attr('id','');
  } else {
    $('#'+parentId).append( $('#'+parentId+'2') ).attr('id','');
    $('#'+parentId+'2').hide().attr('id','');
  }
}

function menuPositioning(parent, menu){
  var offset = parent.offset();
  var leftAdd = 0;
  var topAdd = 0;
  if (parent.parent().hasClass('mMenuLevel2')) leftAdd = parent.width();
  else topAdd =  parent.height();
  menu.css('top', (offset.top + topAdd)+'px' ).css('left', (offset.left + leftAdd)+'px');
  menu.find('li').width( parent.width() );
}


function imgRamkaBind(){
  $('.ramka').each(function(){
    var styles = $(this).attr('style');
    if (styles!='') styles += ';';
    styles += $(this).find('img').attr('style');
    $(this)
      .wrap('<table class="ramka" style="'+styles+'" cellpadding="0" cellspacing="0"><tr><td></td></tr></table>')
      .attr('style', '')
      .removeClass('ramka');
  });
}


// переключение вкладок календарика
function calendarBind(start){
//  $('.mCalendar .mnu li').click(function(){
  $('.mCalendar .mnu [rel="birthdays"]').click(function(){
    $('.mCalendar .mCalendarBlock').hide();
    var rel = $(this).attr('rel');
    $('#'+rel).show();
    $('.mCalendar .mnu').removeClass('events').removeClass('birthdays').removeClass('memory_dates').addClass(rel);
  });

  $('.mCalendar .mnu li:eq('+start+')').click();
}


/****************************************************************************************************
******************  Оглавление *****************************
***************************************************************/
function oglavlenieBind(){
  $('.cOglavlenieList h2').click(function(){
    if ( $(this).hasClass('collapsed') ){
      $(this).removeClass('collapsed');
      $(this).next().slideDown();
    } else {
      var h2 = $(this);
      $(this).next().slideUp('normal', function(){ h2.addClass('collapsed'); });
    }
  });/*.filter(':not(:first)').addClass('collapsed');*/
  $('.cOglavlenieList .collapsed').next().slideUp();
  
}


/***************************************************
***  Скрипты для админки
**********************************/

function personFIOBind(){
  function personFIOchange(){
    $("input[name=\"p1553\"]")
      .val( $("input[name=\"p1509\"]").val()+' '+$("input[name=\"p1523\"]").val()+' '+$("input[name=\"p1524\"]").val() )
      .keyup();
  }

  $("input[name=\"p1509\"]").keyup(function() { personFIOchange() });
  $("input[name=\"p1523\"]").keyup(function() { personFIOchange() });
  $("input[name=\"p1524\"]").keyup(function() { personFIOchange() });
  personFIOchange();
}

function setAskToBlock() {
  $('.punkts div').append('<img src="/gfx/ask.gif">');
  $('.punkts div img').css('margin-left','7px').parent().find('img').hover( function() {
   $(this).attr('src', '/gfx/ask1.gif');
  },
  function() {
  $(this).attr('src', '/gfx/ask.gif');
  });
}

function visibleComments() {
  $(function(){
    $('<div class="tooltip"></div>').appendTo('.cKatalog .punkts');
    var eltooltip = $('.help'),
    tooltip = $('div.tooltip'),
    title;
    eltooltip.live('mouseenter', function(){
      title = $(this).attr('title');
      $(this).removeAttr('title');
      tooltip.html('<img src="/gfx/larr.png">'+'<div>'+title+'</div>').css('opacity', '0.9');
      $(this).mousemove(function(e){
        tooltip.show().css({left: e.pageX+5, top: e.pageY-5});
      });
    });
    eltooltip.live('mouseleave', function(){
      tooltip.empty().hide();
      $(this).attr('title', title);
    });
    eltooltip.live('click', function(){
      tooltip.fadeOut('slow');
    });
  });
}

function setBgToTr() {
  $('.cKatalogList .tbl tr:even').addClass("greyRow");
  }
