<?php
/**
 * SeoHub Framework: Theme specific actions
 *
 * @package	seohub
 * @since	seohub 1.0
 */

// Disable direct call
if ( ! defined( 'ABSPATH' ) ) { exit; }


/* Theme setup section
-------------------------------------------------------------------- */

if ( !function_exists( 'seohub_core_theme_setup' ) ) {
	add_action( 'seohub_action_before_init_theme', 'seohub_core_theme_setup', 11 );
	function seohub_core_theme_setup() {

		// Add default posts and comments RSS feed links to head 
		add_theme_support( 'automatic-feed-links' );
		
		// Enable support for Post Thumbnails
		add_theme_support( 'post-thumbnails' );
		
		// Custom header setup
		add_theme_support( 'custom-header', array('header-text'=>false));
		
		// Custom backgrounds setup
		add_theme_support( 'custom-background');
		
		// Supported posts formats
		add_theme_support( 'post-formats', array('gallery', 'video', 'audio', 'link', 'quote', 'image', 'status', 'aside', 'chat') ); 
 
 		// Autogenerate title tag
		add_theme_support('title-tag');
 		
		// Add user menu
		add_theme_support('nav-menus');
		
		// WooCommerce Support
		add_theme_support( 'woocommerce' );
		
		// Editor custom stylesheet - for user
		add_editor_style(seohub_get_file_url('css/editor-style.css'));	
		
		// Make theme available for translation
		// Translations can be filed in the /languages/ directory
		load_theme_textdomain( 'seohub', seohub_get_folder_dir('languages') );


		/* Front and Admin actions and filters:
		------------------------------------------------------------------------ */

		if ( !is_admin() ) {
			
			/* Front actions and filters:
			------------------------------------------------------------------------ */
	
			// Filters wp_title to print a neat <title> tag based on what is being viewed
			if (floatval(get_bloginfo('version')) < "4.1") {
				add_filter('wp_title',						'seohub_wp_title', 10, 2);
			}

			// Add main menu classes
			//add_filter('wp_nav_menu_objects', 			'seohub_add_mainmenu_classes', 10, 2);
	
			// Prepare logo text
			add_filter('seohub_filter_prepare_logo_text',	'seohub_prepare_logo_text', 10, 1);
	
			// Add class "widget_number_#' for each widget
			add_filter('dynamic_sidebar_params', 			'seohub_add_widget_number', 10, 1);

			// Frontend editor: Save post data
			add_action('wp_ajax_frontend_editor_save',		'seohub_callback_frontend_editor_save');
			add_action('wp_ajax_nopriv_frontend_editor_save', 'seohub_callback_frontend_editor_save');

			// Frontend editor: Delete post
			add_action('wp_ajax_frontend_editor_delete', 	'seohub_callback_frontend_editor_delete');
			add_action('wp_ajax_nopriv_frontend_editor_delete', 'seohub_callback_frontend_editor_delete');
	
			// Enqueue scripts and styles
			add_action('wp_enqueue_scripts', 				'seohub_core_frontend_scripts');
			add_action('wp_footer',		 					'seohub_core_frontend_scripts_inline');
			add_action('seohub_action_add_scripts_inline','seohub_core_add_scripts_inline');

			// Prepare theme core global variables
			add_action('seohub_action_prepare_globals',	'seohub_core_prepare_globals');

		}

		// Register theme specific nav menus
		seohub_register_theme_menus();

		// Register theme specific sidebars
		seohub_register_theme_sidebars();
	}
}




/* Theme init
------------------------------------------------------------------------ */

// Init theme template
function seohub_core_init_theme() {
	global $SEOHUB_GLOBALS;
	if (!empty($SEOHUB_GLOBALS['theme_inited'])) return;
	$SEOHUB_GLOBALS['theme_inited'] = true;

	// Load custom options from GET and post/page/cat options
	if (isset($_GET['set']) && $_GET['set']==1) {
		foreach ($_GET as $k=>$v) {
			if (seohub_get_theme_option($k, null) !== null) {
				setcookie($k, $v, 0, '/');
				$_COOKIE[$k] = $v;
			}
		}
	}

	// Get custom options from current category / page / post / shop / event
	seohub_load_custom_options();

	// Load skin
	$skin = seohub_esc(seohub_get_custom_option('theme_skin'));
	$SEOHUB_GLOBALS['theme_skin'] = $skin;
	if ( file_exists(seohub_get_file_dir('skins/'.($skin).'/skin.php')) ) {
		require_once seohub_get_file_dir('skins/'.($skin).'/skin.php');
	}

	// Fire init theme actions (after skin and custom options are loaded)
	do_action('seohub_action_init_theme');

	// Prepare theme core global variables
	do_action('seohub_action_prepare_globals');

	// Fire after init theme actions
	do_action('seohub_action_after_init_theme');
}


