<?php get_header();

$page_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$short_page_url = $_SERVER["REQUEST_URI"];

// Orderby
if($_GET['orderby'] == "title")  {
	$orderby = "title";
} elseif($_GET['orderby'] == "comments")  {
	$orderby = "comment_count";
} elseif($_GET['orderby'] == "likes")  {
	$orderby = "meta_value_num";
	$meta_key = "_likes";
} elseif($_GET['orderby'] == "date")  {
	$orderby = "date";	
} else {
	$orderby = "date";
}

// Order
if($_GET['order'] == "asc")  {
	$order = "asc";
} elseif($_GET['order'] == "desc")  {
	$order = "desc";	
} else {
	$order = "desc";
}

// Category
if($theme_home_category_type == "Post Category") {
	$category = "post";
	$category_type = "cat";
	$category_name = $theme_home_post_category;	
} else {
	$category = "portfolio";
	$category_type = "portfolio_categories";
	$category_name = $theme_home_portfolio_category;	
}

// Post Query	
$args=array(
'post_type' => $category,
$category_type => $category_name,
'caller_get_posts'=> 1,
'orderby' => $orderby,
'meta_key' => $meta_key,
'order' => $order,
'paged' => $paged
);

$featured_query = new wp_query($args); 

?>

<div id="content-wrapper">
	
	<?php get_sidebar(); ?>

	<div id="main-content">
		
		<!--Begin Box Controls-->
		<?php if($theme_home_page_title == "0" OR $theme_home_order_options == "0" OR $theme_home_page_nav == "Top & Bottom" OR $theme_home_page_nav == "Top") { ?>
		
			<div id="box-details">
			
				<div class="left">
				
					<?php if($theme_home_page_title == "0") { ?>
	
						<h1 class="page-title">Home</h1>
						
						<span class="post-meta-divider">|</span>
					
					<?php } ?>
					
					<?php if($theme_home_order_options == "0") { ?>
					
						<div class="box-order-by"><?php _e('Order By:', 'gp_lang'); ?></div>
						
						<form class="order-by-form">
							<select>
								<option value="<?php echo home_url(); ?>?orderby=date"<?php if(preg_match('/orderby=date/', $page_url)) { ?> selected<?php } ?>><?php _e('Date', 'gp_lang'); ?></option>		
								<option value="<?php echo home_url(); ?>?orderby=title"<?php if(preg_match('/orderby=title/', $page_url)) { ?> selected<?php } ?>><?php _e('Title', 'gp_lang'); ?></option>	
								<option value="<?php echo home_url(); ?>?orderby=likes"<?php if(preg_match('/orderby=likes/', $page_url)) { ?> selected<?php } ?>><?php _e('Likes', 'gp_lang'); ?></option>	
								<option value="<?php echo home_url(); ?>?orderby=comments"<?php if(preg_match('/orderby=comments/', $page_url)) { ?> selected<?php } ?>><?php _e('Comments', 'gp_lang'); ?></option>				
							</select>
						</form>
						
						<div class="box-order"><?php _e('Order:', 'gp_lang'); ?></div>
		
						<form class="order-form">
							<select>
								<option value="<?php if(preg_match('/orderby=/', $page_url)) { ?><?php echo $short_page_url; ?>&order=desc<?php } else { ?><?php echo $short_page_url; ?>?order=desc<?php } ?>"<?php if(preg_match('/order=desc/', $page_url)) { ?> selected<?php } ?>><?php _e('Descending', 'gp_lang'); ?></option>							
								<option value="<?php if(preg_match('/orderby=/', $page_url)) { ?><?php echo $short_page_url; ?>&order=asc<?php } else { ?><?php echo $short_page_url; ?>?order=asc<?php } ?>" <?php if(preg_match('/order=asc/', $page_url)) { ?> selected<?php } ?>><?php _e('Ascending', 'gp_lang'); ?></option>				
							</select>
						</form>
					
					<?php } ?>
					
				</div>
	
				<?php if($theme_home_page_nav == "Top & Bottom" OR $theme_home_page_nav == "Top") { ?>
					<div class="right">	
						<?php gp_pagination($featured_query->max_num_pages); ?>
					</div>
				<?php } ?>
				
			</div>

		<?php } ?>
		<!--End Box Controls-->
			
		<!--Begin Box Container-->
		<div id="box-container"<?php if($theme_home_page_title == "0" OR $theme_home_order_options == "0" OR $theme_home_page_nav == "Top & Bottom" OR $theme_home_page_nav == "Top") {} else { ?> class="box-shift"<?php } ?>>

			<?php if ($featured_query->have_posts()) : while ($featured_query->have_posts()) : $featured_query->the_post(); 

			$args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'numberposts' => 1, 'orderby' => 'menu_order', 'order' => 'ASC');
			
			$attachments = get_children($args);			
			
			?>
	
				<!--Begin Box-->
				<div id="post-<?php the_ID(); ?>" <?php post_class('box'); ?>>
				
					<!--Begin Image-->
					<?php if(has_post_thumbnail() OR $attachments) { ?>
					
						<div class="box-thumbnail" style="width: <?php echo $theme_home_image_width; ?>px; height: <?php echo $theme_home_image_height; ?>px;">
		
							<a href="<?php if(get_post_meta($post->ID, 'ghostpool_lightbox_type', true) == "Video") { ?>file=<?php echo get_post_meta($post->ID, 'ghostpool_custom_link', true); ?>&amp;image=<?php echo get_post_meta($post->ID, 'ghostpool_thumbnail', true); ?><?php } elseif(get_post_meta($post->ID, 'ghostpool_lightbox_type', true) == "Image") { ?><?php echo get_post_meta($post->ID, 'ghostpool_custom_link', true); ?><?php } else { ?><?php if(get_post_meta($post->ID, 'ghostpool_custom_link', true)) { ?><?php echo get_post_meta($post->ID, 'ghostpool_custom_link', true); ?><?php } else { ?><?php the_permalink(); ?><?php } ?><?php } ?>"<?php if(get_post_meta($post->ID, 'ghostpool_lightbox_type', true) == "None") {} else { ?> rel="prettyPhoto"<?php } ?>>
							
								<span class="overlay" style="width: <?php echo $theme_home_image_width; ?>px; height: <?php echo $theme_home_image_height; ?>px;"></span>
								
								<img src="<?php if($theme_timthumb == "0") { ?><?php echo get_template_directory_uri(); ?>/lib/scripts/timthumb.php?src=<?php } ?>
								
								<?php if(has_post_thumbnail()) {
								
									$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
									echo $image[0];
									
								} elseif($attachments) { 
								
									foreach ($attachments as $attachment) {
										echo wp_get_attachment_url($attachment->ID);
									}
									
								} ?>	
								
								<?php if($theme_timthumb == "0") { ?>&amp;h=<?php echo $theme_home_image_height; ?>&amp;w=<?php echo $theme_home_image_width; ?>&amp;zc=1&amp;a=c<?php } ?>" style="width: <?php echo $theme_home_image_width; ?>px; height: <?php echo $theme_home_image_height; ?>px;" alt="<?php the_title(); ?>" />
							
							</a>
		
						</div>
						
						<div class="clear"></div>

					<?php } ?>
					<!--End Image-->								
					
					<!--Begin Box Text-->			
					<div class="box-text" style="width: <?php echo $theme_home_image_width; ?>px;">
					
						<?php if($theme_home_text_display == "Full") { ?>
				
							<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
						
							<?php if($theme_home_excerpt_length == '0') {} else { ?><p><?php echo excerpt($theme_home_excerpt_length); ?></p><?php } ?>
							
							<div class="box-meta">			
								<div class="left"><?php echo time_ago(); ?></div>
								<div class="right">
									<?php printLikes(get_the_ID()); ?>
									<?php if('open' == $post->comment_status) { comments_popup_link(__('0', 'gp_lang'), __('1', 'gp_lang'), __('%', 'gp_lang'), 'box-comments', ''); } ?>
								</div>
							</div>
						
						<?php } else { ?>
						
							<div class="left"><h2><a href="<?php the_permalink(); ?>"><?php _e('Details', 'gp_lang'); ?> &rarr;</a></h2></div>
	
							<div class="right">
								<?php printLikes(get_the_ID()); ?>
								<?php if('open' == $post->comment_status) { comments_popup_link(__('0', 'gp_lang'), __('1', 'gp_lang'), __('%', 'gp_lang'), 'box-comments', ''); } ?>
							</div>
								
						<?php } ?>
						
					</div>
					<!--End Box Text-->	
		
				</div>
				<!--End Box-->
				
			<?php endwhile; ?>	
		
			<?php else : ?>	
		
				<h2 class="page-title-error"><?php _e('No items found.', 'gp_lang'); ?></h2>
				<h4 class="page-title-error"><?php _e('Try using the search form above to search for the item you are looking for.', 'gp_lang'); ?></h4>
				
			<?php endif; wp_reset_query(); ?>	
	
		</div>
		<!--End Box Container-->
		
	</div>
	
</div>

<!--Begin Bottom Box Details-->
<?php if($theme_home_page_nav == "Top & Bottom" OR $theme_home_page_nav == "Bottom") { ?>

	<div class="clear"></div>
	
	<div id="box-details-bottom">
		<?php gp_pagination($featured_query->max_num_pages); ?>
	</div>

<?php } ?>
<!--End Bottom Box Details-->

<script>
jQuery(document).ready(function() {
	var jQuerycontainer = jQuery('#box-container');
	jQuerycontainer.masonry({
		itemSelector: '.box',
		columnWidth: <?php echo $theme_home_image_width + 42; ?>
	}); 
});
</script>

<?php get_footer(); ?>