////////////////////////////////////////////////////////////////////////////////////////////
// Splitter client-side script
////////////////////////////////////////////////////////////////////////////////////////////

addNamespace("Web");
addNamespace("Web.UI");

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.Repeater = Base.extend(
{
	constructor: function(id)
	{
		this.id = id;
		this.control = $(this.id);
		if (!this.control) return;
		
		this.itemTemplate = $(this.id + "ItemTemplate").innerHTML.trim();
		this.itemTemplate = this.itemTemplate.substring(4, this.itemTemplate.length - 7);
		
		this.dataSource = null;
		this.currentIndex = 0;
		
		this.onitembind = null;
	},
	
	dataBind: function(dataSource)
	{
		if (arguments.length > 0)
			this.dataSource = dataSource;
		
		if (this.dataSource == null || (this.dataSource.length != null && this.dataSource.length == 0))
		{
			this.control.innerHTML = "";
			return;
		}
		
		var output = new Core.StringBuilder();
		var script = new Core.StringBuilder();
		
		if (this.dataSource.length != null)
		{
			for (this.currentIndex = 0; this.currentIndex < this.dataSource.length; this.currentIndex++)
				this.bindInstance(output, script, this.dataSource[this.currentIndex], this.itemTemplate);
		}
		else
			this.bindInstance(output, script, this.dataSource, this.itemTemplate);
		
		this.control.innerHTML = output.toString();
		eval(script.toString());
	},
	
	bindInstance: function(output, script, dataItem, template)
	{
		if (dataItem != null)
		{
			output.append(Core.Utility.instantiateClientTemplate(template, null, dataItem));
			
			if (this.onitembind != null)
				script.append(Core.Utility.instantiateClientTemplate(this.onitembind, null, dataItem) + ";");
		}
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

window.Web_UI_Repeater_Loaded = true;



