148c148,200
<   if (in_array($op, array('insert', 'update'))) {
---
>   switch ($op){
> 	  case 'delete':  
> 			//
> 			// User deleted node.
> 			// If node contains any youtube-url's, delete corresponding entries from media_youtube database tables
> 			//
> 		  foreach ($items as $delta => $item) {
> 			  if ($item['provider'] == 'youtube'){
> 				  // delete media_youtube data for this item
> 				  media_youtube_emfield_field_extra_delete($item['value'], $node->vid, $field['field_name'], $delta);
> 				}
> 			}
> 			break;
>     case 'validate': 
> 			//
> 			// User modified node.
> 			// If user just deleted a youtube-url, delete corresponding entries from media_youtube database tables 
> 			// Note: We use $op='validate' in order to have access to the form data, which is stored in $teaser
> 			//
> 		  foreach ($items as $delta => $item) {
> 				//
> 			  // Check whether the provider is 'youtube'.
> 				//
> 				
> 				// Note:
> 				// (a) If user checked the 'delete' box, then provider is stored in $item['provider']
> 				// (b) If user cleared the url, then $item['provider'] is empty.  Grab provider from $teaser
> 				$provider = !empty($item['provider']) ? $item['provider'] : $teaser['#node']->{$field['field_name']}[$delta]['provider'];
> 				if ($provider == 'youtube'){
> 					//
> 					// Check whether user deleted this item
> 					//
> 					
> 					// Note:
> 					// (a) If user checked the 'delete' box, then $item['emvideo']['delete'] = 1.
> 					// (b) If user cleared the url, then $item['embed'] is empty and $item['value'] contains the youtube-url
> 					$deleted = !empty($item['emvideo']['delete']) || ((empty($item['embed']) && !empty($item['value'])));
> 					if ($deleted){
> 						//
> 						// Delete media_youtube data for this item
> 						//
> 						
> 						// Note: 
> 						// If youtube-url is 'http://www.youtube.com/watch?v=abcdef', then $value to 'abcdef'.
> 						// Grab $value from teaser, because $item['value'] sometimes contains full youtube-url.
> 						$value = $teaser['#node']->{$field['field_name']}[$delta]['value'];
> 				  	media_youtube_emfield_field_extra_delete($value, $node->vid, $field['field_name'], $delta);					
> 					}
> 				}
> 			}
> 			break;				
> 		case 'insert': // fall through
> 		case 'update':
166a219
> 
168a222,244
> 			break;
>   }
> }
> 
> /**
>  * Helper function to delete entries from media_youtube database tables.
>  *
>  * @param
>  *		$value:  youtube-url value.  eg if url is 'http://www.youtube.com/watch?v=abcdef', then $value='abcdef'.
>  *		$vid:  vid for node containing this youtube-url
>  *		$fieldname:  name of emfield containing this youtube-url
>  *		$delta: emfield delta corresponding to this youtube-url
>  * @purpose
>  *		db.media_youtube_node_data:  Delete entry with this $value, $vid, $fieldname and $delta
>  *		db.media_youtube_metadata:  Delete entry with this $value, if it is not in use elsewhere
>  */		
> function media_youtube_emfield_field_extra_delete($value, $vid, $fieldname, $delta){
> 	// Delete row from media_youtube_node_data, for this field delta.
> 	db_query("DELETE FROM {media_youtube_node_data} WHERE value = '%s' AND vid = %d AND field_name = '%s' AND delta = %d", $value, $vid, $fieldname, $delta);	
> 	
> 	// Delete the metadata from media_youtube_metadata, if this youtube url is not in use elsewhere
> 	if (!db_result(db_query("SELECT value FROM {media_youtube_node_data} WHERE value = '%s'", $value))) {
> 		db_query("DELETE FROM {media_youtube_metadata} WHERE value = '%s'", $value);
