/**
 * ListItem
 *
 * @author   Sean McCann
 * @version  1.0.0
 * @date     2005.09.14
 *
 * Changelog:
 */


/**
 * Displays formatted set of results for a client quick search
 */

function ListItemView( )
{
	try
	{
		// Keep this here to copy over properties
		// from the parent prototype object
		create_class_properties.call(this, ListItemView);

		// Note: these properties need to point to the
		// appropriate HTMLElements in the createView() method
		// of the subclass.
		this.addControl = null;
		this.removeControl = null;

		// Reference to the List managing this ListItem
		this.list = null;

		/**
		 * The position of this item within the list
		 * @access private
		 */
		this.index = -1;
	}
	catch(e)
	{
		error_message(e, "ListItem.js", 'ListItemView');
	}
}

ListItemView.prototype = new View();
ListItemView.prototype.__CLASS__ = "ListItemView";
ListItemView.prototype.setList = ListItemViewSetList;
ListItemView.prototype.enableRemoveControl = ListItemViewEnableRemoveControl;
ListItemView.prototype.disableRemoveControl = ListItemViewDisableRemoveControl;
ListItemView.prototype.addHandler = ListItemViewAddHandler;
ListItemView.prototype.removeHandler = ListItemViewRemoveHandler;
ListItemView.prototype.setIndex = ListItemViewSetIndex;

/**
 * Disables the control allowing the user to remove the ListItem from the List
 * NOTE:  this is the default abstract method that may need to be reimplimented
 * by classes inheriting from ListItemView
 */
function ListItemViewDisableRemoveControl()
{
	try
	{
		if (!this.removeControl)
		{
			throw new Error('The removeControl property must point to a valid form control or button before attempting to disable it.');
		}
		// By default, we're assuming that the control
		// is an <input> of some kind that has a 'disabled'
		// attribute that may be set to true or false.
		this.removeControl.disabled = true;
		return true;
	}
	catch(e)
	{
		error_message(e, "ListItem.js", 'ListItemViewDisableRemoveControl');
		return false;
	}
}

/**
 * Enables the control allowing the user to remove the ListItem from the List
 * NOTE:  this is the default abstract method that may need to be reimplimented
 * by classes inheriting from ListItemView
 */
function ListItemViewEnableRemoveControl()
{
	try
	{
		if (!this.removeControl)
		{
			throw new Error('The removeControl property must point to a valid form control or button before attempting to enable it.');
		}

		// By default, we're assuming that the control
		// is an <input> of some kind that has a 'disabled'
		// attribute that may be set to true or false.
		this.removeControl.disabled = false;
		return true;
	}
	catch(e)
	{
		error_message(e, "ListItem.js", 'ListItemViewEnableRemoveControl');
		return false;
	}
}

/**
 * Sets a reference to the List that manages this ListItem
 *
 * @param List list
 */
function ListItemViewSetList( list )
{
	try
	{
		var old_list;

		old_list = this.list;
		this.list = list;

		return old_list;
	}
	catch(e)
	{
		error_message(e, "ListItem.js", 'ListItemViewSetList');
		return false;
	}
}


/**
 * Sets the List index of the ListItem
 *
 * @param integer index
 */
function ListItemViewSetIndex( index )
{
	try
	{
		this.index = index;
		return true;
	}
	catch(e)
	{
		error_message(e, "ListItem.js", 'ListItemViewSetIndex');
		return false;
	}
}


function ListItemViewAddHandler()
{
	// By default, insert the new item immediately after
	// the item where the "add" control was pressed.
	var insert_index = 1 + this.view.list.getIndexOfItem(this.view);
	this.view.list.addItem( {}, insert_index );
	fix_height('body_container');
}


function ListItemViewRemoveHandler()
{
	this.view.list.removeItem( this.view );
	fix_height('body_container');
}