<?php

function dw_gamez_page_menu_args( $args ) {
	$args['show_home'] = true;
	return $args;
}
add_filter( 'wp_page_menu_args', 'dw_gamez_page_menu_args' );

function dw_gamez_enhanced_image_navigation( $url, $id ) {
	if ( ! is_attachment() && ! wp_attachment_is_image( $id ) )
		return $url;

	$image = get_post( $id );
	if ( ! empty( $image->post_parent ) && $image->post_parent != $id )
		$url .= '#main';

	return $url;
}
add_filter( 'attachment_link', 'dw_gamez_enhanced_image_navigation', 10, 2 );

function dw_gamez_wp_title( $title, $sep ) {
	global $page, $paged;

	if ( is_feed() )
		return $title;

	// Add the blog name
	$title .= get_bloginfo( 'name' );

	// Add the blog description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $sep $site_description";

	// Add a page number if necessary:
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $sep " . sprintf( __( 'Page %s', 'dw-gamez' ), max( $paged, $page ) );

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

function dw_gamez_logo() {
	$header_display = (dw_gamez_get_theme_option( 'header_display', 'site_title') == 'site_title') ? 'display-title' : 'display-logo';
	$logo = dw_gamez_get_theme_option( 'logo' , get_template_directory_uri().'/assets/img/logo.png' );
	$tagline = get_bloginfo( 'description' );

	echo '<h1 class="site-title '.$header_display.'"><a href="'.esc_url( home_url( '/' ) ).'" title="'.esc_attr( get_bloginfo( 'name', 'display' ) ).'" rel="home">';
	if ($header_display == 'display-logo') {
		echo '<img alt="'.esc_attr( get_bloginfo( 'name', 'display' ) ).'" src="'.$logo.'" />';
		echo '</a></h1>';
	} else {
		echo get_bloginfo( 'name' );
		echo '</a></h1>';
		echo '<h2 class="site-description">'.$tagline.'</h2>';
	}
}

add_action('wp_head', 'dw_gamez_favicon');
function dw_gamez_favicon() {
  $favicon = dw_gamez_get_theme_option( 'favicon', get_template_directory_uri().'/assets/img/favicon.ico' );
  echo '<link rel="shortcut icon" href="'.$favicon.'">';
}

add_action('wp_head', 'dw_gamez_custom_header_code');
function dw_gamez_custom_header_code() {
  echo dw_gamez_get_theme_option( 'header_code' );
}

add_action('wp_footer', 'dw_gamez_custom_footer_code');
function dw_gamez_custom_footer_code() {
  echo dw_gamez_get_theme_option( 'footer_code' );
}

function dw_gamez_top_header() {
	$social_links['facebook'] = dw_gamez_get_theme_option( 'facebook', '' );
	$social_links['twitter'] = dw_gamez_get_theme_option( 'twitter', '' );
	$social_links['google_plus'] = dw_gamez_get_theme_option( 'google_plus', '' );
	$social_links['youtube'] = dw_gamez_get_theme_option( 'youtube', '' );
	$social_links['linkedin'] = dw_gamez_get_theme_option( 'linkedin', '' );
	?>
	<section id="top-header" class="site-top-header">
		<div class="container">
			<button data-target=".navbar-nav > ul" data-toggle="collapse" type="button" class="top-nav-toggle button-toggle visible-xs collapsed pull-left">
        <i class="icon-reorder"></i>
      </button>
      <button data-target=".top-shares" data-toggle="collapse" type="button" class="top-shares-toggle button-toggle visible-xs collapsed pull-right">
        <i class="icon-share-alt"></i>
      </button>
      <button data-target=".site-branding .search_form" data-toggle="collapse" type="button" class="search-form-toggle button-toggle visible-xs collapsed pull-right">
        <i class="icon-search"></i>
      </button>
      <button data-target=".top-nav" data-toggle="collapse" type="button" class="top-shares-toggle button-toggle visible-xs collapsed pull-right">
        <i class="icon-link"></i>
      </button>
					<?php wp_nav_menu( array( 'theme_location' => 'top-header', 'container' => '', 'menu_class' => 'top-nav nav pull-left', 'depth' => 1 ) ); ?>
					<?php if(count($social_links) > 0 ) { ?><ul class="top-shares nav pull-right list-unstyled">
							<?php if($social_links['facebook']!='') { ?><li class="social facebook"><a href="<?php echo $social_links['facebook']; ?>"><i class="icon-facebook"></i></a></li><?php } ?>
							<?php if($social_links['twitter']!='') { ?><li class="social twitter"><a href="<?php echo $social_links['twitter']; ?>"><i class="icon-twitter"></i></a></li><?php } ?>
							<?php if($social_links['google_plus']!='') { ?><li class="social google_plus"><a href="<?php echo $social_links['google_plus']; ?>"><i class="icon-google-plus"></i></a></li><?php } ?>
							<?php if($social_links['youtube']!='') { ?><li class="social youtube"><a href="<?php echo $social_links['youtube']; ?>"><i class="icon-youtube"></i></a></li><?php } ?>
							<?php if($social_links['linkedin']!='') { ?><li class="social linkedin"><a href="<?php echo $social_links['linkedin']; ?>"><i class="icon-linkedin"></i></a></li><?php } ?>
					</ul><?php } ?>
			</div>
	</section>
<?php }
add_action( 'top-header' , 'dw_gamez_top_header' );

function dw_gamez_footer_socials() {
  $social_links['facebook'] = dw_gamez_get_theme_option( 'facebook', '' );
  $social_links['twitter'] = dw_gamez_get_theme_option( 'twitter', '' );
  $social_links['google_plus'] = dw_gamez_get_theme_option( 'google_plus', '' );
  $social_links['youtube'] = dw_gamez_get_theme_option( 'youtube', '' );
  $social_links['linkedin'] = dw_gamez_get_theme_option( 'linkedin', '' );
  ?>
  <?php if(count($social_links) > 0 ) { ?><ul class="footer-shares nav list-unstyled">
    <?php if($social_links['facebook']!='') { ?><li class="social facebook"><a href="<?php echo $social_links['facebook']; ?>"><i class="icon-facebook"></i></a></li><?php } ?>
    <?php if($social_links['twitter']!='') { ?><li class="social twitter"><a href="<?php echo $social_links['twitter']; ?>"><i class="icon-twitter"></i></a></li><?php } ?>
    <?php if($social_links['google_plus']!='') { ?><li class="social google_plus"><a href="<?php echo $social_links['google_plus']; ?>"><i class="icon-google-plus"></i></a></li><?php } ?>
    <?php if($social_links['youtube']!='') { ?><li class="social youtube"><a href="<?php echo $social_links['youtube']; ?>"><i class="icon-youtube"></i></a></li><?php } ?>
    <?php if($social_links['linkedin']!='') { ?><li class="social linkedin"><a href="<?php echo $social_links['linkedin']; ?>"><i class="icon-linkedin"></i></a></li><?php } ?>
  </ul><?php } ?>
<?php }
add_action( 'dw_gamez_credits' , 'dw_gamez_footer_socials' );

function dw_gamez_top_content() { ?>
<section id="features" class="site-feature">
	<div class="wrap-feature">
			<div class="feature-1 feature widget-area">
				<?php dynamic_sidebar( 'feature-1' ); ?>
			</div>

			<div class="feature-2 feature widget-area">
				<?php dynamic_sidebar( 'feature-2' ); ?>
			</div>
	</div>

	<div class="feature-3 feature widget-area">
		<?php dynamic_sidebar( 'feature-3' ); ?>
	</div>
</section>
<?php }
add_action( 'top-content' , 'dw_gamez_top_content' );

function dw_gamez_get_layout() {
	if(isset($_COOKIE["dw_gamez_main_content_layout"]) && $_COOKIE["dw_gamez_main_content_layout"] ) {
		$layout = $_COOKIE["dw_gamez_main_content_layout"];
	} else if ( dw_gamez_get_theme_option('layout', '1') ) {
    $layout = dw_gamez_get_theme_option('layout', '1') == 1 ? 'wide' : 'narrow';
	} else {
		$layout = 'narrow';
	}
	return $layout;
}

function dw_gamez_change_layout() { ?>
<div class="main-content-actions">
	<a href="javascript:void(0);" class="btn btn-default content-layout-narrow <?php echo (dw_gamez_get_layout() == 'narrow') ? 'active' : ''; ?>" title="narrow"><span></span></a>
	<a href="javascript:void(0);" class="btn btn-default content-layout-wide <?php echo (dw_gamez_get_layout() == 'wide') ? 'active' : ''; ?>" title="wide"><span></span></a>
</div>
<?php }

add_filter( 'post_gallery', 'dw_gamez_post_gallery', 10, 2 );
function dw_gamez_post_gallery( $output, $attr) {
  global $post, $wp_locale;

  static $instance = 0;
  $instance++;

  if ( isset( $attr['orderby'] ) ) {
      $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
      if ( !$attr['orderby'] )
          unset( $attr['orderby'] );
  }

  extract(shortcode_atts(array(
      'order'      => 'ASC',
      'orderby'    => 'menu_order ID',
      'id'         => $post->ID,
      'itemtag'    => 'div',
      'icontag'    => 'div',
      'captiontag' => 'div',
      'columns'    => 3,
      'size'       => array(620,350),
      'include'    => '',
      'exclude'    => ''
  ), $attr));

  $id = intval($id);
  if ( 'RAND' == $order )
      $orderby = 'none';

  if ( !empty($include) ) {
      $include = preg_replace( '/[^0-9,]+/', '', $include );
      $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

      $attachments = array();
      foreach ( $_attachments as $key => $val ) {
          $attachments[$val->ID] = $_attachments[$key];
      }
  } elseif ( !empty($exclude) ) {
      $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
      $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  } else {
      $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  }

  if ( empty($attachments) )
      return '';

  if ( is_feed() ) {
      $output = "\n";
      foreach ( $attachments as $att_id => $attachment )
          $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
      return $output;
  }

	$itemtag = tag_escape($itemtag);
	$selector = "carousel-{$instance}";
	$captiontag = tag_escape($captiontag);

  $output = "<div class='entry-gallery'>";

	$output .= "<div id='{$selector}' class='carousel carousel-1 slide carousel-{$id}'>";

	$output .= "<ol class='carousel-indicators'>";
	$j = 0;
  foreach ( $attachments as $id => $attachment ) {
  	$itemclass = ($j==0) ? 'active' : '';
  	$output .= "<li class='{$itemclass}' data-slide-to='{$j}' data-target='#{$selector}'></li>";
  	$j++;
  }
  $output .= "</ol>";

	$i = 0;
  $output .= "<div class='carousel-inner'>";
  foreach ( $attachments as $id => $attachment ) {
  	$itemclass = ($i==0) ? 'item active' : 'item';
  	$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

  	$output .= "<{$itemtag} class='{$itemclass}'>";
  	$output .= "
      <{$icontag} class='carousel-icon'>
        $link
      </{$icontag}>";

  	if ( $captiontag && trim($attachment->post_excerpt) ) {
      $output .= "
        <{$captiontag} class='carousel-caption'>
        " . wptexturize($attachment->post_excerpt) . "
        </{$captiontag}>";
    }
  	$output .= "</{$itemtag}>";
  	$i++;
  }
  $output .= "</div>";
  $output .= "<a data-slide='prev' href='#{$selector}' class='carousel-control left'><i class='icon-chevron-left'></i></a>";
  $output .= "<a data-slide='next' href='#{$selector}' class='carousel-control right'><i class='icon-chevron-right'></i></a>";
  
  $output .= "</div>";
  $output .= "</div>";
  return $output;
}

/**
 * Color Selector
 */
function dw_gamez_typo_color() {
  if ( dw_gamez_get_theme_option('custom-color') != '' ) {
    $gamez_color = dw_gamez_get_theme_option('custom-color');
  } else {
    $gamez_color = dw_gamez_get_theme_option('select-color');
  }

  if($gamez_color != '') { ?>
        <style type="text/css" id="gamez_color" media="screen">
        .carousel-1 .carousel-control:hover,.single .hentry .tags-links a:hover,.carousel-2 .carousel-control:hover,.carousel-3 .carousel-control:hover,.carousel-4 .carousel-control:hover,.pagination .current,.post-navigation a:hover .meta-nav,.footer-shares li,.navbar .navbar-nav:before,.main-content-actions .active,.btn-primary:hover, .btn-primary:focus, .btn-primary:active, .btn-primary.active,.hentry .meta-top .comments-link a {
          background-color: <?php echo $gamez_color; ?>;
        }
        a:hover,.btn-link,.btn-link:hover,.btn-link:focus,.comments-title span,.post-navigation .nav-previous a:hover,.post-navigation .nav-next a:hover,.hentry .entry-title a:hover,.hentry .meta-top a:hover,.related-posts-title span,.related-title a:hover,.site-footer a:hover,.demo-title,#secondary a:hover,#secondary .dw_widget_query_posts.qp_style_1 .query_post_inner .entry-title:hover,#secondary .dw_widget_query_posts.qp_style_1 .query_post_inner .more-link:hover,#sidebar-footer .dw_widget_query_posts.qp_style_1 .query_post_inner .entry-title:hover,#sidebar-footer .dw_widget_query_posts.qp_style_2 .entry-title:hover,#sidebar-content-top a:hover,#sidebar-content-bottom a:hover,#sidebar-content-top .dw_widget_query_posts.qp_style_1 .query_post_inner .more-link:hover,#sidebar-content-bottom .dw_widget_query_posts.qp_style_1 .query_post_inner .more-link:hover,.text-primary {
          color: <?php echo $gamez_color; ?>;
        }
        .carousel-1 .carousel-caption a:hover,.carousel-2 .carousel-caption a:hover,.carousel-3 .carousel-caption a:hover,.carousel-4 .carousel-caption a:hover,.main-content-title span {
          color: <?php echo $gamez_color; ?> !important;
        }
        .carousel-1 .carousel-control:hover,.more-link:hover:before,.dw_widget_query_posts.qp_style_1 .query_post_inner .more-link:hover:before,#sidebar-footer .widget_search .form-control:hover,#sidebar-footer .widget_search .form-control:focus,.feature-1 .widget_search .form-control:hover,.feature-1 .widget_search .form-control:focus,.feature-2 .widget_search .form-control:hover,.feature-2 .widget_search .form-control:focus,.feature-1 .dw_widget_query_posts.qp_style_1 .query_post_inner .entry-title:hover,.feature-1 .dw_widget_query_posts.qp_style_2 .entry-title:hover,.feature-2 .dw_widget_query_posts.qp_style_1 .query_post_inner .entry-title:hover,.feature-2 .dw_widget_query_posts.qp_style_2 .entry-title:hover,.feature-3 .widget_search .form-control:hover,.feature-3 .widget_search .form-control:focus,.carousel-2 .carousel-control:hover,.carousel-2 .carousel-indicators-wrap .active img,.carousel-2 .carousel-indicators-wrap li:hover img,.carousel-3 .carousel-control:hover,.carousel-4 .carousel-control:hover,.form-control:hover,.form-control:focus,.pagination .page-numbers:hover,.pagination .current,.post-navigation a:hover .meta-nav,#secondary .tab-content,#sidebar-footer .tab-content,#sidebar-content-top .tab-content,#sidebar-content-bottom .tab-content,.feature-1 .tab-content, .feature-2 .tab-content,.feature-3 .tab-content{
          border-color: <?php echo $gamez_color; ?>;
        }
        .main-content-actions .active {
          border-color: <?php echo dw_colour_brightness($gamez_color, 0.1); ?>;
        }
        .carousel-1 .carousel-control:hover,.carousel-2 .carousel-indicators-wrap .active img,.carousel-2 .carousel-indicators-wrap li:hover img,.navbar .navbar-nav:before {
          -webkit-box-shadow: 0 0 5px <?php echo $gamez_color; ?>;
          -moz-box-shadow: 0 0 5px <?php echo $gamez_color; ?>;
          box-shadow: 0 0 5px <?php echo $gamez_color; ?>;
        }
        .carousel-2 .carousel-control:hover,.carousel-3 .carousel-control:hover,.carousel-4 .carousel-control:hover,.footer-shares li:hover {
          -webkit-box-shadow: 0 0 10px <?php echo $gamez_color; ?>;
          -moz-box-shadow: 0 0 10px <?php echo $gamez_color; ?>;
          box-shadow: 0 0 10px <?php echo $gamez_color; ?>;
        }
        .site-description,.site-footer a:hover,.site-title {
          text-shadow: 0 0 5px <?php echo $gamez_color; ?>;
        }
        .site-title:hover {
          text-shadow: 0 0 10px  <?php echo $gamez_color; ?>;
        }
        .carousel-1 .carousel-caption a:hover,.carousel-2 .carousel-caption a:hover,.carousel-3 .carousel-caption a:hover,.carousel-4 .carousel-caption a:hover {
          text-shadow: 0 0 5px <?php echo $gamez_color; ?> !important;
        }
        .btn-primary,.accordion-heading a,.nav-tabs .active a {
          border: 1px solid <?php echo $gamez_color; ?>;
          background-color: <?php echo $gamez_color; ?>;
          background-image: none;
        }
        .nav-tabs-by-select,.tab-content {
          border: 2px solid <?php echo $gamez_color; ?>;
        }
        .site-feature {
          border-bottom: 3px solid <?php echo $gamez_color; ?>;
        }
        .hentry .meta-top .comments-link a:before {
          border-top: 6px solid <?php echo $gamez_color; ?>;
        }
        .hentry .meta-top .comments-link a:hover {color: #fff;}
        @media (min-width: 768px) {
          .navbar .navbar-nav > ul > li.current-menu-parent > a,.navbar .navbar-nav > ul > li.current-menu-parent > i,.navbar .navbar-nav > ul > li.current-menu-item > a,.navbar .navbar-nav > ul > li.current-menu-item > i,.navbar .navbar-nav > ul > li:hover > a,.navbar .navbar-nav > ul > li:hover > i,.navbar .sub-menu li.current-menu-item a,.navbar .sub-menu li.current-menu-item i,.navbar .sub-menu li:hover a,.navbar .sub-menu li:hover i {
            color: <?php echo $gamez_color; ?>;
          }
          .navbar .sub-menu li.current-menu-item > a,.navbar .sub-menu li.current-menu-item > i,.navbar .sub-menu li:hover > a,.navbar .sub-menu li:hover > i {
            text-shadow: 0 0 30px <?php echo $gamez_color; ?>;
          }
        }
        </style>
        <?php
    }
}
add_filter('wp_head','dw_gamez_typo_color');

