<?php
/***************************************************************************
 *  					Theme Functions
 * 	----------------------------------------------------------------------
 * 						DO NOT EDIT THIS FILE
 *	----------------------------------------------------------------------
 * 
 *  					Copyright (C) Themify
 * 						http://themify.me
 *
 *  To add custom PHP functions to the theme, create a new 'custom-functions.php' file in the theme folder.
 *  They will be added to the theme automatically.
 * 
 ***************************************************************************/

/////// Actions ////////
// Enqueue scripts and styles required by theme
add_action( 'wp_enqueue_scripts', 'themify_theme_enqueue_scripts', 11 );

// Browser compatibility
add_action( 'wp_head', 'themify_ie_enhancements' );
add_action( 'wp_head', 'themify_viewport_tag' );
add_action( 'wp_head', 'themify_ie_standards_compliant');

if( themify_get( 'setting-disable_responsive_design' ) == 'on' ) {
	add_action( 'init', 'themify_disable_responsive_design' );
}

// Register custom menu
add_action( 'init', 'themify_register_custom_nav');

// Register sidebars
add_action( 'widgets_init', 'themify_theme_register_sidebars' );

/**
 * Enqueue Stylesheets and Scripts
 * @since 1.0.0
 */
function themify_theme_enqueue_scripts(){

	// Get theme version for Themify theme scripts and styles
	$theme_version = wp_get_theme()->display('Version');

	///////////////////
	//Enqueue styles
	///////////////////
	
	// Themify base styling
	wp_enqueue_style( 'theme-style', get_stylesheet_uri(), array(), $theme_version);

	// Themify Media Queries CSS
	wp_enqueue_style( 'themify-media-queries', THEME_URI . '/media-queries.css', array(), $theme_version);
	
	// Google Web Fonts embedding
	wp_enqueue_style( 'google-fonts', themify_https_esc('http://fonts.googleapis.com/css'). '?family=Montserrat:400,700|Open+Sans:400,300,600,700|Copse&subset=latin,latin-ext');

	// Themify Icons
	wp_enqueue_style( 'themify-icons', THEME_URI . '/themify/themify-icons/themify-icons.css', array(), $theme_version);

	///////////////////
	//Enqueue scripts
	///////////////////

	// Excanvas
	wp_enqueue_script( 'themify-excanvas', THEME_URI . '/js/excanvas.js', array(), false, true );

	// Waypoints plugin
	wp_enqueue_script( 'themify-waypoints', THEME_URI . '/js/waypoints.min.js', array('jquery'), $theme_version, true );

	// Slide mobile navigation menu
	wp_enqueue_script( 'slide-nav', THEMIFY_URI . '/js/themify.sidemenu' . themify_minified() . '.js', array( 'jquery' ), $theme_version, true );

	// Isotope
	wp_enqueue_script( 'themify-isotope', THEME_URI . '/js/jquery.isotope.min.js', array(), $theme_version, true );
	
	// Smart Resize for events debouncedresize and throttledresize
	wp_enqueue_script( 'themify-smartresize', THEME_URI.'/js/jquery.smartresize.js', array('jquery'), $theme_version, true );

	// Themify internal scripts
	wp_enqueue_script( 'theme-script', THEME_URI . '/js/themify.script.js', array('jquery'), $theme_version, true );

	// Themify gallery
	wp_enqueue_script( 'themify-gallery', THEMIFY_URI . '/js/themify.gallery.js', array('jquery'), false, true );
	
	// Prepare JS variables
	$themify_script_vars = array(
		'lightbox' => themify_lightbox_vars_init(),
		'lightboxContext' => apply_filters('themify_lightbox_context', '#pagewrap'),
		'isTouch' => themify_is_touch()? 'true': 'false',
		'fixedHeader' => themify_check('setting-fixed_header_disabled')? '': 'fixed-header',
		'chart' => apply_filters('themify_chart_init_vars', array(
			'trackColor' => '#f2f2f2',
			'scaleColor' => false,
			'lineCap' => 'butt',
			'rotate' => 0,
			'size' => 170,
			'lineWidth' => 22,
			'animate' => 2000
		)),
		// Color animation
		'colorAnimation' => themify_check( 'setting-color_animation_disabled' ) ? '' : 'animated-bg',
		'colorAnimationSpeed' => themify_check( 'setting-color_animation_speed' ) ? (int) themify_get( 'setting-color_animation_speed' ) * 1000 : '5000',
	);

	// Set colors for color animation
	$color_key = 'setting-color_stop_';
	$colors = array();
	for ( $i = 1; $i <= 7; $i++ ) {
		if ( themify_check( $color_key . $i ) ) {
			$colors[] = '#' . themify_get( $color_key . $i );
		}
	}
	// If there are no colors set in theme settings, use default colors
	if ( empty( $colors ) ) {
		$colors = array(
			'#33baab',
			'#e33b9e',
			'#4961d7',
			'#2ea85c',
			'#2bb8ed',
			'#dd5135',
		);
	}
	// Assign set of colors to pass to JavaScript
	$themify_script_vars['colorAnimationSet'] = implode( ',', $colors );

	wp_enqueue_script( 'themify-animated-bg', THEMIFY_URI . '/js/animated-bg.js', array( 'jquery', 'theme-script', 'jquery-ui-core', 'jquery-effects-core' ), THEMIFY_VERSION, true );

	// Inject variable values in gallery script
	wp_localize_script( 'theme-script', 'themifyScript', apply_filters('themify_script_vars', $themify_script_vars ) );
	
	// WordPress internal script to move the comment box to the right place when replying to a user
	if ( is_single() || is_page() ) wp_enqueue_script( 'comment-reply' );
}

