
function Template()
{
	this.HeaderStyle = null;
	this.ItemStyle = null;
	this.AlternateStyle = null;
	this.EmptyStyle = null;
	this.FooterStyle = null;
}


function RenderEngine()
{
	this.template = null;
	this.list = [];
	this.element = null;
}

RenderEngine.prototype.RenderElement = function()
{
	var html = "";
		
	html = String.formatEx(this.template.ItemStyle, this.element) + "\r\n";
	
	return this.template.HeaderStyle+"\r\n"+html+this.template.FooterStyle;
}


RenderEngine.prototype.render = function()
{
	var html = [];
	
	if(this.list!=null && this.list.length>0) {
		for(var i=0; i<this.list.length; i++)
		{
			if(i%2 == 0)
			    {
			        html[html.length] = String.formatEx(this.template.ItemStyle, this.list[i]) + "\r\n";
				}
			else
				html[html.length] = String.formatEx(this.template.AlternateStyle, this.list[i]) + "\r\n";
		}
	}
	else {
		html[html.length]=this.template.EmptyStyle;
	}

	return this.template.HeaderStyle+"\r\n"+html.join('')+this.template.FooterStyle;
}


RenderEngine.prototype.RenderPL = function(VideoToDisplayInEvidence)
{
	var html = [];
	var EmptyItems = 7;
	
	if(this.list!=null && this.list.length>0) {
		for(var i=0; i<this.list.length; i++)
		{
			if(i<VideoToDisplayInEvidence)
			    {
				    html[html.length] = String.formatEx(this.template.ItemStyle, this.list[i]) + "\r\n";
				    EmptyItems = EmptyItems - 2;
				}
			else{
				    html[html.length] = String.formatEx(this.template.AlternateStyle, this.list[i]) + "\r\n";
				    EmptyItems--;
				}
		}
	}
	for (var k=EmptyItems; k>0; k--)
	{
	     html[html.length] = this.template.EmptyStyle;
	}
	
	return this.template.HeaderStyle + "\r\n" + html.join('') + this.template.FooterStyle;
}

RenderEngine.prototype.RenderSearchList = function(PageSize)
{
	var html = [];
	
	    var i;
	    for(i=0; ((i<this.list.length) && (i<PageSize)); i++)
		{
		    html[html.length] = String.formatEx(this.template.ItemStyle, this.list[i]) + "\r\n";
		}
		while (i<PageSize){
		    html[html.length] = String.formatEx(this.template.AlternateStyle, this.list[i]) + "\r\n";
		    i++;
		}
	

	return this.template.HeaderStyle+"\r\n"+html.join('')+this.template.FooterStyle;
}


String.formatEx = function(format, obj)
{
	for(var prop in obj) {
	    re = new RegExp("\{" + prop + "\}","g");
		format = format.replace(re, obj[prop].toString());
	}
	
	return format;
}


