F_SELECT_EDITABLE=1
F_SELECT_DISABLED=2

var selectBoxes={}

function SelectBox(container,section,onchange,flags,onoptionchange)
{
	while(selectBoxes[this.uid=getUID()]);
	selectBoxes[this.uid]=this;
	this.options=[]
	this.container=(container)?container:null;
	this.section=(section)?section:'';
	this.onchange=(onchange)?onchange:null;
	this.flags=(flags)?flags:0;
	this.onoptionchange=(onoptionchange)?onoptionchange:null;
	this.selectedIndex=null;
	this.highlightIndex=null;
	this.selectOpen=false;
	this.displayed=false;
	this.width=0;
	this.height=0;
	this.isHead=false;
	this.disabled=(this.flags & F_SELECT_DISABLED);
	this.selectClickOutsideHandler=null;
}

SelectBox.prototype.get=function get()
{
	var out=''
	if(this.options.length)
	{
		out+='<DIV onMouseOver="selectBoxes.'+this.uid+'.isHead=true;" onMouseOut="selectBoxes.'+this.uid+'.isHead=false;" CLASS="selectHead" ID="selectHead_'+this.uid+'">'
		if(this.flags & F_SELECT_EDITABLE)
		{
			out+='<INPUT '
			+((this.disabled)?' DISABLED':'')
			+' onBlur="selectBoxes.'+this.uid+'.setValue(this.value)"'
			+' onKeyPress="if((typeof e)==\'undefined\'){var e=event};if(e.keyCode==13)this.blur()"'	
			+' onFocus="this.select()"'
			+' ID="selectTextEdit_'+this.uid+'" TYPE="text" CLASS="selectTextEdit" VALUE="'+this.options[this.selectedIndex].text+'">'
		}
		else
		{
			out+='<DIV onMouseDown="selectBoxes.'+this.uid+'.toggleSelect(event)" ID="selectText_'+this.uid+'"'
			+' CLASS="selectText'+((this.disabled)?'Disabled'+((isIE)?'IE':''):'')+'">'+this.options[this.selectedIndex].text+'</DIV>'
		}
		out+='<BUTTON'
		+' onMouseDown="selectBoxes.'+this.uid+'.toggleSelect(event)"'
		+' ID="selectButton_'+this.uid+'" CLASS="selectButton">'
		+'<IMG SRC="gfx/select_16.gif" WIDTH="'+sizes.selectButtonIcon.innerWidth+'" HEIGHT="'+sizes.selectButtonIcon.innerHeight+'" BORDER="0">'
		+'</BUTTON>'
		+'</DIV>'
	}
	return out;
}

SelectBox.prototype.getOptions=function getOptions()
{
	var out=''
	for(var x=0;x<this.options.length;x++)
	{
		out+='<DIV ID="selectOption_'+this.uid+'_'+x+'" CLASS="selectOption'+((this.selectedIndex==x)?'Selected':'')+'"'
		+' onMouseDown="selectBoxes.'+this.uid+'.selectOption('+x+',event)"'	
		+' onMouseOver="selectBoxes.'+this.uid+'.highlightOption('+x+')"'	
		+'">'
		+this.options[x].text
		+'</DIV>'
	}
	return out
}

SelectBox.prototype.setWidth=function setWidth()
{
	var element=document.getElementById('selectOptions')
	element.style.position=''
	element.style.width=''
	element.style.height=''
	element.style.position='absolute'
	this.highlightIndex=this.selectedIndex
	element.innerHTML=this.getOptions()
	
	this.height=element.offsetHeight
	var width=element.offsetWidth
	
	if(this.flags & F_SELECT_EDITABLE)
	{
		width2=width+sizes.selectTextEdit.outerWidth-((floatFix)?sizes.selectTextEdit.paddingWidth+sizes.selectTextEdit.borderWidth+1:0)
		document.getElementById('selectTextEdit_'+this.uid).style.width=width2+'px'
		width+=sizes.selectButton.width+sizes.selectText.paddingWidth+sizes.selectText.marginWidth+sizes.selectText.borderWidth-sizes.selectOptions.borderWidth;
		this.width=Math.max(0,width-sizes.selectOptions.outerWidth);
	}
	else
	{
		document.getElementById('selectText_'+this.uid).style.width=width+'px'
		width+=sizes.selectButton.width+sizes.selectText.paddingWidth+sizes.selectText.marginWidth+sizes.selectText.borderWidth-sizes.selectOptions.borderWidth;
		this.width=Math.max(0,width-sizes.selectOptions.outerWidth);
	}
	element=document.getElementById('selectHead_'+this.uid)
	element.style.left='';
	element.style.top='';
	element.style.width=width+'px'
}

SelectBox.prototype.draw=function draw()
{
	var element=document.getElementById(this.container)
	element.innerHTML=this.get()
	this.setWidth();
	this.displayed=true;
}


