﻿$(document).ready(function(){

		// добавлено: кастомные ивенты на обновление выпадающей корзины и краткого содержания корзины
		// заодно сразу триггер на обновление
		$("#cart").bind("refresh", function () {$("#cart").load("ajx/cart.ajax.php", {mode: 'refresh_cart'});});
		$("#cart_dropdown").bind("refresh", function () {$("#cart_dropdown").load("ajx/cart.ajax.php", {mode: 'refresh_dd_cart'});});
		$("#big-cart").bind("refresh", function () {$("#big-cart").load("ajx/cart.ajax.php", {mode: 'refresh_big_cart'});});
		
		$("#cart").trigger('refresh');

		$("input[type='text'][class!='manual']").focus(function(){
    $(this).attr("value","");
  });
  
  $("input.method-radio").filter(":checked").parents("tr").addClass("selected");
  
  $("input.method-radio").change(function(){
    $(".delivery-methods tr").removeClass("selected");
    $(this).filter(":checked").parents("tr").addClass("selected");
  });
  
  $(".faq .toggler").click(function(){
    var answer = $(this).parents(".item").find(".answer");
    var arr = $(this).parents(".item").find(".arr");
    if (answer.css("display") != "block") {
      answer.show();
      arr.html("&darr;");
    } else {
      answer.hide();
      arr.html("&rarr;");
    }
  });
  
  $(".faq .arr").click(function(){
    $(this).parents(".item").find(".toggler").click();
  });
  
  var faqExpanded = 0;
  
  $(".faq-toggler").click(function(){
    if (faqExpanded == 0) {
      $(".faq .answer").show();
      $(".faq .arr").html("&darr;");
      $(".faq-toggler").html("Скрыть все");
      faqExpanded = 1;
    } else {
      $(".faq .answer").hide();
      $(".faq .arr").html("&rarr;");
      $(".faq-toggler").html("Раскрыть все");
      faqExpanded = 0;
    }
  });
  
 
  
  $(".cart-dropdown-toggler").click(function(){
    if ($(".cart-dropdown").css("display") != "block") {
			//добавлено: обновление списка товаров в корзине перед разворачиванием 
			$("#cart_dropdown").trigger('refresh');
			
      $(".cart-collapse").show();
      $(".cart-dropdown").show();
      $(this).attr("src", "img/uarr.gif");
      $(".col-4 .box").addClass("cp-open");
      
    } else {
      $(".cart-dropdown").hide();
      $(this).attr("src", "img/darr.gif");
      $(".col-4 .box").removeClass("cp-open");
      $(".cart-collapse").hide();
    }
  });
  
  $(".cart-collapse .toggler").click(function(){
    $(".cart-dropdown-toggler").click();
  });
  
  var itemRating = $("#form-item-rating input[name='item-rating']").val();
  
  $("#form-item-rating .rating-toggler").html(itemRating);
  
  function ratingUnits(rating) {
    if (parseInt(rating) == 1)
      $("#form-item-rating .rating-units").html("балл");
    if (parseInt(rating) > 1 && parseInt(rating) < 5)
      $("#form-item-rating .rating-units").html("балла");
    if (parseInt(rating) == 5)
      $("#form-item-rating .rating-units").html("баллов");
  }
  
  ratingUnits(itemRating);
  
  $(".rating-selector span").each(function(i) {
    if (parseInt($(this).attr("itemrating")) <= parseInt(itemRating)) {
      $(this).addClass("on");
    }
  });
  
  $(".rating-selector span").hover(function(){
    $("#form-item-rating .rating-selector span").removeClass("on");
    var thisIndex = $(this).attr("itemrating");
    $("#form-item-rating .rating-toggler").html(thisIndex);
    ratingUnits(thisIndex);
    $("#form-item-rating .rating-selector span").each(function(i) {
      if (parseInt($(this).attr("itemrating")) <= parseInt(thisIndex)) {
        $(this).addClass("on");
      }
    });
    
  });
  
  $(".rating-selector").bind("mouseleave",function(){
    itemRating = $("#form-item-rating input[name='item-rating']").val();
    $("#form-item-rating .rating-toggler").html(itemRating);
    ratingUnits(itemRating);
    $("#form-item-rating .rating-selector span").each(function(i) {
      if (parseInt($(this).attr("itemrating")) <= parseInt(itemRating)) {
        $(this).addClass("on");
      }
    });
  });
  
  $(".rating-selector span").click(function(){
    $("#form-item-rating input[name='item-rating']").val($(this).attr("itemrating"));
    $("#form-item-rating .rating-selector").hide();
  });
  
  $(".rating-toggler").click(function(){
    $("#form-item-rating .rating-selector").show();
  });
  
  $(".opinion-toggler").click(function(){
    $(".call-to-rate-wrapper").hide();
    $(".opinion-wrapper").show();
  });

  $(".opinion-hide").click(function(){
    $(".call-to-rate-wrapper").show();
    $(".opinion-wrapper").hide();
  });

  $(".opinion-added-hide").click(function(){
    $(".call-to-rate-wrapper").show();
    $(".opinion-added").hide();
  });
	
	
  $("#opinion-submit").click(function(){
			
			if (document.getElementById('opinion-text').value=='')
				{
					$('#opinion-text').css('background-color','#ffeeee');
					$('#opinion-text').focus();
					return;
				}
				else {$('#opinion-text').css('background-color','white');}
				
			if (document.getElementById('opinion-name').value=='')
				{
					$('#opinion-name').css('background-color','#ffeeee');
					$('#opinion-name').focus();
					return;
				}
				else {$('#opinion-name').css('background-color','white');}
				
				var iname=document.getElementById('opinion-name').value;
				var itext=document.getElementById('opinion-text').value;
				var irating=document.getElementById('item-rating').value;
				var iid=document.getElementById('item-id').value;
				
				$(".opinion-added").load("ajx/review.ajax.php", {mode: 'post', prod_id: iid, name: iname, rating: irating, text: itext}, function(){
					$(".opinion-added").show();
					$(".opinion-wrapper").hide();
				});
			
  });

  /*
  $("#pd-submit").click(function(){
		var defvalues="Фамилия Имя Отчество Адрес E-mail Населенный пункт Мобильный Индекс Область Район";
		var is_ok=false;
			$("input[class*='form-text']").each(function(){
			if ($(this).attr('value')=='' || defvalues.search($(this).attr('value'))!=-1)
				{
					$(this).css('background-color','#ffeeee');
					$(this).focus();
					return false;
				}
				else {
					$(this).css('background-color','white');
					if ($(this).is("#cell")){$('#pd-form').submit();}
				}
				
  });
  });
*/  
$("input[class*='form-text']").blur(function(){
		var defvalues="Фамилия Имя Отчество Адрес E-mail Населенный пункт Мобильный Индекс Область Район";
		var is_ok=false;
			$("input[class*='form-text']").each(function(){
			if ($(this).attr('value')=='' || defvalues.search($(this).attr('value'))!=-1)
				{
					$(this).css('background-color','#ffeeee');
					//$(this).focus();
					$('#pdsubmit').attr("disabled","disabled");
					$('#pdsubmit').css("color","white");
					return false;
				}
				else {
					$(this).css('background-color','white');
					$('#pdsubmit').attr("disabled","");
					$('#pdsubmit').css("color","#DF2771");
					//if ($(this).is("#cell")){$('#pd-form').submit();}
				}
				
  });
		
});
  
$("input[class*='form-text']").trigger('blur');

  $(".popular-search-results .item").hover(function(){
    $(this).find(".title").show();
    $(this).find(".item-pic").animate({
      width: '100px',
      height: '100px'
    },1);
    $(this).find(".pic-frame img").animate({
      width: '100px',
      height: '100px'
    },1);
    $(this).find(".pic").animate({
      left: '10px',
      top: '-25px'
    },1);
  }, function() {
    $(".popular-search-results .title").hide();
    $(".popular-search-results .item-pic").attr("style","width:50px;height:50px;");
    $(".popular-search-results .pic-frame img").attr("style","width:50px;height:50px;");
    $(".popular-search-results .pic").attr("style","left:35px;top:0px;");
  });
  
  $("a.catalogue-link").click(function(){
    $(".catalogue-popup").show();
    return false;
  });
  
  $(".catalogue-popup .catalogue-toggler").click(function(){
    $(".catalogue-popup").hide();
  });
  
  $(".catalogue-popup .h3-ico").click(function(){
    $(".catalogue-popup").hide();
  });
  
  $(".site-ttl").click(function(){
    location.href = "http://shop.sadovnica.ru";
  });
  
  $(".handler img").hover(function(){
    $(this).attr("src","img/ico-delete-a.gif");
  },function(){
    $(this).attr("src","img/ico-delete.gif");
  });
  
    $("#spsr_search").click(function(){
    $("#response").html("Пожалуйста, подождите...");
    $("#response").load("ajx/calc.ajax.php?random=" + Math.random()*99999, {mode: 'checkcity', pname: document.getElementById('cityname').value});
  });

  
});

