<?php

/***************************************************************
@
@	The hooks file should contain all template hooks 
@	actions and filters, etc.
@
/**************************************************************/

/***************************************************************
@
@	Preloading required contents
@
/**************************************************************/

add_action('wp_head', 'pre_headers', 0);
function pre_headers() {
global $news;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html <?php language_attributes(); ?>>
<head>

<meta name="description" content="<?php echo $news['description']; ?>" />
<meta name="keywords" content="<?php echo $news['keywords']; ?>" />
<meta charset="<?php bloginfo('charset'); ?>" />
<title><?php dt_pagetitle(); ?></title>

<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/styles/<?php echo $news['style']; ?>" type="text/css" />
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/js/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<?php dt_favicon(); ?>

<?php
wp_deregister_script( 'jquery' ); // unregistered key jQuery
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2'); // register key jQuery with URL of Google CDN
wp_enqueue_script( 'jquery' ); // include jQuery
if ( is_singular() ) wp_enqueue_script('comment-reply');
?>

<?php
}
add_action('wp_head', 'headers');
function headers() {
global $news;
?>

<script type="text/javascript" src="http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.equal.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.corner.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.cookie.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/fancybox/jquery.easing-1.4.pack.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>

<style type="text/css">
body {
	<?php dt_load_bgcolor($news['bgcolors'][0]); ?>
}

body a {
	<?php dt_load_color($news['colors'][0]); ?>
}
</style>

<?php dt_headers(); ?>
<?php dt_custombg(); ?>

<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/user_preferences/custom.css" type="text/css" media="screen" />

</head>
<body>

<?php
}

