<?php
/**
 * SKT I Am One functions and definitions
 *
 * @package SKT I Am One
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 */
function skt_iamone_string_limit_words($string, $word_limit){
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}


if ( ! function_exists( 'skt_iamone_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 post thumbnails.
 */
function skt_iamone_setup() {

	if ( ! isset( $content_width ) )
		$content_width = 640; /* pixels */
	
	load_theme_textdomain( 'skt-iamone', get_template_directory() . '/languages' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-thumbnails' );
	add_image_size('homepage-thumb',240,145,true);
	register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'skt-iamone' ),
	) );
	add_theme_support( 'custom-background', array(
		'default-color' => '000000'
	) );
	add_editor_style( 'editor-style.css' );
}
endif; // skt_iamone_setup
add_action( 'after_setup_theme', 'skt_iamone_setup' );


function skt_iamone_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Blog Sidebar', 'skt-iamone' ),
		'description'   => __( 'Appears on blog page sidebar', 'skt-iamone' ),
		'id'            => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

}
add_action( 'widgets_init', 'skt_iamone_widgets_init' );

if ( !function_exists( 'optionsframework_init' ) ) {
	define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/inc/' );
	require_once dirname( __FILE__ ) . '/inc/options-framework.php';
}


function skt_iamone_scripts() {
	wp_enqueue_style( 'skt_iamone-gfonts-opensans', '//fonts.googleapis.com/css?family=Open+Sans:400,600,700' );
	wp_enqueue_style( 'skt_iamone-gfonts-roboto', '//fonts.googleapis.com/css?family=Roboto:400,100,300,500,700' );
	wp_enqueue_style( 'skt_iamone-gfonts-lobster', '//fonts.googleapis.com/css?family=Lobster' );
	wp_enqueue_style( 'skt_iamone-gfonts-opensanscondensed', '//fonts.googleapis.com/css?family=Open+Sans+Condensed:300' );
	wp_enqueue_style( 'skt_iamone-gfonts-lato', '//fonts.googleapis.com/css?family=Lato:400,900,400italic,700,300italic,300,700italic' );

	wp_enqueue_style( 'skt_iamone-basic-style', get_stylesheet_uri() );
	wp_enqueue_style( 'skt_iamone-editor-style', get_template_directory_uri().'/editor-style.css' );
	wp_enqueue_style( 'skt_iamone-base-style', get_template_directory_uri().'/css/style_base.css' );
	if ( (of_get_option('innerpageslider', true) != 'hide') || is_home() || is_front_page() ) { 
		wp_enqueue_style( 'skt_iamone-supersized-default-theme', get_template_directory_uri().'/css/supersized.css' );
		wp_enqueue_style( 'skt_iamone-supersized-style', get_template_directory_uri().'/css/supersized.shutter.css' );
		wp_enqueue_script( 'skt_iamone-supersized-easing', get_template_directory_uri() . '/js/jquery.easing.min.js', array('jquery') );
		wp_enqueue_script( 'skt_iamone-supersized-slider', get_template_directory_uri() . '/js/supersized.3.2.7.min.js', array('jquery') );
		wp_enqueue_script( 'skt_iamone-supersized-shutter', get_template_directory_uri() . '/js/supersized.shutter.js', array('jquery') );
	}
	wp_enqueue_style( 'skt_iamone-prettyphoto-style', get_template_directory_uri().'/css/prettyPhoto.css' );
	wp_enqueue_script( 'skt_iamone-prettyphoto-script', get_template_directory_uri() . '/js/jquery.prettyPhoto.js', array('jquery') );
	wp_enqueue_script( 'skt_iamone-customscripts', get_template_directory_uri() . '/js/custom.js', array('jquery') );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'skt_iamone_scripts' );

