/*======================================================================*\
|| #################################################################### ||
|| # PMNet Version 3.0												  #	||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2003-2008 John Slone. All Rights Reserved. 			  #	||
|| # This file may not be redistributed in whole or significant part. # ||
|| #  																  # ||
|| #################################################################### ||
\*======================================================================*/

/* index.js */

/* Variables */
var newshtml = {};	// Holds old news stuff, so if they hit 'Cancel', it just fills the html back in.

/* Contains javascript functions used on the main index page */

function showNewsComments(newsID)
{
	/*
	if ( !$("#articleComments"+newsID).hasClass('commentsloaded') )
	{
	*/
		$.ajax({
			data : { 'action' : 'newscomments', 'newsid' : newsID},
			type : 'get',
			success : function(data,status) { fillCommentBox(newsID,data,status) },
			url : '/ajax.php'
		});
		return false;
	/*
	}
	else
	{
		return false
	}
	*/
}

function fillCommentBox(newsID, data, status)
{
	$("#articleComments"+newsID).html(data);
	if ( $("#articleComments"+newsID).hasClass('nodisplay') )
	{
		$("#articleComments"+newsID).removeClass('nodisplay').addClass('commentsloaded');
	}
}

function makeNewsPageNav(obj,links_max)
{
	// obj contains the DIV in which we're going to place the links/stuff
	if ( parseInt(page_number) <= 1 )
	{
		page_number = 1;
	}
	min_below = Math.floor(links_max/2);
	if ( page_number <= min_below )
	{
		start = 1;
	}
	else
	{
		start = page_number-2;
	}
	if ( page_number >= (total_pages - 2) && (total_pages >= links_max) )
	{
		start = total_pages - 4;
	}
	
	obj.html('Jump to page: ');
	for ( i = start; (i < start + 5 && i<=total_pages); i++ )
	{
		if ( i == page_number)
		{
			obj.append("<strong><a href=\"?page="+i+"\" class=\"activepage\">"+i+"</a></strong>&nbsp;");
		}
		else
		{
			obj.append("<a href=\"?page="+i+"\">"+i+"</a>&nbsp;");
		}
			
	}
}
function showNewsPage(page_num)
{
	// Loads a page worth of news
	// Will need to rebuild the navigation index after it's over
	$("#newsentries").html('<img src="/templates/bigbluebar.gif" alt="Working..." />');
	location.hash = 'newsStart';
	$.ajax({
		data : {'action' : 'getnewspage', 'page' : page_num},
		url : '/ajax.php',
		type : 'get',
		success : function(data,status) { loadNewsPage(page_num,data); }
	});
	return false;
}
function loadNewsPage(page_num,data)
{
	// Replace news entries
	$("#newsentries").html($(data).find('newsbody').text());
	// Replace entry links
	$("#newsarticles").html($(data).find('newslinks').text());
	$("a[rel='facebox']").facebox({'opacity' : 0.9,'overlay':true});
	page_number = page_num;
	makeNewsPageNav($(".newspagenav"),5);
	
}
// Administrator news functions
function deleteNewsPost(postid)
{
	if ( !confirm("Do you really wish to delete this news article?\nThis cannot be undone!") )
	{
		return false;
	}
	$.ajax({
		data : {'action' : 'deletenewspost', 'newsid' : postid},
		url : '/ajax.php',
		type : 'get',
		success : function(d,s) { doDeleteNewsPost(d,s,postid) }
	});
	return false;
}
function doDeleteNewsPost(d,s,p)
{
	// Get rid of the box
	//$("#newsArticleBox"+p).remove();
	showNewsPage(page_number);
	
}

function truncateArticles()
{
	$(".newsArticleBox").each(function(i){
		if ( i > 0 )
		{
			if ( $(this).height() > 150 )
			{
				$(this).height(150).css('overflow','hidden');
				$(this).after('<a href="#" onclick="return expandArticle('+i+');">Expand Article</a>');
			}
		}
	});
}

function expandArticle(i)
{
	a = $(".newsArticleBox")[i];
	$(a).css('overflow','display').height('auto');
	$(a).next("a").remove();
	return false;
}

function matchBoxHeights()
{
	var na = $("#newsarticles div.specBoxContent").height();
	var fp = $("#forumpostbox div.specBoxContent").height();
	
	var newheight = Math.max(na,fp);
	$("#newsarticles div.specBoxContent").height(newheight);
	$("#forumpostbox div.specBoxContent").height(newheight);
}

/*======================================================================*\
|| ####################################################################
|| # File Revision:    $Revision: 1298 $
|| # Last Change:      $LastChangedDate: 2011-09-01 22:35:11 -0700 (Thu, 01 Sep 2011) $
|| # Last Commit By:   $Author: JDS $
|| ####################################################################
\*======================================================================*/
