// ---- section javscript functions ----------------------------------------

function sectionItem( name, subs )
{
// variables
	this.name = name;
	this.subSections = subs;
// functions	
	this.getName = getName;
	this.getSubsections = getSubsections;

	function getName(){ return this.name }	
	function getSubsections(){ return this.subSections }	
	
}

function subSectionItem( name, mnths )
{
// variables
	this.name = name;
	this.months = mnths;
// functions	
	this.getName = getName;
	this.getMonths = getMonths;

	function getName(){ return this.name }	
	function getMonths(){ return this.months }	
	
}

// return the subsections(array) for the supplied section, if not found returns null
function getSubsections( sect )
{
	for (var i=0; i < sections.length; i++)
	{
		if ( sections[i].getName() == sect )
		{
			return sections[i].getSubsections();
		}		
	}
	return null;
}

// return the months(array) for the supplied section-subsection, if not found returns null
function getMonths( sect, subsect )
{
	for (var i=0; i < sections.length; i++)
	{
		if ( sections[i].getName() == sect )
		{
			var subs = sections[i].getSubsections();
			for (var j=0;  j < subs.length; j++)
			{
				if ( subs[j].getName() == subsect )
				{
					return subs[j].getMonths();
				}				
			}
			
		}		
	}
	return null;
}


// ---- ads javscript functions ----------------------------------------

function supplierItem( name, logo, url )
{
// variables
	this.name = name;
	this.logo = logo;
	this.url = url
// functions	
	this.getName = getName;
	this.getLogo = getLogo;
	this.getLink = getLink;

	function getName(){ return this.name }	
	function getLogo(){ return this.logo }	
	function getLink(){ return this.url }	
	
}

function bannerItem( file, url, sect, subsect, supplier )
{
// variables
	this.file = file;
	this.url = url;
	this.section = sect;
	this.subSection = subsect;
	this.supplier = supplier;
// functions	
	this.getFile = getFile;
	this.getURL = getURL;
	this.getSection = getSection;
	this.getSubsection = getSubsection;
	this.getSupplier = getSupplier;

	function getFile(){ return this.file }	
	function getURL(){ return this.url }	
	function getSection(){ return this.section }	
	function getSubsection(){ return this.subSection }	
	function getSupplier(){ return this.supplier }	
	
}

function prerollItem( video, sect, subsect, supplier )
{
// variables
	this.video = video;
	this.section = sect;
	this.subSection = subsect;
	this.supplier = supplier;
// functions	
	this.getVideo = getVideo;
	this.getSection = getSection;
	this.getSubsection = getSubsection;
	this.getSupplier = getSupplier;

	function getVideo(){ return this.video }	
	function getSection(){ return this.section }	
	function getSubsection(){ return this.subSection }	
	function getSupplier(){ return this.supplier }	
	
}


function getSupplier (suplr)
{
	for (var i=0; i < suppliers.length; i++)
	{
		if ( suppliers[i].getName() == suplr )
		{			
			return suppliers[i];
		}
	}
	return null;

}

// get the appropriate banner for the specified section-subsection-supplier. Returns a bannerItem or null if not found. 
function getVideoBanner ( sect, subsect, suplr )
{		
	for (var i=0; i < banners.length; i++)
	{
		if ( (banners[i].getSupplier() == suplr) &&  (banners[i].getSection() == sect) && (banners[i].getSubsection() == subsect))
		{			
			return banners[i];
		}
	}
	return null;
}

// get the appropriate preroll for the specified section-subsection-supplier. Returns a prerollItem or null if not found 
function getVideoPreroll ( sect, subsect, suplr )
{		
	for (var i=0; i < prerolls.length; i++)
	{
		if ( (prerolls[i].getSupplier() == suplr) &&  (prerolls[i].getSection() == sect) && (prerolls[i].getSubsection() == subsect))
		{			
			return prerolls[i];
		}
	}
	return null;
}


// ---- list javscript functions ----------------------------------------

function videoItem(id,subsect,title,video,date,img,intro,cat,age,supplier)
{
// variables
	this.id = id;
	this.subSection = subsect;
	this.title = title;
	this.video = video;
	this.date = new Date(date);
	this.image = img;
	this.intro = intro;
	this.category = cat;
	this.age = age;
	this.supplier = supplier;
// functions	
	this.getID = getID;
	this.getSubsection = getSubsection;
	this.getTitle = getTitle;
	this.getVideo = getVideo;
	this.getTheDate = getTheDate;
	this.getDateStr = getDateStr;
	this.getThumbnail = getThumbnail;
	this.getIntro = getIntro;
	this.getCategory = getCategory;
	this.getAge = getAge;
	this.getSupplier = getSupplier;

	function getID(){ return this.id }	
	function getSubsection(){ return this.subSection }	
	function getTitle(){ return replaceCoded(this.title) }	
	function getVideo(){ return this.video }	
	function getThumbnail(){ return this.image }	
	function getIntro(){ return replaceCoded(this.intro) }	
	function getAge(){ return this.age }
	function getSupplier(){ return this.supplier }
	function getCategory(){ return this.category }
	function getTheDate(){ return this.date }	

	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]);
		}
		return str;
	}
}

// sort in date descending order
function sortByDate(a, b) {
	dateA = a.getTheDate();
	dateB = b.getTheDate();	
	return (dateB.getTime() - dateA.getTime());
}

// get the latest n items 
function getLatestItems( n )
{	
	var returnArray = new Array(); 
	var videosArray = new Array();
	for(i=0; i < videos.length; i++)
	{
		videosArray[i] = videos[i];
	}
	videosArray.sort(sortByDate);
	
	if ( n > videosArray.length ) { n = videosArray.length } // make sure we're not asking for more items than we have
	
	for(i=0; i < n; i++)
	{
		returnArray[i] = videosArray[i];
	}

	return returnArray;
}

function getItemByID( id )
{
	for(i=0; i < videos.length; i++)
	{
		if (videos[i].getID() == id)
		{
			return videos[i];
		}
	}
	return "";
}

// return the n number of items after the id clip ( or as many available if there are less than n items)
function getNextNItems( id, n, wrap )
{
	var returnArray = new Array(); 
	var index = -1;
	for(i=0; i < videos.length; i++)
	{
		if (videos[i].getID() == id)
		{
			index = i;
			break;
		}
	}
	{
		numToEnd = videos.length - (index + 1);
		if ( n > (videos.length - 1) )
		{
			for(j=index+1; j < videos.length; j++)
			{
				returnArray[returnArray.length] = videos[j];
			}
			if ( wrap ) // add the items from the start as well
			{
				for(k=0; k < index; k++)
				{
					returnArray[returnArray.length] = videos[k];
				}
			}
		}
		else
		{
			if ( numToEnd < n )
			{
				for(j=index+1; j < videos.length; j++)
				{
					returnArray[returnArray.length] = videos[j];
				}
				if ( wrap ) // add the items from the start as well
				{
					for(k=0; k < (n - numToEnd); k++)
					{
						returnArray[returnArray.length] = videos[k];
					}
				}
			}
			else
			{

				for(j=index+1; j < (index+1+n); j++)
				{
					returnArray[returnArray.length] = videos[j];
				}
			}
		}
	}

	return returnArray;
}

function getAllItems()
{
	return videos;
}