// Prepare theme global variables
if ( !function_exists( 'seohub_core_prepare_globals' ) ) {
	function seohub_core_prepare_globals() {
		if (!is_admin()) {
			// AJAX Queries settings
			global $SEOHUB_GLOBALS;
		
			// Logo text and slogan
			$SEOHUB_GLOBALS['logo_text'] = apply_filters('seohub_filter_prepare_logo_text', seohub_get_custom_option('logo_text'));
			$slogan = seohub_get_custom_option('logo_slogan');
			if (!$slogan) $slogan = get_bloginfo ( 'description' );
			$SEOHUB_GLOBALS['logo_slogan'] = $slogan;
			
			// Logo image and icons from skin
			$logo_side   = seohub_get_logo_icon('logo_side');
			$logo_fixed  = seohub_get_logo_icon('logo_fixed');
			$logo_footer = seohub_get_logo_icon('logo_footer');
			$SEOHUB_GLOBALS['logo']        = seohub_get_logo_icon('logo');
			$SEOHUB_GLOBALS['logo_icon']   = seohub_get_logo_icon('logo_icon');
			$SEOHUB_GLOBALS['logo_side']   = $logo_side   ? $logo_side   : $SEOHUB_GLOBALS['logo'];
			$SEOHUB_GLOBALS['logo_fixed']  = $logo_fixed  ? $logo_fixed  : $SEOHUB_GLOBALS['logo'];
			$SEOHUB_GLOBALS['logo_footer'] = $logo_footer ? $logo_footer : $SEOHUB_GLOBALS['logo'];
	
			$shop_mode = '';
			if (seohub_get_custom_option('show_mode_buttons')=='yes')
				$shop_mode = seohub_get_value_gpc('seohub_shop_mode');
			if (empty($shop_mode))
				$shop_mode = seohub_get_custom_option('shop_mode', '');
			if (empty($shop_mode) || !is_archive())
				$shop_mode = 'thumbs';
			$SEOHUB_GLOBALS['shop_mode'] = $shop_mode;
		}
	}
}


// Return url for the uploaded logo image or (if not uploaded) - to image from skin folder
if ( !function_exists( 'seohub_get_logo_icon' ) ) {
	function seohub_get_logo_icon($slug) {
		$logo_icon = seohub_get_custom_option($slug);
		return $logo_icon;
	}
}


// Display logo image with text and slogan (if specified)
if ( !function_exists( 'seohub_show_logo' ) ) {
	function seohub_show_logo($logo_main=true, $logo_fixed=false, $logo_footer=false, $logo_side=false, $logo_text=true, $logo_slogan=true) {
		global $SEOHUB_GLOBALS;
		if ($logo_main===true)		$logo_main = $SEOHUB_GLOBALS['logo'];
		if ($logo_fixed===true)		$logo_fixed = $SEOHUB_GLOBALS['logo_fixed'];
		if ($logo_footer===true)	$logo_footer = $SEOHUB_GLOBALS['logo_footer'];
		if ($logo_side===true)		$logo_side = $SEOHUB_GLOBALS['logo_side'];
		if ($logo_text===true)		$logo_text = $SEOHUB_GLOBALS['logo_text'];
		if ($logo_slogan===true)	$logo_slogan = $SEOHUB_GLOBALS['logo_slogan'];
		if ($logo_main || $logo_fixed || $logo_footer || $logo_side || $logo_text) {
		?>
		<div class="logo">
			<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php
				if (!empty($logo_main)) {
					$attr = getimagesize($logo_main);
					echo '<img src="'.esc_url($logo_main).'" class="logo_main" alt=""'.(!empty($attr[3]) ? ' '.trim($attr[3]) : '').'>';
				}
				if (!empty($logo_fixed)) {
					$attr = getimagesize($logo_fixed);
					echo '<img src="'.esc_url($logo_fixed).'" class="logo_fixed" alt=""'.(!empty($attr[3]) ? ' '.trim($attr[3]) : '').'>';
				}
				if (!empty($logo_footer)) {
					$attr = getimagesize($logo_footer);
					echo '<img src="'.esc_url($logo_footer).'" class="logo_footer" alt=""'.(!empty($attr[3]) ? ' '.trim($attr[3]) : '').'>';
				}
				if (!empty($logo_side)) {
					$attr = getimagesize($logo_side);
					echo '<img src="'.esc_url($logo_side).'" class="logo_side" alt=""'.(!empty($attr[3]) ? ' '.trim($attr[3]) : '').'>';
				}
				echo !empty($logo_text) ? '<div class="logo_text">'.trim($SEOHUB_GLOBALS['logo_text']).'</div>' : '';
				echo !empty($logo_slogan) ? '<br><div class="logo_slogan">' . esc_html($SEOHUB_GLOBALS['logo_slogan']) . '</div>' : '';
			?></a>
		</div>
		<?php 
		}
	} 
}