/**
 * Add JavaScript files if IE version is lower than 9
 * @since 1.0.0
 */
function themify_ie_enhancements(){
	echo "\n".'
	<!-- media-queries.js -->
	<!--[if lt IE 9]>
		<script src="' . THEME_URI . '/js/respond.js"></script>
	<![endif]-->
	
	<!-- html5.js -->
	<!--[if lt IE 9]>
		<script src="'.themify_https_esc('http://html5shim.googlecode.com/svn/trunk/html5.js').'"></script>
	<![endif]-->
	'."\n";
}

/**
 * Add viewport tag for responsive layouts
 * @since 1.0.0
 */
function themify_viewport_tag(){
	echo "\n".'<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">'."\n";
}

/**
 * Make IE behave like a standards-compliant browser
 * @since 1.0.0 
 */
function themify_ie_standards_compliant() {
	echo "\n".'
	<!--[if lt IE 9]>
	<script src="'.themify_https_esc('http://s3.amazonaws.com/nwapi/nwmatcher/nwmatcher-1.2.5-min.js').'"></script>
	<script type="text/javascript" src="'.themify_https_esc('http://cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js').'"></script> 
	<![endif]-->
	'."\n";
}

/* Custom Write Panels
/***************************************************************************/

///////////////////////////////////////
// Build Write Panels
///////////////////////////////////////

if ( ! function_exists( 'themify_theme_init_types' ) ) {
	/**
	 * Initialize custom panel with its definitions
	 * Custom panel definitions are located in admin/post-type-TYPE.php
	 * @since 1.0.7
	 */
	function themify_theme_init_types() {
		// Load required files for post, page and custom post types where it applies
		foreach ( array( 'post', 'page', 'team', 'portfolio', 'testimonial' ) as $type ) {
			require_once( "admin/post-type-$type.php" );
		}
		/**
		 * Navigation menus used in page custom panel to specify a custom menu for the page.
		 * @var array
		 * @since 1.2.7
		 */
		$nav_menus = array(	array( 'name' => '', 'value' => '', 'selected' => true ) );
		foreach ( get_terms( 'nav_menu' ) as $menu ) {
			$nav_menus[] = array( 'name' => $menu->name, 'value' => $menu->slug );
		}

		themify_build_write_panels( apply_filters('themify_theme_meta_boxes',
			array(
				array(
					'name'		=> __('Post Options', 'themify'),
					'id' 		=> 'post-options',
					'options'	=> themify_theme_post_meta_box(),
					'pages'		=> 'post'
				),
				array(
					'name'		=> __('Page Options', 'themify'),
					'id' 		=> 'page-options',
					'options'	=> themify_theme_page_meta_box( array( 'nav_menus' => $nav_menus ) ),
					'pages'		=> 'page'
				),
				array(
					"name"		=> __('Query Posts', 'themify'),
					'id'		=> 'query-posts',
					"options"	=> themify_theme_query_post_meta_box(),
					"pages"		=> "page"
				),
				array(
					"name"		=> __('Query Portfolios', 'themify'),
					'id' 		=> 'query-portfolio',
					"options"	=> themify_theme_query_portfolio_meta_box(),
					"pages"		=> "page"
				),
				array(
					'name'		=> __('Portfolio Options', 'themify'),
					'id' 		=> 'portfolio-options',
					'options' 	=> themify_theme_portfolio_meta_box(),
					'pages'		=> 'portfolio'
				),
				array(
					'name'		=> __('Team Options', 'themify'),
					'id' 		=> 'team-options',
					'options' 	=> themify_theme_team_meta_box(),
					'pages'		=> 'team'
				),
				array(
					'name'		=> __('Testimonial Options', 'themify'),
					'id' 		=> 'testimonial-options',
					'options' 	=> themify_theme_testimonial_meta_box(),
					'pages'		=> 'testimonial'
				),
			)
		));
	}
}
add_action( 'after_setup_theme', 'themify_theme_init_types' );

