$(function() {
		   
	$("#shipping-option").change(function() {
		$("#cart").submit();											   
	});		
	
	/* lightbox handler
	------------------------------- */		   
	$('a.lightbox').lightBox(); 
	
	/* shipping overlay event
	------------------------------- */
	/*$('.shipping-overlay').click(function() {
										  
		// get overlay url to load
		var url	= $(this).attr('href');
		url += (url.indexOf('?') > -1 ? '&' : '?') + 'ajax=1';	
		
		launchOverlay(url);
		
		return false;		
							
	});*/
	

	/* handle product submit
	------------------------------- */
	$("form.product").submit(function() { 
									  
		var handling = $(this).children("input[name='handling']").val();
		var returnVal = true;
		
		if (handling <= 0) {
			launchOverlay("shipping-destination.php?ajax=1");
			return false;
		}
		else {
			return true;
		}

	});
	
	
	/* launch overlay
	------------------------------- */	
	function launchOverlay(url) {

		// load overlay content
		$.get(url, function(data) {
			Overlay.show(data, 400);
			
			// enable overlay close
			$('#close a').click(function() {
				Overlay.hide();	
				return false; 
			});
			
			var options = {
				beforeSubmit: getCountry,
				success: returnSuccess
			};
			
			$("#shipping-form").submit(function() { 
				$(this).ajaxSubmit(options);
				return false;
			});
			
			function getCountry(formData, jqForm, options) {
				$("#shipping-nav dd span").html($('#shipping-form select.country').val());
				$("input[name='handling']").val($('#shipping-form select.country').val());
			}
			
			function returnSuccess(responseText, statusText) {
				Overlay.hide();
			}
		});	
		
	}
	
	/* quantity handler
	------------------------------- */		
	$("select.quantity").change(function() { 
		//alert($(this).val());
	});			
	
	/* overlay setup
	------------------------------- */
	Overlay.opacity = 80;
	Overlay.setStyle('overlay');
	
	/* dynamic quantity update
	------------------------------- */	
	$("#wine-bottle-bag select#item_code").change(function() { 
		var value = $(this).val();		
		var quantity = $("option[value="+value+"]").attr("title");
		
		var optionString = "";
		
		for (i=1; i<=quantity; i++)
		{
			optionString += '<option value="' + i + '">' + i + '</option>';
		}
		
		$("#wine-bottle-bag select#quantity").html(optionString);
	});
	
});
