
ICore.controls.Select = function (container, fieldObj) {
	this.fieldObj = fieldObj;
	ICore.controls.Select.superclass.constructor.call(this, container);

	ICore.controls.selectsGlobal.push(this);
};

ICore.controls.selectsGlobal = [];

ICore.extend(ICore.controls.Select, ICore.controls.Control, {
	onDropdown: null,
	onCloseup: null,
	onChange: null,

	items: null,
	itemsContainer: null,
	itemsScroll: null,
	itemsList: null,

	captionContainer: null,
	captionLink: null,
	captionSpan: null,
	captionObj: null,

	afterConstruction: function () {
		this.items = [];

		this.selectedIndex = -1;

		this.onDropdown = new ICore.events.Event(this);
		this.onCloseup = new ICore.events.Event(this);
		this.onChange = new ICore.events.Event(this);
	},

	setup: function () {
		ICore.controls.Select.superclass.setup.call(this);

		this.captionContainer = ICore.dom.getByClass('icore-select-caption', this.container)[0];
		if (!this.captionContainer) {
			this.captionContainer = ICore.dom.createEl('div');
			this.captionContainer.className = 'icore-select-caption';
			this.container.appendChild(this.captionContainer);
		}

		this.captionLink = ICore.dom.getByTag('a', this.captionContainer)[0];
		if (!this.captionLink) {
			this.captionLink = ICore.dom.createEl('a');
			this.captionContainer.appendChild(this.captionLink);
		}
		ICore.dom.addListener(this.captionLink, ICore.events.CLICK, this.linkClick, this);
		
		this.captionObj = this.captionSpan = ICore.dom.getByTag('span', this.captionLink)[0];
		if (!this.captionSpan) {
			this.captionObj = this.captionSpan = ICore.dom.createEl('span');
			this.captionLink.appendChild(this.captionSpan);
		}

		this.itemsContainer = ICore.dom.getByClass('icore-select-items', this.container)[0];
		if (!this.itemsContainer) {
			this.itemsContainer = ICore.dom.createEl('div');
			this.itemsContainer.className = 'icore-select-items';
			this.container.appendChild(this.itemsContainer);
		}

		this.itemsScroll = ICore.dom.getByClass('icore-select-scroll', this.itemsContainer)[0];
		if (!this.itemsScroll) {
			this.itemsScroll = ICore.dom.createEl('div');
			this.itemsScroll.className = 'icore-select-scroll';
			this.itemsContainer.appendChild(this.itemsScroll);
		}

		this.itemsList = ICore.dom.getByTag('ul', this.itemsScroll)[0];
		if (!this.itemsList) {
			this.itemsList = ICore.dom.createEl('ul');
			this.itemsScroll.appendChild(this.itemsList);
		}

		var is = ICore.dom.getByTag('li', this.itemsList);
		var tmp;
		for (var i = 0; i < is.length; i++) {
			tmp = new ICore.controls.SelectItem(this, is[i]);
			this.addItemObj(tmp);
		}
	},

	linkClick: function (e) {
		var t = e.srcElement || e.target;
		t = ICore.dom.findParent(t, function (el){ return el.tagName == 'A'; });
		if (ICore.util.isUndefined(t)) {
			throw new Error('Link não encontrado.');
		} else {
			this.dropdown();
			if (e.stopPropagation) {
				e.stopPropagation();
			}
			e.cancelBubble = true;
		}
	},

	documentClick: function () {
		this.closeup();
	},

	dropdown: function () {
		for (var i = 0; i < ICore.controls.selectsGlobal.length; i++) {
			ICore.controls.selectsGlobal[i].closeup();
		}
		this.itemsContainer.style.display = 'block';
		this.itemsContainer.style.width = this.getContainer().offsetWidth + 'px';
		ICore.dom.addListener(document, ICore.events.CLICK, this.documentClick, this);
		this.onDropdown.fire();
	},

	closeup: function () {
		this.itemsContainer.style.display = 'none';
		ICore.dom.removeListener(document, ICore.events.CLICK, this.documentClick, this);
		this.onCloseup.fire();
	},

	setSelectedIndex: function (value) {
		var tmp, item, oldItem = this.items[this.selectedIndex];
		if (value instanceof ICore.controls.SelectItem) {
			tmp = this.items.indexOf(value);
			if (tmp > -1) {
				this.setCaption(value.getCaption());
				this.selectedIndex = tmp;
				item = value;
			} else {
				throw new Error('Índice não adicionado ao select.');
			}
		} else {
			tmp = this.items[value];
			if (tmp) {
				this.setCaption(tmp.getCaption());
				this.selectedIndex = value;
				item = tmp;
			} else {
				throw new Error('Índice foram do limite.');
			}
		}

		if (oldItem) {
			ICore.dom.removeClass(oldItem.getItem(), 'icore-select-selected');
		}
		ICore.dom.addClass(item.getItem(), 'icore-select-selected');
		var f = ICore.util.object(this.fieldObj);
		if (f) {
			f.value = item.getValue();
		}
	},

	getFieldObj: function () {
		return this.fieldObj;
	},

	setFieldObj: function (value) {
		this.fieldObj = value;
		this.setSelectedIndex(this.getSelectedIndex());
	},

	getSelectedIndex: function () {
		return this.selectedIndex;
	},

	addItem: function (value, caption) {
		if (ICore.util.isArray(value)) {
			var c, v;
			for (var i = 0; i < value.length; i++) {
				c = (ICore.util.isArray(value[i])?value[i][0]:value[i]);
				v = ((ICore.util.isArray(value[i]) && (value[i][1]))?value[i][1]:c);
				this.addItem(c, v);
			}
		} else {
			var r = new ICore.controls.SelectItem(this, null, value, caption);
			this.addItemObj(r);
			return r;
		}
	},

	addItemObj: function (item) {
		if (item instanceof ICore.controls.SelectItem) {
			if (item.getSelect() != this) {
				this.itemsList.appendChild(item.getContainer());
			}
			return this.items.push(item);
		} else {
			throw new Error('Tipo do ítem inválido.');
		}
	},

	clearItems: function () {
		for (var i = 0; i < this.items.length; i++) {
			this.items[i].getContainer().removeChild(this.items[i].getItem());
		}
		this.items = [];
	},

	getCaption: function () {
		return this.captionObj.innerHTML;
	},

	setCaption: function (value) {
		this.captionObj.innerHTML = value;
	},

	getValue: function () {
		var r = this.items[this.selectedIndex];
		if (r) {
			return r.getValue();
		}
	}
});