// Add menu locations
if ( !function_exists( 'seohub_register_theme_menus' ) ) {
	function seohub_register_theme_menus() {
		register_nav_menus(apply_filters('seohub_filter_add_theme_menus', array(
			'menu_main'		=> esc_html__('Main Menu', 'seohub'),
			'menu_user'		=> esc_html__('User Menu', 'seohub'),
			'menu_footer'	=> esc_html__('Footer Menu', 'seohub'),
			'menu_side'		=> esc_html__('Side Menu', 'seohub')
		)));
	}
}


// Register widgetized area
if ( !function_exists( 'seohub_register_theme_sidebars' ) ) {
	function seohub_register_theme_sidebars($sidebars=array()) {
		global $SEOHUB_GLOBALS;
		if (!is_array($sidebars)) $sidebars = array();
		// Custom sidebars
		$custom = seohub_get_theme_option('custom_sidebars');
		if (is_array($custom) && count($custom) > 0) {
			foreach ($custom as $i => $sb) {
				if (trim(chop($sb))=='') continue;
				$sidebars['sidebar_custom_'.($i)]  = $sb;
			}
		}
		$sidebars = apply_filters( 'seohub_filter_add_theme_sidebars', $sidebars );
		$SEOHUB_GLOBALS['registered_sidebars'] = $sidebars;
		if (is_array($sidebars) && count($sidebars) > 0) {
			foreach ($sidebars as $id=>$name) {
				register_sidebar( array(
					'name'          => $name,
					'id'            => $id,
					'before_widget' => '<aside id="%1$s" class="widget %2$s">',
					'after_widget'  => '</aside>',
					'before_title'  => '<h5 class="widget_title">',
					'after_title'   => '</h5>',
				) );
			}
		}
	}
}





/* Front actions and filters:
------------------------------------------------------------------------ */

