<?php
/**
 * Custom template tags for this theme.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package Maskitto Light
 */


if ( ! function_exists( 'maskitto_light_paging_blog' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
 */

function maskitto_light_paging_blog() {

	GLOBAL $wp_query;

	if ( is_front_page() ) {
		$base = get_home_url().'/%_%';
	} else {
		$base = get_permalink( get_option( 'page_for_posts' ) ).'%_%';
	}

	$args = array(
		'base' => $base,
		'format' => ( ( get_option( 'permalink_structure' ) && ! $wp_query->is_search ) || ( is_home() && get_option( 'show_on_front' ) !== 'page' && ! get_option( 'page_on_front' ) ) ) ? '?paged=%#%' : '&paged=%#%',
	);

	echo paginate_links( $args );


	// Disabled, because of pagination
	if( 1 != 1 ) {
		wp_link_pages( $args );
	}
}
endif;


if ( ! function_exists( 'maskitto_light_paging_nav' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
 */

function maskitto_light_paging_nav() {
	// Don't print empty markup if there's only one page.
	if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
		return;
	}
	?>
	<nav class="navigation paging-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'maskitto-light' ); ?></h1>
		<div class="nav-links">

			<?php if ( get_next_posts_link() ) : ?>
			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'maskitto-light' ) ); ?></div>
			<?php endif; ?>

			<?php if ( get_previous_posts_link() ) : ?>
			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'maskitto-light' ) ); ?></div>
			<?php endif; ?>

		</div><!-- .nav-links -->
	</nav><!-- .navigation -->
	<?php
}
endif;


/**
 * Display navigation to next/previous post
 */

if ( ! function_exists( 'maskitto_light_post_nav' ) ) :

    function maskitto_light_post_nav() {
        // Don't print empty markup if there's nowhere to navigate.
        $previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
        $next     = get_adjacent_post( false, '', false );

        if ( ! $next && ! $previous ) {
            return;
        }
        ?>
        <div class="post-navigation">
            <div class="row">
                <?php
                    previous_post_link( '<div class="col-md-6 nav-previous grey">%link</div>', '<i class="fa fa-angle-left"></i>Previous post', ''  );
                    next_post_link(     '<div class="col-md-6 nav-next text-right grey">%link</div>', 'Next post<i class="fa fa-angle-right"></i>', '' );
                ?>
            </div>
        </div>
        <?php
    }

endif;


if ( ! function_exists( 'maskitto_light_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
 */
function maskitto_light_posted_on() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
	}

	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() ),
		esc_attr( get_the_modified_date( 'c' ) ),
		esc_html( get_the_modified_date() )
	);

	$posted_on = sprintf(
		_x( 'Posted on %s', 'post date', 'maskitto-light' ),
		'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
	);

	$byline = sprintf(
		_x( 'by %s', 'post author', 'maskitto-light' ),
		'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
	);

	echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';

}
endif;


if ( ! function_exists( 'maskitto_light_entry_footer' ) ) :
/**
 * Prints HTML with meta information for the categories, tags and comments.
 */
function maskitto_light_entry_footer() {
	// Hide category and tag text for pages.
	if ( 'post' == get_post_type() ) {
		/* translators: used between list items, there is a space after the comma */
		$categories_list = get_the_category_list( __( ', ', 'maskitto-light' ) );
		if ( $categories_list && maskitto_categorized_blog() ) {
			printf( '<span class="cat-links">' . __( 'Posted in %1$s', 'maskitto-light' ) . '</span>', $categories_list );
		}

		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', __( ', ', 'maskitto-light' ) );
		if ( $tags_list ) {
			printf( '<span class="tags-links">' . __( 'Tagged %1$s', 'maskitto-light' ) . '</span>', $tags_list );
		}
	}

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link">';
		comments_popup_link( __( 'Leave a comment', 'maskitto-light' ), __( '1 Comment', 'maskitto-light' ), __( '% Comments', 'maskitto-light' ) );
		echo '</span>';
	}

	edit_post_link( __( 'Edit', 'maskitto-light' ), '<span class="edit-link">', '</span>' );
}
endif;


/**
 * Returns true if a blog has more than 1 category.
 *
 * @return bool
 */
