$(document).ready(function(){

  $('.modal').nyroModal();

  if($.browser.msie && $.browser.version=="6.0")
  {
    /*var x = $.cookies.get('allowIE6');
    if (x == null && !document.location.href.match(/badbrowser/))
    {
      document.location = '/badbrowser';
    }*/
    /*$('body').prepend('<div id="ie6bar"><div>Сайт не поддерживает старую версию Internet Explorer 6. <a href="/badbrowser">Обновите ваш браузер</a></div></div>');
    $('body').css('margin-top', '30px');*/
  }

  $('#usermenu .profilebutton').live('mouseover', function(){$(this).next().show();});
  $('#usermenu .profilebutton').live('mouseout', function(){$(this).next().hide();});
  $('#usermenu ul ul ').live('mouseover', function(){$(this).show();});
  $('#usermenu ul ul').live('mouseout', function(){$(this).hide();});

  $('.categories > ul > li > a').click(function(){
    $('.categories ul li').removeClass('selected');
    $('.categories ul ul').hide(250);
    $(this).next().show(250);
    $(this).parent().addClass('selected');
    this.focus();
    return false;
  });


	$('.tabs a').click(function(){
    var m = this.href.match(/#(.+)_(.+)$/);
    if (m == null) return true;

    var tabedarea_id = m[1];
    var tab_id = m[2];
    $('#'+tabedarea_id+' .panel').hide();
    $('#'+tab_id).show();
    
    $('#'+tabedarea_id+' ul.tabs li').removeClass('selected');
    $('#'+tabedarea_id+' ul.tabs li').removeClass('selectedfirst');
    $('#'+tabedarea_id+' ul.tabs li:first').addClass('first');
    if ($('#'+tabedarea_id+' ul.tabs li:first a')[0].href == this.href)
    {
      $(this).parent().removeClass('first');
      $(this).parent().addClass('selectedfirst');
    }
    else
    {
      $(this).parent().addClass('selected');
    }
    return false;
  });


  $('.filter a[href=#]').click(function(){
    $(this).next().toggle(250);
    return false;
  });


  $('a.replyComment').click(function(){
    var m = this.href.match(/parent_id=(\d+)/);
    if (m != null)
      $('#comment_parent_id').val(m[1]);
    else
      $('#comment_parent_id').val('');
    
    var form = $('#commentForm').remove();
    $(this).after(form);
    $('a.replyComment').show();
    $(this).hide();
    return false;
  });


  $('a.addtocompare, a.removefromcompare, a.cleancompare').live('click', function(){
    $.get(this.href, null, function(){
      $('#comparison_panel').load('/product/comparisonlist');
      return false;
    });
    return false;
  });

  $('a.favoritelink').live('click', function(){
    $(this).load(this.href);
    return false;
  });

  $(".scrollable").scrollable({api: true, size: 4, clickable: false});


  $('ul.limited').each(function(){
    if ($(this).find('input:checkbox:checked').length)
    {
      return;
    }
    var limit = $(this).attr('class').match(/limit\((\d+)\)/)[1];
    var i = 0;
    $(this).children('li').each(function(){
      i++;
      if (i > limit) {
        $(this).hide();
      }
    });
    if (i > limit) {
      $(this).after('<a class="more" href="#">Ещё &darr;</a>');
    }
  });

  $('a.more').live('click', function(){
    $(this).prev('ul.limited').children('li').show();
    $(this).hide();
    return false;
  })

  $('a.ajaxlink').click(function(){
    $(this).parent().load(this.href, [], function(data, status, XMLHttpRequest){
      if (XMLHttpRequest.status == 401) {
        document.location = '/account/login';
      }
    });
    return false;
  })

  
});