<?php	
// Register Widgetized Areas
if(function_exists('register_sidebar')) {
	register_sidebar(array(
		'name' => 'Sidebar',
		'before_widget' => '<div class="widget">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="sidebar-widget-title"><span>',
		'after_title' => '</span></h3>',
	));
	register_sidebar(array(
		'name' => 'Magazine',
		'before_widget' => '<div class="magazine-widget">',
		'after_widget' => '</div><div class="clear"></div>',
		'before_title' => '<div class="widget-line-title"><h3>',
		'after_title' => '</h3></div>',
	));
	register_sidebar(array(
		'name' => 'Footer 1',
		'before_widget' => '<div class="widget-footer">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-footer-title">',
		'after_title' => '</h3>',
	));
	
	register_sidebar(array(
		'name' => 'Footer 2',
		'before_widget' => '<div class="widget-footer">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-footer-title">',
		'after_title' => '</h3>',
	));
	
	register_sidebar(array(
		'name' => 'Footer 3',
		'before_widget' => '<div class="widget-footer">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-footer-title">',
		'after_title' => '</h3>',
	));
	register_sidebar(array(
		'name' => 'Footer 4',
		'before_widget' => '<div class="widget-footer">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-footer-title">',
		'after_title' => '</h3>',
	));
}
// Add post format
add_theme_support('post-formats', array('gallery', 'audio', 'video'));

// Remove Gallery Styling
add_filter( 'gallery_style', 'my_gallery_style', 99 );
function my_gallery_style() {
    return "<div id='gallery'>";
}

// Exclude page from search 
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type',  array( 'post', 'reviews'));
}
return $query;
}
add_filter('pre_get_posts','SearchFilter'); 

// Add post thumbnail functionality
if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support('post-thumbnails', array('post','reviews'));
	add_image_size('carousel-thumb', 130, 90, true);
	add_image_size('big-post-thumb', 300, 150, true);
	add_image_size('small-post-thumb', 75, 60, true);
	add_image_size('related-thumb', 140, 90, true);
	add_image_size('big-nivo-thumb', 1000, 400, true);
	add_image_size('small-nivo-thumb', 620, 340, true);
	add_image_size('featured-thumb', 620, 270, true);
	add_image_size('full-featured-thumb', 1000, 500, true);
	add_image_size('blog1-thumb', 300, 160, true);
}

// Widgets
include_once('panel/widgets/1-column-magazine-widget.php');
include_once('panel/widgets/2-column-magazine-widget.php');
include_once('panel/widgets/recent-posts-widget.php');
include_once('panel/widgets/tabs-sidebar-widget.php');
include_once('panel/widgets/facebook-like-widget.php');
include_once('panel/widgets/tweets-widget.php');
include_once('panel/widgets/flickr-widget.php');
include_once('panel/widgets/carousel-magazine-widget.php');
include_once('panel/widgets/social-widget.php');
include_once('panel/widgets/search-widget.php');
include_once('panel/widgets/adds300x250-widget.php');
include_once('panel/widgets/adds620x90-widget.php');
include_once('panel/widgets/latest-reviews.php');
include_once('panel/widgets/social-counter-widget.php');
include_once('panel/widgets/video-widget.php');

// UI Panel Options
include_once('panel/panel-options.php');

// Metaboxes
include_once('panel/metaboxes/metaboxes.php');

// Profile Metaboxes
include_once('panel/profile.php');

// Shortcodes
include_once('panel/shortcodes/shortcodes.php');


// Include reedwan framework file for the backstreet theme panel
include_once('panel/reedwan-framework.php');

// Translation
load_theme_textdomain('reedwan', get_template_directory() . '/languages');

// Adds RSS feeds link
if(!get_option('reedwan_feedburner')) {
	add_theme_support('automatic-feed-links');
}

//////////////////////////////////////////////////////////////////
// Register reviews post type
//////////////////////////////////////////////////////////////////
add_action( 'init', 'register_reviews_post_type' );

