<?php
/**
 * Framework Functions File
 *
 * Please do not edit this file. This file is part of the CyberChimps Framework and all modifications
 * should be made in a child theme.
 *
 * Text Domain: cyberchimps_core
 *
 * @category CyberChimps Framework
 * @package  Framework
 * @since    1.0
 * @author   CyberChimps
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v3.0 (or later)
 * @link     http://www.cyberchimps.com/
 */

// include plugin.php to use is_plugin_active() condition.
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

/**
 * Enqueue core scripts and core styles
 */
function cyberchimps_core_scripts() {
	global $post;

	// Define paths.
	$directory_uri  = get_template_directory_uri();
	$js_path        = $directory_uri . '/core/lib/js/';
	$bootstrap_path = $directory_uri . '/core/lib/bootstrap/';

	// elements css enqueued for css such as portfolio popup.
	wp_enqueue_style( 'elements_style', $directory_uri . '/core/lib/css/elements.css' );

	// Load JS for slimbox.
	wp_enqueue_script( 'slimbox', $js_path . 'jquery.slimbox.min.js', array( 'jquery' ), '1.0' );

	// Load library for jcarousel.
	wp_enqueue_script( 'jcarousel', $js_path . 'jquery.jcarousel.min.js', array( 'jquery' ), '1.0' );

	// touch swipe gestures.
	wp_enqueue_script( 'jquery-mobile-touch', $js_path . 'jquery.mobile.custom.min.js', array( 'jquery' ) );
	wp_enqueue_script( 'slider-call', $js_path . 'swipe-call.min.js', array( 'jquery', 'jquery-mobile-touch' ) );

	// Load Bootstrap Library Items.
	wp_enqueue_style( 'bootstrap-style', $bootstrap_path . 'css/bootstrap.min.css', false, '3.3.7' );
	wp_enqueue_script( 'bootstrap-js', $bootstrap_path . 'js/bootstrap.min.js', array( 'jquery' ), '3.3.7', true );

	// Load core JS.
	wp_enqueue_script( 'cyberchimps-core-js', $js_path . 'core.min.js', array( 'jquery' ) );

	// Placeholder fix for IE8/9.
	if ( preg_match( '/(?i)msie [8-9]/',$_SERVER['HTTP_USER_AGENT'] ) ) {
		wp_enqueue_script( 'placeholder', $js_path . 'jquery.placeholder.js', array( 'jquery' ) );
	}

	/**
	 * With the use of @2x at the end of an image it will use that to display the retina image. Both images have to been in the same folder
	 */
	wp_enqueue_script( 'retina-js', $js_path . 'retina-1.1.0.min.js', '', '1.1.0', true );

	// Load Core Stylesheet.
	wp_enqueue_style( 'core-style', $directory_uri . '/core/lib/css/core.css', array( 'bootstrap-style' ), '1.0' );

	// Load Theme Stylesheet.
	wp_enqueue_style( 'style', get_stylesheet_uri(), array( 'core-style' ), '1.0' );

	// add javascript for comments.
	if ( is_singular() ) {
		wp_enqueue_script( 'comment-reply' );
	}

	wp_enqueue_script( 'video', $js_path . 'video.min.js' );

}

add_action( 'wp_enqueue_scripts', 'cyberchimps_core_scripts', 20 );

class Cyberchimps_Walker extends Walker_Nav_Menu {

	function start_lvl( &$output, $depth = 0, $args = array() ) {
		// In a child UL, add the 'dropdown-menu' class
		if ( 0 == $depth ) {
			$indent = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
		} else {
			$indent = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul>\n";
		}
	}

	function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		$li_attributes = '';
		$class_names   = $value = '';

		$classes = empty( $item->classes ) ? array() : (array) $item->classes;