SelectBox.prototype.toggleSelect=function toggleSelect(e)
{
	if(window.getSelection)window.getSelection().removeAllRanges();
	else if(document.selection)document.selection.empty();
	if(!this.disabled)
	{
		if(this.selectOpen)
		{
			var element=document.getElementById('selectOptions')
			element.style.visibility='hidden'
			if(this.selectClickOutsideHandler)
			{
				this.selectClickOutsideHandler.remove()	
				this.selectClickOutsideHandler=null;
			}
			this.selectOpen=false;
		}
		else
		{
			
			var element=document.getElementById('selectHead_'+this.uid)
			element.style.position='relative';
			
			if(!(sections[this.section].flags & SECTION_NOSCROLL) && !ieRelative)
			{
				var element2=document.getElementById(this.section);
				var scrollLeft=element2.scrollLeft;
				var scrollTop=element2.scrollTop;
			}
			else
			{
				var scrollLeft=0;
				var scrollTop=0;
			}
			
			
			var top=element.offsetTop+sizes.section.outerAllTop+sizes.bar.height+sizes.tab.height+sections[this.section].parent.size.top+sizes.selectText.height-scrollTop+sizes.container.outerTop;
			var left=element.offsetLeft+sizes.section.outerAllLeft+sections[this.section].parent.size.left-scrollLeft+sizes.container.outerLeft;
			
			if(ieRelative)element.style.position=''
			element=document.getElementById('selectOptions')
			element.innerHTML=this.getOptions()
			element.style.width=this.width+'px';
			var bottom=top+this.height
			if(window.innerHeight)
			{
				top=(window.innerHeight>bottom)?top:(top-this.height-sizes.selectText.height);
			}
			else
			{
				var findElement=element;
				do{findElement=findElement.offsetParent;}
				while(findElement.tagName!='HTML' && findElement.tagName!='BODY')
				top=(findElement.offsetHeight>bottom)?top:(top-this.height-sizes.selectText.height);
			}
			
			element.style.top=top+'px'
			element.style.left=left+'px'
			element.style.zIndex=sectionGroupsCount+1
			this.selectClickOutsideHandler=events.addListener('mousedown',new Function('e','selectBoxes.'+this.uid+'.selectClickedOutside(e)'))
			this.highlightIndex=this.selectedIndex
			element.style.visibility='visible';
			this.selectOpen=true;
		}
	}
	if(typeof e!='undefined')
	{
		if(e.stopPropagation)e.stopPropagation();
		else e.cancelBubble=true;
	}
}

SelectBox.prototype.selectClickedOutside=function selectClickedOutside(e)
{
	if(!this.isHead)
	{
		this.selectClickOutsideHandler.remove()
		this.selectClickOutsideHandler=null;
		if(this.selectOpen)this.toggleSelect()
	}
}

SelectBox.prototype.selectOption=function selectOption(index,e)
{
	var call;
	if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).value=this.options[index].text;
	else document.getElementById('selectText_'+this.uid).innerHTML=this.options[index].text;
	this.selectedIndex=index;
	if(this.onchange)setTimeout('selectBoxes.'+this.uid+'.onchange('+objToJs(this.options[index])+')',0);
	if(this.selectOpen)this.toggleSelect()
	if(e.stopPropagation)e.stopPropagation();
	else e.cancelBubble=true;
}

SelectBox.prototype.highlightOption=function highlightOption(index)
{
	if(index!=this.highlightIndex)
	{
		document.getElementById('selectOption_'+this.uid+'_'+this.highlightIndex).className="selectOption";
		document.getElementById('selectOption_'+this.uid+'_'+index).className="selectOptionSelected";
		this.highlightIndex=index;
	}
}

SelectBox.prototype.selectIndex=function selectIndex(index)
{
	this.selectedIndex=index;
	if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).value=this.options[index].text;
	else document.getElementById('selectText_'+this.uid).innerHTML=this.options[index].text;

}

SelectBox.prototype.selectValue=function selectValue(value)
{
	var found=false;
	for(var x=0;x<this.options.length;x++)
	{
		if(this.options[x].value==value)
		{
			found=true;
			break;
		}
	}
	if(found)
	{
		this.selectedIndex=x;
		if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).value=this.options[x].text;
		else document.getElementById('selectText_'+this.uid).innerHTML=this.options[x].text;
	}
}

SelectBox.prototype.setValue=function setValue(value)
{
	this.options[this.selectedIndex].value=value
	if(this.onoptionchange)setTimeout('selectBoxes.'+this.uid+'.onoptionchange('+objToJs(this.options[this.selectedIndex])+')',0);
	this.setWidth();
}


SelectBox.prototype.setDisabled=function setDisabled(onOff)
{
	this.disabled=onOff;
	if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).disabled=onOff;
	else document.getElementById('selectText_'+this.uid).className='selectText'+((onOff)?'Disabled'+((isIE)?'IE':''):'');
}

SelectBox.prototype.addOption=function addOption(opt)
{
	this.options.push(opt)
	this.draw()
}

SelectBox.prototype.removeOption=function removeOption(index)
{
	this.options.splice(index,1)
	if(this.selectedIndex==index)this.selectedIndex=0
	this.draw()
}

SelectBox.prototype.removeOptionValue=function removeOptionValue(value)
{
	for(var index=0;index<this.options.length;index++)
	{
		if(this.options[index].value==value)
		{
			this.options.splice(index,1)
			if(this.selectedIndex==index)this.selectedIndex=0
			else if(this.selectedIndex>index)this.selectedIndex--
			break;
		}
	}
	this.draw()
}

SelectBox.prototype.setOption=function setOption(index,opt)
{
	this.options[index]=opt;
	if(this.selectedIndex==index)
	{
		if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).value=this.options[index].text;
		else document.getElementById('selectText_'+this.uid).innerHTML=this.options[index].text;
	}
	this.setWidth();
}

SelectBox.prototype.setOptionValue=function setOptionValue(value,opt)
{
	for(var index=0;index<this.options.length;index++)
	{
		if(this.options[index].value==value)
		{
			this.options[index]=opt;
			break;
		}
	}
	if(this.selectedIndex==index)
	{
		if(this.flags & F_SELECT_EDITABLE)document.getElementById('selectTextEdit_'+this.uid).value=this.options[index].text;
		else document.getElementById('selectText_'+this.uid).innerHTML=this.options[index].text;
	}
	this.setWidth();
}

SelectBox.prototype.setOptions=function setOptions(opts)
{
	this.options=opts;
	if(typeof this.selectedIndex=='object')this.selectedIndex=0;
	if(typeof this.highlightIndex=='object')this.highlightIndex=0;
}