//  Enqueue scripts and styles
if ( !function_exists( 'seohub_core_frontend_scripts' ) ) {
	function seohub_core_frontend_scripts() {
		global $SEOHUB_GLOBALS;
		
		// Modernizr will load in head before other scripts and styles
		// Use older version (from photostack)
		seohub_enqueue_script( 'seohub-core-modernizr-script', seohub_get_file_url('js/photostack/modernizr.min.js'), array(), null, false );
		
		// Enqueue styles
		//-----------------------------------------------------------------------------------------------------
		
		// Prepare custom fonts
		$fonts = seohub_get_list_fonts(false);
		$theme_fonts = array();
		$custom_fonts = seohub_get_custom_fonts();
		if (is_array($custom_fonts) && count($custom_fonts) > 0) {
			foreach ($custom_fonts as $s=>$f) {
				if (!empty($f['font-family']) && !seohub_is_inherit_option($f['font-family'])) $theme_fonts[$f['font-family']] = 1;
			}
		}
		// Prepare current skin fonts
		$theme_fonts = apply_filters('seohub_filter_used_fonts', $theme_fonts);
		// Link to selected fonts
		if (is_array($theme_fonts) && count($theme_fonts) > 0) {
			$google_fonts = '';
			foreach ($theme_fonts as $font=>$v) {
				if (isset($fonts[$font])) {
					$font_name = ($pos=seohub_strpos($font,' ('))!==false ? seohub_substr($font, 0, $pos) : $font;
					if (!empty($fonts[$font]['css'])) {
						$css = $fonts[$font]['css'];
						seohub_enqueue_style( 'seohub-font-'.str_replace(' ', '-', $font_name).'-style', $css, array(), null );
					} else {
						$google_fonts .= ($google_fonts ? '|' : '') 
							. (!empty($fonts[$font]['link']) ? $fonts[$font]['link'] : str_replace(' ', '+', $font_name).':300,300italic,400,400italic,700,700italic');
					}
				}
			}
			if ($google_fonts)
				seohub_enqueue_style( 'seohub-font-google_fonts-style', seohub_get_protocol() . '://fonts.googleapis.com/css?family=' . $google_fonts . '&subset=' . seohub_get_theme_option('fonts_subset'), array(), null );
		}
		
		// Fontello styles must be loaded before main stylesheet
		seohub_enqueue_style( 'seohub-fontello-style',  seohub_get_file_url('css/fontello/css/fontello.css'),  array(), null);
		//seohub_enqueue_style( 'seohub-fontello-animation-style', seohub_get_file_url('css/fontello/css/animation.css'), array(), null);

		// Main stylesheet
		seohub_enqueue_style( 'seohub-main-style', get_stylesheet_uri(), array(), null );
		
		// Animations
		if (seohub_get_theme_option('css_animation')=='yes')
			seohub_enqueue_style( 'seohub-animation-style',	seohub_get_file_url('css/core.animation.css'), array(), null );

		// Theme skin stylesheet
		do_action('seohub_action_add_styles');
		
		// Theme customizer stylesheet and inline styles
		seohub_enqueue_custom_styles();

		// Responsive
		if (seohub_get_theme_option('responsive_layouts') == 'yes') {
			$suffix = seohub_param_is_off(seohub_get_custom_option('show_sidebar_outer')) ? '' : '-outer';
			seohub_enqueue_style( 'seohub-responsive-style', seohub_get_file_url('css/responsive'.($suffix).'.css'), array(), null );
			do_action('seohub_action_add_responsive');
			if (seohub_get_custom_option('theme_skin')!='') {
				$css = apply_filters('seohub_filter_add_responsive_inline', '');
				if (!empty($css)) wp_add_inline_style( 'seohub-responsive-style', $css );
			}
		}

		// Disable loading JQuery UI CSS
		//global $wp_styles, $wp_scripts;
		//$wp_styles->done[]	= 'jquery-ui';
		//$wp_styles->done[]	= 'date-picker-css';
		wp_deregister_style('jquery_ui');
		wp_deregister_style('date-picker-css');


		// Enqueue scripts	
		//----------------------------------------------------------------------------------------------------------------------------
		
		// Load separate theme scripts
		seohub_enqueue_script( 'superfish', seohub_get_file_url('js/superfish.min.js'), array('jquery'), null, true );
		if (seohub_get_theme_option('menu_slider')=='yes') {
			seohub_enqueue_script( 'seohub-slidemenu-script', seohub_get_file_url('js/jquery.slidemenu.js'), array('jquery'), null, true );
			//seohub_enqueue_script( 'seohub-jquery-easing-script', seohub_get_file_url('js/jquery.easing.js'), array('jquery'), null, true );
		}

		if ( is_single() && seohub_get_custom_option('show_reviews')=='yes' ) {
			seohub_enqueue_script( 'seohub-core-reviews-script', seohub_get_file_url('js/core.reviews.js'), array('jquery'), null, true );
		}

		seohub_enqueue_script( 'seohub-core-utils-script',	seohub_get_file_url('js/core.utils.js'), array('jquery'), null, true );
		seohub_enqueue_script( 'seohub-core-init-script',	seohub_get_file_url('js/core.init.js'), array('jquery'), null, true );	
		seohub_enqueue_script( 'seohub-theme-init-script',	seohub_get_file_url('js/theme.init.js'), array('jquery'), null, true );	

		// Media elements library	
		if (seohub_get_theme_option('use_mediaelement')=='yes') {
			wp_enqueue_style ( 'mediaelement' );
			wp_enqueue_style ( 'wp-mediaelement' );
			wp_enqueue_script( 'mediaelement' );
			wp_enqueue_script( 'wp-mediaelement' );
		} else {
//			$wp_styles->done[]	= 'mediaelement';
//			$wp_styles->done[]	= 'wp-mediaelement';
//			$wp_scripts->done[]	= 'mediaelement';
//			$wp_scripts->done[]	= 'wp-mediaelement';
			wp_deregister_style('mediaelement');
			wp_deregister_style('wp-mediaelement');
			wp_deregister_script('mediaelement');
			wp_deregister_script('wp-mediaelement');
		}
		
		// Video background
		if (seohub_get_custom_option('show_video_bg') == 'yes' && seohub_get_custom_option('video_bg_youtube_code') != '') {
			seohub_enqueue_script( 'seohub-video-bg-script', seohub_get_file_url('js/jquery.tubular.1.0.js'), array('jquery'), null, true );
		}

		// Google map
		if ( seohub_get_custom_option('show_googlemap')=='yes' ) { 
			seohub_enqueue_script( 'googlemap', seohub_get_protocol().'://maps.google.com/maps/api/js?sensor=false', array(), null, true );
			seohub_enqueue_script( 'seohub-googlemap-script', seohub_get_file_url('js/core.googlemap.js'), array(), null, true );
		}

			
		// Social share buttons
		if (is_singular() && !seohub_get_global('blog_streampage') && seohub_get_custom_option('show_share')!='hide') {
			seohub_enqueue_script( 'seohub-social-share-script', seohub_get_file_url('js/social/social-share.js'), array('jquery'), null, true );
		}

		// Comments
		if ( is_singular() && !seohub_get_global('blog_streampage') && comments_open() && get_option( 'thread_comments' ) ) {
			seohub_enqueue_script( 'comment-reply', false, array(), null, true );
		}

		// Custom panel
		if (seohub_get_theme_option('show_theme_customizer') == 'yes') {
			if (file_exists(seohub_get_file_dir('core/core.customizer/front.customizer.css')))
				seohub_enqueue_style(  'seohub-customizer-style',  seohub_get_file_url('core/core.customizer/front.customizer.css'), array(), null );
			if (file_exists(seohub_get_file_dir('core/core.customizer/front.customizer.js')))
				seohub_enqueue_script( 'seohub-customizer-script', seohub_get_file_url('core/core.customizer/front.customizer.js'), array(), null, true );	
		}
		
		//Debug utils
		if (seohub_get_theme_option('debug_mode')=='yes') {
			seohub_enqueue_script( 'seohub-core-debug-script', seohub_get_file_url('js/core.debug.js'), array(), null, true );
		}

		// Theme skin script
		do_action('seohub_action_add_scripts');
	}
}

