function mobLk() {
  $('#basketdiv').prependTo('#dc');
  $('#basketdiv').css('display','block');
  $('#baskethead').css('display','none');
  $('#basketpic').css('display','none');
  $('#basketdiv').css('width','630px');
  $('#basketdiv').css('border','0px');
}
function mobMenu() {
  if($('#mobMenuMain').css('display')=='none') {
    $('#mobMenuMain').show(500);
    $('#dl').hide();
    $('#dr').hide();
    $('#mobOverlay').hide();
  } else
    $('#mobMenuMain').hide();
}
function showRightMenu() {
  if($('#dr').css('display')=='none') {
    $('#dl').hide();
    $('#mobMenuMain').hide();
    $('#dr').show();
    $('#mobOverlay').show();
  } else {
    $('#dr').hide();
    $('#mobOverlay').hide();
  }
}
function showLeftMenu() {
  if($('#dl').css('display')=='none') {
    $('#dr').hide();
    $('#mobMenuMain').hide();
    $('#dl').show();
    $('#mobOverlay').show();
  } else {
    $('#dl').hide();
    $('#mobOverlay').hide();
  }
}

var winwidth=$(window).width();
$(window).resize(function() {
  if(winwidth<=1200&&$(window).width()>=1200||winwidth>=1200&&$(window).width()<=1200) {
    winwidth=$(window).width();
    location.reload();
  } else
    winwidth=$(window).width();
  //var mql = window.matchMedia('screen and (min-width: 1200px)');
  //if (mql.matches)
    //location.reload();
});

