<?php
/*
Template Name: NewsPulse
*/
?>

<?php get_header(); ?>

	<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
	<?php dt_breadcrumb(); ?>
	
	<div class="maincol">
	
	<!--
	#	header
	-->
	<div class="archiveheader">
		<h1><?php single_post_title(); ?></h1>
		<p><?php _e('Get the news filtered the way you want and see what\'s popular','news'); ?></p>
	</div>
	
	<div id="result"></div>

	</div>
	
	<!--
	#	filters and prefs
	-->
	<ul class="sidebar">
		<li class="widget">
			<h3><span class="title"><?php _e('General','news'); ?></span></h3>
			<div class="np-section">	
				<input type="text" id="posts_per_page" size="1" value="10" />
				<label for="posts_per_page"><?php _e('Number of news to display per page','news'); ?></label>
			</div>
			<div class="np-section">
				<select id="contenttype">
					<option value=""><?php _e('Show all news','news'); ?></option>
					<option value="showphotos"><?php _e('Photo galleries only','news'); ?></option>
					<option value="showvideos"><?php _e('Video stories only','news'); ?></option>
				</select>
				<label for="contenttype"><?php _e('News content type','news'); ?></label>
			</div>
			<div class="np-section">
				<select id="orderby">
					<option value=""><?php _e('Recently published','news'); ?></option>
					<option value="lastmodified"><?php _e('Last modified','news'); ?></option>
					<option value="alphabet"><?php _e('Alphabetically','news'); ?></option>
					<option value="mostcommented"><?php _e('Most commented','news'); ?></option>
					<option value="mostviewed"><?php _e('Most viewed','news'); ?></option>
					<option value="mostrated"><?php _e('Most rated','news'); ?></option>
					<option value="toprated"><?php _e('Top rated','news'); ?></option>
					<option value="leastcommented"><?php _e('Least commented','news'); ?></option>
					<option value="leastviewed"><?php _e('Least viewed','news'); ?></option>
					<option value="leastrated"><?php _e('Least rated','news'); ?></option>
					<option value="worstrated"><?php _e('Worst rated','news'); ?></option>
					<option value="older"><?php _e('Older posts first','news'); ?></option>
					<option value="random"><?php _e('Randomly','news'); ?></option>
				</select>
				<label for="orderby"><?php _e('Order results','news'); ?></label>
			</div>
			<div>
				<p style="text-align:right;padding:0;"><a href="#reset" id="resetSettings"><?php _e('Reset all filters','news'); ?></a></p>
			</div>
		</li>
		<li class="widget">
			<h3><span class="title"><?php _e('Date Filter','news'); ?></span></h3>
			<div>
				<select id="timespan">
					<option value=""><?php _e('Since beginning','news'); ?></option>
					<option value="today"><?php _e('Today','news'); ?></option>
					<option value="week1"><?php _e('1 week ago','news'); ?></option>
					<option value="week2"><?php _e('2 weeks ago','news'); ?></option>
					<option value="week3"><?php _e('3 weeks ago','news'); ?></option>
					<option value="day30"><?php _e('Last 30 days','news'); ?></option>
					<option value="day60"><?php _e('Last 60 days','news'); ?></option>
					<option value="day90"><?php _e('Last 90 days','news'); ?></option>
					<option value="year1"><?php _e('1 year ago','news'); ?></option>
					<option value="year2"><?php _e('2 years ago','news'); ?></option>
				</select>
				<label for="timespan"><?php _e('Show news from specific period','news'); ?></label>
			</div>
		</li>
		<li class="widget">
			<h3><span class="title"><?php _e('Categories','news'); ?></span></h3>
			<div class="np-section">
				<div class="checkbox-div"><input type="checkbox" name="show-all-cats" id="show-all-cats" class="show-all-cats" checked="checked"><label for="show-all-cats"><?php _e('Show all categories','news'); ?></label></div>
				<?php $categories = get_categories(); foreach($categories as $cat) { ?>
				<div class="checkbox-div"><input type="checkbox" name="<?php echo $cat->cat_ID; ?>" id="<?php echo $cat->cat_ID; ?>" class="cats" /><label for="<?php echo $cat->cat_ID; ?>"><?php echo $cat->cat_name; ?></label></div>
				<?php } ?>
				<div class="clear"></div>
			</div>
		</li>
		<li class="widget">
			<h3><span class="title"><?php _e('Tag Filtering','news'); ?></span></h3>
			<div class="np-section">
				<div class="checkbox-div"><input type="checkbox" name="show-all-tags" id="show-all-tags" class="show-all-tags" checked="checked"><label for="show-all-tags"><?php _e('Show all tags','news'); ?></label></div>
				<?php $tags = get_tags(); foreach($tags as $tag) { ?>
				<div class="checkbox-div"><input type="checkbox" name="<?php echo $tag->slug; ?>" id="<?php echo $tag->slug; ?>" class="tags" /><label for="<?php echo $tag->slug; ?>"><?php echo $tag->name; ?></label></div>
				<?php } ?>
				<div class="clear"></div>
			</div>
		</li>
	</ul>
	<div class="clear"></div>
	
	<?php endwhile; ?>
	<?php else : ?>
	<?php endif; ?>
	
