<?php

/*************************** File Directories ***************************/

define( "ghostpool_inc", TEMPLATEPATH . '/lib/inc/' );
define( "ghostpool_scripts", TEMPLATEPATH . '/lib/scripts/' );
define( "ghostpool_admin", TEMPLATEPATH . '/lib/admin/inc/' );


/*************************** Additional Functions ***************************/

// Main Theme Options
require_once(ghostpool_admin . 'theme-options.php');

// Meta Options
require_once(ghostpool_admin . 'theme-meta-options.php');

// Sidebars
require_once(ghostpool_admin . 'theme-sidebars.php');

// Shortcodes
require_once(ghostpool_admin . 'theme-shortcodes.php');

// Custom Post Types
require_once(ghostpool_admin . 'theme-post-types.php');

// TinyMCE
require_once (ghostpool_admin . 'tinymce/tinymce.php');

// Update Notification
require_once(ghostpool_admin . 'theme-update-notification.php');

// URL Shortener
require_once(ghostpool_scripts . 'simple-url-shortener.php');

// Like Posts
require_once(ghostpool_scripts . 'likethis.php');

// Languages
load_theme_textdomain('gp_lang', TEMPLATEPATH . '/languages');
$locale = get_locale();
$locale_file = TEMPLATEPATH."/languages/$locale.php";
if(is_readable($locale_file)) require_once($locale_file);


/*************************** Feed Links ***************************/

add_theme_support('automatic-feed-links');


/*************************** Navigation Menus ***************************/

add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(array(
'side-nav' => __('Side Navigation', 'gp_lang'),
'footer-nav' => __('Footer Navigation', 'gp_lang')
));
}

/*************************** Featured Image Sizes ***************************/

add_theme_support('post-thumbnails');


/*************************** Custom Background Support ***************************/

//add_custom_background();


/*************************** Excerpts ***************************/

// WordPress Excerpt Length
function new_excerpt_length($length) {return 500;}
add_filter('excerpt_length', 'new_excerpt_length');

function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}	
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}

// Replace Excerpt Ellipsis
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
remove_filter('the_excerpt', 'wpautop');


/*************************** Shortcode Support For Text Widget ***************************/

add_filter('widget_text', 'do_shortcode');


/*************************** Page Navigation ***************************/

function gp_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     
	 if (get_query_var('paged')) {
		 $paged = get_query_var('paged');
	 } elseif (get_query_var('page')) {
		 $paged = get_query_var('page');
	 } else {
		 $paged = 1;
	 }

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   
	
     if(1 != $pages)
     {
        echo "<div class='wp-pagenavi'>";
		echo '<span class="pages">'.__('Page', 'gp_lang').' '.$paged.' of '.$pages.'</span>';
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."' class='page-link'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."' class='page-link'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive page-link'>".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."' class='page-link'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."' class='page-link'>&raquo;</a>";
         echo "</div>\n";
     }
}


/*************************** Time Ago ***************************/

function time_ago($type='post') {
	$d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
	return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago', 'gp_lang');
}


/*************************** Shortcode Empty Paragraph Fix ***************************/

// Plugin URI: http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/
add_filter('the_content', 'shortcode_empty_paragraph_fix');
function shortcode_empty_paragraph_fix($content)
{   
	$array = array (
		'<p>[' => '[', 
		']</p>' => ']', 
		']<br />' => ']'
	);

	$content = strtr($content, $array);

	return $content;
}


/*************************** TimThumb WPMU Support ***************************/

function get_image_path ($post_id = null) {
	if ($post_id == null) {
		global $post;
		$post_id = $post->ID;
	}
	$theImageSrc = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
	global $blog_id;
	if (isset($blog_id) && $blog_id > 0) {
		$imageParts = explode('/files/', $theImageSrc[0]);
		if (isset($imageParts[1])) {
			$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
		}
	}
	return $theImageSrc;
}


/*************************** Password Protected Form ***************************/

add_filter( 'the_password_form', 'custom_password_form' );
function custom_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$form =  '<form class="protected-post-form" action="' . get_option('siteurl') . '/wp-pass.php" method="post">
' . _e('This page is password protected. Enter the password below to view this page.', 'gp_lang'). '<br/><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__('Submit', 'gp_lang') . '" />
</form>';
return $form;
}


/*************************** Redirect to Theme Options after Activation ***************************/

if (is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
add_action('admin_head','ct_option_setup');
header( 'Location: '.admin_url().'admin.php?page=theme-options.php' ) ;
}


/*************************** Thickbox Image Path Fix ***************************/

add_action('wp_footer', 'load_tb_fix');
function load_tb_fix() {
	echo '<script type="text/javascript">tb_pathToImage = "' . home_url() . '/wp-includes/js/thickbox/loadingAnimation.gif";
	tb_closeImage = "' . home_url() . '/wp-includes/js/thickbox/tb-close.png";</script>';
}


?>