$(document).ready(function(){

//var thCost=0;
//var oCost;	
//var rCost;
//var discVal=0;

// Для одного товара

  $(".tpl-calc-init-one").click(function(){
      if ($(".one-calc-active").length>0) {
	      $(".tpl-manager-calc-one").hide().removeClass("one-calc-active");
	    } else {
	         var offset = $(this).offset();		     
		     $(".tpl-manager-calc-one").show().css({"top":offset.top - 265,"left":offset.left + 400,"z-index":2002}).addClass("one-calc-active");
			 initCalcCost();
			 var zatemnenie = $('<div class="tpl-zatemnenie">&nbsp;</div>').css({"position":"absolute","top":0,"left":0,"height":2000,"z-index":2001,"width":"100%","background-color":"#000000"}).fadeTo(0,0.19);
			 //$("#tpl-alloverwrap").before(zatemnenie);
			 $("body").append(zatemnenie);
		   }
    }); 

initCalcCostControl = function() {
			 if ($(".tpl-calc-opt:checked").length > 0) {
			     $(".tpl-calc-cost").text(oCost + ".—"); thCost=+oCost;
				 $(".tpl-manager-calc-one").css("background","url(/assets/templates/aminew/img/calc-bg.png) 0 0 no-repeat");
			   } else {
			       $(".tpl-calc-cost").text(rCost + ".—"); thCost=+rCost;
				   $(".tpl-manager-calc-one").css("background","url(/assets/templates/aminew/img/calc-bg-roz.png) 0 0 no-repeat");
				 }
  }	 //background: url(../img/calc-bg.png) 0 0 no-repeat;
	
initCalcCost = function() {
			 arrCost = $(".cnt-product-price-label-value").text().split(".");
			 oCost = +arrCost[0];
			 rCost = +$(".tpl-roz-for-calc").text();
			 //if ($(".tpl-calc-opt:checked").length > 0) {$(".tpl-calc-cost").text(oCost + ".—"); thCost=+oCost;} else {$(".tpl-calc-cost").text(rCost + ".—"); thCost=+rCost;}
			 setTimeout('initCalcCostControl()',100);
  }	
//cnt-product-price-label-value

$('.tpl-calc-bchecked, label').click(function() {
   initCalcCost();
 });

  $(".tpl-calc-one-exit").live("click",function(){
	   $(".tpl-zatemnenie").remove();
       $(this).parent(".tpl-manager-calc-one").hide().removeClass("one-calc-active");
    });


$("label").live("click",function(){
  //alert($(this).children("input").attr("id"));
  if ($(this).children("input").attr("id")=="tpl-calc-opt") {$(".tpl-calc-cost").text(oCost + ".—"); thCost=+oCost;} else {$(".tpl-calc-cost").text(rCost + ".—"); thCost=+rCost;}
});

$(".tpl-calc-go").live("click", function(){
	discVal = +$(".tpl-calc-disc-val").val();
	calcSum = (thCost - (thCost*discVal)/100).toFixed(0);
	$(".tpl-calc-cost").text(calcSum + ".—");
	//alert(thCost + " - " + discVal + "% = " + calcSum);
  });
	
$(".tpl-calc-clear").live("click",function(){
    initCalcCost();
	$(".tpl-calc-disc-val").val("0");
  });

  
  
// Для списка товаров

  $(".tpl-calcs-init").live("click",function(){
      
      //var thCalc = $(this).parent(".cnt-product-listing-inner").children(".tpl-manager-calcs-one");
      if ($(this).parent(".cnt-product-listing-inner").children(".one-calcs-active").length>0) {
	      $($(this).parent(".cnt-product-listing-inner").children(".tpl-manager-calcs-one")).hide().removeClass("one-calcs-active");
	    } else {
			 if ($(".one-calc-active").length<1) {
			   var zatemnenie = $('<div class="tpl-zatemnenie">&nbsp;</div>').css({"position":"absolute","top":0,"left":0,"height":2000,"z-index":2001,"width":"100%","background-color":"#000000"}).fadeTo(0,0.19);
			   //$("#tpl-alloverwrap").before(zatemnenie);
			   $("body").append(zatemnenie);
			  }
	         //var offset = $(this).offset();	alert(offset.top + " x " + offset.left);	     
		     $($(this).parent(".cnt-product-listing-inner").children(".tpl-manager-calcs-one")).show().css({"top":-70,"left":-35,"z-index":2002}).addClass("one-calcs-active");
			 arrCosts = $(this).parent(".cnt-product-listing-inner").children(".cnt-product-listing-price").text().split(".");
			 thCosts = +arrCosts[0];
			 $(this).parent(".cnt-product-listing-inner").children(".tpl-manager-calcs-one").children(".tpl-calcs-cost").text(thCosts + ".—");
		   }
    }); 
			
  $(".tpl-calcs-one-exit").live("click",function(){
	   if ($(".one-calc-active").length<1) {$(".tpl-zatemnenie").remove();}
	   //$(".tpl-zatemnenie").detach();
       $(this).parent(".tpl-manager-calcs-one").hide().removeClass("one-calcs-active");
    });
		
$(".tpl-calcs-go").live("click", function(){
	discVals = +$(this).parent().parent(".tpl-manager-calcs-one").children(".tpl-calcs-disc").children(".tpl-calcs-disc-val").val();
	arrCosts = $(this).parent().parent(".tpl-manager-calcs-one").parent(".cnt-product-listing-inner").children(".cnt-product-listing-price").text().split(".");
	thCosts = +arrCosts[0];
	calcsSum = (thCosts - (thCosts*discVals)/100).toFixed(0);
	$(this).parent().parent(".tpl-manager-calcs-one").children(".tpl-calcs-cost").text(calcsSum + ".—");
	//alert(thCosts + " - " + discVals + "% = " + calcsSum);
  });
	
$(".tpl-calcs-clear").live("click",function(){
    arrCosts = $(this).parent().parent(".tpl-manager-calcs-one").parent(".cnt-product-listing-inner").children(".cnt-product-listing-price").text().split(".");
	thCosts = +arrCosts[0];
    $(this).parent().parent(".tpl-manager-calcs-one").children(".tpl-calcs-cost").text(thCosts + ".—");
	$(this).parent().parent(".tpl-manager-calcs-one").children(".tpl-calcs-disc").children(".tpl-calcs-disc-val").val("0");
  });	
	
});


// Затемнение
$(".tpl-zatemnenie").live("click", function(){
    $(this).remove();
    $(".tpl-manager-calc-one").hide().removeClass("one-calc-active");
	$(".tpl-manager-calcs-one, #wlpeLogin").hide();
  });
  
  
