<?php
/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

global $post;

// Preset variables
$name = __( 'Home', IT_THEME_TEXTDOMAIN );
$blog = __( 'Blog', IT_THEME_TEXTDOMAIN );

// Filter HTML code for breadcrumbs structure
$delimiter = apply_filters( 'it_theme_breadcrumbs_delimiter', '' );
$delimiter = ' ' . trim( $delimiter ) . ' ';

$home         = apply_filters( 'it_theme_breadcrumbs_home'        , '<a class="home" href="%1$s">%2$s</a>' );
$home_current = apply_filters( 'it_theme_breadcrumbs_home_current', '<span class="home">%s</span>'         );

$current_before = apply_filters( 'it_theme_breadcrumbs_current_before', '<span class="current">' );
$current_after  = apply_filters( 'it_theme_breadcrumbs_current_after' , '</span>'                );
$link    = apply_filters( 'it_theme_breadcrumbs_link'   , '<a href="%1$s">%2$s</a>'         );

if ( is_home( ) && 0 == get_query_var( 'paged' ) ) {
	printf( $home_current, $blog );
} else {
	printf( $home, home_url(), $name ) . $delimiter;
}

if ( is_category() ) {
	global $wp_query;

	// Get parent categories
	$cat_obj    = $wp_query->get_queried_object();
	$this_cat   = get_category( $cat_obj->term_id );
	$parent_cat = get_category( $this_cat->parent );

	if ( 0 != $this_cat->parent ) {
		echo '' . get_category_parents( $parent_cat, true, $delimiter );
	}

	echo '' . $current_before;
		single_cat_title();
	echo '' . $current_after;
} elseif ( is_single() ) {
	// Get parent categories
	$cat = get_the_category();

	if ( count( $cat ) ) {
		$cat = $cat[0];

		echo '' . get_category_parents( $cat, true, $delimiter );
	}

	echo '' . $current_before;
		the_title();
	echo '' . $current_after;
} elseif ( is_page() && ! $post->post_parent ) {
	echo '' . $current_before;
		the_title();
	echo '' . $current_after;
} elseif ( is_page() && $post->post_parent ) {
	// Generate breadscrums
	$parent_id   = $post->post_parent;
	$breadcrumbs = array();

	while ( $parent_id ) {
		$page = get_page( $parent_id );

		$breadcrumbs[] = sprintf( $link, get_permalink( $page->ID ), get_the_title( $page->ID ) );

		$parent_id = $page->post_parent;
	}

	$breadcrumbs = array_reverse( $breadcrumbs );

	// Finalize breadscrums
	foreach ( $breadcrumbs as $crumb ) {
		echo '' . "{$crumb} {$delimiter} ";
	}

	echo '' . $current_before;
		the_title();
	echo '' . $current_after;
} elseif ( is_search() ) {
	echo '' . $current_before . 'Search for ' . get_search_query() . $current_after;

} elseif ( is_tag() ) {
	echo '' . $current_before;
		single_tag_title();
	echo '' . $current_after;
} elseif ( is_author() ) {
	global $author;

	// Get author data
	$userdata = get_userdata( $author );

	echo '' . $current_before;
		$userdata->display_name;
	echo '' . $current_after;
}

if ( get_query_var( 'paged' ) ) {
	printf( _e( 'Page %s', IT_THEME_TEXTDOMAIN ), get_query_var( 'paged' ) );
}
