//JS file to dynamically filter options on the set chooser page
$(document).ready(function() {
	
	function calcPrice(price, quantity, currentTotal) {
		
	}
	
	var linenColour = $("select.linen-colour").html();
	var linenStyle = $("select.linen-style").html();
	var changed = false;
	var el = "<option value=\"\" selected=\"selected\">Please Select</option>";
	var style;
	$("select.linen-colour").attr("disabled", true);
	//$("select.itemQuantity").attr("disabled", true);
	$("select.itemQuantity").attr("disabled", false);
	//$("select.sbItem").attr("disabled", true);
	$("select.sbItem").attr("disabled", false);
	$("select.itemQuantity").fadeOut();
	
	$("select.linen-style").change(function() {
		if (changed == false) {
			if ($("select.linen-style option:selected").hasClass("sboption")) {
				$("select.linen-colour").attr("disabled", false);
				className = $("select.linen-style option:selected").attr("class");
				$("select.linen-colour option:not(."+className+")").remove();
				$("select.sbItem").attr("disabled", false);
				$("select.itemQuantity").attr("disabled", false);
				changed = true;
			}
			return;
		}
		if (changed == true) {
			//Someone has selected from the first dropdown twice
			//repopulate with default values
			$("select.linen-colour").html(linenColour).prepend(el).attr("disabled", true);
			$("select.sbItem").attr("disabled", true);
			$("select.itemQuantity").attr("disabled", true);
			//now filter those values, all this to-ing and fro-ing needs deferring to a proper function
			if ($("select.linen-style option:selected").hasClass("sboption")) {
				$("select.linen-colour").attr("disabled", false);
				$("select.sbItem").attr("disabled", false);
				$("select.itemQuantity").attr("disabled", false);
				className = $("select.linen-style option:selected").attr("class");
				$("select.linen-colour option:not(."+className+")").remove();
			} 
			return;			
		}
	});
	
	$("select.sbPillowCase").change(function() {
		if ($("select.sbPillowCase option:selected").attr("value") > 0) {
			$("select.sbPillowCaseQuant").fadeIn();
		} else {
			$("select.sbPillowCaseQuant").fadeOut();
		}
	});
	
	$("select.sbDuvetCovers").change(function() {
		if ($("select.sbDuvetCovers option:selected").attr("value") > 0) {		
			$("select.sbDuvetCoverQuant").fadeIn();
		} else {
			$("select.sbDuvetCoverQuant").fadeOut();
		}
	});
	
	$("select.sbFittedSheets").change(function() {
		if ($("select.sbFittedSheets option:selected").attr("value") > 0) {		
			$("select.sbFittedSheetQuant").fadeIn();
		} else {
			$("select.sbFittedSheetQuant").fadeOut();
		}
	});
	
	$("select.sbFlatSheets").change(function() {
		if ($("select.sbFlatSheets option:selected").attr("value") > 0) {		
			$("select.sbFlatSheetQuant").fadeIn();
		} else {
			$("select.sbFlatSheetQuant").fadeOut();
		}
	});
	
	$("select.sbTopSheets").change(function() {
		if ($("select.sbTopSheets option:selected").attr("value") > 0) {		
			$("select.sbTopSheetQuant").fadeIn();
		} else {
			$("select.sbTopSheetQuant").fadeOut();
		}
	});
	
	/*$(".setBuilderform select.sbItem").change(function() {
		var currentTotal = $("span.settotal").text();
		var myId = $(this).attr("id");
		var price = $("#"+myId+" option:selected").attr("class");
		var quant = $("#"+myId+" + select").val();
		if (quant != "") {
			//alert("Quantity = "+quant+ "Unit price = ");
			var subtotal = price*quant;
			alert("Subtotal = "+subtotal);
			currentTotal = parseFloat(currentTotal);
			subtotal = parseFloat(subtotal);
			var total = currentTotal+subtotal;
			total = total.toFixed(2);
			//alert("New Total = "+total);
			$("span.settotal").html(total);
		}
		return false;
	});*/
	
	/*$(".setBuilderform select.itemQuantity").change(function() {
		var currentTotal = parseFloat($("span.settotal").text());
		var quant = $(this).val();
		var prev = $(this).prev().attr("id");
		var price = $("#"+prev+" option:selected").attr("class");
		if (price != "") {
			//An item has been selected, we've got a usable price 
			var subtotal = quant*price;
			subtotal = parseFloat(subtotal);
			var total = currentTotal+subtotal;
			$("span.settotal").html(total.toFixed(2));
		}
		return false;
	});*/
	/*$(".setBuilderForm").bind("submit", function() {
			$("select.sbItem").attr("disabled", false);
			$("select.itemQuantity").attr("disabled", false);
			$(".setBuilderForm").submit();
	});*/
});


