<?php
 
//adding metabox to gallery items
add_action( 'admin_menu', 'wp_pinterest_to_wp_create_meta_box2' );

function wp_pinterest_to_wp_create_meta_box2(){
	add_meta_box( 'wp_pinterest_to_wp_page-meta-boxes2', 'Gallery Items', 'wp_pinterest_to_wp_page_meta_boxes2', 'wp_pinterest_to_wp', 'normal', 'low' );
}

function wp_pinterest_to_wp_page_meta_boxes2(){
	
	wp_pinterest_to_wp_page_meta_boxes_pin2();
}

//metabox content 
function wp_pinterest_to_wp_page_meta_boxes_pin2(){
	
	//reading meta values
	global $post;
	$id= $post->ID;
	$gallery_user=get_post_meta($id,'gallery_user',1);
	$gallery_type=get_post_meta($id,'gallery_type',1);
	$gallery_board=get_post_meta($id,'gallery_board',1);
	
	

	
	
	if(trim($gallery_type) == ''){
		echo 'Choose options and hit update to see items';
		return;
	}elseif($gallery_type == 'boards'){
		
		require_once 'core.php';
		$boards=$pinterest->wp_boards_get($id,$gallery_user);
		
		$gallery_itms=get_post_meta($id,'gallery_itms',1);
	if(!is_array($gallery_itms)) $gallery_itms=array();
		
		$ids=$boards[0];
		$titles=$boards[1];
		$links=$boards[2];
		$imgs=$boards[3];
		
		$i=0;
		foreach($ids as $id){
			echo ' <div  class="gallery_img" style="background-image:url(\''.$imgs[$i].'\')" >';
			
			
			
			 echo '<input     name="gallery_itms[]" id="field-gallery_itms-1" value="'.$ids[$i].'" type="checkbox">';     
			
			echo '</div>';
			$i++;
		}
		
		echo '<div style="clear:both"></div>';
		
	}elseif($gallery_type == 'pins'){
		 
		require_once 'core.php';
		$pins=$pinterest->wp_pins_get($id,$gallery_user);
		$gallery_itms=get_post_meta($id,'gallery_itms',1);
		if(!is_array($gallery_itms)) $gallery_itms=array();
		$ids=$pins[0];
		$imgs=$pins[1];
		$ttls=$pins[2];
		
		$i=0;
		foreach($ids as $id){
			echo ' <div  class="gallery_img" style="background-image:url(\''.$imgs[$i].'\')" >'; 
			echo '<input     name="gallery_itms[]" id="field-gallery_itms-1" value="'.$ids[$i].'" type="checkbox">';
			echo '</div>';
			$i++;
		}
		
		echo '<div style="clear:both"></div>';
		
		
	}else{
		
		require_once 'core.php';
		$pins=$pinterest->wp_pins_get($id,$gallery_board,true);
		$gallery_itms=get_post_meta($id,'gallery_itms',1);
		if(!is_array($gallery_itms)) $gallery_itms=array();
		$ids=$pins[0];
		$imgs=$pins[1];
		$ttls=$pins[2];
		
		$i=0;
		foreach($ids as $id){
			echo ' <div  class="gallery_img" style="background-image:url(\''.$imgs[$i].'\')" >'; 
			echo '<input     name="gallery_itms[]" id="field-gallery_itms-1" value="'.$ids[$i].'" type="checkbox">';
			echo '</div>';
			$i++;
		}
		
		echo '<div style="clear:both"></div>';
		
	}
	
	?>
		
		<script type="text/javascript">
		    var $vals2 = '<?php echo implode('|' ,$gallery_itms); ?>';
		    $val_arr2 = $vals2.split('|');
		    
		    jQuery.each($val_arr2, function (index, value) {
		        if (value != '') {
		            jQuery('input:checkbox[value="' + value + '"]').attr('checked', 'checked');
		        }
		    });
		</script> 
	
	<?php 
 	
	 
}