//  Enqueue Swiper Slider scripts and styles
if ( !function_exists( 'seohub_enqueue_slider' ) ) {
	function seohub_enqueue_slider($engine='all') {
		if ($engine=='all' || $engine=='swiper') {
			seohub_enqueue_style(  'seohub-swiperslider-style', 			seohub_get_file_url('js/swiper/swiper.css'), array(), null );
			//seohub_enqueue_script( 'seohub-swiperslider-script', 			seohub_get_file_url('js/swiper/swiper.js'), array(), null, true );
			seohub_enqueue_script( 'seohub-swiperslider-script', 			seohub_get_file_url('js/swiper/swiper.jquery.js'), array(), null, true );
		}
	}
}

//  Enqueue Photostack gallery
if ( !function_exists( 'seohub_enqueue_polaroid' ) ) {
	function seohub_enqueue_polaroid() {
		seohub_enqueue_style(  'seohub-polaroid-style', 	seohub_get_file_url('js/photostack/component.css'), array(), null );
		seohub_enqueue_script( 'seohub-classie-script',		seohub_get_file_url('js/photostack/classie.js'), array(), null, true );
		seohub_enqueue_script( 'seohub-polaroid-script',	seohub_get_file_url('js/photostack/photostack.js'), array(), null, true );
	}
}

//  Enqueue Messages scripts and styles
if ( !function_exists( 'seohub_enqueue_messages' ) ) {
	function seohub_enqueue_messages() {
		seohub_enqueue_style(  'seohub-messages-style',		seohub_get_file_url('js/core.messages/core.messages.css'), array(), null );
		seohub_enqueue_script( 'seohub-messages-script',	seohub_get_file_url('js/core.messages/core.messages.js'),  array('jquery'), null, true );
	}
}

//  Enqueue Portfolio hover scripts and styles
if ( !function_exists( 'seohub_enqueue_portfolio' ) ) {
	function seohub_enqueue_portfolio($hover='') {
		seohub_enqueue_style( 'seohub-portfolio-style',  seohub_get_file_url('css/core.portfolio.css'), array(), null );
		if (seohub_strpos($hover, 'effect_dir')!==false)
			seohub_enqueue_script( 'hoverdir', seohub_get_file_url('js/hover/jquery.hoverdir.js'), array(), null, true );
	}
}

//  Enqueue Charts and Diagrams scripts and styles
if ( !function_exists( 'seohub_enqueue_diagram' ) ) {
	function seohub_enqueue_diagram($type='all') {
		if ($type=='all' || $type=='pie') seohub_enqueue_script( 'seohub-diagram-chart-script',	seohub_get_file_url('js/diagram/chart.min.js'), array(), null, true );
		if ($type=='all' || $type=='arc') seohub_enqueue_script( 'seohub-diagram-raphael-script',	seohub_get_file_url('js/diagram/diagram.raphael.min.js'), array(), 'no-compose', true );
	}
}

// Enqueue Theme Popup scripts and styles
// Link must have attribute: data-rel="popup" or data-rel="popup[gallery]"
if ( !function_exists( 'seohub_enqueue_popup' ) ) {
	function seohub_enqueue_popup($engine='') {
		if ($engine=='pretty' || (empty($engine) && seohub_get_theme_option('popup_engine')=='pretty')) {
			seohub_enqueue_style(  'seohub-prettyphoto-style',	seohub_get_file_url('js/prettyphoto/css/prettyPhoto.css'), array(), null );
			seohub_enqueue_script( 'seohub-prettyphoto-script',	seohub_get_file_url('js/prettyphoto/jquery.prettyPhoto.min.js'), array('jquery'), 'no-compose', true );
		} else if ($engine=='magnific' || (empty($engine) && seohub_get_theme_option('popup_engine')=='magnific')) {
			seohub_enqueue_style(  'seohub-magnific-style',	seohub_get_file_url('js/magnific/magnific-popup.css'), array(), null );
			seohub_enqueue_script( 'seohub-magnific-script',seohub_get_file_url('js/magnific/jquery.magnific-popup.min.js'), array('jquery'), '', true );
		} else if ($engine=='internal' || (empty($engine) && seohub_get_theme_option('popup_engine')=='internal')) {
			seohub_enqueue_messages();
		}
	}
}