<script type="text/javascript">
(function($) {

	jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("z-index","100000");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
	}
	
	// reset form (unset cookies)
	$("#resetSettings").click(function() {
		$("#posts_per_page").val(10);
		$("#contenttype, #orderby, #timespan").val('');
		$(".show-all-tags, .show-all-cats").attr('checked', true);
		$(".tags:checked, .cats:checked").attr('checked', false);
		$.cookie('np-paged', 1, { path: '/', expires: 100 });
		loadResults();
	});
	
	// load results conditions
	$("#posts_per_page, #contenttype, #orderby, #timespan").change(function(){
		loadResults();
	});

	// queryform + ajax loader
	/*
	* Retrieve cookies in form
	*/
	if ($.cookie('np-posts_per_page')) { $("#posts_per_page").val($.cookie('np-posts_per_page')); }
	if ($.cookie('np-contenttype')) { $("#contenttype").val($.cookie('np-contenttype')); }
	if ($.cookie('np-orderby')) { $("#orderby").val($.cookie('np-orderby')); }
	if ($.cookie('np-timespan')) { $("#timespan").val($.cookie('np-timespan')); }
	
	$(".tags, #show-all-tags, .cats, #show-all-cats").each(function() {
		if ($.cookie('np-' + $(this).attr('id')) == 'checked') {
			$(this).attr('checked', true);
		}
		if ($.cookie('np-' + $(this).attr('id')) == 'unchecked') {
			$(this).attr('checked', false);
		}
	});
	
	$('.show-all-tags').change(function(){ $('.tags').attr( 'checked', $(this).is( ':checked' ) ? '' : '' ); loadResults(); });
	$('.tags').change(function(){ $('.show-all-tags').attr( 'checked', $(this).is(':checked') ? '' : ''); loadResults(); });
	$('.show-all-cats').change(function(){ $('.cats').attr( 'checked', $(this).is( ':checked' ) ? '' : '' ); loadResults(); });
	$('.cats').change(function(){ $('.show-all-cats').attr( 'checked', $(this).is(':checked') ? '' : ''); loadResults(); });

	/*
	* This function is to add filters
	*/
	function loadResults(){

		var query = '';
		
		query += "posts_per_page=" + $("#posts_per_page").val();

		if ($("#contenttype").val() == 'showphotos') { query += "&meta_key=slideshow"; }
		if ($("#contenttype").val() == 'showvideos') { query += "&meta_key=video"; }

		if ($("#orderby").val() == 'lastmodified') { query+= "&orderby=modified&order=DESC"; }
		if ($("#orderby").val() == 'alphabet') { query += "&orderby=title&order=ASC"; }
		if ($("#orderby").val() == 'mostcommented') { query += "&orderby=comment_count&order=DESC"; }
		if ($("#orderby").val() == 'mostviewed') { query += "&v_sortby=views&v_orderby=desc"; }
		if ($("#orderby").val() == 'mostrated') { query += "&r_sortby=most_rated&r_orderby=desc"; }
		if ($("#orderby").val() == 'toprated') { query += "&r_sortby=highest_rated&r_orderby=desc"; }
		if ($("#orderby").val() == 'leastcommented') { query += "&orderby=comment_count&order=ASC"; }
		if ($("#orderby").val() == 'leastviewed') { query += "&v_sortby=views&v_orderby=asc"; }
		if ($("#orderby").val() == 'leastrated') { query += "&r_sortby=most_rated&r_orderby=asc"; }
		if ($("#orderby").val() == 'worstrated') { query += "&r_sortby=highest_rated&r_orderby=asc"; }
		if ($("#orderby").val() == 'older') { query += "&orderby=date&order=ASC"; }
		if ($("#orderby").val() == 'random') { query += "&orderby=rand"; }
		
		query += "&timespan=" + $("#timespan").val();
		
		if ($.cookie('np-paged')) { query += '&paged=' + $.cookie('np-paged'); } else { query += '&paged=1'; }
		
		/* Checkbox filtering system */
		function add_filters(showall, maincls, type) {
			if ($(showall).is(":checked") == false) {
			$(showall).next('label').css({'font-weight':'normal'}); // css
			$.cookie('np-' + $(showall).attr('id'), 'unchecked', { path: '/', expires: 100 });
			var val = [];
			$(maincls + ':checked').each(function(i){
			$(this).next('label').css({'font-weight':'bold'}); // css
			val[i] = $(this).attr('id');
			$.cookie('np-' + $(this).attr('id'), 'checked', { path: '/', expires: 100 });
			});
			$(maincls + ':not(:checked)').each(function(i){
			$(this).next('label').css({'font-weight':'normal'}); // css
			$.cookie('np-' + $(this).attr('id'), 'unchecked', { path: '/', expires: 100 });
			});
			query += "&" + type + "=" + val;
			} else {
			$(maincls).next('label').css({'font-weight':'normal'}); // css
			$(showall).next('label').css({'font-weight':'bold'}); // css
			$.cookie('np-' + $(showall).attr('id'), 'checked', { path: '/', expires: 100 });
			$(maincls + ':checked').each(function(i){
			$.cookie('np-' + $(this).attr('id'), 'checked', { path: '/', expires: 100 });
			});
			$(maincls + ':not(:checked)').each(function(i){
			$.cookie('np-' + $(this).attr('id'), 'unchecked', { path: '/', expires: 100 });
			});
			query += "&" + type + "=";
			}
		}

		add_filters('.show-all-tags', '.tags', 'tag');
		add_filters('.show-all-cats', '.cats', 'cat');
		/* end */
		
		/* store cookies (save prefs) */
		$.cookie('np-posts_per_page', $("#posts_per_page").val(), { path: '/', expires: 100 });
		$.cookie('np-contenttype', $("#contenttype").val(), { path: '/', expires: 100 });
		$.cookie('np-orderby', $("#orderby").val(), { path: '/', expires: 100 });
		$.cookie('np-timespan', $("#timespan").val(), { path: '/', expires: 100 });
		$.cookie('np-query', query, { path: '/', expires: 100 });
		
		// done. let us load the results
		$("body").append('<div id="modal-overlay"></div>');
		$("#modal-overlay").css("opacity", 0.5).show();
		$("body").append('<div class="loading"><div class="loading-inner"><img src="<?php bloginfo('template_directory'); ?>/images/loading.gif" alt="" /><?php _e('Loading results...','news'); ?></div></div>');
		$(".loading").center().fadeTo(1, 1);
		$("#result").load('<?php bloginfo('template_directory'); ?>/GetFilteredNews.php?' + $.cookie('np-query'), function(){
			// Success OK!
			$("#modal-overlay, .loading").hide(); // hide modal
			$.cookie('np-paged', 1, { path: '/', expires: 100 }); // reset paged
		});
	
	}
	loadResults(); // force change

}) (jQuery);
</script>

<?php get_footer(); ?>