/* this script will receive the javascript output from society_news.php and displays it to the right of 'Society News:' */

/* Still to be done: 

1) add three dots to end of string if too long
2) make sure certain characters dont mess it up i.e. " ' "
3) mark item as old in MySQL database if older than six months or has already happened i.e. an expedition.

*/

var timer; 	/* these four variables are for the setTimeout functions */
var timer2;
var timer3;
var timer4;
var timer5;

var news = ""; /* stores the news text */

var arraylength = societynews.length; /* these three functions are used in the displaynews function */
var rw = arraylength - 1;
var check = 0; 
var rw_store;

var pp_check = 1; /* used in pause/play function */

var pauser = 0; /* used to indicate if pause has been implemented */

var u_count; /* used in underscore_trick function */

var al_count; /* used in all_letter function */

var wul = 0; /* used in white_underline function */

var bc = 0; /* used in button_click function */

/* ---------- */

function pause_play()		/* pauses / plays the news when the mouse is moved over / out the link */
{
	if(typeof dothisnow == 'function') 
	{ 
		dothisnow(); 
	}
	
	if (bc==0)				/* doesn't execute if button has beeen clicked */
	{
	
	pp = document.getElementById("pause_play");
	
	if (pp_check == 0)
	{
	pp.src = "images/play.png";
	pp_check = 1;
	pause_display();
	}
	else
	{
	pp.src = "images/pause.png";
	pp_check = 0;
	displaynews();
	}
	
	}
}

/* ---------- */

function white_underline()			/* gives custom hyperlink underlining for the news item */
{
	var news_div = document.getElementById("news_displayer");
	
	if (wul == 0)
	{
		Core.removeClass(news_div, "news_displayer");
		Core.addClass(news_div, "remove_news_displayer");
		wul = 1;
	}
	else
	{
		Core.removeClass(news_div, "remove_news_displayer");
		Core.addClass(news_div, "news_displayer");
		wul = 0;
	}
	
}

/* ---------- */

function fade_loop()
{
	MM_effectAppearFade('news_displayer', 1000, 100, 0, false);
	timer5 = setTimeout('displaynews()', 1000);
}

/* ---------- */

function underscore_trick()		/* creates the flashing uncerscore at the start of the animation */
{
	var trick = " _ _";
	var value;
	
	if (u_count < trick.length) 
	{
	value = trick.charAt(u_count);
	MM_effectAppearFade('news_displayer', 0, 0, 100, false);
	display.firstChild.data = value;
	u_count = u_count + 1;
	timer2 = setTimeout('underscore_trick()', 375);
	}
	else
	{
	al_count = 0;
	add_letters();
	}
	
}

/* ---------- */

function add_letters()		/* lays down the news item letter by letter */
{
	
	var store;
	
	if (al_count == 0)
	{
	text = "";
	}
	
	if (al_count < news.length) 
	{
	text += news.charAt(al_count);
	display.firstChild.data = text;
	al_count = al_count + 1;
	timer3 = setTimeout('add_letters()', 80);
	}
	else
	{
	display.firstChild.data = text;	
	text = "";
	
	rw = rw - 1; 		/* Remainder of function moves on to the next news item */     
	
	if (rw == -1)     
	{
	rw=arraylength-1;
	}
	
	timer = setTimeout('fade_loop()', 1250); /* infinite loop so news items keep displaying the whole time the page is loaded */
	}
	
}

/* ---------- */

function displaynews() /* rotates the news items & their links in the display */
{
	
	display = document.getElementById("display");
	
	news = societynews[rw][0];		   /* sets news text */ 
	display.href = societynews[rw][1]; /* link */
	
	if (pauser == 0)
	{
	rw_store = rw;
	u_count = 0;
	underscore_trick(); /* initiates the animation */
	}
	else
	{
	pauser = 0;
	rw = rw_store-1;
	
	if (rw == -1)     
	{
	rw=arraylength-1;
	}
	
	timer4 = setTimeout('fade_loop()', 1250);
	}	 
 
}

/* ---------- */

function pause_display() /* stops (not pauses) the timer */
{
clearTimeout(timer);
clearTimeout(timer2);
clearTimeout(timer3);
clearTimeout(timer4);
clearTimeout(timer5);

display.firstChild.data = news;
MM_effectAppearFade('news_displayer', 0, 0, 100, false);

pauser = 1;
}

/* ---------- */



function button_click()			/* Remaining three functions deal with user buttons */
{
	
	if (bc == 0)
	{
	bc = 1;
	pp.src = "images/play.png";
	pp.title = "Play";
	pause_display();
	}
	else
	{
	bc = 0;
	pp.src = "images/pause.png";
	pp.title = "Pause";
	displaynews();
	}
		
}

/* ---------- */

function back()
{
if (bc == 0)		/* if news not already paused: pause */
{
bc = 1;
pp.src = "images/play.png";
pp.title = "Play";
pause_display();
}

rw = rw + 1;

if (rw == arraylength)     
	{
	rw= 0;
	}

rw_store = rw;

news = societynews[rw][0];
display.href = societynews[rw][1];
display.firstChild.data = news;

}

/* ---------- */

function forward()
{

if (bc == 0)		/* if news not already paused: pause */
{
bc = 1;
pp.src = "images/play.png";
pp.title = "Play";
pause_display();
}

rw = rw - 1;

if (rw == -1)     
	{
	rw= arraylength-1;
	}

rw_store = rw;

news = societynews[rw][0];
display.href = societynews[rw][1];
display.firstChild.data = news;
}