//  Add inline scripts in the footer hook
if ( !function_exists( 'seohub_core_frontend_scripts_inline' ) ) {
	function seohub_core_frontend_scripts_inline() {
		do_action('seohub_action_add_scripts_inline');
	}
}

//  Add inline scripts in the footer
if (!function_exists('seohub_core_add_scripts_inline')) {
	function seohub_core_add_scripts_inline() {
		global $SEOHUB_GLOBALS;
		
		$msg = seohub_get_system_message(true); 
		if (!empty($msg['message'])) seohub_enqueue_messages();

		echo "<script type=\"text/javascript\">"
			
			. "if (typeof SEOHUB_GLOBALS == 'undefined') var SEOHUB_GLOBALS = {};"			
			
			// AJAX parameters
			. "SEOHUB_GLOBALS['ajax_url']			 = '" . esc_url($SEOHUB_GLOBALS['ajax_url']) . "';"
			. "SEOHUB_GLOBALS['ajax_nonce']		 = '" . esc_attr($SEOHUB_GLOBALS['ajax_nonce']) . "';"
			. "SEOHUB_GLOBALS['ajax_nonce_editor'] = '" . esc_attr(wp_create_nonce('seohub_editor_nonce')) . "';"
			
			// Site base url
			. "SEOHUB_GLOBALS['site_url']			= '" . get_site_url() . "';"
			
			// VC frontend edit mode
			. "SEOHUB_GLOBALS['vc_edit_mode']		= " . (function_exists('seohub_vc_is_frontend') && seohub_vc_is_frontend() ? 'true' : 'false') . ";"
			
			// Theme base font
			. "SEOHUB_GLOBALS['theme_font']		= '" . seohub_get_custom_font_settings('p', 'font-family') . "';"
			
			// Theme skin
			. "SEOHUB_GLOBALS['theme_skin']			= '" . esc_attr(seohub_get_custom_option('theme_skin')) . "';"
			. "SEOHUB_GLOBALS['theme_skin_color']		= '" . seohub_get_scheme_color('text_dark') . "';"
			. "SEOHUB_GLOBALS['theme_skin_bg_color']	= '" . seohub_get_scheme_color('bg_color') . "';"
			
			// Slider height
			. "SEOHUB_GLOBALS['slider_height']	= " . max(100, seohub_get_custom_option('slider_height')) . ";"
			
			// System message
			. "SEOHUB_GLOBALS['system_message']	= {"
				. "message: '" . addslashes($msg['message']) . "',"
				. "status: '"  . addslashes($msg['status'])  . "',"
				. "header: '"  . addslashes($msg['header'])  . "'"
				. "};"
			
			// User logged in
			. "SEOHUB_GLOBALS['user_logged_in']	= " . (is_user_logged_in() ? 'true' : 'false') . ";"
			
			// Show table of content for the current page
			. "SEOHUB_GLOBALS['toc_menu']		= '" . esc_attr(seohub_get_custom_option('menu_toc')) . "';"
			. "SEOHUB_GLOBALS['toc_menu_home']	= " . (seohub_get_custom_option('menu_toc')!='hide' && seohub_get_custom_option('menu_toc_home')=='yes' ? 'true' : 'false') . ";"
			. "SEOHUB_GLOBALS['toc_menu_top']	= " . (seohub_get_custom_option('menu_toc')!='hide' && seohub_get_custom_option('menu_toc_top')=='yes' ? 'true' : 'false') . ";"
			
			// Fix main menu
			. "SEOHUB_GLOBALS['menu_fixed']		= " . (seohub_get_theme_option('menu_attachment')=='fixed' ? 'true' : 'false') . ";"
			
			// Use responsive version for main menu
			. "SEOHUB_GLOBALS['menu_relayout']	= " . max(0, (int) seohub_get_theme_option('menu_relayout')) . ";"
			. "SEOHUB_GLOBALS['menu_responsive']	= " . (seohub_get_theme_option('responsive_layouts') == 'yes' ? max(0, (int) seohub_get_theme_option('menu_responsive')) : 0) . ";"
			. "SEOHUB_GLOBALS['menu_slider']     = " . (seohub_get_theme_option('menu_slider')=='yes' ? 'true' : 'false') . ";"

			// Right panel demo timer
			. "SEOHUB_GLOBALS['demo_time']		= " . (seohub_get_theme_option('show_theme_customizer')=='yes' ? max(0, (int) seohub_get_theme_option('customizer_demo')) : 0) . ";"

			// Video and Audio tag wrapper
			. "SEOHUB_GLOBALS['media_elements_enabled'] = " . (seohub_get_theme_option('use_mediaelement')=='yes' ? 'true' : 'false') . ";"
			
			// Use AJAX search
			. "SEOHUB_GLOBALS['ajax_search_enabled'] 	= " . (seohub_get_theme_option('use_ajax_search')=='yes' ? 'true' : 'false') . ";"
			. "SEOHUB_GLOBALS['ajax_search_min_length']	= " . min(3, seohub_get_theme_option('ajax_search_min_length')) . ";"
			. "SEOHUB_GLOBALS['ajax_search_delay']		= " . min(200, max(1000, seohub_get_theme_option('ajax_search_delay'))) . ";"

			// Use CSS animation
			. "SEOHUB_GLOBALS['css_animation']      = " . (seohub_get_theme_option('css_animation')=='yes' ? 'true' : 'false') . ";"
			. "SEOHUB_GLOBALS['menu_animation_in']  = '" . esc_attr(seohub_get_theme_option('menu_animation_in')) . "';"
			. "SEOHUB_GLOBALS['menu_animation_out'] = '" . esc_attr(seohub_get_theme_option('menu_animation_out')) . "';"

			// Popup windows engine
			. "SEOHUB_GLOBALS['popup_engine']	= '" . esc_attr(seohub_get_theme_option('popup_engine')) . "';"

			// E-mail mask
			. "SEOHUB_GLOBALS['email_mask']		= '^([a-zA-Z0-9_\\-]+\\.)*[a-zA-Z0-9_\\-]+@[a-z0-9_\\-]+(\\.[a-z0-9_\\-]+)*\\.[a-z]{2,6}$';"
			
			// Messages max length
			. "SEOHUB_GLOBALS['contacts_maxlength']	= " . intval(seohub_get_theme_option('message_maxlength_contacts')) . ";"
			. "SEOHUB_GLOBALS['comments_maxlength']	= " . intval(seohub_get_theme_option('message_maxlength_comments')) . ";"

			// Remember visitors settings
			. "SEOHUB_GLOBALS['remember_visitors_settings']	= " . (seohub_get_theme_option('remember_visitors_settings')=='yes' ? 'true' : 'false') . ";"

			// Internal vars - do not change it!
			// Flag for review mechanism
			. "SEOHUB_GLOBALS['admin_mode']			= false;"
			// Max scale factor for the portfolio and other isotope elements before relayout
			. "SEOHUB_GLOBALS['isotope_resize_delta']	= 0.3;"
			// jQuery object for the message box in the form
			. "SEOHUB_GLOBALS['error_message_box']	= null;"
			// Waiting for the viewmore results
			. "SEOHUB_GLOBALS['viewmore_busy']		= false;"
			. "SEOHUB_GLOBALS['video_resize_inited']	= false;"
			. "SEOHUB_GLOBALS['top_panel_height']		= 0;"
			
			. "</script>";
	}
}


