
var ServerDtTi = 0;


dsiOrgAnnouncements = function()	// Class declaration
{
	this.Announcements = [];
	
	// Adds an announcement to the working array
	this.add = function(RDtTi, NItm, NPDF)
	{ 
		this.Announcements[this.Announcements.length] = {ReleaseDtTi: RDtTi, News: NItm, PDF: NPDF};
	} // end of Method: Add
	

	this.Announce = function()
	{
		var _Today = new Date();
		var DT =[];
		var MDY=[];
		var HM=[];
		var _Release;
		
		// Iterate through array and fetch all announcements
		for (var n=0; n < this.Announcements.length; n++)
		{
			// DateTime of zero means "display now"
			if (this.Announcements[n].ReleaseDtTi == "0") 
			{
				this.WriteToDOMtable(n);
			}
			else
			{
				// Take the DateTime and break it up into it's elements. 
				// Then Create a new Date object to be used for comparing to date time of now
				DT = this.Announcements[n].ReleaseDtTi.split(" ");
				MDY = DT[0].split("/");
				HM = DT[1].split(":");

//				_Release = new Date(parseInt(MDY[2]), parseInt(MDY[0]), parseInt(MDY[1]), parseInt(HM[0]), parseInt(HM[1]), 0);
				_Release = new Date(this.Announcements[n].ReleaseDtTi);

				if (_Release <= ServerDtTi)		// ServerDtTi is defined in the JSP file (releases.htm) to alleviate TimeZone differences.
				{
					this.WriteToDOMtable(n);
				}
				
				// destroy the release object so it can be rebuilt during next iteration
				_Release = null;
				
			}
		}
	} // end of Method: Announce

	// Writes the announcement to the HTML table using DOM
	// This method allows the writting to take place after the document is loaded
	// which ensures the data file is available
	this.WriteToDOMtable = function(n)
	{

		// *** Method 1 ***
		// Following Method works for Mozilla, but _NOT_ for IE	
		{
		//	this.InsertElement("tr", "tr" + n, "bluetable", "");
		//	this.InsertElement("td", "newsItem", "tr" + n, this.Announcements[n].News + " <a href='pdfs/" + this.Announcements[n].PDF + "' target='_blank'><nobr>view full story</nobr></a>")
		//	this.InsertElement("tr", "trNS" + n, "bluetable","");
		//	this.InsertElement("td", "", "trNS" + n, "<div id='newsSep'>&nbsp;</div>");
		}
		
		// *** Method 2 ***
		// Following Method works for Mozilla, but _NOT_ for IE	
		{
		//	var _TBL = document.getElementById("bluetable");
		//	_TBL.innerHTML += "<tr><td valign='middle' id='newsItem'>" + this.Announcements[n].News + " <a href='pdfs/" + this.Announcements[n].PDF + "' target='_blank'><nobr>view full story</nobr></a></td></tr><tr><td><div id='newsSep'>&nbsp;</div></td></tr>";
		}

		// *** Method 3 ***
		// Following Method works for ALL	
		{		
			var _TBL = document.getElementById("bluetable");
			var row, cell;

			row = _TBL.insertRow(_TBL.rows.length-1);
			
			cell = row.insertCell(0);
			cell.setAttribute("id", "newsItem");
			cell.style.verticalAlign = "middle";			
			
			cellContent = "<div style='margin-right: 20px;'>" + this.Announcements[n].News;
			if(typeof(this.Announcements[n].PDF) != "undefined") {
				if (this.Announcements[n].PDF.trim().length > 0) cellContent += " <a href='pdfs/" + this.Announcements[n].PDF + "' target='_blank'><nobr>view full story</nobr></a>";
			}
			cellContent += 	"</div>";
			
			cell.innerHTML = cellContent;
			row = _TBL.insertRow(_TBL.rows.length-1);
			cell = row.insertCell(0);
			cell.innerHTML = "<div id='newsSep'>&nbsp;</div>";
		}
	
	} // end of Method: WriteToDOMTable
	
	// Dynamic include of files. When using Dynamic includes, make sure that logic executes
	// in the OnLoad event. This guarantees that all files are available before dependant 
	// logic executes
	this.IncludeJS = function(FName)
	{
		var H = document.getElementsByTagName("head")[0];
		var IncludeFile = document.createElement("script");
		IncludeFile.type = "text/javascript";
		IncludeFile.src = FName;
		H.appendChild(IncludeFile);
	} // end of Method: IncludeJS
	
	// Creates an element and assigns it to a parent (Used for Method 1 only)
	this.InsertElement = function(NEle, NEID, PEleID, NContent)
	{
		var PE = document.getElementById(PEleID);
		var NE = document.createElement(NEle);
		NE.innerHTML = NContent;
		NE.id = NEID;
		PE.appendChild(NE);
		
	} // end of Method: InsertElement
	
	// Unintrusive way to append a function to the ONLOAD without overwritting
	// any other ONLOAD functions already in place.
	this.addLoadEvent = function (obj, evType, fn)
	{ 
		if (obj.addEventListener)
		{ 
			// Mozilla
			obj.addEventListener(evType, fn, false); 

		} 
		else if (obj.attachEvent)
		{ 
			// IE
			var r = obj.attachEvent("on"+evType, fn); 
		
		} 
		else 
		{ 
			// written by Simon Willison. http://simonwillison.net/
			var oldonload = window.onload;

			if (typeof window.onload != 'function')
			{
				window.onload = func;
			}
			else
			{
				window.onload = function()
				{
					if (oldonload)
					{
						oldonload(); 
					}
					func();
				}
			} 
	
		} 
	} // end of Method: addLoadEvent
	

}  // End of Class Declararion




// AJAX call
function getXmlHttpObject(url, cfunc)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange = cfunc;
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}


// Gets the server date, loads the data and displays the announcements
function getDate()
{
	getXmlHttpObject("/news/ServerDate.jsp", function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			ServerDtTi = new Date(xmlhttp.responseText);
			LoadData(); // loads the object with the announcements.
			OrgNews.Announce(); // Displays the announcements
		}
	});
}



// Prototype the Trim function into the String class for use.
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


// ***** START HERE *****
// Create the announcements object	
var OrgNews = new dsiOrgAnnouncements();
// Imports the announcements file
OrgNews.IncludeJS("OrgAnnouncementData.js");
// Runs all the code after all files are loaded.
OrgNews.addLoadEvent(window, "load", getDate);