		// Add class and attribute to LI element that contains a submenu UL.
		if ( $args->has_children && $depth < 1 ) {
			$classes[] = 'dropdown';
			$li_attributes .= ' data-dropdown="dropdown"';
		}
		if ( $args->has_children && 1 == $depth ) {
			$classes[] = 'grandchild';
		}
		$classes[] = 'menu-item-' . $item->ID;
		// If we are on the current page, add the active class to that menu item.
		$classes[] = ( $item->current ) ? 'active' : '';

		// Make sure you still add all of the WordPress classes.
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = ' class="' . esc_attr( $class_names ) . '"';

		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args );
		$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

		$output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
		// Add attributes to link element.
		$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
		$attributes .= ! empty( $item->url ) ? ' href="' . esc_url( $item->url ) . '"' : '';
		$attributes .= ( $args->has_children && $depth < 1 ) ? ' class="dropdown-toggle"' : '';

		$item_output = $args->before;
		$item_output .= '<a' . $attributes . '>';
		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
		$item_output .= ( $args->has_children && $depth < 1 ) ? ' <span class="glyphicon glyphicon-plus hidden-lg hidden-md"></span> ' : '';
		$item_output .= ( $args->has_children && 1 == $depth ) ? apply_filters( 'cyberchimps_menu_grandchild_caret', '' ) : '';
		$item_output .= '</a>';
		$item_output .= $args->after;

		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}

	// Overwrite display_element function to add has_children attribute. Not needed in >= WordPress 3.4
	function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {

		if ( ! $element ) {
			return;
		}

		$id_field = $this->db_fields['id'];

		// display this element
		if ( is_array( $args[0] ) ) {
			$args[0]['has_children'] = ! empty( $children_elements[ $element->$id_field ] );
		} else {
			if ( is_object( $args[0] ) ) {
				$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
			}
		}
		$cb_args = array_merge( array( &$output, $element, $depth ), $args );
		call_user_func_array( array( &$this, 'start_el' ), $cb_args );

		$id = $element->$id_field;

		// descend only when the depth is right and there are childrens for this element
		if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {

			foreach ( $children_elements[ $id ] as $child ) {

				if ( ! isset( $newlevel ) ) {
					$newlevel = true;
					// start the child delimiter
					$cb_args = array_merge( array( &$output, $depth ), $args );
					call_user_func_array( array( &$this, 'start_lvl' ), $cb_args );
				}
				$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
			}
			unset( $children_elements[ $id ] );
		}

		if ( isset( $newlevel ) && $newlevel ) {
			// end the child delimiter
			$cb_args = array_merge( array( &$output, $depth ), $args );
			call_user_func_array( array( &$this, 'end_lvl' ), $cb_args );
		}

		// end this element
		$cb_args = array_merge( array( &$output, $element, $depth ), $args );
		call_user_func_array( array( &$this, 'end_el' ), $cb_args );
	}
}

class Cyberchimps_Fallback_Walker extends Walker_Page {
	function start_lvl( &$output, $depth = 0, $args = array() ) {
		if ( 0 == $depth ) {
			$indent = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
		} else {
			$indent = str_repeat( "\t", $depth );
			$output .= "\n$indent<ul>\n";
		}
	}

	function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
		if ( $depth ) {
			$indent = str_repeat( "\t", $depth );
		} else {
			$indent = '';
		}

		extract( $args, EXTR_SKIP );
		$class_attr      = '';
		$data            = '';
		$link_class_attr = '';
		$caret           = '';
		if ( 0 == $depth && ! empty( $args['has_children'] ) ) {
			$class_attr .= 'dropdown ';
			$data            = 'data-dropdown="dropdown"';
			$link_class_attr = 'dropdown-toggle';
			$caret           = '<b class="caret"></b>';
		}
		if ( ! empty( $current_page ) ) {
			$_current_page = get_page( $current_page );
			if ( ( isset( $_current_page->ancestors ) && in_array( $page->ID, (array) $_current_page->ancestors ) ) || ( $page->ID == $current_page ) || ( $_current_page && $page->ID == $_current_page->post_parent ) ) {
				$class_attr .= 'current-menu-item current_page_item active';
			}
		} elseif ( ( is_single() || is_archive() ) && ( $page->ID == get_option( 'page_for_posts' ) ) ) {
			$class_attr = '';
		}
		if ( $class_attr != '' ) {
			$class_attr = ' class="' . $class_attr . '"';
		}
		$output .= $indent . '<li' . $class_attr . $data . '><a href="' . esc_url( get_page_link( $page->ID ) ) . '"' . $link_class_attr . '>' . apply_filters( 'the_title', $page->post_title, $page->ID ) . $caret . '</a>';
	}
}