//  Enqueue Custom styles (main Theme options settings)
if ( !function_exists( 'seohub_enqueue_custom_styles' ) ) {
	function seohub_enqueue_custom_styles() {
		// Custom stylesheet
		$custom_css = '';	//seohub_get_custom_option('custom_stylesheet_url');
		seohub_enqueue_style( 'seohub-custom-style', $custom_css ? $custom_css : seohub_get_file_url('css/custom-style.css'), array(), null );
		// Custom inline styles
		wp_add_inline_style( 'seohub-custom-style', seohub_prepare_custom_styles() );
	}
}

// Add class "widget_number_#' for each widget
if ( !function_exists( 'seohub_add_widget_number' ) ) {
	//add_filter('dynamic_sidebar_params', 'seohub_add_widget_number', 10, 1);
	function seohub_add_widget_number($prm) {
		global $SEOHUB_GLOBALS;
		if (is_admin()) return $prm;
		static $num=0, $last_sidebar='', $last_sidebar_id='', $last_sidebar_columns=0, $last_sidebar_count=0, $sidebars_widgets=array();
		$cur_sidebar = !empty($SEOHUB_GLOBALS['current_sidebar']) ? $SEOHUB_GLOBALS['current_sidebar'] : 'undefined';
		if (count($sidebars_widgets) == 0)
			$sidebars_widgets = wp_get_sidebars_widgets();
		if ($last_sidebar != $cur_sidebar) {
			$num = 0;
			$last_sidebar = $cur_sidebar;
			$last_sidebar_id = $prm[0]['id'];
			$last_sidebar_columns = max(1, (int) seohub_get_custom_option('sidebar_'.($cur_sidebar).'_columns'));
			$last_sidebar_count = count($sidebars_widgets[$last_sidebar_id]);
		}
		$num++;
		$prm[0]['before_widget'] = str_replace(' class="', ' class="widget_number_'.esc_attr($num).($last_sidebar_columns > 1 ? ' column-1_'.esc_attr($last_sidebar_columns) : '').' ', $prm[0]['before_widget']);
		return $prm;
	}
}


