<?php
/**
 * Landing page template module
 */
require_once(trailingslashit(get_template_directory()) . 'mods/variantlp-functions.php');

/**
 * Image resize function
 */
require trailingslashit(get_template_directory()) . 'inc/dynamic-image.php';

/**
 * Register Widgets
 */
require trailingslashit(get_template_directory()) . 'inc/widget-init.php';

/**
 * Variant theme setup
 */
if (!function_exists('variantlp_theme_setup')) :

    /**
     * Sets up theme defaults and registers support for various WordPress features.
     *
     * Note that this function is hooked into the after_setup_theme hook, which
     * runs before the init hook. The init hook is too late for some features, such
     * as indicating support for post thumbnails.
     */
    function variantlp_theme_setup() {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on variantlp, use a find and replace
         * to change 'variantlp' to the name of your theme in all the template files
         */
        load_theme_textdomain('variantlp', get_template_directory() . '/language');

        // Add default posts and comments RSS feed links to head.
        add_theme_support('automatic-feed-links');

        /**
         * Post thumbnail
         */
        add_theme_support('post-thumbnails');

        /**
         * Title tag support
         */
        add_theme_support('title-tag');
        /**
         * Post thumbnail image resize		 */
        add_image_size('post_thumbnail', 595, 224, true);
        // This theme uses wp_nav_menu() in one location.

        register_nav_menus(array(
            'header_menu' => __('Header Menu', 'variantlp'),
            'footer_menu' => __('Footer Menu', 'variantlp'),
        ));
        /**
         * Support exerpt on page
         */
        add_post_type_support('page', 'excerpt');

        // Enable support for HTML5 markup.
        add_theme_support('html5', array(
            'comment-list',
            'search-form',
            'comment-form',
            'gallery',
            'caption',
        ));
    }

endif;
/* landing_page_theme_setup */
add_action('after_setup_theme', 'variantlp_theme_setup');

if (!isset($content_width))
    $content_width = 1150;

/**
 * Enqueue styles
 */
function variantlp_styles() {
    if (!is_front_page() || !is_home()) {
        wp_enqueue_style('variantlp-bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.css');
        wp_enqueue_style('variantlp-raleway-font', 'http://fonts.googleapis.com/css?family=Raleway:500,600,700,100,800,900,400,200,300');
        wp_register_style('variantlp-style', get_stylesheet_uri());
        wp_enqueue_style('variantlp-style', get_stylesheet_uri());
    }
}

add_action('wp_enqueue_scripts', 'variantlp_styles');

/**
 * Default nav fallback
 */
function variantlp_nav_fallback() {
    ?>
    <ul class="variantmenu">
        <?php
        wp_list_pages('title_li=&show_home=1&sort_column=menu_order');
        ?>
    </ul>
    <?php
}

/**
 * Nav menu
 */
function variantlp_header_nav() {
    wp_nav_menu(array('theme_location' => 'header_menu', 'container_id' => 'menu', 'menu_class' => 'variantmenu', 'fallback_cb' => 'variantlp_nav_fallback'));
}

function variantlp_footer_nav() {
    wp_nav_menu(array('theme_location' => 'footer_menu', 'container_id' => 'footer_menu',));
}

/**
 * For attachment page
 */
if (!function_exists('variantlp_posted_in')) :

    /**
     * Prints HTML with meta information for the current post (category, tags and permalink).
     *
     */
    function variantlp_posted_in() {
        // Retrieves tag list of current post, separated by commas.
        $tag_list = get_the_tag_list('', ', ');
        if ($tag_list) {
            $posted_in = __('This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'variantlp');
        } elseif (is_object_in_taxonomy(get_post_type(), 'category')) {
            $posted_in = __('This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'variantlp');
        } else {
            $posted_in = __('Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'variantlp');
        }
        // Prints the string, replacing the placeholders.
        printf(
                $posted_in, get_the_category_list(', '), $tag_list, get_permalink(), the_title_attribute('echo=0')
        );
    }

endif;

/**
 * Pagination
 */
function variantlp_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;
        }
    }
    echo paginate_links(array('total' => $pages, 'prev_text' => '&laquo;',
        'next_text' => '&raquo;', 'type' => 'list'));
}

/**
 * Breadcrumbs
 * @global type $post
 * @global type $wp_query
 * @global type $author
 */