///////////////////////////////////////
// Enable WordPress feature image
///////////////////////////////////////
add_theme_support( 'post-thumbnails' );
remove_post_type_support( 'page', 'thumbnail' );
	
/**
 * Register Custom Menu Function
 * @since 1.0.0
 */
function themify_register_custom_nav() {
	register_nav_menus( array(
		'main-nav' => __( 'Main Navigation', 'themify' ),
		'footer-nav' => __( 'Footer Navigation', 'themify' )
	));
}

/**
 * Default Main Nav Function
 * @since 1.0.0
 */
function themify_default_main_nav() {
	echo '<ul id="main-nav" class="main-nav clearfix">';
		wp_list_pages('title_li=');
	echo '</ul>';
}

/**
 * Sets custom menu selected in page custom panel as navigation, otherwise sets the default.
 *
 * @since 1.0.0
 */
function themify_theme_menu_nav() {
	$custom_menu = themify_get( 'custom_menu' );
	if ( isset( $custom_menu ) && '' != $custom_menu ) {
		wp_nav_menu( array( 'menu' => $custom_menu, 'fallback_cb' => 'themify_default_main_nav' , 'container'  => '' , 'menu_id' => 'main-nav' , 'menu_class' => 'main-nav clearfix' ) );
	} else {
		wp_nav_menu( array( 'theme_location' => 'main-nav' , 'fallback_cb' => 'themify_default_main_nav' , 'container'  => '' , 'menu_id' => 'main-nav' , 'menu_class' => 'main-nav clearfix' ) );
	}
}

/**
 * Checks if the browser is a mobile device
 * @return boolean 
 */
