// testimonial slider
function testimonial_slider() {
  var i_testim = 0;
  
  // count the slides and make them disappear
  $("#testimonial-slider ul li").each( function() {
    if(i_testim) {
      $(this).hide();
    }
    i_testim++;
  });
  
  // create navigation
  var testim_nav = '<ol id="testim_nav">';
  var li_class = '';
  for(var x=0; x<=i_testim-1; x++) {
    if(x==0) {
      li_class = ' class="clicked_li"'; 
    }
    else {
      li_class = ''; 
    }
    testim_nav += '<li'+li_class+'>'+(x+1)+'</li>';
  }
  testim_nav += '</ol>';
  
  // append navigation
  $(testim_nav).appendTo('#testimonial-slider');
  
  // hook click function on nav li
  $("#testim_nav li").click(function() {
    var testim_num = $(this).html();
    
    // remove all previous clicks
    $("#testim_nav li").each( function() {
      $(this).removeClass('clicked_li');
    });
    
    // add class for the button
    $(this).addClass('clicked_li');
    
    var i_click = 0;
    $("#testimonial-slider ul li").each( function() {
      if(i_click == testim_num-1)
      {
        $(this).fadeIn(300);
      }
      else
      {
        $(this).fadeOut(100);
      }
      i_click++;
    });
    
    return false;
    
  });
}

// slider ini
function slider_ini() {
  //slider initialization
  $(function() {
		$( "#invoices_num_slider" ).slider({
			range: "min",
			value: 8,
			min: 1,
			max: 50,
			slide: function( event, ui ) {
				$( "#invoices_num" ).html( ui.value );
				count_savings();
			}
		});
		$( "#invoices_num" ).html( $( "#invoices_num_slider" ).slider( "value" ) );
	});
	
	$(function() {
		$( "#invoice_time_slider" ).slider({
			range: "min",
			value: 10,
			min: 1,
			max: 30,
			slide: function( event, ui ) {
				$( "#invoice_time" ).html( ui.value );
				count_savings();
			}
		});
		$( "#invoice_time" ).html( $( "#invoice_time_slider" ).slider( "value" ) );
	});
	
	$(function() {
		$( "#hourly_slider" ).slider({
			range: "min",
			value: 400,
			min: 100,
			max: 1500,
			step: 50,
			slide: function( event, ui ) {
				$( "#hourly_rate" ).html( ui.value );
				count_savings();
			}
		});
		$( "#hourly_rate" ).html( $( "#hourly_slider" ).slider( "value" ) );
	});
	
  count_savings();
  
}

// savings meter
function count_savings() {
  
  //lets get the values
  
  // var invoices_num    = $( "#invoices_num_slider" ).slider( "value" );
  // var invoice_time    = $( "#invoice_time_slider" ).slider( "value" );
  // var hourly_rate     = $( "#hourly_slider" ).slider( "value" );
  
  var invoices_num    = $( "#invoices_num" ).html();
  var invoice_time    = $( "#invoice_time" ).html();
  var hourly_rate     = $( "#hourly_rate" ).html();
  
  var fa_invoice_time = $('#fa_invoice_time').html();
  
  var email_time_est = invoices_num * 5;
  $('#email_time_est').html(email_time_est);
  
  var reminder_time_est = invoices_num * 2;
  $('#reminder_time_est').html(reminder_time_est);
  
  var now_total_time = Math.round( ( ( (invoices_num * invoice_time) + email_time_est + reminder_time_est)/60) * hourly_rate);
  $('#now_total').html(now_total_time);
    
  var fa_total_time = Math.round( ((invoices_num*fa_invoice_time) / 60) * hourly_rate );
  $('#fakturoid_total').html(fa_total_time);
  
  
  var difference = now_total_time - fa_total_time;
  
  //what did we get
  if(difference>0) {
    if (difference > 150 && difference < 300) {
      $('#resume').html("Pronajměte si mě v&nbsp;<a href='https://app.fakturoid.cz/pub/accounts/new?plan=1'>tarifu 01</a> za 150&nbsp;Kč měsíčně a&nbsp;stále <strong>ušetříte "+(difference-150)+"&nbsp;Kč</strong> měsíčně.");
    }
    else if (difference > 300) {
      $('#resume').html("Pronajměte si mě v&nbsp;neomezeném <a href='https://app.fakturoid.cz/pub/accounts/new?plan=2'>tarifu 02</a> za 300&nbsp;Kč měsíčně a&nbsp;stále <strong>ušetříte "+(difference-300)+"&nbsp;Kč</strong> měsíčně.");
    }
    else if (difference < 150) {
      $('#resume').html("Neušetříte sice tolik, aby to samo o&nbsp;sobě stačilo na placený tarif, ale vyzkoušejte <a href='https://app.fakturoid.cz/pub/accounts/new?plan=0'>tarif zdarma</a> a&nbsp;vezměte v úvahu, že dělám i&nbsp;užitečné statistiky, mám automatické zálohy a&nbsp;jsem sympatický robot.");
    }
    
    $('#resume').css("display", "block");
  }
  
}
