$(function() {
	jQuery.fn.detailbox = function() {
		return this.click(clickHandler);
	}

	$('body').append('<div id="dialog" title="Product Details"></div>');

	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		width: 640,
		height:640,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			Close: function() {
				$(this).empty();
				$(this).dialog('close');
			}
		}
	});

	function clickHandler() {
		var parts = this.id.split("_");
		var action = parts[1];
		var pid = parts[parts.length-1];
		if (action == 'addToCart') {
			url = 'store_suggested.php?add_to_cart=' + pid;
		}
		else {
			url = this.href;
		}
		url = url + '&detail=1';

		$.ajax({
			url: url,
			cache: true,
			success: function(html){
				$("#dialog").empty();
				$("#dialog").append(html);
				$('#dialog').dialog('open');
			}
		});
		return false;
	}
});

$(function() {
	$('a[rel*=detailbox]').detailbox();
});