<?php

// Register Sidebars
include("functions/tt_sidebars.php");

// Widgets
include("functions/tt_widgets.php");

// Add the Theme Options Pages
include("functions/tt_admin_options.php");

// Functions
include("functions/tt_new_wp_functions.php");


// Shortcodes
include("functions/tt_shortcodes.php");

if ( ! isset( $content_width ) )
	$content_width = 580;

if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
	add_theme_support('automatic-feed-links');
}

function tt_dyn_css(){

	// Logo
	$logo = get_option('tt_logo');
	if ($logo) {
		echo '<style type="text/css">
				#logo {
					background: url('.$logo.') no-repeat; } 
			</style>';			
	}
}

function tt_init(){
	load_theme_textdomain( 'typethemes', get_template_directory() . '/languages'); 
	add_action('wp_head', 'tt_dyn_css');	
}

add_action('init', 'tt_init');

global $wp_version;
if (version_compare($wp_version, '3.1', '>=')) {
// version is 3.1 or higher

	// Remove admin bar
	add_action( 'show_admin_bar', '__return_false' );
}

// Theme Support
if ( function_exists( 'add_theme_support' ) ) { 
	add_theme_support( 'automatic-feed-links' );
	
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'featured-thumb', 240, 200, true );
	add_image_size( 'slide-thumb', 180, 114, true );
	add_image_size( 'main-thumb', 150, 110, true );
	add_image_size( 'small-thumb', 75, 55, true );
	add_image_size( 'tiny-thumb', 50, 50, true );
}

// Javascripts
if (!is_admin())
        add_action('wp_print_scripts', 'tt_js');

if (!function_exists('tt_js')) {

        function tt_js() {
			wp_deregister_script('jquery');
			wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', false, '1.5.1');
			wp_enqueue_script('jquery');	
			
			wp_enqueue_script('hoverIntent', get_template_directory_uri() . '/js/jquery.hoverIntent.minified.js', array('jquery'), false, false);
			wp_enqueue_script('easing', get_template_directory_uri() . '/js/jquery.easing.1.3.js', array('jquery'), '1.3.0', false);
			wp_enqueue_script('navbar', get_template_directory_uri() . '/js/navbar.js', array('jquery'), false, false);
			wp_enqueue_script('slides', get_template_directory_uri() . '/js/slides.min.jquery.js', array('jquery'), false, false);	
        }
} 		
		

function register_custom_menus() {
	register_nav_menus( array( 'top-menu' => __( 'Top Menu', 'typethemes' ), 'nav-menu' => __( 'Nav Menu', 'typethemes' ), 'footer-menu' => __( 'Footer Menu', 'typethemes' ) ) );
}

add_action( 'init', 'register_custom_menus' );

// Enqueue style-file
add_action('wp_print_styles', 'tt_styles');

function tt_styles() {
	$style = get_template_directory_uri().'/styles/default.css';
	if (get_option('tt_theme_style') != '') { $style = get_template_directory_uri().'/styles/'.get_option('tt_theme_style'); }

	wp_register_style('tt_style', $style);
	wp_enqueue_style( 'tt_style');
}

// Untitled Post
add_filter('the_title', 'tt_title');

function tt_title($title) {
    if ($title == '') {
        return '[Untitled Post]';
    } else {
        return $title;
    }
}

function tt_get_img( $post_id ) {

	$img_url;
	if ( get_option('tt_custom_image') ) { 
		$featured = get_option('tt_image_custom_key');
		$img_url = get_post_meta($post_id, $featured, true); 
	} else {
		$img_url = tt_first_img( $post_id );
	}
	
	if ( empty( $img_url ) ) { 
		$img_url = get_template_directory_uri().'/images/img_default.png';
	}
	
	return $img_url;
}

function tt_first_img( $post_id ) {
	$img_url;
	$images = get_children( 'post_parent='.$post_id.'&post_type=attachment&post_mime_type=image&numberposts=1&orderby=post_date&order=ASC' );	
	if ( !empty($images) ) { 
		foreach ( $images as $att_id => $image ) {            
			$src = wp_get_attachment_image_src($att_id, 'full');
			$img_url =  $src[0] ;
		}
	}
	if ( !empty( $img_url ) ) return $img_url;
}

