<?php
/**
 * tos functions and definitions
 *
 * @package tos
 */

if ( ! function_exists( 'tos_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 tos_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on tos, use a find and replace
	 * to change 'tos' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'tos', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary Menu', 'tos' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );

	/*
	 * Enable support for Post Formats.
	 * See http://codex.wordpress.org/Post_Formats
	 */
	add_theme_support( 'post-formats', array(
		'aside',
		'image',
		'video',
		'quote',
		'link',
	) );

	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'tos_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
}
endif; // tos_setup
add_action( 'after_setup_theme', 'tos_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function tos_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'tos_content_width', 640 );
}
add_action( 'after_setup_theme', 'tos_content_width', 0 );

/**
 * Register widget area.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */
function tos_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'tos' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h1 class="widget-title">',
		'after_title'   => '</h1>',
	) );
}
add_action( 'widgets_init', 'tos_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function tos_scripts() {
	//wp_enqueue_style( 'tos-style', get_stylesheet_uri() );

	wp_enqueue_script( 'tos-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );

	wp_enqueue_script( 'tos-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'tos_scripts' );

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';


/****** TOS FUNCTIONS ********/


//SCRIPTS
add_action( 'wp_enqueue_scripts', 'tos_load_scripts' );
function tos_load_scripts(){
    wp_deregister_script( 'jquery-masonry' );
		wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'bt', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.5', true ); 
    wp_enqueue_script( 'carousel', get_template_directory_uri() . '/js/owl.carousel.min.js', array('jquery'), '1.0', true); 
    //wp_enqueue_script( 'owl-carousel', '//cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js', array(), '2.8.3', true );
		
		wp_enqueue_script( 'nicescroll', get_template_directory_uri() . '/js/jquery.nicescroll.min.js', array('jquery'), '3.6.0', true); 
    
    wp_enqueue_script( 'infinite', get_template_directory_uri() . '/js/jquery.infinitescroll.min.js', array('jquery'), '1.0', true );
    wp_enqueue_script( 'isotope', get_template_directory_uri() . '/js/isotope.pkgd.min.js', array('jquery'), '2.2.1', true );
		
    // Load only on frontpage
    if (is_front_page()) {
        wp_enqueue_script( 'm-horizontal', get_template_directory_uri() . '/js/masonry-horizontal.js', array('jquery', 'isotope'), '1.1.2', true  );
    }
		// Load only on Blog template
    if (is_page_template( 'tpl-blog.php' )) {
        wp_enqueue_script( 'masonry', get_template_directory_uri() . '/js/masonry.pkgd.min.js', array('jquery', 'isotope'), '3.3.1', true  );
    }

    wp_enqueue_script( 'imagesloaded', get_template_directory_uri() . '/js/imagesloaded.pkgd.min.js', array('jquery', 'isotope'), '1.1.0', true  );
		wp_enqueue_script( 'custom', get_template_directory_uri() . '/js/scripts.js', array(), '1.0.0', true );

    //init additional style
		wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');
		wp_enqueue_style( 'owl', get_template_directory_uri() . '/css/owl.carousel.css');
		wp_enqueue_style( 'owl-style', get_template_directory_uri() . '/css/owl.theme.css');
		wp_enqueue_style( 'owl-transitions', get_template_directory_uri() . '/css/owl.transitions.css');
		wp_enqueue_style( 'font', get_template_directory_uri() . '/css/font-awesome.min.css');
		//wp_enqueue_style( 'animate', get_template_directory_uri() . '/css/animate.css'); 
		wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom.css' );
		wp_enqueue_style( 'main-style', get_stylesheet_uri() );
    wp_enqueue_style( 'responsive', get_template_directory_uri() . '/css/responsive.css' );
}


/*
 * FEATURED IMAGE
 * Additional image size
 */

add_image_size( 'team-img', 640, 640, true );
add_image_size( 'blog-img', 640, 428, array( 'center', 'center' ) );
add_image_size( 'instagram-img', 385, 352, array( 'center', 'center' ) );
add_image_size( 'post-img', 1920, 695, array( 'center', 'center' ) );
add_image_size( 'fullHD', 1920, 1080, array( 'center', 'center' ) );
add_image_size( 'isotop-img', 455, 305, array( 'center', 'center' ) );
add_image_size( 'youtube-thumb', 1024, 576, array( 'center', 'center' ));



/*
 *   TEAM MEMBERS CUSTOM POST TYPE
 */

if ( ! function_exists('Team_members') ) {

// Register Custom Post Type
function Team_members() {

	$labels = array(
		'name'                => 'Blogeri',
		'singular_name'       => 'Bloger',
		'menu_name'           => 'Blogeri',
		'name_admin_bar'      => 'Blogeri',
		'parent_item_colon'   => 'Parent Item:',
		'all_items'           => 'All Items',
		'add_new_item'        => 'Add New Item',
		'add_new'             => 'Add New',
		'new_item'            => 'New Item',
		'edit_item'           => 'Edit Item',
		'update_item'         => 'Update Item',
		'view_item'           => 'View Item',
		'search_items'        => 'Search Item',
		'not_found'           => 'Not found',
		'not_found_in_trash'  => 'Not found in Trash',
	);
	$args = array(
		'label'               => 'Team members',
		'description'         => 'Team members',
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'thumbnail', 'post-formats', ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'menu_position'       => 20,
		'menu_icon'           => 'dashicons-groups',
		'show_in_admin_bar'   => true,
		'show_in_nav_menus'   => true,
		'can_export'          => true,
		'has_archive'         => true,		
		'exclude_from_search' => true,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	register_post_type( 'Team members', $args );

}

// Hook into the 'init' action
add_action( 'init', 'Team_members', 0 );

}


/*
 *   VIDEO CUSTOM POST TYPE
 */

// Register Custom Post Type
function Video() {

	$labels = array(
		'name'                => 'videos',
		'singular_name'       => 'video',
		'menu_name'           => 'Video',
		'name_admin_bar'      => 'Video',
		'parent_item_colon'   => 'Parent Item:',
		'all_items'           => 'All Items',
		'add_new_item'        => 'Add New Item',
		'add_new'             => 'Add New',
		'new_item'            => 'New Item',
		'edit_item'           => 'Edit Item',
		'update_item'         => 'Update Item',
		'view_item'           => 'View Item',
		'search_items'        => 'Search Item',
		'not_found'           => 'Not found',
		'not_found_in_trash'  => 'Not found in Trash',
	);
	$args = array(
		'label'               => 'video',
		'description'         => 'video',
		'labels'              => $labels,
		'supports'            => array( ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'menu_position'       => 15,
		'menu_icon'           => 'dashicons-video-alt',
		'show_in_admin_bar'   => true,
		'show_in_nav_menus'   => true,
		'can_export'          => true,
		'has_archive'         => true,		
		'exclude_from_search' => true,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	register_post_type( 'video', $args );

}

// Hook into the 'init' action
add_action( 'init', 'Video', 0 );

/*
 *   HEADER TWEETS CUSTOM POST TYPE
 */

if ( ! function_exists('Header_tweets') ) {

// Register Custom Post Type
function Header_tweets() {

	$labels = array(
		'name'                => 'Header tweets',
		'singular_name'       => 'Header tweet',
		'menu_name'           => 'Header tweets',
		'name_admin_bar'      => 'Header tweets',
		'parent_item_colon'   => 'Parent Item:',
		'all_items'           => 'All tweets',
		'add_new_item'        => 'Add New tweet',
		'add_new'             => 'Add tweet',
		'new_item'            => 'New tweet',
		'edit_item'           => 'Edit tweet',
		'update_item'         => 'Update tweet',
		'view_item'           => 'View tweet',
		'search_items'        => 'Search tweets',
		'not_found'           => 'Not found',
		'not_found_in_trash'  => 'Not found in Trash',
	);
	$args = array(
		'label'               => 'Header tweets',
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'thumbnail', 'post-formats', ),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'menu_position'       => 22,
		'menu_icon'           => 'dashicons-twitter',
		'show_in_admin_bar'   => true,
		'show_in_nav_menus'   => true,
		'can_export'          => true,
		'has_archive'         => true,		
		'exclude_from_search' => true,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	register_post_type( 'tweet', $args );
}

// Hook into the 'init' action
add_action( 'init', 'Header_tweets', 0 );

}

	// Remove post formats support for tweets
	add_action('init', 'remove_tweets_support', 11);
	function remove_tweets_support() {
			remove_post_type_support('tweet', 'post-formats');
			remove_post_type_support('tweet', 'thumbnail');
	}


/*
 *   CHARACTER LIMIT
 */

function get_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 438);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    //$excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
}

/*
 *   Custom Language list function
 */
if ( ! function_exists('languages_list_header') ) {
	function languages_list_header(){
		$languages = icl_get_languages('skip_missing=0&orderby=code');
		$result = "";
		if(!empty($languages)) {
				$active_language = "";
				$other_languages = "";
				foreach($languages as $l) {
					$language_name = icl_disp_language($l['native_name'], $l['translated_name']);
					if ($l['active']) {
						$active_language = "<a href='${l['url']}' class='lang'>${language_name}</a>";
					} else {
						$other_languages .= "<li><a href='${l['url']}'>${language_name}</a>";
					}
				}
				$result = $active_language."<ul class='dropdown'>".$other_languages."</ul>";
		}
		echo $result;
	}
}

/*
 * Remove some scripts from frontend
 */
 
 if ( !is_admin() ){ 

 }
 
 function deregister_frontend_ss_scripts() {
    if ( !is_admin() ){ 
			wp_deregister_script('moment');
    	wp_deregister_script('alertify');
			wp_deregister_script('livestamp');
			wp_deregister_script('spin');
			wp_deregister_script('selectize');
			
    } 
 }
add_action( 'wp_print_scripts', 'deregister_frontend_ss_scripts');
