﻿var currentNewsId = 0;

LoadArticle = function(Id)
{
	
	CloseArticle();
	Public.News.NewsArticle(Id, PopulateArticle);
}

CloseArticle = function()
{
	if (ArticleWindow != null)
	{
		ArticleWindow.style.display="none";
	}
}

OpenArticle = function()
{
	if (ArticleWindow != null)
	{
		ArticleWindow.style.display="block";
	}
}

sIFRArticle = function()
{
	if (typeof sIFR == "function")
	{
		sIFR.replaceElement("#ArticleContainer h2", named({sFlashSrc: "./flash/h.swf", sColor: "#A0A0A0", sBgColor: "#3D3D3D", sCase: "upper", sWmode: "opaque"}));
	}
}

PopulateArticle = function(array)
{ 
	var content = array.value;
	if (content != null && content.length > 0)
	{
		if (aDate != null && content[2] != null) { aDate.innerHTML = content[2]; }
		var Headline = document.createElement("h2");
		if (aContainer != null && aContainer.hasChildNodes()) { while(aContainer.firstChild) { aContainer.removeChild(aContainer.firstChild); } }
		if (content[1] != null && aContainer != null) { Headline.innerHTML = content[1]; aContainer.appendChild(Headline); }
		var Body = document.createElement("div");
		if (content[3] != null && aContainer != null) { Body.innerHTML = content[3]; aContainer.appendChild(Body); }
		CreateButtons(content[0]);
		OpenArticle();
		sIFRArticle();
	}
}

DisplayPrev = function(array)
{
	var hasPrevArticles = false;
	var Prev = document.createElement("a");
	if (array != null)
	{
		if (array.value != null)
		{	
			hasPrevArticles = true;		
			Prev.innerHTML = "<img src=\"images/btn_previous.gif\" onmouseover=\"this.src='images/btn_previous_o.gif'\" onmouseout=\"this.src='images/btn_previous.gif'\" />";
			Prev.href = "#";
			Prev.onclick = function()
			{
				Public.News.GetPreviousArticle(currentNewsId, PopulateArticle);
				return false;
			}
		}
	}
	
	if (hasPrevArticles != true)
	{
		Prev.innerHTML = "<img src=\"images/btn_prev_dis.gif\" onmouseover=\"this.style.cursor='default'\"/>";
	}

	var Loc = document.getElementById("ButtonPrev");
	
	if (Loc != null)
	{
		Loc.appendChild(Prev);
	}
}

DisplayNext = function(array)
{
	var hasNextArticles = false;
	var Next = document.createElement("a");
	if (array != null)
	{
		if (array.value != null)
		{
			hasNextArticles = true;
			Next.innerHTML = "<img src=\"images/btn_next.gif\" onmouseover=\"this.src='images/btn_next_o.gif'\" onmouseout=\"this.src='images/btn_next.gif'\" />";
			Next.href = "#";
			Next.onclick = function()
			{
				Public.News.GetNextArticle(currentNewsId, PopulateArticle);
				return false;
			}
		}
	}
	
	if (hasNextArticles != true)
	{
		Next.innerHTML = "<img src=\"images/btn_next_dis.gif\" onmouseover=\"this.style.cursor='default'\"/>";
	}

	var Loc = document.getElementById("ButtonNext");
	
	if (Loc != null)
	{
		Loc.appendChild(Next);
	}
}

CreateButtons = function(Id)
{
	if (!ClearButtons()) { return;}
	
	currentNewsId = Id;

	//see if there are any prev/next articles
	Public.News.GetPreviousArticle(Id, DisplayPrev);
	Public.News.GetNextArticle(Id, DisplayNext);

}

ClearButtons = function()
{
	var LocPrev = document.getElementById("ButtonPrev");
	var LocNext = document.getElementById("ButtonNext");
	
	if (LocPrev != null)
	{
		if (LocPrev.hasChildNodes()) { while(LocPrev.firstChild) { LocPrev.removeChild(LocPrev.firstChild); } };
	}
	
	if (LocNext != null)
	{
		if (LocNext.hasChildNodes()) { while(LocNext.firstChild) { LocNext.removeChild(LocNext.firstChild); } };
	}
	return true;
}