$(function() {

		// Set the change event for when an option is changed
	$("#itemoptions select").change(function() {
		var op = {};
		op[1] = op[2] = op[3] = 0;
		$.each(opsetOrder, function(k, v) {
			if ($("#itemoptions select#" + v).length > 0) {
				op[k+1] = $("#itemoptions select#" + v).val();
			}
		}) ;
		var newPrice = priceMatrix[op[1]][op[2]][op[3]] ;
		$("#itemprice span").html("Price: &pound;" + newPrice) ;
	}) ;
	
		// Reset the options to the first one available
	$("#itemoptions select").each(function() {
		$(this).val($(this).find("option:first").attr("value"));
	});
	$("#itemoptions select:first").change();

		// Add the custom menu's
	$("#itemoptions select").hide() ;
	$("body").bind('click', function() {
		$("#itemoptions .expanded").switchClass('expanded', 'collapsed', 300) ;
		$("#itemoptions div.optionset").removeClass('focused');
		$("#itemoptions .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
	}) ;
	$("#itemoptions select").each(function() {
		$(this).after('<ul class="opset collapsed"><li class="first"><span val="'+ $(this).find("option:first").attr("value") +'">'+ $(this).find("option:first").text() +'</span></li></ul>') ;
	}) ;
	$("#itemoptions option").each(function(k, v) {
		$(this).parent().next().append('<li><span val="' + $(this).attr("value") + '">' + $(this).text() + '</span></li>') ;
	}) ;
	
	$("#itemoptions .opset li.first span").click(function() {
		if ($(this).parent().parent().hasClass('expanded'))
			return false;
		$("#itemoptions .expanded").switchClass('expanded', 'collapsed', 300) ;
		$("#itemoptions .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
		$(this).parent().parent().switchClass('collapsed', 'expanded', 300) ;
		$(this).parent().parent().parent().parent().addClass('focused');

		return false;
	}) ;
	$("#itemoptions .opset li:not(.first) span").click(function() {
		$("#itemoptions div.optionset").removeClass('focused');
		$(this).parent().parent().prev().val($(this).attr("val")).change() ;
		$(this).parent().parent().find('li.first span').text($(this).text()).attr("val", $(this).attr("val")) ;
		
			// Work out what should and shouldn't be disabled
		doDisableUnavailableItems() ;
		
		$(this).parent().parent().switchClass('expanded', 'collapsed', 300) ;
		$("#itemoptions .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
		return false ;
	}) ;
	
	$("#itemoptions p#instock input.btn").bind('click', function() {
		if ($(this).hasClass('unavailable'))
			return false ;
	}) ;
	
	doDisableUnavailableItems() ;
});

function doDisableUnavailableItems() {
	opsetInfo = new Array(opsetOrder.length) ;
	for (i=0; i<=(opsetOrder.length-1); i++) {
		opsetInfo[i] = new Array(2) ;
		opsetInfo[i][0] = $("#itemoptions .first span:eq(" + i + ")").attr("val") ; 
		opsetInfo[i][1] = $("#itemoptions select:eq(" + i + ")").attr("id") ;
	}
	
	$(".opset li").addClass("disabled") ;
	$(".opset li").not(".first").attr("title", "This option is not available with the other option(s) you have chosen.") ;
	
	if (opsetOrder.length == 1) {
		$.each(availabilityMatrix[opsetInfo[0][1]], function(valID, isAvailable) {
			if (isAvailable == 1)
				$(".opset span[val=" + valID + "]").parent().removeClass("disabled").attr("title", "") ;
		}) ;
	}
	else if (opsetOrder.length == 2) {
		var otherOpsets = new Array(1, 0) ;
		$.each(opsetInfo, function(k, chosenOpData) {
				// If there is a val, they must have selected something
			if (chosenOpData[0] != 0) {
				var otherOpsetTitle = opsetInfo[otherOpsets[k]][1] ;
				$.each(availabilityMatrix[chosenOpData[1]][chosenOpData[0]][otherOpsetTitle], function(valID, isAvailable) {
					if (isAvailable == 1) {
						$(".opset span[val=" + valID + "]").parent().removeClass("disabled").attr("title", "") ;
					}
				}) ;
			}
		}) ;
	}
	else if (opsetOrder.length == 3) {
		var otherOpsets = new Array(new Array(1,2), new Array(0,2), new Array(0,1)) ;
		$.each(opsetInfo, function(k, chosenOpData) {
				// If there is a val, they must have selected something
			if (chosenOpData[0] != 0) {
				var otherOpsetTitle = opsetInfo[otherOpsets[k][0]][1] ;
				var otherOpsetVal = opsetInfo[otherOpsets[k][0]][0] ;
				var remainingOpsetTitle = opsetInfo[otherOpsets[k][1]][1] ;
				var remainingOpsetVal = opsetInfo[otherOpsets[k][1]][0] ;
				$.each(availabilityMatrix[chosenOpData[1]][chosenOpData[0]][otherOpsetTitle][otherOpsetVal][remainingOpsetTitle], function(valID, isAvailable) {
					if (isAvailable == 1) {
						$(".opset span[val=" + valID + "]").parent().removeClass("disabled").attr("title", "") ;
					}
				}) ;
				$.each(availabilityMatrix[chosenOpData[1]][chosenOpData[0]][remainingOpsetTitle][remainingOpsetVal][otherOpsetTitle], function(valID, isAvailable) {
					if (isAvailable == 1) {
						$(".opset span[val=" + valID + "]").parent().removeClass("disabled").attr("title", "") ;
					}
				}) ;
			}
		}) ;
	}
	
	var enableAddToBasket = true ;
	$("#itemoptions .opset li.first").each(function() {
		if ($(this).hasClass('disabled')) 
			enableAddToBasket = false ;
	}) ;

	if (enableAddToBasket) {
		$("#itemoptions p#instock input.btn").removeClass('unavailable').attr("value", "Add to basket") ;
	} else {
		$("#itemoptions p#instock input.btn").addClass('unavailable').attr("value", "Combination Unavailable") ;
	}

	return false ;
}
