// JavaScript Document
function newsItem(category,id,title,url,date,img,intro,content,supplier)
{
// variables
	this.category = category;
	this.id = id;
	this.title = title;
	this.url = url;
	this.date = new Date(date);
	this.image = img;
	this.intro = intro;
	this.content = content;
	this.supplier = supplier;
// functions	
	this.getCategory = getCategory;
	this.getID = getID;
	this.getTitle = getTitle;
	this.getURL = getURL;
	this.getTheDate = getTheDate;
	this.getThumbnail = getThumbnail;
	this.getIntro = getIntro;
	this.getContent = getContent;
	this.getDateStr = getDateStr;
	this.getSupplier = getSupplier;

	function getCategory(){ return this.category }	
	function getID(){ return this.id }	
	function getTitle(){ return replaceCoded(this.title) }	
	function getURL(){ return this.url }	
	function getTheDate(){ return this.date }	
	function getThumbnail(){ return this.image }	
	function getIntro(){ return replaceCoded(this.intro) }	
	function getContent(){ return replaceCoded(this.content) }
	function getSupplier(){ return this.supplier }	


	var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	function LZ(x) {return(x<0||x>9?"":"0")+x}


	// ------------------------------------------------------------------
	// getDateStr (date_object, format)
	// Returns a date in the output format specified.
	// The format string consists of the following abbreviations:
	// 
	// Field        | Full Form          | Short Form
	// -------------+--------------------+-----------------------
	// Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
	// Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
	//              | NNN (abbr.)        |
	// Day of Month | dd (2 digits)      | d (1 or 2 digits)
	// Day of Week  | EE (name)          | E (abbr)
	// Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
	// Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
	// Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
	// Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
	// Minute       | mm (2 digits)      | m (1 or 2 digits)
	// Second       | ss (2 digits)      | s (1 or 2 digits)
	// AM/PM        | a                  |
	// ------------------------------------------------------------------
	function getDateStr(format) {
		format=format+"";
		var result="";
		var i_format=0;
		var c="";
		var token="";
		var y=this.date.getYear()+"";
		var M=this.date.getMonth()+1;
		var d=this.date.getDate();
		var E=this.date.getDay();
		var H=this.date.getHours();
		var m=this.date.getMinutes();
		var s=this.date.getSeconds();
		var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
		// Convert real date parts into formatted versions
		var value=new Object();
		if (y.length < 4) {y=""+(y-0+1900);}
		value["y"]=""+y;
		value["yyyy"]=y;
		value["yy"]=y.substring(2,4);
		value["M"]=M;
		value["MM"]=LZ(M);
		value["MMM"]=MONTH_NAMES[M-1];
		value["NNN"]=MONTH_NAMES[M+11];
		value["d"]=d;
		value["dd"]=LZ(d);
		value["E"]=DAY_NAMES[E+7];
		value["EE"]=DAY_NAMES[E];
		value["H"]=H;
		value["HH"]=LZ(H);
		if (H==0){value["h"]=12;}
		else if (H>12){value["h"]=H-12;}
		else {value["h"]=H;}
		value["hh"]=LZ(value["h"]);
		if (H>11){value["K"]=H-12;} else {value["K"]=H;}
		value["k"]=H+1;
		value["KK"]=LZ(value["K"]);
		value["kk"]=LZ(value["k"]);
		if (H > 11) { value["a"]="pm"; }
		else { value["a"]="am"; }
		value["m"]=m;
		value["mm"]=LZ(m);
		value["s"]=s;
		value["ss"]=LZ(s);
		while (i_format < format.length) {
			c=format.charAt(i_format);
			token="";
			while ((format.charAt(i_format)==c) && (i_format < format.length)) {
				token += format.charAt(i_format++);
				}
			if (value[token] != null) { result=result + value[token]; }
			else { result=result + token; }
			}
		return result;
	}


	function replaceCoded(str)
	{
		var codes = new Array( new Array("&amp;","&"), new Array("&quot;","\""), new Array("&apos;","\'") , new Array("&#45;","-") , new Array("&lt;","<"), new Array("&gt;",">") );
		for (var i=0; i<codes.length; i++)
		{
			rxp = new RegExp(codes[i][0], "g");
			str = str.replace(rxp,codes[i][1]);
		}
		// take out urls that have been chopped midway and terminated with ... (patch for NZHerald problem)
		str = str.replace(/<a [^>]*\.\.\./gi,"");
		//take out urls
		str = str.replace(/<a .*>/gi,"");
		return str;
	}
}

// get the latest n items for the given category if empty category given then assume latest across all categories
// valid categories are: national, world, business, entertainment, technology, politics, sport
function getLatestItems( n, cat )
{	
	cat = cat.toLowerCase(); // in case parameter has upper case in it
	var returnArray = new Array(); 
	var theCatArray = new Array();
	var counter = 0;

	if (cat == "")
	{	
		theCatArray = newsArray;
	}
	else // search through to find items for the selected category
	{
		for (i=0; i < newsArray.length; i++)
		{
			if (newsArray[i].getCategory().toLowerCase() == cat)
			{
				theCatArray[counter] = newsArray[i];
				counter++;
			}
		}
	}
	theCatArray.sort(sortByDate);
	
	if ( n > theCatArray.length ) { n = theCatArray.length } // make sure we're not asking for more items than we have
	
	for(i=0; i < n; i++)
	{
		returnArray[i] = theCatArray[i];
	}

	return returnArray;
}



// get the recent n items (excluding the most recent) for the given category if empty category given then assume latest across all categories
// valid categories are: national, world, business, entertainment, technology, politics, sport
function getRecentItems(n, cat)
{
	cat = cat.toLowerCase(); // in case parameter has upper case in it
	var returnArray = new Array();
	var tempArray = new Array();
	var theCatArray = new Array();
	var counter = 0;

	if (cat == "")
	{	
		theCatArray = newsArray;
	}
	else // search through to find items for the selected category
	{
		for (i=0; i < newsArray.length; i++)
		{
			if (newsArray[i].getCategory().toLowerCase() == cat)
			{
				theCatArray[counter] = newsArray[i];
				counter++;
			}
		}
	}
	theCatArray.sort(sortByDate);
	
	if ( n > theCatArray.length - 1 ) { n = theCatArray.length - 1 } // make sure we're not asking for more items than we have
	
	if ( cat != "" )
	{
		for(var k=1; k < n + 1; k++) // start at 1 to skip the most recent
		{
			returnArray[k - 1] = theCatArray[k];
		}
	}
	else
	{
		counter = 0;
		index = 0;
		while (counter != n && index != theCatArray.length)
		{
			found = 0;
			for(var j=0; j < tempArray.length; j++)
			{
				if (tempArray[j].getCategory().toLowerCase() == theCatArray[index].getCategory().toLowerCase())
				{
					found = 1;
					break;
				}
			}
			if ( found )
			{
				returnArray[counter] = theCatArray[index];
				counter++;
			}
			else
			{
				tempArray[tempArray.length] = theCatArray[index];
			}
			index++;
		}
	}

	return returnArray;
}


function getItem(id)
{
	for (var i=0; i < newsArray.length; i++)
	{
		if (newsArray[i].getID() == id)
		{
			return newsArray[i];
		}
	}
	return null;
}

// sort in date descending order
function sortByDate(a, b) {
	dateA = a.getTheDate();
	dateB = b.getTheDate();	
	return (dateB.getTime() - dateA.getTime());
}
