$(document).ready(changeVariantTopText);

function checkCount()
  // kontroluje, aby se mnozstvi neprehouplo pres mnozstvi maximalni
  {
  
  if (!maxGoodsCount) return false;
  
  buyCount=document.detail_form.buyCount.value;
  
  if (maxGoodsCount<buyCount) document.detail_form.buyCount.value=maxGoodsCount;
  }
  

function checkMinBuyCount(element, minCount)
{
  selectedCount = parseInt(element.value);
  if (minCount > selectedCount) element.value=minCount;
}


function changePrice()
  {
  reg=/^(pattern_id_(.*)).*/
  
  totalExtraPrice=0;
  
  for(i=0; i<document.getElementsByTagName('input').length; i++)
    {
    str=document.getElementsByTagName('input')[i].name;
    regs=false;
    regs=str.match(reg);
    if (regs)
      {
      if (document.getElementsByTagName('input')[i].checked===true)
        {
        patternId=regs[2];
        value=document.getElementsByTagName('input')[i].value;
        //alert (value);
        totalExtraPrice+=pattern[patternId][value];
        //alert(totalExtraPrice);
        }
      }
    }
  var totalPrice=0;
  totalExtraPrice = Math.round(totalExtraPrice);
  totalPrice = Math.round(goodsPrice+totalExtraPrice);
  changeTextPrice(totalPrice,totalExtraPrice);
  }
  

function changeTextPrice (totalPrice, totalExtraPrice)
  // funkce pro zmenu textu v cenach vyrobku
  {
  var totalExtraPriceVat=0;
  var totalPriceVat=0;
  totalExtraPriceVat = Math.round( (totalExtraPrice * ((vat/100) + 1) ) );
  totalPriceVat=Math.round( (totalPrice * ((vat/100) + 1) ) );


  
  extraprice=document.getElementById('extraPrice');
  while (extraprice.firstChild) extraprice.removeChild(extraprice.firstChild);
  extraprice.innerHTML=Math.round(totalExtraPrice)+",- Kč";

  totalPriceEl=document.getElementById('totalPrice');

  while (totalPriceEl.firstChild) totalPriceEl.removeChild(totalPriceEl.firstChild);
  totalPriceEl.innerHTML=Math.round(totalPrice)+",- Kč";

  totalPriceVatEl=document.getElementById('totalPriceVat');
  while (totalPriceVatEl.firstChild) totalPriceVatEl.removeChild(totalPriceVatEl.firstChild);
  totalPriceVatEl.innerHTML=Math.round(totalPriceVat)+",- Kč";

  changeVariantTopText();
  }


  function changeVariantTopText()
  {
    $('.goodsDetail-pattern').find('input').each(function () {
      if ($(this).attr('checked') == true)
      {
        idAtr = $(this).attr('id');
        
        re = /^id_pattern_([0-9]*)_superior_([0-9]*)$/;
        temp = idAtr.match(re);
        id = temp[1];
        superiorId = temp[2];

        name = $(this).attr('title');
        $('#JSvariantChosen_'+superiorId).text(name);
      }
    });
  }