// Sets fallback menu for 1 level. Could use preg_split to have children displayed too
function cyberchimps_fallback_menu() {
	$walker  = new cyberchimps_fallback_walker();
	$args    = array(
		'depth'       => 0,
		'show_date'   => '',
		'date_format' => '',
		'child_of'    => 0,
		'exclude'     => '',
		'include'     => '',
		'title_li'    => '',
		'echo'        => 0,
		'authors'     => '',
		'sort_column' => 'menu_order, post_title',
		'link_before' => '',
		'link_after'  => '',
		'walker'      => $walker,
		'post_type'   => 'page',
		'post_status' => 'publish',
	);
	$pages   = wp_list_pages( $args );
	$prepend = '<ul id="menu-menu" class="nav navbar-nav">';
	$pages   = apply_filters( 'cyberchimps_fallback_menu_filter', $pages, $args );
	$append  = '</ul>';
	$output  = $prepend . $pages . $append;
	echo $output;
}



// Prints HTML with meta information for the current post date/time.
if ( ! function_exists( 'cyberchimps_posted_on' ) ) {

	function cyberchimps_posted_on() {

		// Get all data related to date.
		$date_url   = esc_url( get_permalink() );
		$date_title = esc_attr( get_the_time() );
		$date_time  = esc_attr( get_the_time() );
		$date_time  = esc_attr( get_the_date( 'c' ) );
		$date       = esc_html( get_the_date() );

		// Set the HTML for date link.
		$posted_on = sprintf(
			/* translators: %s: Date */
			__( 'Posted on %s', 'cyberchimps_core' ),
			'<a href="' . $date_url . '" title="' . $date_title . '" rel="bookmark">
							<time class="entry-date updated" datetime="' . $date_time . '">' . $date . '</time>
						</a>'
		);

		// If post byline date toggle is on then print HTML for date link.
		echo apply_filters( 'cyberchimps_posted_on', $posted_on );
	}
}

// Prints HTML for author link of the post.
if ( ! function_exists( 'cyberchimps_posted_by' ) ) {
	function cyberchimps_posted_by() {

		// Get url of all author archive( the page will contain all posts by the author).
		$auther_posts_url = esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) );

		// Set author title text which will appear on hover over the author link.
		/* translators: %s: Author name */
		$auther_link_title = esc_attr( sprintf( __( 'View all posts by %s', 'cyberchimps_core' ), get_the_author() ) );

		// Set the HTML for author link.
		$posted_by = sprintf(
			/* translators: %s: Author details */
			'<span class="byline"> ' . __( 'by %s', 'cyberchimps_core' ),
			'<span class="author vcard">
								<a class="url fn n" href="' . $auther_posts_url . '" title="' . $auther_link_title . '" rel="author">' . esc_html( get_the_author() ) . '</a>
							</span>
						</span>'
		);

		// If post byline author toggle is on then print HTML for author link.
		echo apply_filters( 'cyberchimps_posted_by', $posted_by );
	}
}