function maskitto_light_categorized_blog() {
	if ( false === ( $all_the_cool_cats = get_transient( 'maskitto_categories' ) ) ) {
		// Create an array of all the categories that are attached to posts.
		$all_the_cool_cats = get_categories( array(
			'fields'     => 'ids',
			'hide_empty' => 1,

			// We only need to know if there is more than one category.
			'number'     => 2,
		) );

		// Count the number of categories that are attached to the posts.
		$all_the_cool_cats = count( $all_the_cool_cats );

		set_transient( 'maskitto_categories', $all_the_cool_cats );
	}

	if ( $all_the_cool_cats > 1 ) {
		// This blog has more than 1 category so maskitto_categorized_blog should return true.
		return true;
	} else {
		// This blog has only 1 category so maskitto_categorized_blog should return false.
		return false;
	}
}


/**
 * Initialize admin edit button
 */

if ( ! function_exists( 'maskitto_light_admin_edit' ) ) :

    function maskitto_light_admin_edit( $blog_id ) {
        if ( current_user_can('edit_post', $blog_id) ) { ?>
            <a href="<?php echo get_edit_post_link( $blog_id ); ?>" class="admin-edit"><i class="fa fa-pencil-square-o"></i> <?php _e( 'edit', 'maskitto-light' ); ?></a>
    <?php } }

endif;


/**
 * Social icons
 */

if ( ! function_exists( 'maskitto_light_social_icons' ) ) :

    function maskitto_light_social_icons() {
    	global $maskitto_light;
    ?>

		<?php 
			if( !isset($maskitto_light['header-social-links']) || $maskitto_light['header-social-links'] == 1 ) :
				$new_tab = ' target = "_blank" ';
			else :
				$new_tab = '';
			endif;
		?>

		<?php if(isset($maskitto_light['social-network-facebook']) && $maskitto_light['social-network-facebook']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-facebook']); ?>"<?php echo $new_tab; ?>><i class="fa fa-facebook"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-twitter']) && $maskitto_light['social-network-twitter']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-twitter']); ?>"<?php echo $new_tab; ?>><i class="fa fa-twitter"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-google']) && $maskitto_light['social-network-google']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-google']); ?>"<?php echo $new_tab; ?>><i class="fa fa-google-plus"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-youtube']) && $maskitto_light['social-network-youtube']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-youtube']); ?>"<?php echo $new_tab; ?>><i class="fa fa-youtube"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-instagram']) && $maskitto_light['social-network-instagram']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-instagram']); ?>"<?php echo $new_tab; ?>><i class="fa fa-instagram"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-flickr']) && $maskitto_light['social-network-flickr']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-flickr']); ?>"<?php echo $new_tab; ?>><i class="fa fa-flickr"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-foursquare']) && $maskitto_light['social-network-foursquare']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-foursquare']); ?>"<?php echo $new_tab; ?>><i class="fa fa-foursquare"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-skype']) && $maskitto_light['social-network-skype']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-skype']); ?>"<?php echo $new_tab; ?>><i class="fa fa-skype"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-vk']) && $maskitto_light['social-network-vk']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-vk']); ?>"<?php echo $new_tab; ?>><i class="fa fa-vk"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-wordpress']) && $maskitto_light['social-network-wordpress']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-wordpress']); ?>"<?php echo $new_tab; ?>><i class="fa fa-wordpress"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-linkedin']) && $maskitto_light['social-network-linkedin']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-linkedin']); ?>"<?php echo $new_tab; ?>><i class="fa fa-linkedin"></i></a>
		<?php } ?>

		<?php if(isset($maskitto_light['social-network-pinterest']) && $maskitto_light['social-network-pinterest']){ ?>
			<a href="<?php echo esc_url($maskitto_light['social-network-pinterest']); ?>"<?php echo $new_tab; ?>><i class="fa fa-pinterest"></i></a>
		<?php } ?>

    <?php }

endif;


/**
 * Generate custom CSS
 */

