<?php
/**
 * Gule functions and definitions
 *
 * @package WordPress
 * @subpackage GuleTheme
 *
 */

//  SET THEME CONTENT WIDTH BASED ON SIZE.
if ( ! isset( $content_width ) )
	$content_width = 1170; // pixels

// CUSTOM HEADER SUPPORT FOR THE THEME
$args = array(
	'width'         => 1170,
	'height'        => 150,
	'header-text'	=> false,
	'default-image' => get_template_directory_uri() . '/images/header-default.png',
	
);
add_theme_support( 'custom-header', $args );

// CUSTOM BACKGROUND SUPPORT FOR THE THEME
$args = array(
	//'default-color' => 'F8F8F8',
	'default-image' => get_template_directory_uri() . 'images/textures/subtle-pattern-1.jpg',
);
add_theme_support( 'custom-background', $args );

// Register Custom Navigation Walker
require_once('inc/bootstrap_navwalker.php');

// THEME SETUP
function gule_theme_setup() {

	// Custom template tags/hooks for GuleTheme.
	locate_template('/inc/template-tags.php', true );

	// Custom template tags/hooks for GuleTheme.
	locate_template('/inc/widget-areas.php', true );

	// Adds RSS feed links to <head> for posts and comments.
	add_theme_support( 'automatic-feed-links' );

	// Theme POST FORMATS
	add_theme_support( 'post-formats', array(
		'aside',
		'gallery',
		'image',
		'link',
		'quote',
		'status',
		'video',
		'audio',
		'chat'
		)
	);

	// Theme MAIN MENU
	register_nav_menu( 'primary', __( 'Main Menu', 'GuleTheme' ) );

	// add Theme Post Thumbnail Support
	add_theme_support( 'post-thumbnails' );

}
add_action( 'after_setup_theme', 'gule_theme_setup' );

// THEME SCRIPTS AND STYLES
function gule_scripts_styles() {
	global $wp_styles;

	// add javascript to threated comments
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();

	// JQuery-js script file
	wp_enqueue_script( 'Jquery-js', get_template_directory_uri() . '/js/jquery.js', array(), '2.0.3', true );

	// bootstrap-js script file
	wp_enqueue_script( 'Bootstrap-js', get_template_directory_uri() . '/js/bootstrap.js', array(), '3.0', true );

	// Bootstrap-css
	wp_enqueue_style( 'Bootstrap-css', get_template_directory_uri() . '/css/bootstrap.css' );

	// Bootstrap-Theme
	wp_enqueue_style( 'Bootstrap-Theme', get_template_directory_uri() . '/css/bootstrap-theme.css' );

	// Elegant-font-icons
	wp_enqueue_style( 'Gule-Icons', get_template_directory_uri() . '/css/GuleIcons.css' );

	// Loads our main stylesheet.
	wp_enqueue_style( 'Theme-Style', get_stylesheet_uri() );

}
add_action( 'wp_enqueue_scripts', 'gule_scripts_styles' );

// THEME WP TITLE
function gule_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( __( 'Page %s', 'GuleTheme' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'gule_wp_title', 10, 2 );


// CONTENT NAVE BELOW POSTS/PAGES
if ( ! function_exists( 'gule_content_nav' ) ) :
function gule_content_nav( $html_id ) {
	global $wp_query;

	$html_id = esc_attr( $html_id );

	if ( $wp_query->max_num_pages > 1 ) : ?>
	<nav class="next-prev">
		<ul id="<?php echo $html_id; ?>" class="pager" role="navigation">
			<li class="previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'GuleTheme' ) ); ?></li>
			<li class="next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'GuleTheme' ) ); ?></li>
		</ul><!-- #<?php echo $html_id; ?> .navigation -->
	</nav>
	<?php endif;
}
endif;

// COMMENTS AND PINGBACKS
if ( ! function_exists( 'gule_comment' ) ) :
function gule_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;

	if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>

	<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
		<div class="comment-body">
			<?php _e( 'Pingback:', 'GuleTheme' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'GuleTheme' ), '<span class="edit-link">', '</span>' ); ?>
		</div>

	<?php else : ?>

	<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
		<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
			<footer class="comment-meta">
				<div class="comment-author vcard">
					<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
					<?php printf( __( '%s <span class="says">says:</span>', 'GuleTheme' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
				</div><!-- .comment-author -->

				<div class="comment-metadata">
					<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
						<time datetime="<?php comment_time( 'c' ); ?>">
							<?php printf( _x( '%1$s at %2$s', '1: date, 2: time', 'GuleTheme' ), get_comment_date(), get_comment_time() ); ?>
						</time>
					</a>
					<?php edit_comment_link( __( 'Edit', 'GuleTheme' ), '<span class="edit-link">', '</span>' ); ?>
				</div><!-- .comment-metadata -->

				<?php if ( '0' == $comment->comment_approved ) : ?>
				<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'GuleTheme' ); ?></p>
				<?php endif; ?>
			</footer><!-- .comment-meta -->

			<div class="comment-content">
				<?php comment_text(); ?>
			</div><!-- .comment-content -->

			<?php
				comment_reply_link( array_merge( $args, array(
					'add_below' => 'div-comment',
					'depth'     => $depth,
					'max_depth' => $args['max_depth'],
					'before'    => '<div class="reply">',
					'after'     => '</div>',
				) ) );
			?>
		</article><!-- .comment-body -->

	<?php
	endif;
}
endif; // ends check for gule_comment()

if ( ! function_exists( 'gule_comment_reply_link' ) ):
// Style comment reply links as buttons
function gule_comment_reply_link( $link ) {
	return str_replace( 'comment-reply-link', 'btn btn-default btn-xs', $link );
}
add_filter( 'comment_reply_link', 'gule_comment_reply_link' );
endif;

// THEME BODY CLASSES
function gule_body_class( $classes ) {
	$background_color = get_background_color();
	$background_image = get_background_image();

	if ( ! is_active_sidebar( 'sidebar-main' ) || is_page_template( 'templates/full-width.php' ) )
		$classes[] = 'full-width';

	if ( empty( $background_image ) ) {
		if ( empty( $background_color ) )
			$classes[] = 'custom-background-empty';
		elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
			$classes[] = 'custom-background-white';
	}

	if ( ! is_multi_author() )
		$classes[] = 'single-author';

	return $classes;
}
add_filter( 'body_class', 'gule_body_class' );


// PAGINATION FOR BLOG AND OTHER PAGES
function gule_pagination_nav($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-wrap'><ul class='pagination'>";
		if($paged > 2 && $paged > $range+1 && $showitems < $pages) 
			echo "<li><a href='".get_pagenum_link(1)."'>&laquo;</a></li>";
		if($paged > 1 && $showitems < $pages) 
			echo "<li><a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a></li>";
		for ($i=1; $i <= $pages; $i++) {
			if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
			{
				echo ($paged == $i)? "<li class='active'><span class='current'>".$i."</span></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>";
			}
		}
		
		if ($paged < $pages && $showitems < $pages) 
			echo "<li><a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a></li>";
		if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) 
			echo "<li><a href='".get_pagenum_link($pages)."'>&raquo;</a></li>";
		echo "</ul></div>";
	}
}
