<?php
	
	function smt_show_option_video_upload( $param ) { ?>
				
		<label><?php echo $param[ 'title' ]; ?>:</label>
						
		<div class="smt_object_list_new videoupload_cl" data-name="<?php echo $param[ 'name' ]; ?>">
						
			<input type="hidden" name="<?php echo $param['name']; ?>" value="<?php echo $param['value']; ?>" />
			
				<video style="display:<?php echo (!empty($param['value'])?'auto':'none'); ?>" controls>
					<source src="<?php echo $param['value']; ?>" type="video/mp4">
				</video>
				
			<span class="smt_object_list_element_title"><?php echo (!empty($param['value'])?'Change Video File':'Upload Video File'); ?></span>
			<div class="clear"></div>
		</div>
		
	<script>
	
		jQuery( document ).on( 'click', '.videoupload_cl', function() {
			
			var videoOption = jQuery( this );
			var videoInput = videoOption.find( 'input[type="hidden"]' );
			
			if( wp.media.frames[ videoInput.attr( 'name' ) ] ) {
				wp.media.frames[ videoInput.attr( 'name' ) ].open();
				return;
			}
						
			wp.media.frames[ videoInput.attr( 'name' ) ] = wp.media({
			   title: 'Select video',
			   multiple: false,
			   library: {
				  type: 'video'
			   },
			   button: {
				  text: 'Use selected video'
			   }
			});
			
			wp.media.frames[ videoInput.attr( 'name' ) ].on( 'select', function() {
				var selection = wp.media.frames[ videoInput.attr( 'name' ) ].state().get('selection');
			 
				if (!selection) {
					return;
				}
			 					
				// iterate through selected elements
				selection.each(function(attachment) {
					
					console.log( attachment );
					var url = attachment.attributes.url;
					videoOption.find( 'input[type="hidden"]' ).val( url );
					videoOption.find( 'video' ).attr( 'src', '' );
					videoOption.find( 'video' ).attr( 'src', url );
					videoOption.find( 'video' ).css( 'display', 'inline-block' );
					videoOption.find( '.smt_object_list_element_title' ).text( 'Change video' );
					console.log( url );
					
				});
			} );
			wp.media.frames[ videoInput.attr( 'name' ) ].open();
			
		});
	
	</script>
	
	<?php }
	