/***************************************************************
@
@	Footer
@
/**************************************************************/
add_action('wp_footer', 'footers');
function footers() {

global $news, $query_string;
query_posts($query_string.'&posts_per_page=-1');
global $wp_query;
$allposts = $wp_query->post_count;
wp_reset_query();
?>

<script type="text/javascript">
(function($) {

	// overlay fade
	$(".overlay, .layeroverlay").fadeTo(1, 0.70);
	
	// click to zoom button
	$(".zoom-overlay, .zoom-black").css({display: "none"});
	$(".fancybox-zoom").hover(function(){
		$(this).children("a").children(".zoom-black").show(1).fadeTo(1, 0.40);
		$(this).children("a").children(".zoom-overlay").fadeIn(500);
	},function(){
		$(this).children("a").children(".zoom-black, .zoom-overlay").hide(1);
	});
	$(".fancybox-zoom").click(function(){
		$(".zoom-black:visible, .zoom-overlay:visible").hide(1);
	});
	
	// navigation menus
	$("#menu ul").css({display: "none"}); // Opera Fix
	$("#linklist ul").css({display: "none"}); // Opera Fix
	//$("#menu > li:has(ul)").append('<span class="arrow"></span>');
	$("#menu ul > li:has(ul)").append('<span class="arrow2"></span>');
	$("#linklist > li:has(ul)").append('<span class="arrow3"></span>');

	$("#menu > li:has(ul)").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).<?php echo $news['drop1effect']; ?>(<?php echo $news['drop1speed']; ?>);
		$(this).addClass('active-tab');
		$(this).append('<span class="arrow"></span>');
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		$(this).removeClass('active-tab');
		$(".arrow").remove();
	});

	$("#menu ul > li:has(ul)").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).<?php echo $news['drop1effect']; ?>(<?php echo $news['drop1speed']; ?>);
		$(this).addClass('active-tab-child');
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		$(this).removeClass('active-tab-child');
	});

	$("#linklist > li:has(ul)").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).<?php echo $news['drop2effect']; ?>(<?php echo $news['drop2speed']; ?>);
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
	});

	// clear forms on focus
	$('.clearfield').each(function() {
		var default_value = this.value;
		$(this).css('color', '#555'); // this could be in the style sheet instead
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});

	// same height calculations
	$(".slidingposts li div.title").equalHeights();
	$(".slidingposts li div.excerpt").equalHeights();
	$(".wea-column").equalHeights();
	$(".story p").equalHeights();
	$(".story .thumbntitle").equalHeights();
	$(".bottom-cat .equal").equalHeights();
	$(".bottom-cat p").equalHeights();

	// rounded corners calculations
	$(".toptabs a").corner("top 3px");
	$(".tabbox ol").corner("bottom 5px");
	$(".morevideo a, .sliding, .next, .prev, .all, .widget_tag_cloud a, .sidebar li:parent, .rounded-regular, .rounded-orange, .rounded-black, #footer").corner("5px");
	$("#reset").corner("10px");
	$(".post .taglist li a").corner("10px");

	// force css modifications and fixes
	$(".tabbox ol li:last-child").css({'border-bottom':'0'});
	$(".removeRightBorder").css({'border-right':'0'});
	$(".widget_tag_cloud a, .next, .prev, .all, .morevideo a, .rounded-regular").hover(
		function(){
		$(this).fadeTo(1, 0).css({"background": "#ff3000", "color": "#fff", "text-decoration": "none"}).fadeTo(300, 1);
		},function(){
		$(this).css({"background": "#fff", "color":"#15527e"});
		}
	);
	
	// plus/minus on main page
	$('div.lisummary').css({'display':'none'});
	$('.item-more').toggle(
		function() {
		$(this).css({'background-position' : 'bottom'});
		$(this).parent().children('div').show();
		},
		function() {
		$(this).css({'background-position' : 'top'});
		$(this).parent().children('div').hide();
		}
	);

	// hot news slideshow
	$("#hotnewshide").tabs("#hotnews ul li", {
		effect: "<?php echo $news['hotnewseffect']; ?>",
		<?php if (!empty($news['hotnewsfadein'])) { ?>
		fadeInSpeed: <?php echo $news['hotnewsfadein']; ?>,
		<?php } ?>
		<?php if (!empty($news['hotnewsfadeout'])) { ?>
		fadeOutSpeed: <?php echo $news['hotnewsfadeout']; ?>,
		<?php } ?>
		rotate: true
	}).slideshow({autoplay: true, interval: <?php echo $news['hotnewsinterval']; ?>, clickable: false});
	
	// main slideshow
	$(".slideposts").tabs(".item", {
		event: "<?php echo $news['fs_event']; ?>",
		effect: "<?php echo $news['fs_effect']; ?>",
		fadeInSpeed: <?php echo $news['fs_fadein']; ?>,
		fadeOutSpeed: <?php echo $news['fs_fadeout']; ?>,
		rotate: <?php echo $news['fs_rotate']; ?>
	}).slideshow({autoplay: <?php echo $news['fs_autoplay']; ?>, interval: <?php echo $news['fs_interval']; ?>, clickable: false});
	
	// sidebar gallery
	$(".gallery-items").tabs(".gallery a", {
		effect: "slide",
		rotate: true
	}).slideshow({autoplay: false, clickable: false, next: '.nextgal', prev: '.prevgal'});
	
	// main tabs
	$(".toptabs ul").tabs(".tabbox", {
		event: "click",
		effect: "default",
		rotate: true
	}).slideshow({autoplay: false, clickable: false});
	
	// scrollable tabs
	$("ul.slidingtabs").tabs("div.slidingcontent", {
		event: "click",
		effect: "default",
		rotate: true
	}).slideshow({autoplay: false, clickable: false});
	
	// initialize scrollables
	$(".scrollable").scrollable({
		speed: 800
	});
	
	// zooming and images
	$("a.zoom").fancybox({
		centerOnScroll: true,
		hideOnContentClick: true
	});
	
	$("a.zoomgallery").fancybox({
		centerOnScroll: true,
		hideOnContentClick: false,
		titlePosition: 'over',
		cyclic: true
	});
	
	// control threaded replies
	$("ol.commentlist li ol.children").css({'display':'none'});
	$("ol.commentlist li ol.children").each(function(){
		var parentCommentID = $(this).parent().attr('id');
		var num = $(this).children('li').length;
		if (num > 0) {
			$(this).parent().children('div').children('div.reply').append('<span class="threads"><a href="#viewreplies" class="viewreplies">View Replies (' + num + ')</a></span>');
			if ($.cookie(parentCommentID) == 'showthread') {
				$(this).toggle();
			}
		}
	});
	
	// replies - threads
	$(".viewreplies").live('click', function(){
		$(this).parent().parent().parent().parent().children('ol.children').toggle();
		var childs = $(this).parent().parent().parent().parent().children('ol.children');
		var parentCommentID = $(this).parent().parent().parent().parent().attr('id');
		if (childs.is(':visible')) {
			// register visible cookie
			$.cookie(parentCommentID, 'showthread', { path: '/', expires: 100 });
		} else {
			$.cookie(parentCommentID, 'hidethread', { path: '/', expires: 100 });
		}
	});

}) (jQuery);
</script>

