<?php 
add_filter('wp_title', 'teo_filter_wp_title', 9, 3);
function teo_filter_wp_title( $old_title, $sep, $sep_location ) {
	$ssep = ' ' . $sep . ' ';
	if (is_home() ) {
		return get_bloginfo('name');
	}
	elseif(is_single() || is_page() )
	{
		return get_the_title();
	}
	elseif( is_category() ) $output = $ssep . 'Category';
	elseif( is_tag() ) $output = $ssep . 'Tag';
	elseif( is_author() ) $output = $ssep . 'Author';
	elseif( is_year() || is_month() || is_day() ) $output = $ssep . 'Archives';
	else $output = NULL;
	 
	// get the page number we're on (index)
	if( get_query_var( 'paged' ) )
	$num = $ssep . 'page ' . get_query_var( 'paged' );
	 
	// get the page number we're on (multipage post)
	elseif( get_query_var( 'page' ) )
	$num = $ssep . 'page ' . get_query_var( 'page' );
		 
	// else
	else $num = NULL;
		 
	// concoct and return new title
	return get_bloginfo( 'name' ) . $output . $old_title . $num;
}

function truncate_content($limit, $echo = true, $paragraphs = true, $customfield = 0) {
	global $post;
	$output = '';
	if($customfield !== 0)
		$output = get_post_meta($post->ID, $customfield, true);
	if($output == '')
	{
		$content = get_the_content();
		$content = strip_shortcodes(strip_tags($content));
		$len = strlen($content);
		if($len > $limit)
		{
			$output = substr($content, 0, $limit);
			$output .= '...';
		}
		else
		{
			$output = $content;
		}
	}
	if($paragraphs == true)
		$output = '<p>' . $output . '</p>';
	if($echo)
		echo $output;
	else
		return $output;
}

function truncate_title($limit, $echo = true) {
	global $post;
	$output = '';
	$content = get_the_title();
	$len = strlen($content);
	if($len > $limit) {
		$output = substr($content, 0, $limit);
		$output .= '...';
	}
	else {
		$output = $content;
	}

	if($echo)
		echo $output;
	else
		return $output;
}

function truncate_text($text, $limit, $echo = true) {
	$output = '';
	$len = strlen($text);
	if($len > $limit) {
		$output = substr($text, 0, $limit);
		$output .= '...';
	}
	else {
		$output = $text;
	}

	if($echo)
		echo $output;
	else
		return $output;
}