function register_reviews_post_type() {
	register_post_type( 'reviews',
		array(
			'labels' => array(
				'name' => __( 'Reviews' ),
				'singular_name' =>  __( 'Review' ),
				'add_new' =>  __( 'Add New' ),
				'add_new_item' =>  __( 'Add New Review' ),
				'edit' =>  __( 'Edit' ),
				'edit_item' =>  __( 'Edit Review' ),
				'new_item' =>  __( 'New Review' ),
				'view' =>  __( 'View Review' ),
				'view_item' =>  __( 'View Review' ),
				'search_items' =>  __( 'Search Reviews' ),
				'not_found' =>  __( 'No reviews found' ),
				'not_found_in_trash' =>  __( 'No reviews found in Trash' ),
				'parent' =>  __( 'Parent Review' ),
			),
			'public' => true,
			'show_ui' => true,
			'has_archive' => true,
			'publicly_queryable' => true,
			'show_in_nav_menus' => false,
			'exclude_from_search' => false,
			'hierarchical' => false,
            'rewrite' => array('slug'=>'review'),
			'supports' => array('title', 'editor', 'thumbnail', 'comments', 'author'),
		)
	);
flush_rewrite_rules();
}

//////////////////////////////////////////////////////////////////
// Register reviews category taxonomy
//////////////////////////////////////////////////////////////////
add_action( 'init', 'create_review_category', 0 );

function create_review_category() 
{

  $labels = array(
    'name' => _x( 'Review Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Review Category', 'taxonomy singular name' ),
    'search_items' =>   __( 'Search Review Categories' ),
    'all_items' =>  __( 'All Review Categories' ),
    'parent_item' =>  __( 'Parent Review Category' ),
    'parent_item_colon' =>  __( 'Parent Review Category:' ),
    'edit_item' =>  __( 'Edit Review Category' ), 
    'update_item' =>  __( 'Update Review Category' ),
    'add_new_item' =>  __( 'Add New Review Category' ),
    'new_item_name' =>  __( 'New Review Category Name' ),
    'menu_name' =>  __( 'Categories' ),
  ); 	

  register_taxonomy('review_category',array('reviews'), array(
	'public' => true,
	'show_in_nav_menus' => true,
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
  ));

}

// How comments are displayed
function reedwan_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	
		<div class="the-comment">
			<div class="alignleft">
				<?php echo get_avatar($comment,$size='70'); ?>
				<div class="clear"></div>
				<div class="reply-comment"><?php comment_reply_link(array_merge( $args, array('reply_text' => 'Reply', 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?></div>
			</div>
			<div class="comment-box">
				<div class="comment-author">
					<span class="name"><?php echo get_comment_author_link() ?></span>
					<small><?php printf(__('%1$s at %2$s', 'Backstreet'), get_comment_date(),  get_comment_time()) ?><a><?php edit_comment_link(__(' Edit','Backstreet')) ?></a></small>
				</div>
			
				<div class="comment-text">
					<?php if ($comment->comment_approved == '0') : ?>
					<em><?php _e('Your comment is awaiting moderation.', 'Backstreet') ?></em>
					<br />
					<?php endif; ?>
					<?php comment_text() ?>
				</div>
			
			</div>
			
		</div>

<?php }


// Related Post
function get_related_posts($post_id, $tags = array()) {
	$query = new WP_Query();
	
	$post_types = get_post_types();
	unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
	
	if($tags) {
		foreach($tags as $tag) {
			$tagsA[] = $tag->term_id;
		}
	}
	$query = new WP_Query( array('showposts' => 4,'post_type' => $post_types,'post__not_in' => array($post_id),'tag__in' => $tagsA,'ignore_sticky_posts' => 1,
	));
  	return $query;
}

// Kresi Pagination
function kriesi_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'><span class='arrows'>&laquo;</span> First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'><span class='arrows'>&lsaquo;</span> Previous</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>Next <span class='arrows'>&rsaquo;</span></a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last <span class='arrows'>&raquo;</span></a>";
         echo "</div>\n";
     }
}

// Limit Word
function string_limit_words($string, $word_limit)
{
	$words = explode(' ', $string, ($word_limit + 1));
	
	if(count($words) > $word_limit) {
		array_pop($words);
	}
	
	return implode(' ', $words);
}