<?php if (is_archive() || is_search()) { // we want to enable this JS for archives only ?>
<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;
}

	// toggle archive options
	if (!$.cookie('n_view')) {
		$.cookie('n_view', 'standard', { path: '/', expires: 100 });
	}
	
	$(".headers").click(function(){
		$.cookie('n_view', 'headers', { path: '/', expires: 100 });
		$(".QueryElement").trigger('change');
	});
	
	$(".standard").click(function(){
		$.cookie('n_view', 'standard', { path: '/', expires: 100 });
		$(".QueryElement").trigger('change');
	});
	
	$(".textonly").click(function(){
		$.cookie('n_view', 'textonly', { path: '/', expires: 100 });
		$(".QueryElement").trigger('change');
	});
	
	// list or grid?
	if ($.cookie('n_style')) {
		if ($.cookie('n_style') == 'list') {
			$(".view-list").css({'cursor' : 'default'}).fadeTo(1, 0.30);
			$(".view-grid").css({'cursor' : 'pointer'}).fadeTo(1,1);
		}
		if ($.cookie('n_style') == 'grid') {
			$(".view-grid").css({'cursor' : 'default'}).fadeTo(1, 0.30);
			$(".view-list").css({'cursor' : 'pointer'}).fadeTo(1,1);
		}
	} else {
		$.cookie('n_style', 'list', { path: '/', expires: 100 });
		$(".view-list").css({'cursor' : 'default'}).fadeTo(1, 0.30);
	}
	
	$(".view-list").click(function() {
		$.cookie('n_style', 'list', { path: '/', expires: 100 });
		$(this).css({'cursor' : 'default'}).fadeTo(1, 0.30);
		$(".view-grid").css({'cursor' : 'pointer'}).fadeTo(1,1);
		$(".QueryElement").trigger('change');
	});
	
	$(".view-grid").click(function() {
		$.cookie('n_style', 'grid', { path: '/', expires: 100 });
		$(this).css({'cursor' : 'default'}).fadeTo(1, 0.30);
		$(".view-list").css({'cursor' : 'pointer'}).fadeTo(1,1);
		$(".QueryElement").trigger('change');
	});
	
	// load more
	$(".loadmore").click(function(){
	
	var allposts = <?php echo $allposts; ?>;
	var posts_visible = <?php echo $news['num_per_page']; ?>;
	var posts_more = <?php echo $news['num_per_more']; ?>;
	var loadedposts = posts_visible + (posts_more * $.cookie('n_more'));
	if ( loadedposts < allposts) {
	
		if ($.cookie('n_more')) {
			count = $.cookie('n_more');
			count++;
			$.cookie('n_more', count, { path: '/', expires: 100 });
		} else {
			$.cookie('n_more', 1, { path: '/', expires: 100 });
		}
		$.cookie('is_loaded', 1, { path: '/', expires: 100 }); // we need to make it aware that the user clicked to scroll down to new posts
		$(".QueryElement").trigger('change'); // load more posts
	
	} else {
	
	alert('<?php _e('There are no more news to display in this archive.','news'); ?>');
	return false;
	
	}
	
	});
	
	// reset form (unset cookies)
	$("#reset").click(function() {
		$.cookie('n_more', 1, { path: '/', expires: -1 }); // remove more posts
		$.cookie('n_view', 1, { path: '/', expires: -1 }); // remove cookie for view
		$.cookie('n_style', 1, { path: '/', expires: -1 }); // remove cookie for style
		$.cookie('is_loaded', 1, { path: '/', expires: -1 }); // destory the click cookie
		$("#orderby").val('recent'); // reset order
		$(".view-list").css({'cursor' : 'default'}).fadeTo(1, 0.30); // reset list
		$(".view-grid").css({'cursor' : 'pointer'}).fadeTo(1,1); // avoid grid
		$(".QueryElement").trigger('change'); // trigger change
	});
	
	// queryform + ajax loader
	/*
	* Retrieve cookies in form
	*/
	if ($.cookie('nw-' + 'orderby')) { $("#orderby").val($.cookie('nw-' + 'orderby')); }
	/*
	* This function is to add filters
	*/
	$(".QueryElement").change(function() {

		var query = '<?php echo $query_string; ?>';
		query += "&posts_per_page=<?php echo $news['num_per_page']; ?>";
		
		if ($("#orderby").val() == 'recent') { /*query += "&orderby=date&order=DESC";*/ }
		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"; }
		
		if ($.cookie('n_view')) { query += "&n_view=" + $.cookie('n_view'); }
		if ($.cookie('n_style')) { query += "&n_style=" + $.cookie('n_style'); }
		if ($.cookie('n_more')) { query += "&n_more=" + $.cookie('n_more'); }

		$.cookie('nw-orderby', $("#orderby").val(), { path: '/', expires: 100 });
		$.cookie('nw-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'); ?>/GetResults.php?' + $.cookie('nw-query'), function(){
			// Success OK!
			$("#modal-overlay, .loading").hide();
			if ($.cookie('is_loaded') == 1) {
				var posts_visible = <?php echo $news['num_per_page']; ?>;
				var posts_more = <?php echo $news['num_per_more']; ?>;
				var loadedposts = posts_visible + (posts_more * $.cookie('n_more'));
				var thispost = $("#post-" + loadedposts).position();
				$('html,body').animate({scrollTop: thispost.top},1000);
			}
			$.cookie('is_loaded', 1, { path: '/', expires: -1 }); // destory the click cookie
		});
	
	});
	$(".QueryElement").trigger('change'); // force change

}) (jQuery);
</script>
<?php } ?>