add_action('wp_head', 'teo_customization');
//This function handles the Colorization tab of the theme options
if(! function_exists('teo_customization'))
{
	function teo_customization() {
		//favicon
		global $redux_demo;

		if(isset($redux_demo['favicon']['url']) && $redux_demo['favicon']['url'] != '') {
			echo '<link rel="shortcut icon" href="' . $redux_demo['favicon']['url'] . '" />';
			echo '<link rel="apple-touch-icon" href="' . $redux_demo['favicon']['url'] . '" />';
		}

		if(isset($redux_demo['faviconios']['url']) && $redux_demo['faviconios']['url'] != '') {
			echo '<link rel="apple-touch-icon" sizes="72x72"  href="' . $redux_demo['faviconios']['url'] . '" />';
		}

		if(isset($redux_demo['faviconretina']['url']) && $redux_demo['faviconretina']['url'] != '') {
			echo '<link rel="apple-touch-icon" sizes="114x114" href="' . $redux_demo['favicon']['url'] . '" />';
		}

		//customization start
		echo "\n<style type='text/css'> \n";

		if($redux_demo['enable_stickyheaderbar'] == 2) {
			echo '.main-post .buttons-bar { opacity: 1 !important; top: 0 !important; } ';
		}
		if($redux_demo['enable_stickyleftsidebar'] == 2) {
			echo '.sidebar-menu .sidebar-content { height: 100% !important; }
				.left-aside { top: 0 !important; } ';
		}
		if(isset($redux_demo['enable_postdate']) && $redux_demo['enable_postdate'] == 2) {
			echo '.counters-line .date { display: none !important; }';
		}
		if(isset($redux_demo['enable_postauthor']) && $redux_demo['enable_postauthor'] == 2) {
			echo '.counters-line .user { display: none !important; }';
		}
		if(isset($redux_demo['enable_postcomments']) && $redux_demo['enable_postcomments'] == 2) {
			echo '.counters-line .comments { display: none !important; }';
		}

		if($redux_demo['enable_customization'] == 1) {
			$color = $redux_demo['color_scheme'];
			if(strlen($color) == '7') {
				$lightcolor = shadeColor($color, 25);
				$darkcolor = shadeColor($color, -10);
				$darkercolor = shadeColor($color, -14);
				$darkercolor2 = shadeColor($color, -30);
				echo '
				.custom-button.btn-primary { background-color: ' . $color . '; border-color: ' . $color . '}
				.header-wrap { background-color: ' . $color . ';}
				.post-list article figure figcaption { background-color: ' . $color . ';}
				header .right-container .social-share .drop-down { background-color: ' . $color . ';}
				header .navbar { background-color: ' . $color . '; }
				#main a:hover { color: ' . $color . ' !important;}
				.main-post .buttons-bar { background-color: ' . $color . ';}
				.tooltip.bottom .tooltip-arrow { border-bottom-color: ' . $color . ';}
				.tooltip-inner { background-color: ' . $color . ';}
				.custom-checkbox.custom-checkbox-vertical label { color: ' . $color . ';}
				.custom-checkbox.custom-checkbox-vertical input[type=checkbox]:checked + label { color: ' . $color . ';}
				.custom-checkbox input[type=checkbox]:checked + label { color: ' . $color . ';}
				.modal-body .modal-bottom-menu .half .btn-link { background-color: ' . $color . ';}
				.modal-body .post-window .window-left .form-control:focus { border-color: ' . $color . '; color: ' . $color . ';}
				.modal-body .post-window .window-left .upload-wrap .fileinput-button .purple { color: ' . $color . ';}
				.modal-top-menu { background-color: ' . $color . ';}
				.modal-top-menu .half button.active-ind:after { border-top: 6px solid ' . $color . ';}
				.article-infos .tags ul li a:hover { background-color: ' . $color . ';}
				.videos-widget .article.big-article figure .play-button { background-color: ' . $color . ';}
				.videos-widget .article.small-article figure .play-button { background-color: ' . $color . ';}
				.random_post:hover { background-color: ' . $color . ';}
				.error404 .main-wrap span.pink { color: ' . $color . ';}

				header .right-container .user-box { background-color: ' . $darkcolor . ';}
				header .navbar-default .navbar-nav > .active > a,
				header .navbar-default .navbar-nav > .active > a:hover,
				header .navbar-default .navbar-nav > .active > a:focus { background: ' . $darkcolor . ';}
				header .navbar-default .navbar-nav > li > a:hover,
				header .navbar-default .navbar-nav > li > a:focus { background: ' . $darkcolor . ';}
				header .navbar-default .navbar-nav > a,
				header .navbar-default .navbar-nav > a:hover,
				header .navbar-default .navbar-nav > a:focus { background: ' . $darkcolor . ';}
				.top-ten { background-color: ' . $darkcolor . ';}
				.main-nav > ul > li:hover { background-color: ' . $darkcolor . ';}
				.main-post .buttons-bar .buttons .count { background-color: ' . $darkcolor . ';}
				.corner-tag { background-color: ' . $darkcolor . ';}
				.modal-body .modal-bottom-menu .half .btn-link.alternate-btn { background-color: ' . $darkcolor . ';}
				.modal-top-menu .half { border-left: 1px solid ' . $darkcolor . ';}
				.videos-widget .article.big-article figure .play-button .button-text { background-color: ' . $darkcolor . ';}
				.videos-widget .article.small-article figure .play-button .button-text { background-color: ' . $darkcolor . ';}
				header .right-container .social-share .drop-down a:hover { background-color: ' . $darkcolor . ';}
				header .navbar-nav > li:hover > a { background: ' . $darkcolor . ';}
				@media (min-width: 200px) and (max-width: 767px) {
					header .navbar-nav > li .sub-menu { background-color: ' . $darkcolor . ';}
					header .navbar-default .navbar-nav .sub-menu > li > a:hover { background: ' . $darkcolor . ';}
					header .navbar-nav > li > .sub-menu li:hover > a { background: ' . $darkcolor . ';}
					header .navbar-nav > li > .sub-menu li > ul > li > a:hover { background: ' . $darkcolor . ';}
					header .navbar-nav > li > .sub-menu li > ul > li > a:focus { background: ' . $darkcolor . ';}
				}
				header .navbar-default .navbar-brand { border-right: 1px solid ' . $darkcolor . ';}
				.header-wrap .logo { border-right: 1px solid ' . $darkcolor . ';}
				header .right-container .social-share .drop-down li { border-top: 1px solid ' . $darkcolor . ';}
				.main-post .buttons-bar .social-icons a:hover { background-color: ' . $darkcolor . ';}
				.main-post .buttons-bar .close-button:hover { background-color: ' . $darkcolor . ';}

				.main-post .buttons-bar .buttons { background-color: ' . $darkercolor . ';}
				header .right-container, header .right-container .social-share { background: ' . $darkercolor . ';}

				.top-ten .activator:after { border-top: 4px solid ' . $lightcolor . ';}
				.top-ten .activator .left .small { color: ' . $lightcolor . ';}

				.main-post .buttons-bar .buttons a:hover { background-color: ' . $darkercolor2 . ';}

				';
			}
		}

		echo '</style>'; 
		//customization end

	}
}

function teo_views_number($id) {
	$number = get_post_meta($id, '_teo_views', true);
	if($number) {
		if($number >= 1000000) {
			$number = (int)($number / 1000000) . 'kk';
		}
		elseif($number >= 1000) {
			$number = (int)($number / 1000) . 'k';
		}
	}
	else {
		$number = 0;
	}
	return $number;
}

