﻿function ListView(divParent)
{
	if (!divParent)
		throw new Error("Parent element for ListView must be supplied");

	var THIS = this;
	this.ContainerParent = divParent;
	this.Container = null;
	this.Line1Format = "";
	this.Line2Format = "";
	this.MoreInfoFormat = "";
	this.KeyField = "";
	this.Items = new Array();
	this.SelectedItems = new Array();
	this.Options = new Array();
	this.MultiSelect = false;
	this.ItemClicked = null;
	this.ValidateItem = null;
	this.ItemCreated = null;
	this.ItemExpanded = null;
	this.Count = 0;
	this.Enabled = true;
	this.MultiSelectRequiresCtrlKey = true;
	this.AlwaysShowExpandButton = false;
	this.Pagination = null;

	//==========================================
	// Get the window object
	//==========================================
	this.GetWindow = function GetWindow()
	{
		var objDocument = this.ContainerParent.ownerDocument;
		var objWindow = objDocument.parentWindow ? objDocument.parentWindow : objDocument.defaultView;
		return objWindow;
	}

	//==========================================
	// Construct the list view elements
	//==========================================
	function Init()
	{
		THIS.Container = document.createElement("DIV");
		THIS.Container.id = "results";

		THIS.ContainerParent.appendChild(THIS.Container);
	}

	//==========================================
	// Remove all list view items from this list view
	//==========================================
	this.Clear = function Clear()
	{
		while (this.Items.length > 0)
		{
			var objItem = this.Items.shift(); //remove the item at the start of the array, and return it
			this.Container.removeChild(objItem.Container);
		}

		this.SelectedItems = new Array();
		this.Count = 0;

		if (this.Pagination != null)
		{
			this.Pagination.PageCount = 0;
			this.Pagination.PageIndex = 0;
		}
	}

	//==========================================
	// Add an item to the list view, using the information currently in the recordset (the current row)
	//==========================================
	this.Add = function Add(recordset)
	{
		try
		{
			var objItem = new ListViewItem(this, recordset);
			this.Items.push(objItem);

			if (this.ItemCreated != null)
				this.ItemCreated(objItem, this);

			this.Count = this.Items.length;
		}
		catch(e)
		{
			alert(e.Message);
		}
		return objItem;
	}

	//==========================================
	// Remove an item from the list view.
	//==========================================
	this.Remove = function Remove(itmItem)
	{
		if (itmItem == null)
			return;

		this.Items.removeItems([ itmItem ]);
		this.SelectedItems.removeItems([ itmItem ]);
		this.Count--;

		this.Container.removeChild(itmItem.Container);
	}

	//==========================================
	// Add all rows in the given XML to the list view
	//==========================================
	this.Import = function Import(strXML)
	{
		if (this.Pagination == null)
		{
			var recordset = new Recordset();
			recordset.LoadXML(strXML);

			while (recordset.EOF() == false)
			{
				if (this.ValidateItem == null || this.ValidateItem(recordset, this) == true)
					this.Add(recordset);

				recordset.MoveNext();
			}
		}
		else
		{
			if (this.Pagination != null)
				this.Pagination.Import(strXML);
		}
	}

	//==========================================
	// Remove the selected state from any currently selected items
	//==========================================
	this.ClearSelection = function ClearSelection()
	{
		while (this.SelectedItems.length > 0)
		{
			var objItem = this.SelectedItems.shift(); //remove the item at the start of the array, and return it
			
			if (objItem != null)
				objItem.SetSelected(false);
		}
	}

	//==========================================
	// Get the item at the given index
	//==========================================
	this.GetItem = function GetItem(intIndex)
	{
		return this.Items[intIndex];
	}

	//==========================================
	// Return the first item in the list where it matches the given id
	//==========================================
	this.GetItemWithId = function GetItemWithId(intId)
	{
		for (var intIndex = 0; intIndex < this.Items.length; intIndex++)
		{
			var objItem = this.Items[intIndex];
			if (objItem.Id == intId)
				return objItem;
		}

		return null;
	}

	//==========================================
	// Return the first item in the list where it matches the given id
	//==========================================
	this.GetItemWithCustomId = function GetItemWithCustomId(FieldName, ID)
	{
		for (var intIndex = 0; intIndex < this.Items.length; intIndex++)
		{
			var objItem = this.Items[intIndex];
			if (objItem[FieldName] == ID)
				return objItem;
		}

		return null;
	}

	//==========================================
	// Add an option that the user can perform on each item
	//==========================================
	this.AddOption = function AddOption(strText, fncCallback)
	{
		var arrOption = new Array();
		arrOption.push(strText);
		arrOption.push(fncCallback);

		this.Options.push(arrOption);
	}

	Init();
}
