define_namespace("com.homeCenter.ui.common");
com.homeCenter.ui.Control.extend("com.homeCenter.ui.common.ComboBoxItem", {
	metadata: {
		properties: {
			key: {type: "string", defaultValue: ""},
			text: {type: "string", defaultValue: ""},
			value: {type: "object", defaultValue: null},
			colorBox: {type: ""}
		},
		
		aggregations: {
		},
		
		events: {
		}
	},
	
	constructor: function(props) {
		this.base(props);
	},
	
	render: function(oRm) {
		oRm.write("<li ");
		oRm.writeControlData();
		oRm.writeClasses();
		oRm.writeStyles();
		oRm.write(">");
		oRm.writeText(this.getProperty("text") || "");
		oRm.write("</li>");
	},
	
	onClick: function(oEvent) {
		com.homeCenter.lib.Event.eventNeutralize(oEvent);
		this.___parent__.choseItem(this);
	}
});

com.homeCenter.ui.Control.extend("com.homeCenter.ui.common.ComboBox", {
	metadata: {
		properties: {
			selectedItem: {type: "integer", defaultValue: 0},
			open: {type: "boolean", defaultValue: false},
			visible: {type: "boolean", defaultValue: true},
			enabled: {type: "boolean", defaultValue: true},
			width: {type: "com.homeCenter.css.Style", defaultValue: "auto"}
		},
		
		aggregations: {
			items: {type: "com.homeCenter.ui.common.ComboBoxItem", multiple: true, visible: true, singularName: "item"},
			_icon: {
				type: "com.homeCenter.ui.Icon",
				multiple: false
			}
		},
		
		events: {
			change: {}
		}
	},
	
	constructor: function(props) {
		this.base(props);
		
		this.setAggregation("_icon", new com.homeCenter.ui.Icon({
			name: com.homeCenter.lib.IconUrl("arrow_circle_down")
		}));
	},
	
	render: function(oRm) {
		var items = this.getAggregation("items"),
			i = 0;
		
		oRm.write("<div ");
		oRm.writeControlData();
		if(!this.getEnabled()) {
			oRm.addClass("disabled");
		}
		oRm.writeClasses();
		if(!this.getVisible()) {
			oRm.addStyle("display", "none");
		}
		oRm.addStyle("width", this.getWidth());
		oRm.writeStyles();
		oRm.write(">");
		
		oRm.write("<div ");
		oRm.addClass("hcUIComboBoxButton");
		oRm.writeClasses();
		oRm.writeStyles();
		oRm.write(">");
		oRm.renderAggregation(this.getAggregation("_icon"));
		oRm.write("</div>");
		
		oRm.write("<div ");
		oRm.addClass("hcUIComboBoxSelected");
		oRm.writeClasses();
		oRm.writeStyles();
		oRm.write(">");
		if(items.length > 0 && this.getProperty("selectedItem") < items.length) {
			oRm.writeText(items[this.getProperty("selectedItem")].getText());
		}
		oRm.write("</div>");
		
		oRm.write("<ul ");
		oRm.addClass("hcUIComboBoxList");
		oRm.writeClasses();
		oRm.writeStyles();
		oRm.write(">");
		for(i = 0; i < items.length; ++i) {
			items[i].___parent__ = this;
			oRm.renderAggregation(items[i]);
		}
		oRm.write("</ul>");
		
		oRm.write("</div>");
	},
	
	setSelectedItemByValue: function(oValue) {
		var items = this.getAggregation("items"),
			idx = 0,
			i = 0;
		
		if(oValue === undefined || oValue === null) {
			return;
		}
		
		for(i = 0; i < items.length; ++i) {
			if(items[i].getValue() === oValue) {
				idx = i;
				break;
			}
		}
		
		this.setProperty("selectedItem", idx);
	},
	
	setSelectedItemByKey: function(sKey) {
		var aItems = this.getItems(),
			i = 0;
		
		if(!sKey) {
			return;
		}
		
		for(i = 0; i < aItems.length; ++i) {
			if(aItems[i].getKey() === sKey) {
				this.setProperty("selectedItem", i);
				break;
			}
		}
		
	},
	
	onClick: function(oEvent) {
		if(!this.getEnabled()) {
			return;
		}
		
		var target = $(oEvent.target);
		if(target.hasClass("hcUIComboBoxButton") || target.hasClass("hcUIIcon")) {
			this.toggleList();
		}
	},
	
	toggleList: function() {
		var list = this.$().find(".hcUIComboBoxList");
		if(list.hasClass("open")) {
			list.removeClass("open");
			list.slideUp("fast");
		//	list.hide();
		} else {
			list.addClass("open");
			list.slideDown("fast");
			//list.show();
		}
	},
	
	choseItem: function(oItem) {
		var self = this,
			items = this.getAggregation("items"),
			ctrl = $("#" + this.getIdForLabel()),
			list = ctrl.find(".hcUIComboBoxList"),
			index = -1,
			i = 0;
		
		for(i = 0; i < items.length; ++i) {
			if(items[i] === oItem) {
				index = i;
				break;
			}
		}
		
		if(index <= -1) {
			com.homeCenter.log.debug("onClick", "Cannot find item #" + index, "com.homeCenter.ui.common.ComboBox");
			return;
		}
		
		com.homeCenter.log.debug("onClick", "Select item #" + index, "com.homeCenter.ui.common.ComboBox");
		list.removeClass("open");
		list.slideUp("fast", function() {
			self.setProperty("selectedItem", index);
		});
		
		this.fireChange({
			oSource: this,
			oItem: oItem
		});
	},
	
	getValue: function() {
		return this.getAggregation("items")[this.getProperty("selectedItem")].getValue();
	},

	getSelectedKey: function() {
		return this.getAggregation("items") && 
				this.getAggregation("items")[this.getProperty("selectedItem")] && 
				this.getAggregation("items")[this.getProperty("selectedItem")].getKey();
	}
});