$(document).ready(function(){
	
	// koszyk
	$('#koszyk .ilosc input').each(function(){
		
		var ilosc = $(this).val();
		var cena = $(this).parent('td').parent('tr').children('.cena').children('span').text();
		var wartosc = $(this).parent('td').parent('tr').children('.wartosc').children('span').text();
		
		if(isNaN(ilosc)) {
			ilosc = 0;
			$(this).val('0');
		}
		
		if (ilosc < 0) {
			ilosc = 0;
			$(this).val('0');
		}
		
		wartosc = ilosc * cena;
		$(this).parent('td').parent('tr').children('.wartosc').children('span').text(wartosc.toFixed(2));
		
		var total = parseInt(0);
		$('#koszyk .wartosc').each(function(){
			var wartosc = $(this).children('span').text();
			total = parseFloat(total) + parseFloat(wartosc);
			$('#koszyk .cenaTotal').children('span').text(total.toFixed(2));
		});
		
	});
	
	$('#koszyk .ilosc input').keyup(function(){
		
		var ilosc = $(this).val();
		var cena = $(this).parent('td').parent('tr').children('.cena').children('span').text();
		var wartosc = $(this).parent('td').parent('tr').children('.wartosc').children('span').text();
		
		if(isNaN(ilosc)) {
			ilosc = 0;
			$(this).val('0');
		}
		
		if (ilosc < 0) {
			ilosc = 0;
			$(this).val('0');
		}
		
		wartosc = ilosc * cena;
		$(this).parent('td').parent('tr').children('.wartosc').children('span').text(wartosc.toFixed(2));
		
		var total = parseInt(0);
		$('#koszyk .wartosc').each(function(){
			var wartosc = $(this).children('span').text();
			total = parseFloat(total) + parseFloat(wartosc);
			$('#koszyk .cenaTotal').children('span').text(total.toFixed(2));
		});
		
	});
	
	$('#koszyk .ilosc input').blur(function(){
		
		var ilosc = $(this).val();
		var cena = $(this).parent('td').parent('tr').children('.cena').children('span').text();
		var wartosc = $(this).parent('td').parent('tr').children('.wartosc').children('span').text();
		
		if(isNaN(ilosc)) {
			ilosc = 0;
			$(this).val('0');
		}
		
		if (ilosc < 0) {
			ilosc = 0;
			$(this).val('0');
		}
		
		wartosc = ilosc * cena;
		$(this).parent('td').parent('tr').children('.wartosc').children('span').text(wartosc.toFixed(2));
		
		var total = parseInt(0);
		$('#koszyk .wartosc').each(function(){
			var wartosc = $(this).children('span').text();
			total = parseFloat(total) + parseFloat(wartosc);
			$('#koszyk .cenaTotal').children('span').text(total.toFixed(2));
		});
		
	});
		
	$('#koszyk .plus a').click(function(){
		
		var produkt = $(this).parent('td').parent('tr').children('.ilosc').children('input').attr('name');
		var ilosc = $(this).parent('td').parent('tr').children('.ilosc').children('input').val();
		var cena = $(this).parent('td').parent('tr').children('.cena').children('span').text();
		var wartosc = $(this).parent('td').parent('tr').children('.wartosc').children('span').text();
		var iloscChange = $(this).attr('rel');
		
		ilosc = parseInt(ilosc) + parseInt(iloscChange);
		
		$(this).parent('td').parent('tr').children('.ilosc').children('input').val(ilosc);
		
		wartosc = ilosc * cena;
		$(this).parent('td').parent('tr').children('.wartosc').children('span').text(wartosc.toFixed(2));
		
		var total = parseInt(0);
		$('#koszyk .wartosc').each(function(){
			var wartosc = $(this).children('span').text();
			total = parseFloat(total) + parseFloat(wartosc);
			$('#koszyk .cenaTotal').children('span').text(total.toFixed(2));
		});
		
		return false;
		
	});
	
	$('#koszyk .minus a').click(function(){
		
		var produkt = $(this).parent('td').parent('tr').children('.ilosc').children('input').attr('name');
		var ilosc = $(this).parent('td').parent('tr').children('.ilosc').children('input').val();
		var cena = $(this).parent('td').parent('tr').children('.cena').children('span').text();
		var wartosc = $(this).parent('td').parent('tr').children('.wartosc').children('span').text();
		var iloscChange = $(this).attr('rel');
		
		if (parseInt(ilosc) > parseInt(iloscChange)) {
			ilosc = parseInt(ilosc) - parseInt(iloscChange);
		} else {
			ilosc = 0;
		}
		$(this).parent('td').parent('tr').children('.ilosc').children('input').val(ilosc);
		
		wartosc = ilosc * cena;
		$(this).parent('td').parent('tr').children('.wartosc').children('span').text(wartosc.toFixed(2));
		
		var total = parseInt(0);
		$('#koszyk .wartosc').each(function(){
			var wartosc = $(this).children('span').text();
			total = parseFloat(total) + parseFloat(wartosc);
			$('#koszyk .cenaTotal').children('span').text(total.toFixed(2));
		});
		
		return false;
		
	});
	
	// target="_blank"
	$('.tBlank').click(function(){
		this.target='_blank';
	});
	
	// delivery
	$('#deliveryCheck').click(function(){
		$("#deliveryFields").toggle();
	});
	
});
