(function($) {
	$(document).ready (function () {
		// Don't submit upon enter keypress when in textarea
		var textareaHasFocus = false;
		
		$('textarea')
			.focus (function () { textareaHasFocus = true; })
			.blur (function () { textareaHasFocus = false; });
					
		$(document).keydown (function (event) {
			if (event.keyCode == 13 && !textareaHasFocus) {
				$('div.save input').click ();
			}
		});
		
		// Change product sizes when a different product is selected
		var select_boxes = $('select');
		var product_box = null;
		var size_box = null;
		var finish_box = null;
		var border_box = null;
		
		// Get the right boxes
		select_boxes.each (function () {
			var id = $(this).attr ('id');
			if (id.substring (id.length - 11) == 'canvas_size') {
				size_box = $(this);
			}
			else if (id.substring (id.length - 12) == 'photo_finish') {
				finish_box = $(this);
			}
			else if (id.substring (id.length - 4) == 'edge') {
				border_box = $(this);
			}
		});
		
		select_boxes.each (function () {
			// If the product box, add the event handler
			var id = $(this).attr ('id');
			if (id.substring (id.length - 7) == 'product') {
				product_box = $(this);
				product_box.change (function () {
					var product_name = product_box.val ();

					// Now we have the name of the product selected, AJAX the related sizes
					updateOptions (product_name, size_box, finish_box, border_box);
				});
			}
		});
		
		var updateOptions = function (product_name, size_box, finish_box, border_box) {
			// Disable the boxes until request is complete
			size_box.attr ('disabled', 'disabled');
			finish_box.attr ('disabled', 'disabled');
			border_box.attr ('disabled', 'disabled');
			
			$.getJSON (
				'/upload/ajx-get-options',
				{
					name: product_name
				},
				function (json) {
					// Save the current selection
					var cur_size_val = size_box.val ();
					var cur_finish_val = finish_box.val ();
					var cur_border_val = border_box.val ();
					
					// Empty the current values
					size_box.empty ();
					finish_box.empty ();
					border_box.empty ();
					
					// Add the new ones
					$.each (json['sizes'], function (i, size) {
						$('<option value="' + size.replace ('"', '&quot;') + '">' + size + '</option>').appendTo (size_box);
					});
					$.each (json['finishes'], function (i, finish) {
						$('<option value="' + finish.replace ('"', '&quot;') + '">' + finish + '</option>').appendTo (finish_box);
					});
					$.each (json['border_options'], function (i, option) {
						$('<option value="' + option.replace ('"', '&quot;') + '">' + option + '</option>').appendTo (border_box);
					});
					
					// Reselect the item if it still exists
					$('option[value="' + cur_size_val + '"]', size_box).attr ('selected', 'selected');
					$('option[value="' + cur_finish_val + '"]', finish_box).attr ('selected', 'selected');
					$('option[value="' + cur_border_val + '"]', border_box).attr ('selected', 'selected');
					
					// Hide boxes if there are no options
					if ($('option', finish_box).length == 0) { finish_box.parent ().css ('display', 'none'); }
					else {finish_box.parent ().css ('display', 'block'); }
					
					if ($('option', border_box).length == 0) { border_box.parent ().css ('display', 'none'); }
					else {border_box.parent ().css ('display', 'block'); }
					
					// Re-enable boxes
					size_box.attr ('disabled', '');
					finish_box.attr ('disabled', '');
					border_box.attr ('disabled', '');
					
					// Temporary note
					if (product_name == "Framed Canvas Block") {
						border_box.after ($('<p id="out-of-stock" style="margin: 4px 0 0 190px"><strong>Please note:</strong> The white frame option is currently out of stock</p>'));
					} else {
						border_box.parent ().find ('#out-of-stock').remove ();
					}
				}
			);
		};
		
		// It's a bit dodgy, but to make this AJAX stuff and Zend work together, call the change event on document load
		// This is because Zend wants all items loaded at init for validation purposes, but we just want to show the
		// appropriate ones
		
		product_box.change ();
	});
})(jQuery);