$(document).ready( function() {

  var price;

  setFirstPart();

  // Устанавливает изначальные значения
  function setFirstPart()
  {
    $('.calcPart01').addClass('calcPartSel');
    getId('designTmp').checked = true;
    getId('cmsNoShop').checked = true;
    setCheckInputs();
    price = 10000;
    updatePrice();
  }

  // Обработчик наведения на пакеты
  $("#calcPartsTbl td").hover(
    function(){ $(this).addClass('calcPartAct'); },
    function(){ $(this).removeClass('calcPartAct'); }
	);

  $("#calcTbl tr").hover(
    function(){ $(this).addClass('calcTblAct'); },
    function(){ $(this).removeClass('calcTblAct'); }
	);

  function commonParams(node)
  {
    resetPartsActive();
    $(node).addClass('calcPartSel');
    setCheckInputs();
    updatePrice();
  }

  function updatePrice()
  {
    var discount = 3; // Скидка в %
    if (price >= 70000) {
      discount = 10;
    } else if (price >= 50000) {
      discount = 7;
    } else if(price >= 30000) {
      discount = 5;
    }
    $(".calcPrice").text(price);
    $(".calcDiscount").text(price*discount/100);
    $(".calcCorrectPrice").text(price-(price*discount/100));
    $(".calcProcDisc").text(discount);
  }

  // Выбор пакетов, при клике обновляем все данные в форме
  $(".calcPart01").click( function() {
		getId('designTmp').checked = true;
    getId('cmsNoShop').checked = true;
		price = 10000;
		commonParams(this);
	});
  $(".calcPart02").click( function() {
		getId('designTyp').checked = true;
    getId('cmsNoShop').checked = true;
		price = 30000;
		commonParams(this);
	});
  $(".calcPart03").click( function() {
		getId('designInd').checked = true;
    getId('cmsNoShop').checked = true;
		price = 50000;
		commonParams(this);
	});
  $(".calcPart04").click( function() {
		getId('designTmp').checked = true;
    getId('cmsShop').checked = true;
		price = 15000;
		commonParams(this);
	});
  $(".calcPart05").click( function() {
		getId('designTyp').checked = true;
    getId('cmsShop').checked = true;
		price = 35000;
		commonParams(this);
	});
  $(".calcPart06").click( function() {
		getId('designInd').checked = true;
    getId('cmsShop').checked = true;
		price = 55000;
		commonParams(this);
	});

  // Дизайн
  // Общий пересчет цен на дизайн
  function correctPrice()
  {
    resetPartsActive();
    price = 0;
    if(getId('designTmp').checked == true) { price = price;
    } else if(getId('designTyp').checked == true) { price = price + 20000;
    } else if(getId('designInd').checked == true) { price = price + 40000;
    } else if(getId('designNotOur').checked == true) { price = price + 6000;
    }
    if(getId('cmsNoShop').checked == true) { price = price + 10000;
    } else if(getId('cmsShop').checked   == true) { price = price + 15000;    
    }
    if(getId('seoCalcParam').checked == true) { price = price + 20000; }
    updatePrice();
  }
  // Функции захвата
  $("#designTmp, #designTmpTr").click( function() {
    //$("#designTmpTr").addClass('calcTblSel');
    getId('designTmp').checked = true;
    correctPrice();
	});
  $("#designTyp, #designTypTr").click( function() {
    getId('designTyp').checked = true;
    correctPrice();
	});
  $("#designInd, #designIndTr").click( function() {
    getId('designInd').checked = true;
    correctPrice();
	});
  $("#designNotOur, #designNotOurTr").click( function() {
    getId('designNotOur').checked = true;
    correctPrice();
	});

  $("#cmsNoShop, #cmsNoShopTr").click( function() {
    getId('cmsNoShop').checked = true;
    correctPrice();
	});
  $("#cmsShop, #cmsShopTr").click( function() {
    getId('cmsShop').checked = true;
    correctPrice();
	});

  $("#seoCalcParamTr, #seoCalcParam").click( function() {
    if (getId('seoCalcParam').checked == true) {
      getId('seoCalcParam').checked = false;
    } else {
      getId('seoCalcParam').checked = true;
    }
    correctPrice();
	});

});



function resetPartsActive()
{
  for (i = 0; i <= 6; i++ ) {
    $(document.getElementsByClassName('calcPart0'+i)).removeClass('calcPartSel');
  }
}

function setCheckInputs()
{
  for (i = 0; i <= 17; i++ ) {
    getId('cmsCalcParam'+i).checked = true;
  }
}

function getId(id)
{
	var node = document.getElementById(id);
	if (node){
    return node;
  } else {
    return false;
  }
}