function tt_get_post($amount,$echo=true) {
	global $post, $shortname;
	
	$postExcerpt = '';
	$postExcerpt = $post->post_excerpt;
	
	if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') { 
		if ($echo) echo $postExcerpt;
		else return $postExcerpt;	
	} else {
		$truncate = $post->post_content;
		
		$truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate);
		
		if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
		$truncate = apply_filters('the_content', $truncate);
		$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate);
		$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate);
		
		$truncate = strip_tags($truncate);
		
		if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
		else $truncate = substr($truncate, 0, $amount);

		if ($echo) echo $truncate,$echo_out;
		else return ($truncate . $echo_out);
	};
}

function tt_fill_category(){
	$cats = get_categories('type=post&hide_empty=0&orderby=name');  
	$tt_cats = array();
	$tt_cats[0] = array( "id" => '0', "name" => 'Select a category' );
	foreach ($cats as $cat ) { 		
		$tt_cats[] = array( "id" => $cat->term_id, 
							"name" => $cat->name ); };		
	return $tt_cats;
}

function tt_fill_dd_numbers($limit){
	$tt_no = array();
	for ($i = 1; $i <= $limit; $i++) { 
		$tt_no[] = array( "key" => $i, 
						  "val" => $i ); };		
	return $tt_no;
}

function tt_get_comment_by_category( $args = '' ) {
	global $wpdb;

	$defaults = array(
		'category_id' => '',
		'number' => '',
		'offset' => '',
		'status' => '',
	);

	$args = wp_parse_args( $args, $defaults );
	extract( $args, EXTR_SKIP );
	
	// $args can be whatever, only use the args defined in defaults to compute the key
	$key = md5( serialize( compact(array_keys($defaults)) )  );
	$last_changed = wp_cache_get('last_changed', 'comment');
	if ( !$last_changed ) {
		$last_changed = time();
		wp_cache_set('last_changed', $last_changed, 'comment');
	}
	$cache_key = "get_comments:$key:$last_changed";

	if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
		return $cache;
	}

	if ( 'hold' == $status )
		$approved = "A.comment_approved = '0'";
	elseif ( 'approve' == $status )
		$approved = "A.comment_approved = '1'";
	elseif ( 'spam' == $status )
		$approved = "A.comment_approved = 'spam'";
	elseif ( 'trash' == $status )
		$approved = "A.comment_approved = 'trash'";
	else
		$approved = "( A.comment_approved = '0' OR A.comment_approved = '1' )";	
		
	$order = 'DESC';
	
	if ( ! empty( $orderby ) ) {
		$ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
		$ordersby = array_intersect(
			$ordersby,
			array(
				'comment_agent',
				'comment_approved',
				'comment_author',
				'comment_author_email',
				'comment_author_IP',
				'comment_author_url',
				'comment_content',
				'comment_date',
				'comment_date_gmt',
				'comment_ID',
				'comment_karma',
				'comment_parent',
				'comment_post_ID',
				'comment_type',
				'user_id',
			)
		);
		$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
	} else {
		$orderby = 'comment_date_gmt';
	}
	
	$category_id = absint($category_id);
	$number		 = absint($number);
	$offset 	 = absint($offset);

		if ( !empty($number) ) {
		if ( $offset )
			$number = 'LIMIT ' . $offset . ',' . $number;
		else
			$number = 'LIMIT ' . $number;

	} else {
		$number = '';
	}
	
	$post_where = $wpdb->prepare( 'B.term_taxonomy_id = %d AND ', $category_id );
	
	$comments = $wpdb->get_results( 
		"SELECT A.* FROM $wpdb->comments A INNER JOIN  $wpdb->term_relationships B
			ON A.comment_post_ID = B.object_id
			WHERE $post_where $approved ORDER BY $orderby $order $number" );
			
	wp_cache_add( $cache_key, $comments, 'comment' );

	return $comments;
}

?>