function initOpSetChangeFunkyness() {
	$("form#chooseoptions select").change(function() {
		var op1 = op2 = op3 = 0 ;
		$.each(opsetOrder, function(k, v) {
			if ($("form#chooseoptions select#" + v).length > 0)
				eval("op" + (k+1) + " = " + $("form#chooseoptions select#" + v).val()) ;
		}) ;
		var newPrice = priceMatrix[op1][op2][op3] ;
		var newPicSrc = itemPictureMatrix[op1][op2][op3]['small'] ;
		var oldPicSrc = $("#thumbpic").attr("src") ;
		if ((newPicSrc != oldPicSrc) && newPicSrc)
			$("#thumbpic").attr('src', newPicSrc) ;
		$(".price").html("&pound;" + newPrice) ;
	}) ;
	$("form select").val(0) ;
}

function initZoomPicFunkyness() {
	$("#picbox").after('<p id="zoomnote"><em>* Click the magnifying glass to enable zoom.<br/>* Click and drag the panel once activated to pan.</em></p><div id="detailbox"><div id="viewport"></div></div>').append('<div id="previewHighlighter"></div><div id="previewEnabler"></div>') ;
	$("#detailbox, #previewHighlighter").css("opacity", "0.5").hide() ;
	
	$("body").bind("click", function() {
		if (zoomerActive)
			$("#previewEnabler").click() ;
	}) ;
	$("#picbox, #detailbox").bind("click", function() { return false ; }) ;
	
	$("#previewEnabler").toggle(function() {
		var op1 = op2 = op3 = 0 ;
		$.each(opsetOrder, function(k, v) {
			if ($("form#chooseoptions select#" + v).length > 0)
				eval("op" + (k+1) + " = " + $("form#chooseoptions select#" + v).val()) ;
		}) ;
		var newPicSrc = itemPictureMatrix[op1][op2][op3]['large'] ;

		$("#previewHighlighter").fadeIn(300) ;
		$("#detailbox #viewport").append('<img src="'+ newPicSrc +'" />') ;
		$("#detailbox #viewport img:last").hide().bind("load", function() {
			$(this).show() ;
		}) ;
		
		$("#detailbox").fadeTo(1, 0.001).show(2) ;
		$("#previewHighlighter").effect('transfer', { to: '#detailbox' }, 1000) ;
		setTimeout(function() {
			$("#detailbox").fadeTo(1, 1) ;
		}, 1302)
		zoomerActive = true ;
	}, function() {
		$("#previewHighlighter").fadeOut(300) ;
		$("#detailbox:visible").fadeOut(300, function() {
			$("#detailbox #viewport img:first").remove() ;
		}) ;
		zoomerActive = false ;
	}) ;
	
	$("#previewHighlighter").draggable({ 
		containment: 'parent',
		drag: function(e, ui) {
			var smallWidth = $("#thumbpic").width();
			var smallHeight = $("#thumbpic").height();
			
			var zoomWidth = $("#viewport").width();
			var zoomHeight = $("#viewport").height();

				// WORK OUT PAGEX AND PAGEY OF THE THUMBPIC
			var thumbPageOffsets = $("#thumbpic").offset() ;
			var thumbPageX = thumbPageOffsets.left ;
			var thumbPageY = thumbPageOffsets.top ;

			var centreOriginLeft = Math.round(thumbPageX + (smallWidth / 2)) ;
			var centreOriginTop = Math.round(thumbPageY + (smallHeight / 2)) ;
			var posLeftFromCentre = centreOriginLeft - (ui.absolutePosition.left + 29) ;
			var posTopFromCentre = centreOriginTop - (ui.absolutePosition.top + 29) ;
			var posLeftFromCentreForBigImg = Math.round(posLeftFromCentre * zoomWidth / smallWidth) ;
			var posTopFromCentreForBigImg = Math.round(posTopFromCentre * zoomHeight / smallHeight) ;
			var bigLeftCentre = Math.round(zoomWidth / 2) ;
			var bigTopCentre = Math.round(zoomHeight / 2) ;

			var backgroundLeftMove = 0 - bigLeftCentre + posLeftFromCentreForBigImg + Math.round(smallWidth/2) ;
			var backgroundTopMove = 0 - bigTopCentre + posTopFromCentreForBigImg + Math.round(smallHeight/2) ;

			$("#detailbox #viewport").css("left", backgroundLeftMove +"px").css("top", backgroundTopMove +"px") ;
		}
	}) ;
}

function initUnavailableOptionTester() {
	$("#options select").hide() ;
	$("body").bind('click', function() {
		$("#options .expanded").switchClass('collapsed', 'expanded', 300) ;
		$("#options .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
	}) ;
	$("#options 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>') ;
	}) ;
	$("#options option").each(function(k, v) {
		$(this).parent().next().append('<li><span val="' + $(this).attr("value") + '">' + $(this).text() + '</span></li>') ;
	}) ;
	
	$("#options .opset li.first span").click(function() {
		if (zoomerActive)
			$("#previewEnabler").click() ;
		$("#options .expanded").switchClass('collapsed', 'expanded', 300) ;
		$("#options .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
		$(this).parent().parent().switchClass('expanded', 'collapsed', 300) ;
		$("#options .select").css("z-index", "5") ;
		$(this).parent().parent().parent().css("z-index", "6") ;
	}) ;
	$("#options .opset span").click(function() {
		if ($(this).parent().hasClass('first')) return false ;
		$(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() ;
		
		if (!suppressAnimation)
			$(this).parent().parent().switchClass('collapsed', 'expanded', 300) ;
		$("#options .opset").each(function() { $(this).get(0).scrollTop = 0 ; }) ;
		return false ;
	}) ;
	
	$("form#chooseoptions").before('<div id="basketerror"><p>Sorry that combination is not available. Please retry referring to the ticks and crosses in the options lists and to the table below. To close this box click on the red cross</p><p><a id="closebasketerror" href="#">Close</a></p></div>') ;
	$("#closebasketerror").click(function() {
		$("#basketerror").hide() ;
		return false ;
	}) ;
	
	$("form#chooseoptions p.buy input").bind('click', function() {
		if ($(this).hasClass('unavailable')) {
			$("#basketerror").show() ;
			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] = $("form#chooseoptions .first span:eq(" + i + ")").attr("val") ; 
		opsetInfo[i][1] = $("form#chooseoptions 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 ;
	$("#options .opset li.first").each(function() {
		if ($(this).hasClass('disabled')) 
			enableAddToBasket = false ;
	}) ;

	if (enableAddToBasket) {
		$("form#chooseoptions p.buy input").removeClass('unavailable') ;
	} else {
		$("form#chooseoptions p.buy input").addClass('unavailable') ;
	}

	return false ;
}