$(document).ready(function () {
   
  var mql = window.matchMedia('screen and (max-width: 1200px)');
  if (mql.matches) {

    //по умолчанию
    $('#maintable').unwrap();

    //по клику на подложку закрываем меню
    $('#mobOverlay').click(function(){
      $('#dl').hide();
      $('#dr').hide();
      $('#mobOverlay').hide();
    });

    //перенос "лишних" элементов в отдельное меню
    $('.fullmenuitem').each(function(){
      $('#mobMenuMain').append($(this).find($('a.menu')));
    });
    
    $('#dl p').hide();

    //скрытие "лишних" блоков аксессуаров на главной
    /*
    var accblocks = [];
    $('.accsblock').each(function(){
      accblocks.push($(this));
    });
    var al = accblocks.length;
    for(var i=0;i<al-5;i++) {
      var rnd = Math.floor(Math.random() * accblocks.length);
      accblocks[rnd].hide();
      accblocks.splice( $.inArray(accblocks[rnd],accblocks) ,1 );
    }
    */
    
    //переформатирование таблицы прайсов
    $('#pricetable thead').remove();
    $('#pricetable script').remove();
    $('#pricetable .rtrs:not(.mobPTname)').each(function(){
      $(this).find('td:last').remove();
      $(this).before('<tr id="moving" class="mobPTname"></tr>');
      fcol=$(this).find('td:first[id]');
      fcol.removeAttr('rowspan');
      fcol.attr('colspan',4);
      $('#moving').append(fcol);
      $('#moving').removeAttr('id');      
      if($(this).find('td').eq(1).text()!='')
        $(this).find('td').eq(1).append('&nbsp;шт.');
      if($(this).find('td').eq(2).text()!='')
        $(this).find('td').eq(2).append('&nbsp;р.');
    });

    $('#pricetable_zapchast tr:not(.mobPTname)').each(function(){
      $(this).find('td:last').remove();
      $(this).before('<tr id="moving" class="mobPTname"></tr>');
      fcol=$(this).find('td:first[id]');
      fcol.removeAttr('rowspan');
      fcol.attr('colspan',4);
      $('#moving').append(fcol);
      $('#moving').removeAttr('id');      
      $(this).find('td').eq(1).append('&nbsp;шт.');
      $(this).find('td').eq(2).append('&nbsp;р.');
    });
    
    

    $('#pricetable_allzap thead').remove();
    $('#pricetable script').remove();
    $('#pricetable_allzap .rtrs:not(.mobPTname)').slice(0,35).each(function(){
      $(this).find('td:last').remove();
      $(this).before('<tr id="moving" class="mobPTname"></tr>');
      fcol=$(this).find('td:first[id]');
      fcol.removeAttr('rowspan');
      fcol.attr('colspan',4);
      $('#moving').append(fcol);
      $('#moving').removeAttr('id');      
      $(this).find('td').eq(1).append('&nbsp;шт.');
      $(this).find('td').eq(2).append('&nbsp;р.');
    });

    $('.searchresults thead').remove();
    $('.searchresults tr:not(.mobPTname)').each(function(){
      //удаляем отчерки
      if($(this).children().length==1&&$(this).find(':first-child').attr('colspan')=='7') {
        $(this).remove();
        return;
      }
      if($(this).attr('class')=='shortinf') { //при поиске по номеру вызывает скрытие всех остальных элементов
        $(this).css('display','none');
        return;
      }
      $('.addtobasket').find('img').attr('src',$('.addtobasket').find('img').attr('src').replace('_sm',''));
      if($(this).children().length==7) { //наличие в единственном экземпляре
        $(this).before('<tr id="moving" class="mobPTname"></tr>');
        fcol=$(this).find('td').eq(0);
        fcol.append(' ('+$(this).find('td').eq(2).html()+')'); //производитель
        fcol.removeAttr('rowspan');
        fcol.attr('colspan',3);
        $('#moving').append(fcol);
        $(this).find('td').eq(0).removeAttr('rowspan');
        $('#moving').append($(this).find('td').eq(0)); //артикул
        $(this).find('td').eq(0).remove();
        $('#moving').removeAttr('id');
      }
      if($(this).children().length==3) { //будет мобильный заголовок
        $(this).addClass('mobPTname');
        fcol=$(this).find('td').eq(0);
        fcol.append(' ('+$(this).find('td').eq(2).html()+')'); //добавление производителя
        $(this).find('td:last').remove();
        $(this).children().removeAttr('rowspan');
        $(this).find('td:first').attr('colspan',3);
      }
      if($(this).children().length==4) { //будут мобильные цены
        $(this).find('td').eq(0).append('&nbsp;шт.');
        $(this).find('td').eq(1).append('&nbsp;р.');
      }
    });
    
    $('#models .modelel').each(function(){
      $(this).text($(this).text().replace(' Mercedes',''));
    });

  }
  
  var mql = parent.window.matchMedia('screen and (max-width: 1200px)');
  if (mql.matches) {
    $('.searchresults_analogs .thead').remove();
    $('.searchresults_analogs tr:not(.mobPTname)').each(function(){
      $('.addtobasket2').find('img').attr('src',$('.addtobasket2').find('img').attr('src').replace('_sm',''));
      if($(this).find('td:first').attr('class')=='name') {
        $(this).find('td:first').removeAttr('rowspan');
        $(this).find('td:first').attr('colspan',6);
        $(this).addClass('mobPTname');
      }
      if($(this).hasClass('inf')) {
        $(this).find('td').eq(2).append('&nbsp;шт.');
        $(this).find('td').eq(3).append('&nbsp;р.');
      }
      if($(this).children().length==4) {
        $(this).find('td').eq(0).append('&nbsp;шт.');
        $(this).find('td').eq(1).append('&nbsp;р.');
      }
    });
  }
  
  $('.card').on('click',function(){
    if($(this).attr('href'))
      location.href=$(this).attr('href');
  });
  
  /*
  $('body *').each(function(i,elem) {
    if($(elem).children().length==0) {
      $(elem).css('font-size',Math.round(parseInt($(elem).css('font-size'))*1.5));
      $(elem).addClass('QQQ');
    }
  });

  var mql = window.matchMedia('screen and (max-width: 1199px)');
  if (mql.matches) {
    $('#basketdiv').appendTo($('#uc'));
    $('#basketdiv').addClass('fixed');
    if($('#tsum').text()!='')
      $('#basketsum').html('<b>'+$('#tsum').text()+'&nbsp;р.</b>');
    else
      $('#basketsend').style('display','none','important');
    $(window).scroll(function() {
      if($(document).scrollTop() == 0)
        $('#basketdiv').addClass('fixed');
    });
  }
  */ 
});