/**
 * Font Selector
 */
function dw_gamez_typo_font(){
  if (dw_gamez_get_theme_option('heading_font') && dw_gamez_get_theme_option('heading_font') != '') {
    $heading_font = explode(':dw:', dw_gamez_get_theme_option('heading_font') );
    ?>
      <style type="text/css" id="heading_font" media="screen">
        @font-face {
          font-family: "<?php echo $heading_font[0]; ?>";
          src: url('<?php echo $heading_font[1] ?>');
        } 
        h1,h2,h3,h4,h5,h6,.accordion-heading a,.site-title,.navbar .navbar-nav,.nav-tabs li,.dw_widget_query_posts .entry-title {
          font-family: "<?php echo $heading_font[0]; ?>";
        }
        .widget-title {
          font-family: "<?php echo $heading_font[0]; ?>" !important;
        }
      </style>
    <?php
  }
  if (dw_gamez_get_theme_option( 'body_font') && dw_gamez_get_theme_option( 'body_font') != '') {
    $body_font = explode( ':dw:', dw_gamez_get_theme_option( 'body_font' ));
    ?>
      <style type="text/css" id="body_font" media="screen">
        @font-face {
          font-family: "<?php echo $body_font[0]; ?>";
          src: url('<?php echo $body_font[1] ?>');
        } 
        body,input,button,select,textarea,.site-description,.comment-list .fn,.comment-list .reply a,.post-navigation .nav-previous,.post-navigation .nav-next {
          font-family: "<?php echo $body_font[0]; ?>";
        }
      </style>
    <?php
  }
}
add_filter('wp_head','dw_gamez_typo_font');