

$(document).ready(function(){
  
  ShowCalculator();
  
  $(".cr_tarif input").change(function(){
   $(this).attr("checked", !$(this).attr("checked"));
   showCalculatorLevel($(this).parent(".calculator_row").find("input:checked").attr("value"), 1, $("#calculator_televize"), 'cr_televize', "Tématické balíčky");
   showCalculatorSuma();
   scrollWin();
  });
    
  $(".cr_tarif").click(function(){
    $(this).children("input:radio").trigger("change");
  });
});

function scrollWin(){
$('html, body').animate({
scrollTop: $("#calculator").offset().top
}, 500);
}

function initializeCalculatorRow(obj)
  {
    obj.find(".calculator_row input").click(function(){
      $(this).attr("checked", !$(this).attr("checked"));
    });

    obj.find(".calculator_row").click(function(){
      $(this).find('input').attr("checked", !$(this).find('input').attr("checked"));
      showCalculatorSuma();
    });
  }

function ShowCalculator()
  {
   if($('#calculator').length)
    {
      $('#calculator').append(/*"<h2>Programová nabídka IPTV</h2>"+*/
        "<div id=\"calculator_tarif\"></div>"+
        "<div id=\"calculator_televize\"></div>"+
        "<div id=\"calculator_internet\"></div>"+
        "<div id=\"calculator_telefon\"></div>"+
        "<div id=\"calculator_suma\"></div>");
    
        showCalculatorTarif();
        $(".cr_tarif input[value=basic]").attr("checked", true);
        showCalculatorLevel('basic', 1, $("#calculator_televize"), 'cr_televize', "Tématické balíčky");
        showCalculatorLevel('', 2, $("#calculator_internet"), 'cr_internet', "Internet");
        showCalculatorLevel('', 3, $("#calculator_telefon"), 'cr_telefon', "Telefon VoIP");
        showCalculatorSuma();
    }
  }

function showCalculatorTarif()
  {
    //if(CalcItem[0].length)
    //  {
        for (x in CalcItem[0])
          {
            $('#calculator_tarif').append('<div class="calculator_row cr_tarif">\n'+
                                      '<input type="radio" name="tarif" value="'+x+'" free_child="'+CalcItem[0][x]['free_child']+'" />\n'+
                                      '<label for="tarif">'+CalcItem[0][x]['title']+'</label>\n'+
                                      '<i><span>'+CalcItem[0][x]['price']+'</span> Kč</i>\n'+
                                      '<p>'+CalcItem[0][x]['desc']+'</p>\n'+
                                      '</div>\n');
          }
    //  }
  }

function showCalculatorLevel(key, level, container, cls, title)
  {
    var key = key || '';
    var html = '';
    
    //container.hide('', function(){
      if(key.length)
        {
          if((typeof CalcItem[level][key]) != "undefined" && CalcItem[level][key].length)
            {
              html+="<h2>"+title+"</h2>";
              for (x in CalcItem[level][key])
                {
                  html+='<div class="calculator_row '+cls+'">\n'+
                                      (CalcItem[level][key][x]['type'] == "radio" ? '<input type="radio" name="item_'+level+'" value="'+CalcItem[level][key][x]['id']+'"/>\n' : '<input type="checkbox" name="item_'+level+'['+x+']" value="'+CalcItem[level][key][x]['id']+'"/>\n')+
                                      '<label for="item_'+level+'">'+CalcItem[level][key][x]['title']+'</label>\n'+
                                      '<i free="'+CalcItem[level][key][x]['switch_free']+'"><span>'+CalcItem[level][key][x]['price']+'</span> Kč</i>\n'+
                                      '<p>'+CalcItem[level][key][x]['desc']+'</p>\n'+
                                      '</div>\n';
                }
            }
        }
      else
        {
          if(CalcItem[level].length)
            {
              html+="<h2>"+title+"</h2>";
              for (x in CalcItem[level])
                {
                  html+='<div class="calculator_row '+cls+'">\n'+
                                      (CalcItem[level][x]['type'] == "radio" ? '<input type="radio" name="item_'+level+'" value="'+CalcItem[level][x]['id']+'"/>\n' : '<input type="checkbox" name="item_'+level+'['+x+']" value="'+CalcItem[level][x]['id']+'"/>\n')+
                                      '<label for="item_'+level+'">'+CalcItem[level][x]['title']+'</label>\n'+
                                      '<i free="'+CalcItem[level][x]['switch_free']+'"><span>'+CalcItem[level][x]['price']+'</span> Kč</i>\n'+
                                      '<p>'+CalcItem[level][x]['desc']+'</p>\n'+
                                      '</div>\n';
                }
            }
        }
      container.html(html);
      //$(this).fadeIn();
      initializeCalculatorRow(container);
    //});
  }

function showCalculatorSuma()
  {
    var price = 0;
    var free_child = 0;
    
    $(".calculator_row").each(function(){
    
      if($(this).find('input').attr("checked"))
        {
          if($('.cr_tarif').find('input:checked'). attr('free_child') <= free_child || $(this).find('i').attr('free') == 0 || !$(this).hasClass("cr_televize"))
            {
              price = price + parseFloat($(this).find("i span").html());
            }
          else
            {
              free_child++;
            }
        }
    
    });
    
    $("#calculator_suma").html('<span>Celkem</span><strong>' + price + ' Kč</strong><i>Ceny jsou uvedeny včetně DPH 20%</i>');
  }