function variantlp_breadcrumbs() {
    $delimiter = '&raquo;';
    $home = 'Home'; // text for the 'Home' link
    $before = '<span class="current">'; // tag before the current crumb
    $after = '</span>'; // tag after the current crumb
    echo '<div id="crumbs">';
    global $post;
    $homeLink = home_url();
    echo '<a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
    if (is_category()) {
        global $wp_query;
        $cat_obj = $wp_query->get_queried_object();
        $thisCat = $cat_obj->term_id;
        $thisCat = get_category($thisCat);
        $parentCat = get_category($thisCat->parent);
        if ($thisCat->parent != 0)
            echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
        echo $before . 'Archive by category "' . single_cat_title('', false) . '"' . $after;
    }
    elseif (is_day()) {
        echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
        echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
        echo $before . get_the_time('d') . $after;
    } elseif (is_month()) {
        echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
        echo $before . get_the_time('F') . $after;
    } elseif (is_year()) {
        echo $before . get_the_time('Y') . $after;
    } elseif (is_single() && !is_attachment()) {
        if (get_post_type() != 'post') {
            $post_type = get_post_type_object(get_post_type());
            $slug = $post_type->rewrite;
            echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
            echo $before . get_the_title() . $after;
        } else {
            $cat = get_the_category();
            $cat = $cat[0];
            echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
            echo $before . get_the_title() . $after;
        }
    } elseif (!is_single() && !is_page() && get_post_type() != 'post') {
        $post_type = get_post_type_object(get_post_type());
        echo $before . $post_type->labels->singular_name . $after;
    } elseif (is_attachment()) {
        $parent = get_post($post->post_parent);
        $cat = get_the_category($parent->ID);
        $cat = $cat[0];
        echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
        echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
        echo $before . get_the_title() . $after;
    } elseif (is_page() && !$post->post_parent) {
        echo $before . get_the_title() . $after;
    } elseif (is_page() && $post->post_parent) {
        $parent_id = $post->post_parent;
        $breadcrumbs = array();
        while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
            $parent_id = $page->post_parent;
        }
        $breadcrumbs = array_reverse($breadcrumbs);
        foreach ($breadcrumbs as $crumb)
            echo $crumb . ' ' . $delimiter . ' ';
        echo $before . get_the_title() . $after;
    } elseif (is_search()) {
        echo $before . 'Search results for "' . get_search_query() . '"' . $after;
    } elseif (is_tag()) {
        echo $before . 'Posts tagged "' . single_tag_title('', false) . '"' . $after;
    } elseif (is_author()) {
        global $author;
        $userdata = get_userdata($author);
        echo $before . 'Articles posted by ' . $userdata->display_name . $after;
    } elseif (is_404()) {
        echo $before . 'Error 404' . $after;
    }
    if (get_query_var('paged')) {
        if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
            echo ' (';
        echo __('Page', 'swiftray') . ' ' . get_query_var('paged');
        if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
            echo ')';
    }
    echo '</div>';
}

/**
 * This function thumbnail id and
 * returns thumbnail image
 * @param type $iw
 * @param type $ih 
 */
function variantlp_get_thumbnail($iw, $ih) {
    global $post;
    $permalink = get_permalink();
    $thumb = get_post_thumbnail_id();
    $image = variantlp_thumbnail_resize($thumb, '', $iw, $ih, true, 90);
    if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
        print "<a href='$permalink'><img class='postimg' src='{$image['url']}' width='{$image['width']}' height='{$image['height']}' alt='" . get_the_title($post->ID) . "' /></a>";
    }
}

/**
 * This function gets image width and height and
 * Prints attached images from the post        
 */
function variantlp_get_image($width, $height) {
    $w = $width;
    $h = $height;
    global $post, $posts;
//This is required to set to Null
    $img_source = '';
    $permalink = get_permalink();
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    if (isset($matches [1] [0])) {
        $img_source = $matches [1] [0];
    }
    $img_path = variantlp_image_resize($img_source, $w, $h);
    if (!empty($img_path['url'])) {
        print "<a href='$permalink'><img src='{$img_path['url']}' class='postimg' alt='Post Image'/></a>";
    }
}

/**
 * Portfolio Image
 */
function variantlp_blog_image($imgwh, $imght) {
    global $post, $posts;
//This is required to set to Null
    $id = '';
    $the_title = '';
// Till Here
    $permalink = get_permalink();
    $homeLink = get_template_directory_uri();
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    if (isset($matches [1] [0])) {
        $first_img = $matches [1] [0];
    }
    if (!empty($first_img)) { //Defines a default image
        print "<img src='$first_img' class='postimg wp-post-image' alt='$the_title' />";
    }
}

/**
 * Alter default excerpt length
 */
function variantlp_excerpt_length($length) {
    return 160;
}

add_filter('excerpt_length', 'variantlp_excerpt_length');

/**
 * Enqueue scripts and styles.
 */
function variantlp_scripts() {
    if (!is_front_page() || !is_home()) {
        wp_enqueue_script('variantlp-menu', get_template_directory_uri() . '/assets/js/variantmenu.js', array('variantlp-scripts'), '1.0', true);
        wp_register_script('variantlp-scripts', get_template_directory_uri() . '/assets/js/scripts.js', array('jquery'), '1.0', true);
        wp_enqueue_script('variantlp-scripts-init', get_template_directory_uri() . '/assets/js/init.js', array('variantlp-scripts'), '1.0', true);
        if (is_singular() && get_option('thread_comments'))
            wp_enqueue_script('comment-reply');
    }
}

add_action('wp_enqueue_scripts', 'variantlp_scripts');

/**
 * Custom excerpt
 */
function variantlp_trim_excerpt($length) {
    global $post;
    $explicit_excerpt = $post->post_excerpt;
    if ('' == $explicit_excerpt) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    } else {
        $text = apply_filters('the_content', '<p>' . $explicit_excerpt . '</p>');
    }
    if ('' == $explicit_excerpt) {
        $text = strip_tags($text);
    }
    $text = strip_shortcodes($text); // optional

    $excerpt_length = $length;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
        array_pop($words);
        array_push($words, '[&hellip;]');
        $text = implode(' ', $words);
        $text = apply_filters('the_excerpt', $text);
    }
    return $text;
}