if ( ! function_exists( 'maskitto_light_generate_css' ) ) :

    function maskitto_light_generate_css() {
    	global $maskitto_light;

		if( isset( $maskitto_light ) ) : ob_start(); ?>
			<style type="text/css">
				<?php if( isset($maskitto_light['primary-color']) && $maskitto_light['primary-color'] && $maskitto_light['primary-color'] != '#e15454' ) { ?>
					body a, body a:hover {
						color: <?php echo esc_attr($maskitto_light['primary-color']); ?>;
					}
					.service-icon i, header .navbar-nav .dropdown.open .dropdown-toggle, .active-category, .post-gallery-title, .comment-navigation .current, .contact-social-icons a, .countup-circle {
						color: <?php echo esc_attr($maskitto_light['primary-color']); ?>!important;
					}

					.service-icon, .service-line, .current-menu-item a, .current_page_parent a, blockquote, header .navbar-nav .dropdown.open .dropdown-toggle, .contact-social-icons a {
						border-color: <?php echo esc_attr($maskitto_light['primary-color']); ?>!important;
					}

					.btn-danger, .section-title-line, .blog-category, header .navbar-nav .dropdown.open .current-menu-item, .widget-area-2 .widget_search, .widget-area-2 .search-field {
						background-color: <?php echo esc_attr($maskitto_light['primary-color']); ?>!important;
					}

					@media (min-width: 1000px) {

						 header .navbar-nav .current-menu-item a, header .navbar-nav .current_page_parent a {
							color: <?php echo esc_attr($maskitto_light['primary-color']); ?>!important;
						}

					}

					@media (max-width: 1000px) {

						header .navbar-nav .current-menu-item, header .navbar-nav .current_page_parent {
							background-color: <?php echo esc_attr($maskitto_light['primary-color']); ?>;
						}

					}
				<?php } ?>

				<?php if( isset($maskitto_light['body-background-color']) && $maskitto_light['body-background-color'] != '#ffffff' ) { ?>
					body {
						background-color: <?php echo esc_attr($maskitto_light['body-background-color']); ?>!important;
					}
				<?php } ?>

				<?php if( isset($maskitto_light['footer-background-color']) && $maskitto_light['footer-background-color'] != '#565656' ) { ?>
					footer.bottom {
						background-color: <?php echo esc_attr($maskitto_light['footer-background-color']); ?>!important;
					}
				<?php } ?>

				<?php if( isset($maskitto_light['body-font']) ) { ?>
					body {
						font-family: "<?php print_r($maskitto_light['body-font']['font-family']); ?>"!important;
						color: <?php print_r($maskitto_light['body-font']['color']); ?>;
					}
				<?php } ?>

				<?php if( isset($maskitto_light['blog-background']) ) { ?>
					.page-blog {
						<?php if( isset($maskitto_light['blog-background']['background-color']) && $maskitto_light['blog-background']['background-color'] ) : ?>
							background-color: <?php echo esc_attr($maskitto_light['blog-background']['background-color']); ?>!important;
						<?php endif; ?>
						<?php if( isset($maskitto_light['blog-background']['background-image']) && $maskitto_light['blog-background']['background-image'] ) : ?>
							background-image: url(<?php echo esc_url($maskitto_light['blog-background']['background-image']); ?>)!important;
						<?php endif; ?>
					}
				<?php } ?>

				<?php if( isset($maskitto_light['custom-css']) && $maskitto_light['custom-css'] ) { ?>
					<?php echo esc_attr($maskitto_light['custom-css']); ?>
				<?php } ?>

			</style>
			<?php echo preg_replace( '/\s+/', ' ', ob_get_clean() ); 
		endif;
	}
endif;


/**
 * Generate custom JS
 */

if ( ! function_exists( 'maskitto_light_generate_js' ) ) :
    function maskitto_light_generate_js() {
    	global $maskitto_light; 
    	if( !isset( $maskitto_light['nacigation-dropdown'] ) || $maskitto_light['nacigation-dropdown'] == 1 ) : ?>

			<script type="text/javascript">
			jQuery( document ).ready(function( $ ) {
				$('.navbar .dropdown').hover(function() {
				  $(this).find('.dropdown-menu').first().stop(true, true).slideDown(150);
				}, function() {
				  $(this).find('.dropdown-menu').first().stop(true, true).slideUp(105)
				});

				$( '.dropdown-toggle' ).on( 'click', function() {
					window.location.href = $(this).attr( 'href' );
					return false;
				});
			});
			</script>

    <?php endif; }
endif;