function media_css_hook(){
	$output = '';
	$output ='<link rel="stylesheet" href="'.get_template_directory_uri().'/css/computer.css" type="text/css" media="screen and (min-width:940px)" />
	<link rel="stylesheet" href="'.get_template_directory_uri().'/css/tablet.css" type="text/css" media="screen and (min-width: 720px) and (max-width: 939px)" />
	<link rel="stylesheet" href="'.get_template_directory_uri().'/css/mobile_hz.css" type="text/css" media="screen and (min-width: 480px) and (max-width: 719px)" />
	<link rel="stylesheet" href="'.get_template_directory_uri().'/css/mobile.css" type="text/css" media="screen and (max-width: 479px)" />';
	echo $output; 

	if ( (of_get_option('innerpageslider', true) != 'hide') || is_home() || is_front_page() ) {

		$slAr = array();
		for ($i=1; $i<6; $i++) {
			if ( of_get_option('slide'.$i, true) != "" ) {
				$imgUrl 	= of_get_option('slide'.$i, true);
				$imgTitle	= of_get_option('slidetitle'.$i, true);
				if ( strlen($imgUrl) > 3 ) $slAr[$i] = of_get_option('slide'.$i, true);
			}
		}
		?>
		<script>
		jQuery(document).ready(function(){ 
			jQuery.supersized({
				// Functionality
				slideshow               :   1,			// Slideshow on/off
				autoplay				:	1,			// Determines whether slideshow begins playing when page is loaded. 
				start_slide             :   1,			// Start slide (0 is random)
				stop_loop				:	0,			// Pauses slideshow on last slide
				random					: 	0,			// Randomize slide order (Ignores start slide)
				slide_interval          :   5000,		// Length between transitions
				transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
				transition_speed		:	1000,		// Speed of transition
				new_window				:	1,			// Image links open in new window/tab
				pause_hover             :   0,			// Pause slideshow on hover
				keyboard_nav            :   1,			// Keyboard navigation on/off
				performance				:	1,			// 0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
				image_protect			:	0,			// Disables image dragging and right click with Javascript
				
				// Size & Position
				min_width		        :   0,			// Min width allowed (in pixels)
				min_height		        :   0,			// Min height allowed (in pixels)
				vertical_center         :   1,			// Vertically center background
				horizontal_center       :   1,			// Horizontally center background
				fit_always				:	0,			// Image will never exceed browser width or height (Ignores min. dimensions)
				fit_portrait         	:   1,			// Portrait images will not exceed browser height
				fit_landscape			:   0,			// Landscape images will not exceed browser width
				
				// Components 				
				slide_links				:	'blank',	// Individual links for each slide (Options: false, 'num', 'name', 'blank')
				thumb_links				:	1,			// Individual thumb links for each slide
				thumbnail_navigation    :   0,			// Thumbnail navigation
				slides 					:  	[			// Slideshow Images
												<?php
												$n = 0;
												foreach( $slAr as  $sv ){
													$n++;
													echo "{image : '".$sv."', title : '', thumb : '".$sv."', url : ''}".( (count($slAr) == $n) ? '' : ',' )."\n";
												}
												?>
											],
				// Theme Options 
				progress_bar			:	1,			// Timer for each slide							
				mouse_scrub				:	0
			});
		});
		</script><?php 
	}
}
add_action('wp_head','media_css_hook');


function skt_iamone_custom_head_codes() { 
	if ( function_exists('of_get_option') ){
		if ( of_get_option('style2', true) != '' ) {
			echo "<style>". esc_html( of_get_option('style2', true) ) ."</style>";
		}
	}
}
add_action('wp_head', 'skt_iamone_custom_head_codes');


function skt_iamone_pagination() {
	global $wp_query;
	$big = 12345678;
	$page_format = paginate_links( array(
	    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
	    'format' => '?paged=%#%',
	    'current' => max( 1, get_query_var('paged') ),
	    'total' => $wp_query->max_num_pages,
	    'type'  => 'array'
	) );
	if( is_array($page_format) ) {
		$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
		echo '<div class="pagination"><div><ul>';
		echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
		foreach ( $page_format as $page ) {
			echo "<li>$page</li>";
		}
		echo '</ul></div></div>';
	}
}
/**
 * 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';


/**
 * Load custom functions file.
 */
require get_template_directory() . '/inc/custom-functions.php';


