/**
 * Говнокод, оставленный «разработчиками» компании Ин-Сайт (www.in-site.ru) вычищен 11.11.2010 г.
 * (c) Неверов Евгений, http://www.1c-bitrix.ru/partners/129454.php
 * */

function AddOne(id) {
  var preVal = parseInt($('#q' + id).val()) + 1;
  if ($('#limit' + id).length && preVal > $('#limit' + id).val()) {
    alert('Вы уже добавили максимальное количество товара в корзину');
    preVal = $('#limit' + id).val();
  }

  $('#q' + id).val(preVal);
}

function DecOne(id) {
  var preVal = parseInt($('#q' + id).val()) - 1;
  $('#q' + id).val(preVal > 0 ? preVal : 1);
}

$(function() {
  $('input[name=how_much], input.hm').change(function() {
    if (parseInt($(this).val()) < 1 || isNaN(parseInt($(this).val()))) $(this).val('1');

    var ID = parseInt($(this).attr('id').substr(1));
    if ($('#limit' + ID).length && parseInt($(this).val()) > parseInt($('#limit' + ID).val())) $(this).val($('#limit' + ID).val());
  });

  $(".add_busket").click(function() {
    var atStore = parseInt($('#rest' + $(this).attr('rel')).val());
    var inBasket = parseInt($('#_cnt_' + $(this).attr('rel')).text());
    var tryAdd = parseInt($('#q' + $(this).attr('rel')).val());

    if (!atStore) {
      alert('Товара нет на складе');
      return false;
    }

    if (atStore == inBasket) {
      alert('Вы уже добавили в корзину максимальное количество товара');
      return false;
    }

    if (atStore < inBasket + tryAdd) {
      var v = prompt('В вашей корзине уже есть ' + inBasket + ' ед. данного товара.\nНа складе — ' + atStore + ' ед.\n\nВы не можете добавить больше ' + (atStore - inBasket) + ' ед.\n\nУкажите требуемое количество товара:', (atStore - inBasket));
      if (v <= (atStore - inBasket))
        $('#q' + $(this).attr('rel')).val(v);
      else return false;
    }

    $('#busket').load('/catalog/add2basket.php', {id: $(this).attr('rel'), count: $('#q' + $(this).attr('rel')).val()});

    $(this).text("Добавляется...").addClass("in_busket").css('background-image','url(/images/button/in_basket.png)');
    $("#busket .caption").append('<img src="/img/chel_big.png" alt=""/>');

    return false;
  });

  $(".add_busket").mouseover(function() { if ($(this).hasClass("in_busket")) $(this).text('Добавить товар в корзину'); });
  $(".add_busket").mouseout(function() { if ($(this).hasClass("in_busket")) $(this).text('Товар в корзине'); });

  $(".order").hover(function(){$(".a_class").css("display","block")},function(){$(".a_class").css("display","none")})
  $(".schop").hover(function(){$(".d_class").css("display","block")},function(){$(".d_class").css("display","none")})
  $(".interesting").hover(function(){$(".b_class").css("display","block")},function(){$(".b_class").css("display","none")})
  $(".club").hover(function(){$(".c_class").css("display","block")},function(){$(".c_class").css("display","none")})
  $(".catalog li").hover(function(){src=$(this).find('img').attr("src");$(this).find('img').attr("src",$(this).find('img').attr("alt"))},function(){$(this).find('img').attr("src",src)})
  $(".main_menu>li").hover(function(){$(this).find("ul").css("display","block")},function(){$(this).find("ul").css("display","none")})
  $(".main_menu li li").hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")})

  $(".first_li .top_div a").hover(function(){$(".first_li .top_div").animate({top: 0}, 300);})

  $(".second_li .top_div a").hover(function(){$(".second_li .top_div").animate({top: 0}, 300);})

  $(".third_li .top_div a").hover(function(){$(".third_li .top_div").animate({top: 0}, 300);})

  $(".forum").hover(function(){$(".first_li .top_div").animate({top: -117}, 300);})

  $(".action").hover(function(){$(".second_li .top_div").animate({top: -117}, 300);})

  $(".question").hover(function(){$(".third_li .top_div").animate({top: -117}, 300);})

  slide_n=5;
if (document.getElementById('gallery2'))
                     {

                       position = parseInt($('#inside_gallery2').css('left').replace('px',''));
                       number = $("#gallery2").find('dl').length;
                       if (number>5) {$('#left_slide2').css('display','block');$('#right_slide2').css('display','block')}
                       different = number-5;
                       if (different <= 0) {} else
                       $('#right_slide2').removeClass();
                       different_flag = different;
                       $('#right_slide2').click(function(){
////
 if (different>0)
 {$('#left_slide2').removeClass();
 if  (different>=5) {slide_n=5} else{slide_n=different}
 position = position - 108*slide_n;
 $('#inside_gallery2').animate({left:position},  2000);
 different = different - slide_n;
 if (different == 0){$(this).addClass('no_active');}
 return false;}
 else return false;
 })
                       $('#left_slide2').click(function(){
 if (different < different_flag)
 {$('#right_slide2').removeClass();

 position = position + 108*slide_n;
 slide_n=5;
 $('#inside_gallery2').animate({left:position},  2000);
 different = different + slide_n;
 if (different == different_flag){$(this).addClass('no_active');}
 return false;}
 else return false;
 })
////




                     }




                      slide_n3=5;
if (document.getElementById('gallery3'))
                     {

                       position3 = parseInt($('#inside_gallery3').css('left').replace('px',''));
                       number3 = $("#gallery3").find('dl').length;
                       if (number3>5) {$('#left_slide2').css('display','block');$('#right_slide2').css('display','block')}
                       different3 = number3-5;
                       if (different3 <= 0) {} else
                       $('#right_slide3').removeClass();
                       different_flag3 = different3;
                       $('#right_slide3').click(function(){
////
  if (different3>0)
  {$('#left_slide3').removeClass();
  if  (different3>=5) {slide_n3=5} else{slide_n3=different3}
  position3 = position3 - 108*slide_n3;
  $('#inside_gallery3').animate({left:position3},  2000);
  different3 = different3 - slide_n3;
  if (different3 == 0){$(this).addClass('no_active');}
  return false;}
  else return false;
  })
                       $('#left_slide3').click(function(){
  if (different3 < different_flag3)
  {$('#right_slide3').removeClass();

  position3 = position3 + 108*slide_n3;
  slide_n3=5;
  $('#inside_gallery3').animate({left:position3},  2000);
  different3 = different3 + slide_n3;
  if (different3 == different_flag3){$(this).addClass('no_active');}
  return false;}
  else return false;
  })
////




                     }


slide_n=4;
if (document.getElementById('gallery'))
                     {

                       position = parseInt($('#inside_gallery').css('left').replace('px',''));
                       number = $("#gallery").find('span').length;
                       if (number>4) {$('#left_slide').css('display','block');$('#right_slide').css('display','block')}
                       different = number-4;
                       if (different <= 0) {} else
                       $('#right_slide').removeClass();
                       different_flag = different;
                       $('#right_slide').click(function(){
  if (different>0)
  {$('#left_slide').removeClass();
  if  (different>=4) {slide_n=4} else{slide_n=different}
  position = position - 118*slide_n;
  $('#inside_gallery').animate({left:position},  2000);
  different = different - slide_n;
  if (different == 0){$(this).addClass('no_active');}
  return false;}
  else return false;
  })
                       $('#left_slide').click(function(){
  if (different < different_flag)
  {$('#right_slide').removeClass();

  position = position + 118*slide_n;
  slide_n=4;
  $('#inside_gallery').animate({left:position},  2000);
  different = different + slide_n;
  if (different == different_flag){$(this).addClass('no_active');}
  return false;}
  else return false;
  })
////




                     }





                     //добавляем необходимые элементы
  $("#catalog .cat").prepend('<div class="select"></div>');

  //анимируем каталог
  $("#catalog .cat_caption").click(function(){
    if ( $(this).next().css("display") == "block" ){
      $(this).next().slideUp("slow",function(){$("#catalog .active").removeClass("active");});
      $(this).parent().find(".select").removeClass("fix");
    } else {
      $("#catalog .cat_list").slideUp("slow",function(){$("#catalog .active").removeClass("active");});
      if ($.browser.msie){
        $("#catalog .select").not($(this).parent().find(".select")).width(0).removeClass("fix");
      } else {
        $("#catalog .select").not($(this).parent().find(".select")).fadeTo(250,0).removeClass("fix");
      }
      var1=$(this)
      $(this).next().slideDown("slow",function(){var1.addClass("active");});
      $(this).parent().find(".select").addClass("fix");
    }


  });

  //подсвечиваем курсор в каталоге
  $("#catalog .cat_caption").hover(function(){
    if ($.browser.msie){
      $(this).parent().find(".select").stop().width(270);
    } else {
      $(this).parent().find(".select").stop().fadeTo(250,1);
    }
  }, function(){
    if ( ! $(this).parent().find(".select").hasClass("fix") ){
      if ($.browser.msie){
        $(this).parent().find(".select").stop().width(0);
      } else {
        $(this).parent().find(".select").stop().fadeTo(250,0);
      }
    }
  });

  

  $('.cat_caption').mouseenter(function(){
	w1 = $(this).find('span#line').width();
	w2 = w1-155;
	$(this).find('span#line').animate( {'left':'-'+w2+'px'}, 500 );
  })
  
  $('.cat_caption').mouseleave(function(){
	w1 = $(this).find('span#line').width();
	w2 = w1-155;
	$(this).find('span#line').animate( {'left':'0px'}, 500 );
  })
  
});