if ( ! function_exists( 'cyberchimps_posted_in' ) ) {
	// add meta entry category to single post, archive and blog list if set in options
	function cyberchimps_posted_in() {
		global $post;

		$categories_list = get_the_category_list( ', ' );
		if ( $categories_list ) :
			/* translators: %s: Categories list */
			$cats = sprintf( __( 'Posted in %s', 'cyberchimps_core' ), $categories_list );
			?>
			<span class="cat-links">
			<?php echo apply_filters( 'cyberchimps_post_categories', $cats ); ?>
		</span>
			<span class="sep"> <?php echo apply_filters( 'cyberchimps_entry_meta_sep', '|' ); ?> </span>
		<?php
		endif;
	}
}

if ( ! function_exists( 'cyberchimps_post_tags' ) ) {
	// add meta entry tags to single post, archive and blog list if set in options
	function cyberchimps_post_tags() {
		global $post;

		$tags_list = get_the_tag_list( '', ', ' );
		if ( $tags_list ) :
			/* translators: %s: Tags list */
			$tags = sprintf( __( 'Tags: %s', 'cyberchimps_core' ), $tags_list );
			?>
			<span class="taglinks">
			<?php echo apply_filters( 'cyberchimps_post_tags', $tags ); ?>
		</span>
			<span class="sep"> <?php echo apply_filters( 'cyberchimps_entry_meta_sep', '|' ); ?> </span>
		<?php
		endif; // End if $tags_list
	}
}

if ( ! function_exists( 'cyberchimps_post_comments' ) ) {
	// add meta entry comments to single post, archive and blog list if set in options
	function cyberchimps_post_comments() {
		global $post;

		$leave_comment = ( is_single() || is_page() ) ? '' : __( 'Leave a comment', 'cyberchimps_core' );
		if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) :
		?>
			<span class="comments-link"><?php comments_popup_link( $leave_comment, __( '1 Comment', 'cyberchimps_core' ), '% ' . __( 'Comments', 'cyberchimps_core' ) ); ?></span>
			<span class="sep"> <?php echo ( '' != $leave_comment ) ? apply_filters( 'cyberchimps_entry_meta_sep', '|' ) : ''; ?> </span>
		<?php
		endif;
	}
}

// add featured image to single post, archive and blog page if set in options
function cyberchimps_featured_image() {

	if ( has_post_thumbnail() ) :
	?>
		<div class="featured-image">
			<a href="<?php the_permalink(); ?>" title="<?php
											/* translators: %s: Title attribute */
											printf( esc_attr__( 'Permalink to %s', 'cyberchimps_core' ), the_title_attribute( 'echo=0' ) );
											?>" rel="bookmark">
				<?php the_post_thumbnail(); ?>
			</a>
		</div>
	<?php
	   endif;
}

function cyberchimps_post_format_icon() {
	global $post;

	$format = get_post_format( $post->ID );
	if ( '' == $format ) {
		$format = 'default';
	}

	if ( ! is_page() ) :

		// array of post formats and the matching font icons
		$icons = array(
			'aside' => '<span class="glyphicon glyphicon-list-alt"></span>',
			'audio' => '<span class="glyphicon glyphicon-volume-up"></span>',
			'chat' => '<span class="glyphicon glyphicon-comment"></span>',
			'default' => '<span class="glyphicon glyphicon-file"></span>',
			'gallery' => '<span class="glyphicon glyphicon-film"></span>',
			'image' => '<span class="glyphicon glyphicon-picture"></span>',
			'link' => '<span class="glyphicon glyphicon-link"></span>',
			'quote' => '<span class="glyphicon glyphicon-share"></span>',
			'status' => '<span class="glyphicon glyphicon-th"></span>',
			'video' => '<span class="glyphicon glyphicon-facetime-video"></span>',
		);
		?>

		<div class="postformats"><!--begin format icon-->
			<?php echo $icons[ $format ]; ?>
		</div><!--end format-icon-->
	<?php
	endif;
}

// FOR IE compatiblilty mode.
add_action( 'send_headers', 'cyberchimps_add_header_xua' );
function cyberchimps_add_header_xua() {
	if ( ! headers_sent() ) {
		header( 'X-UA-Compatible: IE=edge,chrome=1' );
	}
}

?>