<?php dt_footers(); ?>

</body>
</html>

<?php
}

/***************************************************************
@
@	Hooks and hooks only
@
/**************************************************************/
function add_author_box($content) {

	// make globals and reset query first
	wp_reset_query();

	$display = new WP_query();
	$display->query(array('showposts' => 5, 'author' => get_the_author_meta('ID'), 'orderby' => 'rand' ));
	global $wp_query; $wp_query->in_the_loop = true;
	if ($display->have_posts()) :
	
	// display (if results)
	$author_box = '';
	$author_box .= '<div class="post-author">';
	$author_box .= '<div class="post-author-box">';
	$author_box .= '<div class="post-author-img">'.get_avatar(get_the_author_meta('user_email'), $size = '32').'</div>';
	$author_box .= '<div class="post-author-info"><span class="edited">'.__('Edited by','news').'</span><span class="editor"><a href="'. get_author_link(false, get_the_author_meta('ID')).'">'.get_the_author_meta('display_name').'</a></span></div><div class="clear"></div>';
	$author_box .= '</div>';
	$author_box .= '<ul>';
	$author_box .= '<li class="read-also">'.__('Also Wrote','news').'</li>';
	while ($display->have_posts()) : $display->the_post();
	$author_box .= '<li><a href="'.get_permalink($post->ID).'">'.dt_get_posttype().get_the_title().'</a></li>';
	endwhile;
	wp_reset_query();
	$author_box .= '</ul>';
	$author_box .= '</div>';
	$content = $author_box.$content;
	return $content;
	
	endif;

}

function poststats($content) {
?>
			<p class="before_story"><?php dt_photos_number(); ?><?php printf(__('By <span style="text-transform:uppercase;">%1$s</span> - %2$s','news'), get_the_author_meta('display_name'), get_the_time('D M d, g:i a')); ?></p>

			<ul class="post_stats">
				<li class="commentpop"><?php comments_popup_link(__('<span>0</span> Comments','news'), __('<span>1</span> Comment','news'), __('<span>%</span> Comments','news')); ?></li>
				<li class="viewnum"><?php dt_views_number(__('0 views','news'), __('1 view','news'), __('% views','news')); ?></li>
				<?php dt_facebooklike('before=<li>&after=</li>'); dt_tweetbutton('before=<li>&after=</li>'); dt_email('before=<li class="emailit">&after=</li>'); dt_print('before=<li class="printit">&after=</li>'); ?>
			</ul><div class="clear"></div>
			<div class="niceline"></div>
<?php
	return $content;
}
function poststats2() {
?>
			<div class="niceline"></div>
			<ul class="post_stats stats2">
				<li class="commentpop"><?php comments_popup_link(__('<span>0</span> Comments','news'), __('<span>1</span> Comment','news'), __('<span>%</span> Comments','news')); ?></li>
				<li class="votepop"><?php the_ratings(); ?></li>
				<li class="viewnum"><?php dt_views_number(__('0 views','news'), __('1 view','news'), __('% views','news')); ?></li>
				<?php dt_facebooklike('before=<li>&after=</li>'); dt_tweetbutton('before=<li>&after=</li>'); dt_email('before=<li class="emailit">&after=</li>'); dt_print('before=<li class="printit">&after=</li>'); ?>
			</ul><div class="clear"></div>
			<div class="niceline"></div>
<?php
}

/* add twitter, facebook, rss after post */
function custom_story($content) {
	
	global $post, $news;
	
	// avoid duplicate links by excluding photo galleries
	if (!get_post_meta($post->ID, 'slideshow')) {
	$content .= wp_link_pages(array('before' => '<p class="link-pages">'.__('Pages:','news'), 'echo' => 0));
	}
	
	if (is_single() && $news['twitter'] && $news['facebook'] && $news['feedburner']) {
	$content .= '<p class="after_story">'.sprintf(__('Follow %1$s on <a href="http://www.twitter.com/%2$s" class="post-tw">Twitter</a>, become a fan on <a href="%3$s" class="post-fb">Facebook</a>. Stay updated via <a href="http://feeds.feedburner.com/%4$s" class="post-rs">RSS</a>','news'), get_bloginfo('name'), $news['twitter'], $news['facebook'], $news['feedburner']).'</p>';
	}
	
	return $content;

}

/* accurate comments count - exclusive */
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
	if ( ! is_admin() ) {
		global $id;
		$comments_by_type = &separate_comments(get_comments('status=approve&parent=true&post_id=' . $id));
		return count($comments_by_type['comment']);
	} else {
		return $count;
	}
}

/* add special stuff to comments */
function deluxe_comment() {
	do_action('deluxe_comment');
}

?>