$(function()
{
	var upsellId;

	$(".optionLink").bind("click", function()
	{
		$("#dialog")
			.dialog({
				resizable: false,
				modal: true,
				height: 300,
				width: 475,
				close: function(){
					$(this).empty();
				}
			})
			.load("/option/option/index/option/" + this.id.substr(6));
		$.get("/option/option/title/option/" + this.id.substr(6), function(data)
		{
			$("#dialog").dialog(
			{
				"title": data
			});
		});
		return false;
	});
	$(".upsellLink").bind("click", function()
	{
		upsellId = this.id.substr(6);

		$("#dialog")
			.dialog({
				resizable: false,
				modal: true,
				height: 300,
				width: 475,
				close: function(){
					$(this).empty();
				}
			})
			.load("/upsell/upsell/index/upsell/" + upsellId);
		$.get("/upsell/upsell/title/upsell/" + upsellId, function(data)
		{
			$("#dialog").dialog(
			{
				"title": data
			});
		});
		return false;
	});

	$(".addUpsell").live("click", function() {
		$("#dialog").dialog('close');
		$("#productUpsells-" + upsellId).attr("checked", true);
	});

	$("[name=productOptions]").change(function() {
		checkMatrix();
	});

	$("[name=productColours]").change(function() {
		checkMatrix();
	});

	var defaultImage = $("#productMainImage").html();

	function checkMatrix()
	{
		$.get('/option/matrix', {option: $("[name=productOptions]:checked").val(), colour: $("[name=productColours]:checked").val()}, function(data)
		{
			if(data)
			{
				$("#productMainImage").html(data);
			}
			else
			{
				$("#productMainImage").html(defaultImage);
			}
		});
	}

	checkMatrix();
});
