﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Renins");

Renins.ComboBox = function(element) {
    Renins.ComboBox.initializeBase(this, [element]);

    this._availableValues = null;
    this._id = "";
    this._name = "";
    this._value = "";
    this._selectedItem = null;

    this._clickControlDelegate = null;
    this._clickWindowDelegate = null;

    this._button = null;
    this._list = null;
    this._caption = null;
    this._listContent = null;
    this._visible = true;
}

Renins.ComboBox.prototype = {
	get_id: function() { return this._id; },
	set_id: function(value) { this._id = value; },

	get_name: function() { return this._name; },
	set_name: function(value) { this._name = value; },

	get_value: function() { return this._value; },
	set_value: function(value) { this._value = value; },

	get_availableValues: function() { return this._availableValues; },
	set_availableValues: function(value) { this._availableValues = value; },

	get_button: function() { return this._button; },
	set_button: function(value) { this._button = $get(value); },

	get_list: function() { return this._list; },
	set_list: function(value) { this._list = $get(value); },

	get_listContent: function() { return this._listContent; },
	set_listContent: function(value) { this._listContent = $get(value); },

	get_caption: function() { return this._caption; },
	set_caption: function(value) { this._caption = $get(value); },

	get_visible: function() { return this._visible; },
	set_visible: function(value) { this._visible = value; },

	add_changed: function(handler) { this.get_events().addHandler("changed", handler); },
	remove_changed: function(handler) { this.get_events().removeHandler("changed", handler); },

	add_listVisible: function(handler) { this.get_events().addHandler("listVisible", handler); },
	remove_listVisible: function(handler) { this.get_events().removeHandler("listVisible", handler); },

	_get_itemName: function(item) {
		if (typeof (item) == "string") {
			return item;
		}
		else return item.Name;
	},

	_createList: function() {
		var list = this.get_listContent();
		list.innerHTML = "";
		var data = this.get_availableValues();
		list.innerHTML = '';
		for (var i = 0; i < data.length; i++) {
			var value = data[i];
			var option = document.createElement('li');
			var span = document.createElement('span');
			option.appendChild(span);
			span.innerHTML = this._get_itemName(value);
			span.ComboBoxItem = value;
			list.appendChild(option);
		}
	},

	initialize: function() {
		Renins.ComboBox.callBaseMethod(this, 'initialize');

		// Add custom initialization here
		if (this._clickControlDelegate == null) this._clickControlDelegate = Function.createDelegate(this, this._onClickControlHandler);
		$addHandler(this.get_button(), "click", this._clickControlDelegate);

		this._createList();

		this.setVisible(this.get_visible());
		this.selectByName(this.get_value());
	},

	dispose: function() {
		//Add custom dispose actions here
		if (this._clickControlDelegate != null) {
			$removeHandler(this.get_button(), "click", this._clickControlDelegate);
			delete this._clickControlDelegate;
		}
		Renins.ComboBox.callBaseMethod(this, 'dispose');
	},

	_isAncestor: function(parendID, ancestor) {
		if (ancestor && ancestor.parentNode) {
			if (ancestor.parentNode.id == parendID) return true;
			else return this._isAncestor(parendID, ancestor.parentNode);
		}
		else return false;
	},

	_onChanged: function(e) {
		var h = this.get_events().getHandler("changed");
		if (h) h(this, e);
	},

	_onListVisible: function(e) {
		var h = this.get_events().getHandler("listVisible");
		if (h) h(this, e);
	},

	_setVisibleList: function(visible) {
		if (visible) {
			this.get_list().style.display = "block";
			if (this._clickWindowDelegate == null) this._clickWindowDelegate = Function.createDelegate(this, this._onClickWindowHandler);
			$addHandler(window.document.body, "click", this._clickWindowDelegate);
			this._onListVisible(Sys.EventArgs.Empty);
		}
		else {
			this.get_list().style.display = "none";
			if (this._clickWindowDelegate != null) {
				$removeHandler(window.document.body, "click", this._clickWindowDelegate);
				delete this._clickWindowDelegate;
			}
		}
	},

	setListVisible: function(visible) {
		this._setVisibleList(visible);
	},

	isListVisible: function() {
		return this.get_list().style.display == "block";
	},

	_onClickControlHandler: function(e) {
		this._setVisibleList(!this.isListVisible());
		e.stopPropagation();
	},

	_selectInternal: function(item) {
		this.set_value(this._get_itemName(item));
		this._selectedItem = item;
		this.get_caption().innerHTML = this.get_value();
	},

	selectByName: function(name) {
		var data = this.get_availableValues();
		var i = 0;
		for (i = 0; i < data.length; i++) {
			if (this._get_itemName(data[i]) == name) {
				this._selectInternal(data[i]);
				break;
			}
		}
	},

	selectByNameOrFirst: function(name) {
		var data = this.get_availableValues();
		var i = 0;
		for (i = 0; i < data.length; i++) {
			if (this._get_itemName(data[i]) == name) {
				this._selectInternal(data[i]);
				return;
			}
		}
		if (data.length > 0) {
			this._selectInternal(data[0]);
		}
	},

	selectByIndex: function(index) {
		var data = this.get_availableValues();
		this._selectInternal(data[index]);
	},

	_onClickWindowHandler: function(e) {
		if (this.isListVisible()) {
			this._setVisibleList(false);
			if (this._isAncestor(this.get_list().id, e.target)) {
				if (e.target.ComboBoxItem) {
					this._selectInternal(e.target.ComboBoxItem);
				}
				else if (e.target.childNodes[0].ComboBoxItem) {
					this._selectInternal(e.target.childNodes[0].ComboBoxItem);
				}
				else if (e.target.childNodes[0].childNodes[0].ComboBoxItem) {
					this._selectInternal(e.target.childNodes[0].childNodes[0].ComboBoxItem);
				}
				this._onChanged(Sys.EventArgs.Empty);
			}
		}
	},

	setVisible: function(visible) {
		if (visible)
			this.get_element().style.display = "block";
		else
			this.get_element().style.display = "none";
	},

	unselect: function() {
		this.set_value("");
		this._selectedItem = null;
		this.get_caption().innerHTML = "";
	},

	setNewData: function(data) {
		this.unselect();
		this.set_availableValues(data);
		this._createList();
	},

	clearList: function() {
		this.unselect();
		this.set_availableValues(new Array);
		this._createList();
	},

	getSelectedItem: function() {
		return this._selectedItem;
	},

	getNameValue: function() {
		return this.get_name() + "=" + this.get_value() + paramsSeparator;
	}
}
Renins.ComboBox.registerClass('Renins.ComboBox', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

