/* Erityislasten Omaiset R.Y Jarno Fabritius Edward Stewart, Meisso.com 2009 */

	$(function(){			   
		
		// Aktivoi tuotteiden esittelyt		
		$(".itemDescription").hide();
		$("a.showDescription").fancybox({
		'overlayShow' : false,
		'zoomSpeedIn' : 600,
		'zoomSpeedOut' : 500
		});
		
		// PÃ¤ivitÃ¤ tuotelista
		updateItems();
		$(".mainForm").hide();
			   
		$(".shoplistButtons input").click(function(){
			var shoplistButton_clicked = $(this).attr("class");
			if(shoplistButton_clicked == "shopEmpty"){
				$(".amount").attr("value", "0");
				updateItems();
			}
			if(shoplistButton_clicked == "shopUpdate"){
				updateItems();
			}
			if(shoplistButton_clicked == "shopBuy"){
				updateItems(true);
			}
		});			   
	});
	
	function updateItems(post){		
		// Resetoi tiedot		
		var finalPrice = 0;
		$("#shopListing").empty();
		
		// Hae tuotteiden mÃ¤Ã¤rÃ¤ ja listaa jonoon
		var itemNames = new Array();		
		var itemAmounts = new Array();
		var itemPrices = new Array();
		var itemSaleAmounts = new Array();
		var itemSales = new Array();
		var itemPricesFinal = new Array();
		
		var shopItems = $("#shoppingTable tbody tr");		
		for(i=0; i < shopItems.size() - 2; i++){
			var itemName = shopItems.eq(i).children().eq(0).html();
			var itemAmount = parseFloat(shopItems.eq(i).children().eq(2).children().eq(0).attr("value"));
			var itemSaleMount = parseFloat(shopItems.eq(i).children().eq(2).children().eq(1).attr("value"));
			var itemSale = parseFloat(shopItems.eq(i).children().eq(2).children().eq(2).attr("value"));
			var itemPrice = parseFloat(shopItems.eq(i).children().eq(3).html());
			
			shopItems.eq(i).children().eq(1).children().eq(0).attr("href", "#itemDescription_"+i);
			shopItems.eq(i).children().eq(1).children().eq(1).attr("id", "itemDescription_"+i);
			
			if(itemAmount>0){
				itemNames.push(itemName);				
				itemAmounts.push(itemAmount);				
				itemSaleAmounts.push(itemSaleMount);
				itemSales.push(itemSale);				
				itemPrices.push(itemPrice);
			}
		}
		// For loppu, tarkistetaan onko yhtÃ¤Ã¤n tuotetta valittu
		if(itemNames.length < 1){
			$(".shoplistInfo").hide();
			
			if(post == true){
				alert("Et ole valinnut yhtään tuotetta");	
			}
		} else {
			$(".shoplistInfo").fadeIn("fast");
			
			// Tee lista tuotteista ja laske lopullinen hinta
			for(i=0; i < itemNames.length; i++){				
						
				if(itemAmounts[i] >= itemSaleAmounts[i]){
				var toRound = itemPrices[i] * itemSaleAmounts[i] - (itemPrices[i]*itemSaleAmounts[i]*(1-itemSales[i]/100));
				var sale = Math.round(toRound*100)/100;				
				var saleText = " ("+itemSales[i]+"% alennus "+itemSaleAmounts[i]+" tuotteesta)";
				} else {
				var saleText = "";
				var sale = 0;
				}
				
			var itemPriceCalc = (itemPrices[i] * itemAmounts[i]) - sale;
				
				var itemListFinal = itemNames[i]+" x "+itemAmounts[i]+" - "+itemPriceCalc+" euroa"+saleText;
				itemPricesFinal.push(itemListFinal);
			
			var itemListing = "<li><a>"+itemNames[i]+" x"+itemAmounts[i]+" : </a><b>"+itemPriceCalc+"euroa</b>"+saleText+"</li>";			
			$("#shopListing").append(itemListing);			
			finalPrice += itemPriceCalc;
			}
			finalPrice = Math.round(finalPrice*100)/100;
			//PÃ¤ivitÃ¤ lopullinen hinta
			$("#finalPrice").html("<b>Lopullinen hinta: "+finalPrice+" euroa</b>");
			
			if(post == true){
				shopToggle("close");
				$("input[name='tuotteet']").val(itemPricesFinal);
				$("input[name='lopullinenhinta']").val(finalPrice+" euroa");
			}
		}
	}
	
	function shopToggle(toggle){		
		var shopSize = $("#shoppingTable tbody tr").size() - 2;
		
		if(toggle == "close"){
		$("#shoppingTable tbody tr").slice(0, shopSize).hide();
		$(".shoplistButtons, #shoppingTable thead").hide();				
		$(".mainForm").show();
		} else {			
		$("#shoppingTable tbody tr").slice(0, shopSize).show();
		$(".shoplistButtons, #shoppingTable thead").show();				
		$(".mainForm").hide();
		}
	}
