<?php 


/*-----------------------------------------------------------------------------------*/
/* Footer Custom Scripts
/*-----------------------------------------------------------------------------------*/
add_action('wp_footer', 'aw_custom_footer_scripts', 100);
function aw_custom_footer_scripts() { 

	$drag = of_get_option('slide_drag', 'true');
	$arrow = of_get_option('slide_arrow', 'true');
	$btn = of_get_option('slide_btn', 'true');
	$slideshow = of_get_option('slide_slideshow', 'true');
	$delay = of_get_option('slide_delay', '3000');
	$animation = of_get_option('slide_animation', 'random');

?>

<script type="text/javascript" charset="utf-8">	
jQuery(document).ready(function($) {
$('#slider').oneByOne({
	className: 'oneByOne1',	   
	responsive: true,	
	easeType: '<?php echo $animation; ?>',
	slideShow: <?php echo $slideshow; ?>,
	enableDrag: <?php echo $drag; ?>,
	showArrow: <?php echo $arrow; ?>,
	showButton: <?php echo $btn; ?>,
	slideShowDelay: <?php echo $delay; ?>
});  	
$("#slider").addClass('visible');
});	
</script>

<?php }


/*-----------------------------------------------------------------------------------*/
/* Ajax
/*-----------------------------------------------------------------------------------*/

add_action( "wp_ajax_aw_get_ajax_project", "aw_get_ajax_project" );
add_action( "wp_ajax_nopriv_aw_get_ajax_project", "aw_get_ajax_project" );

function aw_get_ajax_project() {

    if ( !wp_verify_nonce( $_REQUEST['nonce'], "portfolio_item_nonce" ) ) {
    	exit("No naughty business please");
    }     
	
	$grid_classes = 'grid_9 alpha';
	$quality = 90;
	$desired_width = 700;
	$desired_height = 500;
	$current_post_id = $_REQUEST['post_id'];
	$terms = get_the_terms( $_REQUEST['post_id'] , 'portfolio_category', 'string' );
	$content_post = get_post( $_REQUEST['post_id'] );
	$content = $content_post->post_content;
	$content = apply_filters('the_content', $content);
	$content = str_replace(']]>', ']]>', $content);
	$post = get_post( $current_post_id ); 
	$client = get_post_meta( $current_post_id, 'pm_client', true );	
	$clienturl = get_post_meta( $current_post_id, 'pm_clienturl', true );	
	$img_align = get_post_meta( $current_post_id, 'pm_img_align', true );	
	$portfolio_images = aw_get_the_portfolio_images( $_REQUEST['post_id'] );	
	
	ob_start();
?>

<!-- START #portfolio-details -->
<div id="portfolio-details">

	<div class="slideshow clearfix">
	
	
	<!-- START Image -->
	<div id="<?php if ($img_align == 'Right') { ?>single-item-right<?php }  else { ?> single-item<?php } ?>" class="slideshowImage <?php echo $grid_classes; ?>">
		
		<?php if( count( $portfolio_images ) === 1 ) {		
		
		$portfolio_img_url = $portfolio_images[0];
		
		$image_details = wp_get_attachment_image_src( aw_get_attachment_id_from_src( $portfolio_img_url ), 'portfolio-large');
		$image_full_width = $image_details[1];
		$image_full_height = $image_details[2];		
		
		?>
			
			<?php foreach ( $portfolio_images as $portfolio_img_url ) { ?>
			
				<?php echo $portfolio_img_url; ?>
			
			<?php } ?>
		
		<?php } ?>
		
	</div><!-- /.slideshowImage -->
	<!-- END Image -->
	
	<!-- START #portfolio-item-meta -->	
	<div id="<?php if ($img_align == 'Right') { ?>portfolio-item-meta-right<?php }  else { ?> portfolio-item-meta<?php } ?>" class="slideshowText">
		
		<h2><?php echo get_the_title( $current_post_id ); ?></h2>
		
		<?php if ( $terms ) { 
					
			// Portfolio category		
			foreach ( $terms as $term ) {
				echo '<p class="jobDesc">' . $term -> name . '</p>';
			}
				
		} ?>		
		
			
			<p class="client"><span><?php _e( 'Client: ', 'awesome' ); ?></span> <?php if( $client ) {  echo $client; }?></p>
			
		
		<?php echo $content; ?>
				
		<?php if( $clienturl ) { ?>			
		<a class="read-more" href="<?php echo $clienturl; ?>">Visit Website</a>						
		<?php } ?>
		
		<?php
			
		$nonce_prev = wp_create_nonce("portfolio_item_nonce");
		$nonce_next = wp_create_nonce("portfolio_item_nonce");	
		
		?>	

		<div class="slideNav clearfix">
		<?php if ( $_REQUEST['prev_post_id'] && $_REQUEST['next_post_id'] ) { ?>			
			<a class="prev prev-portfolio-post" href="#" rel="prev" href="#" data-post_id="<?php echo $_REQUEST['prev_post_id']; ?>" data-nonce="<?php echo $nonce_prev; ?>">Prev</a>
			<a class="next next-portfolio-post" href="#" rel="next" href="#" data-post_id="<?php echo $_REQUEST['next_post_id']; ?>" data-nonce="<?php echo $nonce_next; ?>">Next</a>
		<?php } else if ( $_REQUEST['prev_post_id'] ) { ?>	
			<a class="prev prev-portfolio-post" href="#" rel="prev" href="#" data-post_id="<?php echo $_REQUEST['prev_post_id']; ?>" data-nonce="<?php echo $nonce_prev; ?>">Prev</a>	
		<?php } else if ( $_REQUEST['next_post_id'] ) { ?>
			<a class="next next-portfolio-post" href="#" rel="next" href="#" data-post_id="<?php echo $_REQUEST['next_post_id']; ?>" data-nonce="<?php echo $nonce_next; ?>">Next</a>
		<?php } ?>	
			<a class="exit close-current-post">Close</a>
		</div><!-- /.slideNav -->		
					
	</div>
	<!-- END #portfolio-item-meta -->		
	
	
	
	<div class="portfolio-border grid_12 alpha omega group">&nbsp;</div>
	
	
	</div><!-- /.slideshow -->
	
</div>
<!-- END #portfolio-details -->	

<?php

 	$result['html'] = ob_get_clean();

   	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
      	$result = json_encode($result);
      	echo $result;
   	}
   	else {
      	header("Location: ".$_SERVER["HTTP_REFERER"]);
   	}

   	die();

} // End of aw_get_ajax_project


