  $(document).ready(function(){
      $(".itemTableLeft a,.smallPreviews a,.itemImgSmall a").fancybox({
        'overlayShow': true,
      	'hideOnContentClick': true
      });
      
      $(".ico-buy").click(function() {
        var art = $(this).attr('art');
        
  			$.get("/addtocart.php", { art_id: art, n: "1" },
         function(data){
           $('#sumInCart').html(data);
         });
         
        $(this).parent().addClass("added");
		});

      $("[id^=itemAdder]").change(function() {
        var art = $(this).attr('art');
        var kolvo = $(this).attr('value');
        
        if(kolvo == "0") {
          $("#artTR"+art).hide("slow");
        }
        
  			$.get("/addtocart.php", { art_id: art, n: kolvo },
         function(data){
           $('#sumInCart').html(data);
         });
     
		});

      $("[id^=itemDel]").click(function() {
        var art = $(this).attr('art');
        var kolvo = '0';
        
        if(kolvo == "0") {
          $("#artTR"+art).fadeOut("slow");
        }
        
  			$.get("/addtocart.php", { art_id: art, n: kolvo },
         function(data){
           $('#sumInCart').html(data);
         });
     
		});
		
      $("#addCom").click(function() {
        $("#comForm").toggle("slow");
		});		

      $("#logout").click(function() {
        $.get('/includes/logout.php');
        $("#log").html('<a href="#" id="login" class="login"><span>Вход</span></a>');
        //document.location = '/';
        return false;
		});	

      $("#showAllComments").click(function() {
        var art = $(this).attr('art');
  			$.get("/includes/showComments.php", { art_id: art },
         function(data){
           $('#comments').html(data);
         });
         return false;
		});

      $("#addComment").submit(function() { 
        $("#result").hide().html('');
        var str = $("#addComment").serialize();      
        $.post("/includes/addComments.php", str,
           function(json){
              if(json.status == 'ok') { 
                document.addComment.reset();
                var add = $("#adder").html();
                $("#adder").hide().html('').after(add);     //очищаем див для добавления и скрываем его.     
                $("#result").html('').append(json.message); //выводим сообщение об успехе
                //$("#comments span").remove(); //удаляем надпись об остуствиие коментов
                $("#comments").show(); //показываем блок с комментами
                $("#adder").fadeIn(1500).html(json.comment); //плавно рисуем новый коммент
              }else{
                $("#result").show().html('').append(json.message);    //alert(json.message);         
              }  
           }, "json");
           return false;       
		});

      $("#orderBut").click(function() {
      $("#result").html('');
      var str = $("#orderForm").serialize();       
      $.post("/includes/order.php", str,
         function(json){
            if(json.status == 'ok') { 
              document.orderForm.reset();
              $("#result").html('').append(json.message); //выводим сообщение об успехе
              $.get("/addtocart.php", {action: "reset"},
               function(data){
                 $('#cart').html(data);
               });              
              $(".itemsInCart").html('').fadeTo(6000,0,
              function() {
                  document.location = "/";
              });
            }else{
              $("#result").html('').append(json.message);            
            }  
         }, "json");
         return false;       
		});

//mafh
    $('#login').click(function(){
        $('#loginForm').toggle();
        $('#overlay').toggle();
        return false;
    });
    
    $('#loginForm .close').click(function(){
        $('#loginForm').hide();
        $('#overlay').hide();
        return false;
    });
    
    $('#loginForm LI A').click(function() {
        //$('#loginForm LI FORM').slideUp('slow');
        $('#loginForm LI').removeClass('active');
        //$(this).next().show('slow');
        $(this).parent().addClass('active');
        return false;
    });    
    
    $('#list #head .tags').click(function(){
        $('#list #head A').addClass('inactive');
        $(this).removeClass('inactive');
        $('#list .in-genres').css('display', 'none');
        $('#list .in-tags').css('display', 'block');
        return false;
    });    
    
    $('#list #head .genres').click(function(){
        $('#list #head A').addClass('inactive');
        $(this).removeClass('inactive');
        $('#list .in-tags').css('display', 'none');
        $('#list .in-genres').css('display', 'block');
        return false;
    });    
    
    $('#sort A').click(function(){
        $('#sort A').removeClass('active');
        $('#abc').hide();
        $(this).addClass('active');
        if ($(this).hasClass('sort-abc')){
            $('#abc').show();
        }
        if ($(this).hasClass('popular')){
            return true;
        }
        return false;
    })    
    
  });

$(window).resize(function(){
    $('#loginForm').hide();
    $('#overlay').hide();
});

function user(str,id) { 
  $.post("/includes/users.php", str,
     function(json){ 
        if(json.status == 'ok') { 
          $("#"+id+"User").hide();
          $("#"+id+"Result").css('color','#000').html('').append(json.message);
          if(id == 'login') {
            document.location = "/";
          }
        }else{ 
          $("#"+id+"Result").html('');
          $.each(json.error, function(i,item){
            $("#"+id+"Result").css('color','#F00').append(item + '<br />');
          });          
        }  
     }, "json");
}

function registrUser() { 
  var str = $("#registrUser").serialize();       
  user(str, 'registr');
  return false;
}

function loginUser() { 
  var str = $("#loginUser").serialize();             
  user(str, 'login'); 
  return true;
}

function rememUser() { 
  var str = $("#rememUser").serialize();             
  user(str, 'remem');
  return false;
}
