// $Id:$
if (Drupal.jsEnabled) {
  $(document).ready(function() {
		
		$('#jasons_nutrition_map_form option:selected').each( function() {
			if ($(this).attr('value') == 'select') {
				$(this).parent()
					.css({
						'color' : '#ff0000',
						'font-weight' : 'bold'
						}
					);
				$(this).siblings()
					.css({
						'color' : '#000',
						'font-weight' : 'normal'
						}
					);
				$(this).parent().change( function() {
					$(this).css({
						'color' : '#000',
						'font-weight' : 'normal'
						}
					);
				});
			}
		});
		
		// Hide the submit button for the nutrition menu item select.
		$('#jasons-nutrition-item-select-form #edit-submit').hide();
		// Auto submit nutrition menu item select on change.
		$("#edit-menu-item-select").change(function() {
			$("#jasons-nutrition-item-select-form").submit();
		});
		
		
		$('#dialog').dialog({
				autoOpen: false,
				height: 500,
				width: 600,
				dialogClass: 'add-ingredients',
				modal: true,
				buttons: {
					'Update Ingredients': function() {
						
						var ingredients = new Array;
						
						$('#jasons-nutrition-ingredient-select-form input.form-checkbox:checked').each( function() {
							ingredients.push($(this).val());
						});
						
						var saveIngredients = function(data) {
							$('#' + data.parent_id + ' .ingredient-list').html(data.ing_names).hide().fadeIn();
						}
						
						$.ajax({
							type: 'POST',
							url: '/ajax/nutrition-item/save-ingredients',
							dataType: 'json',
							success: saveIngredients,
							data: {
								'ajax': true,
								'parent_id': window.name,
								'ingredients': ingredients.join(',')
							}
						});
						
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				},
				open: function(event, ui) {
					$('#jasons-nutrition-ingredient-select-form .form-checkboxes div').css({
						'width': '49.9%'
						});
					$('#jasons-nutrition-ingredient-select-form .form-checkboxes div:even').css({
						'float': 'left'
						});
					$('#jasons-nutrition-ingredient-select-form .form-checkboxes div:odd').css({
						'float': 'right'
						});
					
					//$('.ui-dialog-titlebar').show();
					$('.ui-dialog-content').show();
					
					// Uncheck all checkboxes
					$('#jasons-nutrition-ingredient-select-form input.form-checkbox').attr('checked', false);
					
					var setDefaults = function(data) {
						if (data.defaults.length > 0) {
						  $('#jasons-nutrition-ingredient-select-form input.form-checkbox').val(data.defaults);
						}
					}
					
					$.ajax({
						type: 'POST',
						url: '/ajax/nutrition-item/get-default-ingredients',
						dataType: 'json',
						success: setDefaults,
						data: {
							'ajax': true,
							'parent_id': window.name
						}
					});
				},
				
				close: function() {
					$(this).attr('id', 'dialog').fadeOut();
				}
			
		});
		
		$('.edit-ingredients').click(function() {
			var parentId = $(this).parents('tr').attr('id');
			window.name = parentId;
			$('#dialog').dialog('open').attr('id', 'dialog-' + parentId);
		});
		
		// orderTalk description dialog
		$('#ot-desc-dialog').dialog({
				autoOpen: false,
				height: 300,
				width: 600,
				dialogClass: 'ot-desc',
				modal: true,
				buttons: {
					Cancel: function() {
						$(this).dialog('close');
					}
				},
				open: function(event, ui) {
					
					var showOtDesc = function(data) {
						$('#ot-desc-dialog').html('<div class="ui-dialog-content-inner">' + data.desc + '</div>');
					}
					
					$.ajax({
						type: 'POST',
						url: '/ajax/nutrition-item/get-ot-desc',
						dataType: 'json',
						success: showOtDesc,
						data: {
							'ajax': true,
							'parent_id': window.name
						}
					});
				}
			
		});
		
		$('.ot-desc .has-desc').click(function() {
			var parentId = $(this).parents('tr').attr('id');
			window.name = parentId;
			$('#ot-desc-dialog').dialog('open');
		});
		
		
		// Custom description dialog
		$('#custom-desc-dialog').dialog({
				autoOpen: false,
				height: 300,
				width: 600,
				dialogClass: 'custom-desc',
				modal: true,
				buttons: {
					'Edit this item': function() {
						var nid = window.name.replace('menu-item-', '');
						window.location = '/node/' + nid + '/edit?destination=admin/nutrition/manage';
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				},
				open: function(event, ui) {
					
					var showCustomDesc = function(data) {
						$('#custom-desc-dialog').html('<div class="ui-dialog-content-inner">' + data.desc + '</div>');
					}
					
					$.ajax({
						type: 'POST',
						url: '/ajax/nutrition-item/get-custom-desc',
						dataType: 'json',
						success: showCustomDesc,
						data: {
							'ajax': true,
							'parent_id': window.name
						}
					});
				}
			
		});
		
		$('.custom-desc .has-desc').click(function() {
			var parentId = $(this).parents('tr').attr('id');
			window.name = parentId;
			$('#custom-desc-dialog').dialog('open');
		});
		
		$('.custom-desc .no-desc').hover(
			function() {
			  var parentId = $(this).parents('tr').attr('id');
				var nid = parentId.replace('menu-item-', '');
				$(this).html('<a href="/node/' + nid + '/edit?destination=admin/nutrition/manage">Edit</a>');
			},
			function() {
			  $(this).html('No');
			}
		);
		
		$('.custom-desc .has-desc').hover(
			function() {
			//  var parentId = $(this).parents('tr').attr('id');
			//	var nid = parentId.replace('menu-item-', '');
				$(this).html('<a href="#">View</a>');
			},
			function() {
			  $(this).html('Yes');
			}
		);
		
		$('.ot-desc .has-desc').hover(
			function() {
			//  var parentId = $(this).parents('tr').attr('id');
			//	var nid = parentId.replace('menu-item-', '');
				$(this).html('<a href="#">View</a>');
			},
			function() {
			  $(this).html('Yes');
			}
		);
		
		// Change node status ajax
		$('.update-node-status span').click( function() {
			
			var parentId = $(this).parents('tr').attr('id');
			if ($(this).hasClass('node-status-0')) {
				var curStatus = 0;
			}
			else {
				curStatus = 1;
			}
			
			var statusUpdated = function(data) {
				$('#' + data.parent_id + ' .update-node-status span').removeClass().addClass('node-status-' + data.new_status).html(data.new_status_text).hide().fadeIn();
			}
			
			$.ajax({
				type: 'POST',
				url: '/ajax/update-node-status',
				dataType: 'json',
				success: statusUpdated,
				data: {
					'ajax': true,
					'parent_id': parentId,
					'cur_status': curStatus
				}
			});
		});
		
  });
}