function themify_is_mobile(){
	return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

/**
 * Register sidebars
 * @since 1.0.0
 */
function themify_theme_register_sidebars() {
	$sidebars = array(
		array(
			'name' => __('Sidebar', 'themify'),
			'id' => 'sidebar-main',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<h4 class="widgettitle">',
			'after_title' => '</h4>',
		),
		array(
			'name' => __('Social Widget', 'themify'),
			'id' => 'social-widget',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<strong class="widgettitle">',
			'after_title' => '</strong>',
		),
		array(
			'name' => __('Footer Social Widget', 'themify'),
			'id' => 'footer-social-widget',
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget' => '</div>',
			'before_title' => '<strong class="widgettitle">',
			'after_title' => '</strong>',
		),
	);
	foreach( $sidebars as $sidebar ) {
		register_sidebar( $sidebar );
	}

	// Footer Sidebars
	themify_register_grouped_widgets();
}

if ( ! function_exists( 'themify_theme_default_social_links' ) ) {
	/**
	 * Replace default squared social link icons with circular versions
	 * @param $data
	 * @return mixed
	 * @since 1.0.0
	 */
	function themify_theme_default_social_links( $data ) {
		$pre = 'setting-link_img_themify-link-';
		$data[$pre.'0'] = THEME_URI . '/images/twitter.png';
		$data[$pre.'1'] = THEME_URI . '/images/facebook.png';
		$data[$pre.'2'] = THEME_URI . '/images/google-plus.png';
		$data[$pre.'3'] = THEME_URI . '/images/youtube.png';
		$data[$pre.'4'] = THEME_URI . '/images/pinterest.png';
		return $data;
	}
	add_filter( 'themify_default_social_links', 'themify_theme_default_social_links' );
}

if ( ! function_exists( 'themify_theme_custom_post_css' ) ) {
	/**
	 * Outputs custom post CSS at the end of a post
	 * @since 1.0.0
	 */
	function themify_theme_custom_post_css() {
		global $themify;
		if ( in_array( get_post_type(), array( 'page', 'portfolio' ) ) ) {
			$post_id = get_the_ID();
			if ( is_page() ) {
				$headerwrap = '.page-id-' . $post_id . ' #headerwrap';
				$headerwrap_links = '.page-id-' . $post_id . ' ';
			} else {
				$headerwrap = '.postid-' . $post_id . ' #headerwrap';
				$headerwrap_links = '.postid-' . $post_id . ' ';
			}
			$css = array();
			$style = '';
			$rules = array();

			if ( 'transparent' != themify_get( 'header_wrap' ) ) {
				$rules = array(
					$headerwrap => array(
						array(
							'prop' => 'background-color',
							'key'  => 'background_color'
						),
						array(
							'prop' => 'background-image',
							'key'  => 'background_image'
						),
						array(
							'prop' => 'background-repeat',
							'key'  => 'background_repeat'
						),
						array(
							'prop' => 'color',
							'key'  => 'headerwrap_text_color'
						),
					),
				);
			}

			$rules[$headerwrap_links . ' #site-logo a, '
				 . $headerwrap_links . ' #headerwrap #searchform .icon-search:before, '
				 . $headerwrap_links . ' #main-nav > li > a, '
				 . $headerwrap_links . ' .social-widget a, '
				 . $headerwrap_links . ' .header-widget a, '
				 . $headerwrap_links . ' #headerwrap #menu-icon '] = array(
				array(
					'prop' => 'color',
					'key'  => 'headerwrap_link_color'
				),
			);

			if ( is_singular( 'portfolio' ) ) {
				$rules['.postid-' . $post_id . ' .featured-area'] =	array(
					array(	'prop' => 'background-color',
							'key' => 'featured_area_background_color'
					),
					array(	'prop' => 'background-image',
							'key' => 'featured_area_background_image'
					),
					array(	'prop' => 'background-repeat',
							'key' => 'featured_area_background_repeat'
					),
				);
				$rules['.postid-' . $post_id . ' .portfolio-post-wrap, .postid-' . $post_id . ' .portfolio-post-wrap .post-date'] = array(
					array(	'prop' => 'color',
							'key' => 'featured_area_text_color'
					),
				);
				$rules['.postid-' . $post_id . ' .portfolio-post-wrap a'] =	array(
					array(	'prop' => 'color',
							'key' => 'featured_area_link_color'
					),
				);
			}
			foreach ( $rules as $selector => $property ) {
				foreach ( $property as $val ) {
					$prop = $val['prop'];
					$key = $val['key'];
					if ( is_array( $key ) ) {
						if ( $prop == 'font-size' && themify_check( $key[0] ) ) {
							$css[$selector][$prop] = $prop . ': ' . themify_get( $key[0] ) . themify_get( $key[1] );
						}
					} elseif ( themify_check( $key ) && 'default' != themify_get( $key ) ) {
						if ( $prop == 'color' || stripos( $prop, 'color' ) ) {
							$css[$selector][$prop] = $prop . ': #' . themify_get( $key );
						}
						elseif ( $prop == 'background-image' && 'default' != themify_get( $key ) ) {
							$css[$selector][$prop] = $prop .': url(' . themify_get( $key ) . ')';
						}
						elseif ( $prop == 'background-repeat' && 'fullcover' == themify_get( $key ) ) {
							$css[$selector]['background-size'] = 'background-size: cover';
						}
						elseif ( $prop == 'font-family' ) {
							$font = themify_get( $key );
							$css[$selector][$prop] = $prop .': '. $font;
							if ( ! in_array( $font, themify_get_web_safe_font_list( true ) ) ) {
								$themify->google_fonts .= str_replace( ' ', '+', $font.'|' );
							}
						}
						else {
							$css[$selector][$prop] = $prop .': '. themify_get( $key );
						}
					}
				}
				if ( ! empty( $css[$selector] ) ) {
					$style .= "$selector {\n\t" . implode( ";\n\t", $css[$selector] ) . "\n}\n";
				}
			}

			if ( '' != $style ) {
				echo "\n<!-- Entry Style -->\n<style>\n$style</style>\n<!-- End Entry Style -->\n";
			}
		}
	}
	add_action( 'wp_head', 'themify_theme_custom_post_css', 77 );
}

if ( ! function_exists( 'themify_theme_enqueue_google_fonts' ) ) {
	/**
	 * Enqueue Google Fonts
	 * @since 1.0.0
	 */
	function themify_theme_enqueue_google_fonts() {
		global $themify;
		if ( ! isset( $themify->google_fonts ) || '' == $themify->google_fonts ) return;
		$themify->google_fonts = substr( $themify->google_fonts, 0, -1 );
		wp_enqueue_style( 'section-styling-google-fonts', themify_https_esc( 'http://fonts.googleapis.com/css' ). '?family='.$themify->google_fonts );
	}
	add_action( 'wp_footer', 'themify_theme_enqueue_google_fonts' );
}

if ( ! function_exists('themify_theme_comment') ) {
	/**
	 * Custom Theme Comment
	 * @param object $comment Current comment.
	 * @param array $args Parameters for comment reply link.
	 * @param int $depth Maximum comment nesting depth.
	 * @since 1.0.0
	 */
	function themify_theme_comment( $comment, $args, $depth ) {
	   $GLOBALS['comment'] = $comment; ?>

		<li id="comment-<?php comment_ID() ?>" <?php comment_class(); ?>>
			<p class="comment-author">
				<?php printf( '%s <cite>%s</cite>', get_avatar( $comment, $size='85' ), get_comment_author_link() ); ?>
				<br />
				<small class="comment-time">
					<strong>
					<?php comment_date( apply_filters('themify_comment_date', '') ); ?>
					</strong> @
					<?php comment_time( apply_filters('themify_comment_time', '') ); ?>
					<?php edit_comment_link( __('Edit', 'themify' ),' [',']' ); ?>
				</small>
			</p>
			<div class="commententry">
				<?php if ( '0' == $comment->comment_approved ) : ?>
					<p><em><?php _e( 'Your comment is awaiting moderation.', 'themify' ); ?></em></p>
				<?php endif; ?>
				<?php comment_text(); ?>
			</div>
			<p class="reply">
				<?php comment_reply_link( array_merge( $args, array( 'add_below' => 'comment', 'depth' => $depth, 'reply_text' => __( 'Reply', 'themify' ), 'max_depth' => $args['max_depth'] ) ) ) ?>
			</p>
		<?php
	}
}

if ( ! function_exists( 'themify_theme_content_width' ) ) {
	/**
	 * Checks if the single post or page has default or full width.
	 * @param array $classes
	 * @return array
	 * @since 1.0.0
	 */
	function themify_theme_content_width( $classes = array() ) {
		if ( is_search() ) {
			$classes[] = 'default_width';
		} else {
			$classes[] = themify_check( 'content_width' ) ? themify_get( 'content_width' ) : 'default_width';
		}
		$classes[] = themify_check( 'setting-fixed_header_disabled' ) ? '' : 'has-fixed-header';
		if ( ( is_single() || is_page() ) && 'transparent' == themify_get( 'header_wrap' ) ) {
			$classes[] = 'transparent-header';
		}
		return $classes;
	}
	add_filter( 'body_class', 'themify_theme_content_width' );
}

if ( ! function_exists( 'themify_theme_featured_area_style' ) ) {
	/**
	 * Returns the background repeat style as a class.
	 *
	 * @return mixed|string
	 */
	function themify_theme_featured_area_style() {
		return themify_check( 'background_repeat' ) ? themify_get( 'background_repeat' ) : '';
	}
}

if ( ! function_exists( 'themify_theme_maybe_do_animated_bg' ) ) {
	/**
	 * Checks if .animated-bg must be set and sets the first animation color as initial background
	 *
	 * @return string
	 */
	function themify_theme_maybe_do_animated_bg() {
		if ( is_singular() && ! is_singular( 'post' ) ) {
			if ( themify_check( 'background_color' ) || ( themify_check( 'header_wrap' ) && 'transparent' == themify_get( 'header_wrap' ) ) ) {
				return '';
			}
		}
		// Set first color as initial background-color to avoid a color jump
		$first_color = themify_check( 'setting-color_stop_1' ) ? themify_get( 'setting-color_stop_1' ) : '33baab';
		return themify_check( 'setting-color_animation_disabled' ) ? '' : 'class="animated-bg" style="background-color: #' . $first_color . ';"';
	}
}

if ( ! function_exists( 'themify_theme_entry_title_tag' ) ) {
	/**
	 * Displays the entry title tag, outputting h1 for singular views and h2 otherwise.
	 *
	 * @since 1.0.6
	 *
	 * @return string
	 */
	function themify_theme_entry_title_tag() {
		echo apply_filters( 'themify_entry_title_tag', is_singular() ? 'h1' : 'h2' );
	}
}

/**
 * Set the fixed-header selector for the scroll highlight script
 *
 * @since 1.1.3
 */
function themify_theme_scroll_highlight_vars( $vars ) {
	$vars['fixedHeaderSelector'] = '#headerwrap.fixed-header';
	return $vars;
}
add_filter( 'themify_builder_scroll_highlight_vars', 'themify_theme_scroll_highlight_vars' );