function teo_share_number($url) {
	$count = 0;
	//twitter
    $api = "http://cdn.api.twitter.com/1/urls/count.json?callback=?&url=" . $url;
    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $api);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$content = curl_exec($ch);
	$content = json_decode($content, true);
	$count = $count + $content['count'];
	curl_close($ch);

	//facebook
	$api = "http://graph.facebook.com/?id=" . $url;
    $ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $api);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$content = curl_exec($ch);
	$content = json_decode($content, true);
	if(isset($content['shares']) ) {
		$count = $count + $content['shares'];
	}
	curl_close($ch);
	return $count;	
}


/**
* Title		: Aqua Resizer
* Description	: Resizes WordPress images on the fly
* Version	: 1.1.6
* Author	: Syamil MJ
* Author URI	: http://aquagraphite.com
* License	: WTFPL - http://sam.zoy.org/wtfpl/
* Documentation	: https://github.com/sy4mil/Aqua-Resizer/
*
* @param	string $url - (required) must be uploaded using wp media uploader
* @param	int $width - (required)
* @param	int $height - (optional)
* @param	bool $crop - (optional) default to soft crop
* @param	bool $single - (optional) returns an array if false
* @uses		wp_upload_dir()
* @uses		image_resize_dimensions() | image_resize()
* @uses		wp_get_image_editor()
*
* @return str|array
*/

function aq_resize( $url, $width, $height = null, $crop = null, $single = true ) {
	
	//validate inputs
	if(!$url OR !$width ) return false;
	
	//define upload path & dir
	$upload_info = wp_upload_dir();
	$upload_dir = $upload_info['basedir'];
	$upload_url = $upload_info['baseurl'];
	
	//check if $img_url is local
	if(strpos( $url, $upload_url ) === false) return false;
	
	//define path of image
	$rel_path = str_replace( $upload_url, '', $url);
	$img_path = $upload_dir . $rel_path;
	
	//check if img path exists, and is an image indeed
	if( !file_exists($img_path) OR !getimagesize($img_path) ) return false;
	
	//get image info
	$info = pathinfo($img_path);
	$ext = $info['extension'];
	list($orig_w,$orig_h) = getimagesize($img_path);
	
	//get image size after cropping
	$dims = image_resize_dimensions($orig_w, $orig_h, $width, $height, $crop);
	$dst_w = $dims[4];
	$dst_h = $dims[5];
	
	//use this to check if cropped image already exists, so we can return that instead
	$suffix = "{$dst_w}x{$dst_h}";
	$dst_rel_path = str_replace( '.'.$ext, '', $rel_path);
	$destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
	
	if(!$dst_h) {
		//can't resize, so return original url
		$img_url = $url;
		$dst_w = $orig_w;
		$dst_h = $orig_h;
	}
	//else check if cache exists
	elseif(file_exists($destfilename) && getimagesize($destfilename)) {
		$img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
	} 
	//else, we resize the image and return the new resized image url
	else {
		
		// Note: This pre-3.5 fallback check will edited out in subsequent version
		if(function_exists('wp_get_image_editor')) {
		
			$editor = wp_get_image_editor($img_path);
			
			if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
				return false;
			
			$resized_file = $editor->save();
			
			if(!is_wp_error($resized_file)) {
				$resized_rel_path = str_replace( $upload_dir, '', $resized_file['path']);
				$img_url = $upload_url . $resized_rel_path;
			} else {
				return false;
			}
			
		} else {
		
			$resized_img_path = image_resize( $img_path, $width, $height, $crop ); // Fallback foo
			if(!is_wp_error($resized_img_path)) {
				$resized_rel_path = str_replace( $upload_dir, '', $resized_img_path);
				$img_url = $upload_url . $resized_rel_path;
			} else {
				return false;
			}
		
		}
		
	}
	
	//return the output
	if($single) {
		//str return
		$image = $img_url;
	} else {
		//array return
		$image = array (
			0 => $img_url,
			1 => $dst_w,
			2 => $dst_h
		);
	}
	
	return $image;
}

function shadeColor($color, $percent) {
	$color = str_replace('#', '', $color);
	$rgb = array(hexdec(substr($color,0,2)), hexdec(substr($color,2,2)), hexdec(substr($color,4,2)));

	$rgb[0] = $rgb[0] * (100 + $percent) / 100;
	$rgb[1] = $rgb[1] * (100 + $percent) / 100;
	$rgb[2] = $rgb[2] * (100 + $percent) / 100;

	$rgb[0] = $rgb[0] < 255 ? $rgb[0] : 255;
	$rgb[1] = $rgb[1] < 255 ? $rgb[1] : 255;
	$rgb[2] = $rgb[2] < 255 ? $rgb[2] : 255;

	//// RBG to Hex
	$hex = '#';
	for($i=0; $i < 3; $i++) {
		// Convert the decimal digit to hex
		$hexDigit = dechex($rgb[$i]);
		// Add a leading zero if necessary
		if(strlen($hexDigit) == 1) {
			$hexDigit = "0" . $hexDigit;
		}
		// Append to the hex string
		$hex .= $hexDigit;
	}
	return $hex;
}
?>