<?php
    // Define content width
    if ( ! isset( $content_width ) ) $content_width = 1180;

    //Google fonts
    function muziq_jellythemes_fonts_url() {
        $font_url = '';
        $added_font = '';
        $jellythemes = muziq_jellythemes_theme_options();

        if ($jellythemes['style'] == '-hardrock') {
            $added_font = '|Cinzel:400,700,900';
        } elseif (($jellythemes['style'] == '-hiphop')) {
            $added_font = '|Black+Ops+One|Russo+One';
        } elseif (($jellythemes['style'] == '-electronic')) {
            $added_font = '|Bitter:400,400italic,700';
        }

        if ( 'off' !== _x( 'on', 'Google font: on or off', 'muziq-jellythemes' ) ) {
            $font_url = add_query_arg('family', 'Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic' . $added_font , "//fonts.googleapis.com/css" );
        }
        return $font_url;
    }

	// Load scripts and styles files
    function muziq_jellythemes_scripts_and_styles() {
        $jellythemes = muziq_jellythemes_theme_options();
        if (!is_admin()) {
            wp_enqueue_style( 'muziq-jellythemes-fonts', muziq_jellythemes_fonts_url(), array(), '1.0.0' );
            wp_enqueue_script('html5shiv', get_template_directory_uri() . '/js/html5shiv.js', array(), '3.7.2');
            wp_enqueue_script('respond', get_template_directory_uri() . '/js/respond.js', array(), '1.4.2');
            wp_script_add_data( 'html5shiv', 'conditional', 'lt IE 9' );
            wp_script_add_data( 'respond', 'conditional', 'lt IE 9' );

            wp_enqueue_style('bootstrap',
                get_template_directory_uri() . '/css/vendor/bootstrap.css');
            wp_enqueue_style('font-awesome',
                get_template_directory_uri() . '/css/vendor/font-awesome.min.css');
            wp_enqueue_style('superslides',
                get_template_directory_uri() . '/css/vendor/superslides.css');
            wp_enqueue_style('owl-carousel',
                get_template_directory_uri() . '/css/vendor/owl.carousel.css');
            wp_enqueue_style('owl-transitions',
                get_template_directory_uri() . '/css/vendor/owl.transitions.css');
            wp_enqueue_style('flickity',
                get_template_directory_uri() . '/css/vendor/flickity.min.css');
            wp_enqueue_style('swipebox',
                get_template_directory_uri() . '/css/vendor/swipebox.min.css');
            wp_enqueue_style('TimeCircles',
                get_template_directory_uri() . '/css/vendor/TimeCircles.css');
            wp_enqueue_style('main',
                get_template_directory_uri() . '/css/main' . $jellythemes['style'] . '.css');

            $color = empty($jellythemes['color']) ? 'color-red.css' : $jellythemes['color'];
            wp_enqueue_style('muziq-jellytheme-color', get_template_directory_uri() . '/css/colors/' . $color );
            wp_enqueue_style( 'muziq-jellythemes-style', get_stylesheet_uri() );

            wp_enqueue_script(
                'modernizr',
                get_template_directory_uri() . '/js/vendor/modernizr.js',
                false);

            wp_enqueue_script(
                'superslides',
                get_template_directory_uri() . '/js/vendor/jquery.superslides.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'bootstrap',
                get_template_directory_uri() . '/js/vendor/bootstrap.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'imagesLoaded',
                get_template_directory_uri() . '/js/vendor/imagesloaded.pkgd.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'flickity',
                get_template_directory_uri() . '/js/vendor/flickity.pkgd.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'isotope',
                get_template_directory_uri() . '/js/vendor/isotope.pkgd.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'isotope-modulocolumns',
                get_template_directory_uri() . '/js/vendor/isotope.modulo.columns.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'owl-carousel',
                get_template_directory_uri() . '/js/vendor/owl.carousel.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'audio',
                get_template_directory_uri() . '/js/vendor/audio.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'YTPlayer',
                get_template_directory_uri() . '/js/YTPlayer.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'twitterFetcher',
                get_template_directory_uri() . '/js/vendor/twitterFetcher_min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'swipebox',
                get_template_directory_uri() . '/js/vendor/jquery.swipebox.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'TimeCircles',
                get_template_directory_uri() . '/js/vendor/TimeCircles.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'jquery-parallax',
                get_template_directory_uri() . '/js/vendor/jquery.parallax.min.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'muziq-jellythemes-discography',
                get_template_directory_uri() . '/js/discography.js',
                array('jquery'),false,true );

            wp_enqueue_script(
                'muziq-jellythemes-news',
                get_template_directory_uri() . '/js/news.js',
                array('jquery'),false,true );
            wp_enqueue_script(
                'muziq-jellythemes-main',
                get_template_directory_uri() . '/js/main.js',
                array('jquery'),false,true );

        }
        if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
            wp_enqueue_script( 'comment-reply' );
        }
    }
    add_action('wp_enqueue_scripts', 'muziq_jellythemes_scripts_and_styles');

    // Define walker nav menu to display custom html output
    class muziq_jellythemes_walker_nav_menu extends Walker_Nav_Menu {

        function start_el( &$output, $item, $depth = 0, $args = array(), $curr = 0 ) {
            global $wp_query;
            $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent

            $depth_classes = array(
                ( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
                ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
                ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
                'menu-item-depth-' . $depth
            );
            $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );

            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
            $class_names = str_replace('current_page_item', 'active', $class_names);
            if (strpos($item->url, '#')) {
                $class_names = str_replace('current-menu-item', '', $class_names);
                $class_names = str_replace('current', '', $class_names);
            }
            $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="page-scroll ' . $depth_class_names . ' ' . $class_names . '">';

            $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 .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';

            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

            global $wp;
            $attributes .= ! empty( $item->url )        ? ' data-target="'   . esc_attr( str_replace(home_url($wp->request . '/'), '', $item->url)) .'"' : '';

            $item_output = '';
            if (is_object($args)) :
            $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
                $args->before,
                $attributes,
                $args->link_before,
                apply_filters( 'the_title', $item->title, $item->ID ),
                $args->link_after,
                $args->after
            );
            endif;
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args, 0 );
        }
    }

    //Return array of section's IDs in Main menu
    function muziq_jellythemes_get_sections(){
        if(!has_nav_menu( 'main' )) {
            return;
        }
        if ( ( $locations = get_nav_menu_locations() ) && $locations['main']  ) {
            $menu = wp_get_nav_menu_object( $locations['main'] );
            $items  = wp_get_nav_menu_items($menu->term_id);
            $sections = array();
            foreach((array) $items as $menu_items){
                if ($menu_items->object == 'page-sections') {
                    $sections[] = $menu_items->object_id;
                }
            }
        }
        return $sections;
    }

    //Comment format and Walker
    function muziq_jellythemes_comments_format($comment, $args, $depth) {
            $id = $comment->comment_ID;
    ?>
            <li <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?>>

            <div <?php comment_class(empty( $args['has_children'] ) ? 'media' : 'media parent') ?> id="comment-<?php echo esc_attr($id); ?>">
                <a class="pull-left" href="#">
                <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, 76, '', false, array('class' => 'media-object')); ?>
                </a>
                <div class="media-body">
                    <h4 class="media-heading"><?php comment_author_link(); ?>
                        <small><?php printf( esc_html__('%1$s at %2$s', 'muziq-jellythemes'), get_comment_date(),  get_comment_time()); ?></small>
                    </h4>
                    <?php if ($comment->comment_approved == '0') : ?>
                    <span class="comment-awaiting-moderation"><?php echo esc_html__('Your comment is awaiting moderation.', 'muziq-jellythemes') ?></span>
                    <br/>
                    <?php endif; ?>
                    <?php comment_text(); ?>
                    <?php comment_reply_link(array_merge( $args, array('add_below' => "div-comment", 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                </div>
            </div>
    <?php
    }

    add_filter('comment_reply_link', 'muziq_jellythemes_replace_reply_link_class');
    function muziq_jellythemes_replace_reply_link_class($class){
        $class = str_replace("class='comment-reply-link", "class='reply", $class);
        return $class;
    }

    //Format title
    function muziq_jellythemes_wp_title( $title, $sep ) {
        $jellythemes = muziq_jellythemes_theme_options();

        // Add the site name.
        $title .= strip_tags($jellythemes['blogname']);

        return $title;
    }
    add_filter( 'wp_title', 'muziq_jellythemes_wp_title', 10, 2 );


    //Comments form structure
    add_filter( 'comment_form_default_fields', 'muziq_jellythemes_comment_form_fields' );
    function muziq_jellythemes_comment_form_fields( $fields ) {
        $commenter = wp_get_current_commenter();

        $req      = get_option( 'require_name_email' );
        $aria_req = ( $req ? " aria-required='true'" : '' );
        $html5    = current_theme_supports( 'html5', 'comment-form' ) ? 1 : 0;
        $fields   =  array(
            'author' => '<input placeholder="' . esc_html__( 'Name', 'muziq-jellythemes' ) . ( $req ? ' *' : '' ) . '" class="form-control" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />',
            'email'  => '<input placeholder="' . esc_html__( 'Email', 'muziq-jellythemes' ) . ( $req ? ' *' : '' ) . '" id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" class="form-control"' . $aria_req . ' />',
            'url'    => '<input class="form-control" id="url" placeholder="' . esc_html__( 'Website', 'muziq-jellythemes' ) . ( $req ? ' *' : '' ) . '" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" />'
        );


        return $fields;
    }
    add_filter( 'comment_form_defaults', 'muziq_jellythemes_comment_form' );
    function muziq_jellythemes_comment_form( $args ) {
        $args['comment_field'] = '<textarea name="comment" id="comment"class="form-control" rows="7" placeholder="' . esc_html__( 'Comment', 'muziq-jellythemes' ) . '"></textarea>';
        $args['class_submit'] = 'btn rounded'; // since WP 4.1
        $args['submit_button']= '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />';
        $args['title_reply_before'] = '<h3 id="reply-title" class="title small">';
        $args['comment_notes_before'] = '';

        $args['submit_field'] = '%1$s %2$s';

        return $args;
    }


    add_filter( 'body_class','muziq_jellythemes_body_classes' );
    function muziq_jellythemes_body_classes( $classes ) {
        global $post;
        if (isset($post->ID)) {
            $header_image = get_post_meta($post->ID, '_muziq_jellythemes_slider_images', true );
        }
        $jellythemes = muziq_jellythemes_theme_options();
        $classes[] = $jellythemes['color'];
        $classes[] = $jellythemes['color_class'];
        if ((is_page()) && empty($header_image) && !is_page_template('homepage.php') && !is_page_template('homepage-video.php') && !is_page_template('homepage-soon.php')) {
            $classes[] = 'overflowed';
        }
        if (function_exists('is_shop') && is_shop()) {
            $classes[] = 'page-shop';
        }
        return $classes;
    }

    // Widgets zones definition
    function muziq_jellythemes_widgets_init() {
        register_sidebar(array(
            'id' => 'muziq-jellythemes-sidebar',
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget' => '</div>',
            'before_title' => '<div class="title small">',
            'after_title' => '</div>',
            'name' => esc_html__('Blog sidebar', 'muziq-jellythemes')
        ));

    }
    add_action('widgets_init', 'muziq_jellythemes_widgets_init');


    function muziq_jellythemes_pagination()
    {
        if (is_singular()) {
            return;
        }
        global $wp_query;
        /** Stop execution if there's only 1 page */
        if ($wp_query->max_num_pages <= 1) {
            return;
        }
        $paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
        $max = intval($wp_query->max_num_pages);
        /** Add current page to the array */
        if ($paged >= 1) {
            $links[] = $paged;
        }
        /** Add the pages around the current page to the array */
        if ($paged >= 3) {
            $links[] = $paged - 1;
            $links[] = $paged - 2;
        }
        if (($paged + 2) <= $max) {
            $links[] = $paged + 2;
            $links[] = $paged + 1;
        }
        echo '<div class="pagination_wrapper"><ul class="pagination">' . "\n";
        /** Previous Post Link */
        if (get_previous_posts_link()) {
            printf('<li>%s</li>' . "\n", get_previous_posts_link("<<"));
        }
        /** Link to first page, plus ellipses if necessary */
        if (!in_array(1, $links)) {
            $class = 1 == $paged ? ' class="first active"' : ' class="first"';
            printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link(1)), '1');
            if (!in_array(2, $links)) {
                echo '<li><span class="btn disabled">…</span></li>' . "\n"; // Here is the correction
            }
        }
        /** Link to current page, plus 2 pages in either direction if necessary */
        sort($links);
        foreach ((array)$links as $link) {
            $class = $paged == $link ? ' class="last active"' : ' class="last"';
            printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($link)), $link);
        }
        /** Link to last page, plus ellipses if necessary */
        if (!in_array($max, $links)) {
            if (!in_array($max - 1, $links)) {
                echo '<li><span class="btn disabled">…</span></li>' . "\n";
            }
            $class = $paged == $max ? ' class="active"' : '';
            printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($max)), $max);
        }
        /** Next Post Link */
        if (get_next_posts_link()) {
            printf('<li>%s</li>' . "\n", get_next_posts_link(">>"));
        }
        echo '</ul></div>' . "\n";
    }
?>