// Filters wp_title to print a neat <title> tag based on what is being viewed.
if ( !function_exists( 'seohub_wp_title' ) ) {
	// add_filter( 'wp_title', 'seohub_wp_title', 10, 2 );
	function seohub_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.
		if ( is_home() || is_front_page() ) {
			$site_description = seohub_get_custom_option('logo_slogan');
			if (empty($site_description)) 
				$site_description = get_bloginfo( 'description', 'display' );
			if ( $site_description )
				$title .= " $sep $site_description";
		}
		// Add a page number if necessary:
		if ( $paged >= 2 || $page >= 2 )
			$title .= " $sep " . sprintf( esc_html__( 'Page %s', 'seohub' ), max( $paged, $page ) );
		return $title;
	}
}

// Add main menu classes
if ( !function_exists( 'seohub_add_mainmenu_classes' ) ) {
	// add_filter('wp_nav_menu_objects', 'seohub_add_mainmenu_classes', 10, 2);
	function seohub_add_mainmenu_classes($items, $args) {
		if (is_admin()) return $items;
		if ($args->menu_id == 'mainmenu' && seohub_get_theme_option('menu_colored')=='yes' && is_array($items) && count($items) > 0) {
			foreach($items as $k=>$item) {
				if ($item->menu_item_parent==0) {
					if ($item->type=='taxonomy' && $item->object=='category') {
						$cur_tint = seohub_taxonomy_get_inherited_property('category', $item->object_id, 'bg_tint');
						if (!empty($cur_tint) && !seohub_is_inherit_option($cur_tint))
							$items[$k]->classes[] = 'bg_tint_'.esc_attr($cur_tint);
					}
				}
			}
		}
		return $items;
	}
}


// Save post data from frontend editor
if ( !function_exists( 'seohub_callback_frontend_editor_save' ) ) {
	function seohub_callback_frontend_editor_save() {
		global $_REQUEST;

		if ( !wp_verify_nonce( $_REQUEST['nonce'], 'seohub_editor_nonce' ) )
			die();

		$response = array('error'=>'');

		parse_str($_REQUEST['data'], $output);
		$post_id = $output['frontend_editor_post_id'];

		if ( seohub_get_theme_option("allow_editor")=='yes' && (current_user_can('edit_posts', $post_id) || current_user_can('edit_pages', $post_id)) ) {
			if ($post_id > 0) {
				$title   = stripslashes($output['frontend_editor_post_title']);
				$content = stripslashes($output['frontend_editor_post_content']);
				$excerpt = stripslashes($output['frontend_editor_post_excerpt']);
				$rez = wp_update_post(array(
					'ID'           => $post_id,
					'post_content' => $content,
					'post_excerpt' => $excerpt,
					'post_title'   => $title
				));
				if ($rez == 0) 
					$response['error'] = esc_html__('Post update error!', 'seohub');
			} else {
				$response['error'] = esc_html__('Post update error!', 'seohub');
			}
		} else
			$response['error'] = esc_html__('Post update denied!', 'seohub');
		
		echo json_encode($response);
		die();
	}
}

// Delete post from frontend editor
if ( !function_exists( 'seohub_callback_frontend_editor_delete' ) ) {
	function seohub_callback_frontend_editor_delete() {
		global $_REQUEST;

		if ( !wp_verify_nonce( $_REQUEST['nonce'], 'seohub_editor_nonce' ) )
			die();

		$response = array('error'=>'');
		
		$post_id = $_REQUEST['post_id'];

		if ( seohub_get_theme_option("allow_editor")=='yes' && (current_user_can('delete_posts', $post_id) || current_user_can('delete_pages', $post_id)) ) {
			if ($post_id > 0) {
				$rez = wp_delete_post($post_id);
				if ($rez === false) 
					$response['error'] = esc_html__('Post delete error!', 'seohub');
			} else {
				$response['error'] = esc_html__('Post delete error!', 'seohub');
			}
		} else
			$response['error'] = esc_html__('Post delete denied!', 'seohub');

		echo json_encode($response);
		die();
	}
}


// Prepare logo text
if ( !function_exists( 'seohub_prepare_logo_text' ) ) {
	function seohub_prepare_logo_text($text) {
		$text = str_replace(array('[', ']'), array('<span class="theme_accent">', '</span>'), $text);
		$text = str_replace(array('{', '}'), array('<strong>', '</strong>'), $text);
		return $text;
	}
}
?>