function skt_iamone_custom_blogpost_pagination( $wp_query ){
	$big = 999999999; // need an unlikely integer
	if ( get_query_var('paged') ) { $pageVar = 'paged'; }
	elseif ( get_query_var('page') ) { $pageVar = 'page'; }
	else { $pageVar = 'paged'; }
	$pagin = paginate_links( array(
		'base' 			=> str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format' 		=> '?'.$pageVar.'=%#%',
		'current' 		=> max( 1, get_query_var($pageVar) ),
		'total' 		=> $wp_query->max_num_pages,
		'prev_text'		=> '&laquo; Prev',
		'next_text' 	=> 'Next &raquo;',
		'type'  => 'array'
	) ); 
	if( is_array($pagin) ) {
		$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
		echo '<div class="pagination"><div><ul>';
		echo '<li><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
		foreach ( $pagin as $page ) {
			echo "<li>$page</li>";
		}
		echo '</ul></div></div>';
	} 
}

// get slug by id
function skt_iamone_get_slug_by_id($id) {
	$post_data = get_post($id, ARRAY_A);
	$slug = $post_data['post_name'];
	return $slug; 
}

// Add custom script in admin footer
function custom_admin_footer_js() {
	echo '<script>
		var nums = jQuery("#numsection").val();
		jQuery(".toggle_title h3").addClass("toggleTitle");
		jQuery(".toggle_title").each( function( index ){
			jQuery( "#section-sectiontitle"+(index+1)+", #section-sectioncontent"+(index+1)+", #section-menutitle"+(index+1)+", #section-sectionbgcolor"+(index+1)+", #section-sectionbgimage"+(index+1)+", #section-outerclass"+(index+1)+", #section-sectionclass"+(index+1) ).wrapAll( "<div class=\'toggle_wrapper\' />");
		});
		jQuery(".toggle_title h3").click( function(){
			jQuery(this).parent().next().slideToggle();
		});
	</script>"';
}
add_action('admin_footer', 'custom_admin_footer_js');

// add shortcode for features
function skt_iamone_features($skt_var, $content = null){
		extract( shortcode_atts(array(
			'title' => 'title',
			'icon'  => get_template_directory_uri().'/images/icon-customizable.png',
		), $skt_var));
		
		return '<div class="feature-box">
				<img src="'.$icon.'" />
				<h5>'.$title.'</h5>
				<p>'.$content.'</p>
				</div>';
}
add_shortcode('feature','skt_iamone_features');

// add shortcode for work
function skt_iamone_work($skt_work, $work_content = null) {
		extract( shortcode_atts(array(
			'title'	=> 'title',
			'step'	=> 'step',
			'icon'	=> get_template_directory_uri().'/images/icon-discuss.png',
		), $skt_work));
		
		return '<div class="work-box">
					<div class="step-'.$step.'">
					</div>
					<div class="work-info"><img src="'.$icon.'" />
					<h3>'.$title.'</h3>
					<p>'.$work_content.'</p>
					</div><div class="clear"></div>
				</div>';
}

add_shortcode('work','skt_iamone_work');


// add shortcode for statistics
function skt_iamone_stat($skt_stat_var, $stat_content = null){
	extract( shortcode_atts(array(
		'value'		=> 'value',
	), $skt_stat_var));
	
	return '<div class="count-box">
			<div class="count-no">'.$value.'</div>
			<div class="count-content">'.$stat_content.'</div>
			<div class="clear"></div></div>';
}

add_shortcode('stat','skt_iamone_stat');


// add shortcode for sercices
function skt_iamone_services($skt_service, $service_content = null){
		extract( shortcode_atts(array(
			'title'		=> 'title',
			'icon'		=> get_template_directory_uri().'/images/icon-doc.png',
		), $skt_service));
		
		return '<div class="feature-cell">
				<div class="feature-icon"><img src="'.$icon.'" /></div>
				<div class="feature-desc"><h5>'.$title.'</h5>
				'.$service_content.'
				</div><div class="clear"></div>
				</div>';
}

add_shortcode('service','skt_iamone_services');	

function remove_header_menu(){
	remove_submenu_page( 'themes.php', 'custom-header' );
	remove_submenu_page( 'themes.php', 'custom-background' );
}
add_action( 'admin_menu', 'remove_header_menu', 999 );

function skt_credit_link(){
		echo 'I am one theme by <a href="http://www.sktthemes.net">SKTThemes.</a>';
	}

