/**
 * List of selectable items representation data type
 */
cAIList = function ( template )
{
	// Setting default template
	if (template == false)
		template = "<div id='{id list_id}'>{v items}</div><div style='text-align: right; font-size: 10px; margin-top: 5px;'>показано: {v count} из {v total_count}</div>";
	
	// Parent constructor (cAIOutputtable) calling
	cAIList.superclass.constructor.apply(this, new Array(template))

	// Getting this list id
	this.list_id = this.id("list_id");
}

// Extending cAIList with cAIOutputtable
extend(cAIList, cAIOutputtable);

// Adding methods to cAIList prototype
mixin(cAIList.prototype, {
	myChoices: [], // List choices
	myCurentPosition: -1, // Current selected item
	myKeyboardStarted: false, // keyboardStart() function was called
	myOnSelectFunction: false, // function called when item selects
	myMousePosition: -1, // Selected by mouse item
	
	/**
	 * Setting choices function
	 */
	setChoices: function ( choices )
	{
		// Setting choices
		this.myChoices = choices;
		
		// Generting items HTML
		var items = new Array();
		var items_num = 0;
		for (var i in choices)
		{
			var item = new cAIOutputtable("<div id='" + this.list_id + i + "' style='cursor: pointer; width: 100%;' onmouseover=\"$(this).css('background', '#E2ECF4');\" onmouseout=\"$(this).css('background', '#FFF');\">{v name}</div>");
			item.set("name", choices[i].name);
			items[items_num++] = item;
		}
		
		// Setting current position to -1 (no item selected)
		this.myCurrentPosition = -1;
			
		// Setting items
		this.set("items", items);
	},
	
	/**
	 * Setting actions on items function
	 */
	setActions: function ()
	{
		for (var i in this.myChoices)
			this.setAction(i);
	},
	
	setAction: function ( i )
	{
		_this = this;
		$("#" + this.list_id + i).click(function ( e ) {
			if (_this.myOnSelectFunction != false)
				_this.myOnSelectFunction(_this.myChoices[i]);
		});
		$("#" + this.list_id + i).mouseover(function ( e ) {
			_this.myMousePosition = i;
		});
		$("#" + this.list_id + i).mouseout(function ( e ) {
			if (_this.myMousePosition == i)
				_this.myMousePosition = -1;
		});
	},
	
	selectByMouse: function ()
	{
		if (this.myMousePosition != -1)
			this.myOnSelectFunction(this.myChoices[this.myMousePosition]);
	},
	
	/**
	 * Setting list params from server response function
	 * @param string data - server response
	 */
	obtainResponse: function ( data )
	{
		json = jsonMakeObject(data);
		
		this.setChoices(json.options);
		this.set('count', json.count);
		this.set('total_count', json.total_count);
	},
	
	/**
	 * Initialising list function (setting current position to first element)
	 * @param int id - id of item to select
	 */
	select: function ( id )
	{
		// Correcting item id value
		if (id < 0)
			id = this.myChoices.length - 1;
		if (id > this.myChoices.length - 1)
			id = 0;
		if (id == -1)
			return;
		
		// Unhighlighting lat current position
		$($("#" + this.list_id + " div").get(this.myCurrentPosition)).css("background", "#FFF");
			
		// Setting current position
		this.myCurrentPosition = id;
		
		// Highlighting current position
		$($("#" + this.list_id + " div").get(this.myCurrentPosition)).css("background", "#E2ECF4");
	},
	
	/**
	 * Setting backref function on item selection function
	 * @param function fn - backref function
	 */
	onselect: function ( fn )
	{
		this.myOnSelectFunction = fn;
	},
	
	/**
	 * Selecting next item function
	 */
	nextItem: function ()
	{
		this.select(this.myCurrentPosition + 1);
	},
	
	/**
	 * Selecting previous item function
	 */
	previousItem: function ()
	{
		this.select(this.myCurrentPosition - 1);
	},
	
	/**
	 * Starting listening keyboard function
	 * @param string obj - jQuery object to assign listener
	 */
	keyboardStart: function ( obj )
	{
		if (!this.myKeyboardStarted)
		{
			var _this = this;
			obj.keydown(function ( e ) {
				if (_this.keyboardStopped == true)
					return true;
				
				switch (e.keyCode)
				{
				case 40:
					_this.nextItem();
					return false;
				case 38:
					_this.previousItem();
					return false;
				case 13:				
					if (_this.myOnSelectFunction != false && _this.myCurrentPosition != -1)
						_this.myOnSelectFunction(_this.myChoices[_this.myCurrentPosition]);
					return false;
				}
				
				return true;
			});
		}

		this.myKeyboardStarted = true;
	}
});