/*-----------------------------------------------------------------------------------*/
/* Get the id of the attachment by providing the source of the image. Needed for
 * finding the image's meta info, such as its width and height.
/*-----------------------------------------------------------------------------------*/

function aw_get_attachment_id_from_src( $image_src ) {
	
	global $wpdb;
	
	$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
	$id = $wpdb->get_var($query);
	
	return $id;
	
}


/*-----------------------------------------------------------------------------------*/
/* General Settings */
/*-----------------------------------------------------------------------------------*/

/* Output the URL of the current page */

function aw_the_current_page_url() {
	
	$page_url = 'http';
	
	if ( $_SERVER["HTTPS"] == "on" ) {
		$page_url .= "s";
	}
	
	$page_url .= "://";
		
	if ( $_SERVER["SERVER_PORT"] != "80" ) {
		$page_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
	} 
	else {
		$page_url .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	}
	
	return $page_url;
	
}

/*-----------------------------------------------------------------------------------*/
/* Portfolio Meta Box Values */
/*-----------------------------------------------------------------------------------*/

function aw_get_the_preview_img_url() {
	
	global $post;
	$preview_img = get_post_meta( $post->ID, 'portfolio-preview-img', true );
	
	return $preview_img;
	
}


function aw_get_the_portfolio_images( $post_id ) {
		
	$img1 = get_the_post_thumbnail($post_id, 'portfolio-large' );
	
	$meta_fields = array( $img1 );
	$image_urls = array();
	
	foreach($meta_fields as $meta_field) {
		if( $meta_field ) {
			$image_urls[] = $meta_field;
		}
	}
	
	return $image_urls;
	
}

?>