<?php

/*
 * @package WordPreciousss
 * @subpackage Precious_Parent_Theme
*/


/*This filter let you change the 404 message*/
function precious_404_message() {
	if(is_search()) {
		$message = '<h2 id="pagetitle">'.__('Sorry!','precious') .'</h2>
		<p>'. __('Your search returned no results.','precious') .'</p>';
	} else {
		$message = '<h2 id="pagetitle">'.__('404 - Not Found','precious') .'</h2>
		<p>'. __('Whatever you were looking for can\'t be found','precious') .'</p>';
	}
	echo apply_filters('precious_404_message', $message);
}

/*This filter let you change the welcome message on you homepage - used in page-home.php*/
function precious_welcome_message() {
	$message = ''.__('Welcome to ','precious') . get_bloginfo('name') .'';

	echo apply_filters('precious_welcome_message', $message);
}

/*This filter let you change the doctype*/
function precious_doctype() {
	$doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';

	echo apply_filters('precious_doctype', $doctype);
}


/*This filter let you change the awaiting moderation message*/
function precious_moderation_message() {
	$message = ''.__('Your comment is awaiting moderation','precious').'';

	echo apply_filters('precious_moderation_message', $message);
}

function precious_footer_navigation() { 

	if(!is_page()) {?>
	<ul id="footernav">
		
	<?php if(is_single()) : ?>
		<?php next_post_link('<li class="next">%link &raquo;</li>'); ?>
		<?php previous_post_link('<li class="prev">&laquo; %link</li>'); ?>

	<?php elseif(is_home() || is_search() || is_archive()) : ?>
			<li class="prev"><?php next_posts_link(__('&laquo; Older Posts','precious')) ?></li>
			<li class="next"><?php previous_posts_link(__('Newer Posts &raquo;','precious')) ?></li>
	<?php endif; ?>
		
	</ul>

<?php }
}

function precious_rssicon($alttext) { 
	$rssicon = apply_filters('precious_rssicon', get_template_directory_uri() . '/gfx/rss.png');
	$alttext = __('RSS Feed','precious');

	echo "<img src='$rssicon' class='rssimg' alt='$alttext' />";

}

function precious_styles() {
	$print_css = apply_filters('precious_print_css', get_template_directory_uri() . '/css/print.css');
	$global_css = apply_filters('precious_global_css', get_template_directory_uri() . '/css/global.css');

?>
	<link type="text/css" href="<?php echo $global_css ?>" media="screen" rel="stylesheet" />
	<link type="text/css" href="<?php bloginfo('stylesheet_url'); ?>" media="screen" rel="stylesheet" />
<?php if(file_exists(STYLESHEETPATH . '/style-ie.css')) { ?>
	<!--[if lte IE 7]><link type="text/css" href="<?php echo get_bloginfo('stylesheet_directory'); ?>/style-ie.css" media="screen" rel="stylesheet" /><![endif]-->
<?php } ?>
	<link type="text/css" href="<?php echo $print_css ?>" media="print" rel="stylesheet" />
		<?php do_action('precious_styles'); ?>
<?php
}

/*Blix archive http://rmarsh.com/plugins/blix-archive/*/
function precious_archive($show_comment_count=false, $before='<h3>', $after='</h3>', $listclass='postspermonth') {
 	$result = false;
	// if the Plugin Output Cache is installed we can cheat...
	if (defined('POC_CACHE')) {
		$key = 'b_a'.$before.$after.$listclass;
		$cache = new POC_Cache();
		$cache->timer_start();
		$result = $cache->fetch($key);
		if ($result) $cache_time = sprintf('<!-- Compact Archive took %.3f seconds from the cache -->', $cache->timer_stop());
	}
	// ... otherwise we do it the hard way
	if (false === $result) {
		$result = get_precious_archive($show_comment_count, $before, $after, $listclass);
		if (defined('POC_CACHE')) $cache->store($key, $result);
	} 
	echo $result;
	if (defined('POC_CACHE')) echo $cache_time;
}

/********************************************************************************************************
	Stuff below this point is not meant to be used directly
*********************************************************************************************************/

function get_precious_archive($show_comment_count, $before, $after, $listclass) {
	global $month, $wpdb, $wp_version;
	$result = '';
	$below21 = version_compare($wp_version, '2.1','<');
	// WP 2.1 changed the way post_status and post_type fields work
	if ($below21) {
		$now        = current_time('mysql');
		$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_date <'" . $now . "' AND post_status='publish' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
	} else {
		$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_type='post' AND post_status='publish' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
	}
	if ($arcresults) {
		foreach ($arcresults as $arcresult) {
			$url  = get_month_link($arcresult->year, $arcresult->month);
    		$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
    		$result .= get_archives_link($url, $text, '', $before, $after);

			$thismonth   = zeroise($arcresult->month,2);
			$thisyear = $arcresult->year;
			if ($below21) {
				$arcresults2 = $wpdb->get_results("SELECT ID, post_date, post_title, comment_status FROM " . $wpdb->posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_status='publish' AND post_password='' ORDER BY post_date DESC");
			} else {
				$arcresults2 = $wpdb->get_results("SELECT ID, post_date, post_title, comment_status FROM " . $wpdb->posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC");
			}
        	if ($arcresults2) {
        		$result .= "<ul class=\"$listclass\">\n";
            	foreach ($arcresults2 as $arcresult2) {
               		if ($arcresult2->post_date != '0000-00-00 00:00:00') {
                 		$url       = get_permalink($arcresult2->ID);
                 		$arc_title = $arcresult2->post_title;

                 		if ($arc_title) $text = strip_tags($arc_title);
                    	else $text = $arcresult2->ID;

                   		$result .= "<li>".get_archives_link($url, $text, '');
						if ($show_comment_count) {
							$comments = mysql_query("SELECT comment_ID FROM " . $wpdb->comments . " WHERE comment_post_ID=" . $arcresult2->ID);
							$comments_count = mysql_num_rows($comments);
							if ($arcresult2->comment_status == "open" OR $comments_count > 0) $result .= '&nbsp;('.$comments_count.')';
						}
						$result .= "</li>\n";
                 	}
            	}
            	$result .= "</ul>\n";
        	}
		}
	}
	return $result;
}


?>