ICore.controls.SelectItem = function (select, item, value, caption) {
	this.select = select;
	this.item = item;
	this.tmpCaption = caption;
	this.tmpValue = value;

	ICore.controls.SelectItem.superclass.constructor.call(this, select.itemsList);
};

ICore.extend(ICore.controls.SelectItem, ICore.controls.Control, {
	caption: null,
	captionObj: null,
	htmlValue: null,
	link: null,
	item: null,
	select: null,
	setup: function () {
		ICore.controls.Select.superclass.setup.call(this);

		if (!this.item) {
			this.item = ICore.dom.createEl('li');
			this.container.appendChild(this.item);
		}

		this.link = ICore.dom.getByTag('a', this.item)[0];
		if (!this.link) {
			this.link = ICore.dom.createEl('a');
			this.link.href = 'javascript:;';
			this.item.appendChild(this.link);
		}
		ICore.dom.addListener(this.link, ICore.events.CLICK, this.linkClick, this);

		this.captionObj = ICore.dom.getByTag('span', this.link)[0];
		if (!this.captionObj) {
			this.captionObj = ICore.dom.createEl('span');
			this.link.appendChild(this.captionObj);
		}

		this.captionObj.item = this.link.item = this.item.item = this;

		if (this.tmpCaption) {
			this.setCaption(this.tmpCaption);
		}

		if (this.tmpValue) {
			this.setValue(this.tmpValue);
		}
	},

	setCaption: function (value) {
		this.captionObj.innerHTML = value;
	},

	getCaption: function () {
		return this.captionObj.innerHTML;
	},

	setSelect: function (value) {
		if (value != this.select) {
			if (typeof this.select != 'undefined') {
				this.select.removeItem(this);
			}
			this.select = value;
		}
	},

	getSelect: function () {
		return this.select;
	},

	getItem: function () {
		return this.item;
	},

	linkClick: function (e) {
		var t = e.srcElement || e.target;
		t = ICore.dom.findParent(t, function (el){ return el.tagName == 'A'; });
		if (ICore.util.isUndefined(t)) {
			throw new Error('Link não encontrado.');
		} else {
			this.select.setSelectedIndex(this);
			this.select.closeup();
			this.select.onChange.fire();
			//
			if (e.stopPropagation) {
				e.stopPropagation();
			}
			e.cancelBubble = true;
		}
	},

	getValue: function () {
		if (this.item.getAttribute('valor')) {
			return this.item.getAttribute('valor');
		} else {
			return this.getCaption();
		}
	},

	setValue: function (value) {
		this.item.setAttribute('valor', value);
	}
});
