// ***************************************************
//
// writer by geato 2010-01-15
// mail:fairyprince@163.com , free_getao@hotmail.com
//
// ***************************************************

function ComboBoxItem(caption,value)
{
	this.caption = caption;
	this.value = value;
}

function ComboBox(own,id,style,eventFun)
{
	this.ID = id;
	this.style = style;
	this.ItemList = new Array();
	this.eventFun = eventFun;
	this.own = own;
}

ComboBox.prototype.AddItem = function (caption,value) {
	var itemobj = new ComboBoxItem(caption,value);
	this.ItemList.push(itemobj);
	itemobj = null;
}

ComboBox.prototype.getHtml = function (defaultIndex) {
	var html = new strcat();
	html.append('<div style="position:relative;z-index:200" id="' + this.ID + '" class="' + this.style + '" onMouseOut="' + this.own + '.onMouseOut(event);" onMouseOver="' + this.own + '.onMouseOver(event);" onClick="' + this.own + '.onClick(event);">');
	html.append('<span class="' + this.style + '_arrow"></span>');

	var c = this.ItemList.size();
	if(c < 1 && defaultValue < c)
	{
		html.append('<span id="' + this.ID + '_windowtext" class="' + this.style + '_windowtext"></span>');
	}
	else
	{
		html.append('<span id="' + this.ID + '_windowtext" class="' + this.style + '_windowtext">' + this.ItemList[defaultIndex].caption + '</span>');
		html.append('<div style="position:absolute;display:none;height:100px" id="' + this.ID + '_oplist" class="' + this.style + '_oplist"><ul class="' + this.style + '_ul" onMouseOut="' + this.own + '.onMouseOut(event);" onMouseOver="' + this.own + '.onMouseOver(event);" style="overflow:hidden;">');
		var i = 0;
		for(i = 0 ; i < c ; i ++)
		{
			html.append('<li><a href="' + this.ItemList[i].caption + '" onClick="' + this.own + '.select(' + i + ');return false;">' + this.ItemList[i].caption + '</a></li>');
		}
		html.append('</ul></div>');
	}
	html.append('</div>');
	return html.toString();
}

ComboBox.prototype.writeHtml = function (defaultIndex) {
	document.write(this.getHtml(defaultIndex));
}

ComboBox.prototype.select = function (index) {
	var c = this.ItemList.size();
	if(index < 0 || index >= c)
		return;
	var wt = $(this.ID + '_windowtext');
	if(wt == null || typeof wt == 'undefined')
		return;
	wt.update(this.ItemList[index].caption);
	wt = null;
	if(this.eventFun != null && typeof this.eventFun != 'undefined')
	{
		this.eventFun(this.ItemList[index]);
	}
}

ComboBox.prototype.__HideList = function () {
	var list = $(this.ID + '_oplist');
	if(list == null || typeof list == 'undefined')
		return;
	list.hide();
	list = null;
}

ComboBox.prototype.__ShowList = function () {
	var list = $(this.ID + '_oplist');
	if(list == null || typeof list == 'undefined')
		return;
	list.show();
	list = null;
}

ComboBox.prototype.onMouseOver = function (e) {
	if(this.delayHide)
		this.delayHide = false;
}

ComboBox.prototype.onMouseOut = function (e) {
	this.delayHide = true;
	setTimeout(this.own + '.__delayHideList()',300);
}

ComboBox.prototype.__delayHideList = function () {
	if(this.delayHide)
		this.__HideList();
	this.delayHide = false;
}

ComboBox.prototype.onClick = function (e) {
	this.delayHide = false;
	var list = $(this.ID + '_oplist');
	if(list == null || typeof list == 'undefined')
		return;
	if(list.getStyle('display') == 'none')
	{
		this.__ShowList();
	}
	else
	{
		this.__HideList();
	}
}