var SB_ARR = new Array();

function SB_cancelEvent (event)
{
	event.cancelBubble = true;
	event.returnValue = false;
}

function SB_init(srcHTML, ListMax)
{
	this.ssID = SB_ARR.length;

	this.CR = new Object();
	this.CR.ReverseBackground = '#225588';
	this.CR.ReverseText = 'white';
	this.CR.Border = '#808080';
	this.CR.BorderActive = '#225588';
	this.ImgPrefix = 'http://image.club5678.com/imgs/plug/040906/';
	this.DefaultHeight = 20;
	this.ButtonWidth = 13;
	this.OptionsDivStyle = ''
	+ ' display:none;'
	+ ' z-index:10;'
	+ ' position:relative;'
	+ ' border:1 solid '+ this.CR.Border+';'
	+ ' background-color:white;'
	+ ' scrollbar-face-color:#D4D0C8;'
	+ ' scrollbar-shadow-color:white;'
	+ ' scrollbar-highlight-color:#F6F5F4;'
	+ ' scrollbar-3dlight-color:white'
	+ ' scrollbar-darkshadow-color:#86837E;'
	+ ' scrollbar-track-color:#F6F5F4;'
	+ ' scrollbar-arrow-color:#86837E;';

	this.OptionNobrStyle = ''
	+ ' font-size:10pt;'
	+ ' font-family:굴림;';

	this.DivDummy = document.createElement("DIV");
	this.handleOverTitle = SB_handleOverTitle;
	this.handleOutTitle = SB_handleOutTitle;
	this.releaseTitle = SB_releaseTitle;
	this.pressTitle = SB_pressTitle;
	this.toggleOptions = SB_toggleOptions;
	this.toggleTitle = SB_toggleTitle;
	this.handleOverOption = SB_handleOverOption;
	this.blurExcept = SB_blurExcept;
	this.moveOption = SB_moveOption;
	this.releaseOption = SB_releaseOption;
	this.handleMousewheel = SB_handleMousewheel;
	this.syncSelectedIndex = SB_syncSelectedIndex;
	this.adjustOptionsDiv = SB_adjustOptionsDiv;

	// private method
	this.createElement = SB_createElement;
	this.createTable = SB_createTable;
	this.createOptionTr = SB_createOptionTr;
	this.syncOptions = SB_syncOptions;
	this.prt = SB_prt;
	this.rePrt = SB_rePrt;

	// property
	this.select = this.createElement(srcHTML);
	this.selectedIndex = this.select.selectedIndex;
	this.options = this.select.options;
	this.width = parseInt(this.select.style.width);
	this.optionsLen = this.options.length;
	this.height = (this.select.style.height) ? parseInt(this.select.style.height) : this.DefaultHeight;
	this.OptionHeight = this.height - 4;
	this.bListDown = (ListMax && ListMax.toString().substr(0, 1) == '-') ? false : true;
	this.ListMax = (!isNaN(parseInt(ListMax))) ? Math.abs(ListMax) : this.options.length;

	this.Table;
	this.TitleTable;
	this.TitleWrapper;
	this.OptionsDiv;
	this.OptionsWrapper;
	this.OptionsTable;
	this.bFocused = false;
	this.bExpanded = false;
	this.bReverse = false;

	this.select.setAttribute('SS', this);
	this.select.onpropertychange = SB_handlePropertychange;
}

//단계생성 : 1단계 - 일단 셀렉트 박스 초기화를 한다.
function SB_start(srcHTML, ListMax)
{
	var oSS = new SB_init(srcHTML, ListMax);
	return oSS;
}
//단계생성 : 2단계 - 각종 초기값을 세팅한다.
//단계생성 : 3단계 - 초기화된 셀렉트박스와 2단계에서 수정한 초기값으로 출력한다.
function SB_prt(tmpDiv)
{
	// initiate
	this.createTable();
	
	if(tmpDiv){
		var myDiv = eval("document.all."+tmpDiv);
		myDiv.innerHTML = '';
		myDiv.insertAdjacentElement('afterBegin', this.Table);
		myDiv.removeNode();
	}else{
		document.write("<div id=SB_TempDiv></div>");
		document.all.SB_TempDiv.insertAdjacentElement('afterBegin', this.Table);
		document.all.SB_TempDiv.removeNode();
	}
}	

//단번생성 : 하나의 함수 호출로 전체 출력할때
function SB_write(srcHTML, ListMax, tmpDiv)
{
	var oSS = new SB_init(srcHTML, ListMax);
	oSS.prt(tmpDiv);
	
	return oSS;
}

function SB_createElement(html)
{
	this.DivDummy.insertAdjacentHTML('afterBegin', html);
	var oEl = this.DivDummy.children(0);
	while (this.DivDummy.children.length > 0) {
		this.DivDummy.removeChild(this.DivDummy.children(0));
	}
	return oEl;
}

function SB_createTable()
{
	this.OptionsDiv = this.createElement(""
		+ "<div style='"+this.OptionsDivStyle+"'"
		+ "	onscroll='SB_ARR["+this.ssID+"].moveOption(window.event)'"
		+ "	onmousedown='SB_cancelEvent(window.event)'"
		+ ">"
		+ "	<table border=0 cellpadding=0 cellspacing=0 width=100% style='table-layout:fixed'>"
		+ "	</table>"
		+ "</div>"
	);
	this.OptionsTable = this.OptionsDiv.childNodes(0);
	for (var i=0; i < this.options.length; i++) {
		this.createOptionTr(i);
	}
	this.syncOptions();
	this.OptionsWrapper = document.createElement(""
		+ "<img src='"+this.ImgPrefix+"/img_blank.gif'"
		+ "	style='position:absolute; top:0; left:0; width:100%;'"
		+ "	onmousedown='SB_cancelEvent(window.event)'"
		+ "	onmousemove='SB_ARR["+this.ssID+"].moveOption(window.event)'"
		+ "	onmouseup='SB_ARR["+this.ssID+"].releaseOption(window.event)'"
		+ "	onmouseout='SB_ARR["+this.ssID+"].releaseOption(window.event)'"
		+ "	ondragstart='SB_cancelEvent(window.event)'"
		+ " border=0>"
	);
	this.OptionsDiv.appendChild(this.OptionsWrapper);
	this.adjustOptionsDiv();

	this.TitleTable = this.createElement(""
		+ "<table border=0 cellpadding=0 cellspacing=1"
		+ "	height="+this.height
		+ "	bgcolor=white"
		+ "	style='table-layout:fixed; border:1 solid "+this.CR.Border+";'"
		+ ">"
		+ "<tr>"
		+ "	<td style='line-height:16px'><nobr style='text-oveflow:hidden;"+this.OptionNobrStyle+"'></nobr></td>"
		+ "	<td width="+this.ButtonWidth+" align=center style='word-wrap:normal;'></td>"
		+ "	<td style='display:none' width="+this.ButtonWidth+" align=center style='word-wrap:normal'></td>"
		+ "	<td style='display:none'></td>"
		+ "</tr>"
		+ "</table>"
	);
	this.TitleTable.cells(0).childNodes(0).innerText = this.options[this.selectedIndex].innerText;
	this.TitleTable.cells(1).innerHTML = "<img src='"+this.ImgPrefix+"/btn_down.gif' border=0 align=absmiddle>";
	this.TitleTable.cells(2).innerHTML = "<img src='"+this.ImgPrefix+"/btn_down_s.gif' border=0 align=absmiddle>";
	this.TitleTable.cells(3).appendChild(this.select);
	this.TitleWrapper = document.createElement(""
		+ "<img src='"+this.ImgPrefix+"/img_blank.gif' "
		+ "	style='position:relative; top:-"+this.height+"; left:0; z-index:2; width:100%; height:"+this.height+";'"
		+ "	onmouseover='SB_ARR["+this.ssID+"].handleOverTitle()'"
		+ "	onmouseout='SB_ARR["+this.ssID+"].handleOutTitle(); SB_ARR["+this.ssID+"].releaseTitle(window.event);'"
		+ "	onmousedown='SB_ARR["+this.ssID+"].pressTitle(window.event)'"
		+ "	ondblclick='SB_ARR["+this.ssID+"].pressTitle(window.event); SB_ARR["+this.ssID+"].releaseTitle(window.event);'"
		+ "	onmouseup='SB_ARR["+this.ssID+"].releaseTitle(window.event)'"
		+ "	onmousewheel='SB_ARR["+this.ssID+"].handleMousewheel(window.event)'"
		+ "	ondragstart='SB_cancelEvent(window.event)'"
		+ " border=0>"
	);
	this.TitleWrapper.setAttribute('SB', this);

	this.Table = this.createElement(""
		+ "<table border=0 cellpadding=0 cellspacing=0 style='table-layout:fixed; cursor:default'>"
		+ "<tr height="+this.height+">"
		+ "	<td></td>"
		+ "</tr>"
		+ "</table>"
	);
	if (!isNaN(this.width)) {
		this.Table.style.width = this.width;
	}
	this.Table.cells(0).appendChild(this.TitleTable);
	this.Table.cells(0).appendChild(this.TitleWrapper);
	this.Table.cells(0).appendChild(this.OptionsDiv);

	SB_ARR[this.ssID] = this;

	return this;
}
function SB_createOptionTr(idx)
{
	idx = (typeof(idx) != 'undefined') ? idx : this.options.length - 1;
	var OptionTr = this.OptionsTable.insertRow(-1);
	var OptionTd = document.createElement("<td height="+this.OptionHeight+"></td>");
	OptionTd.appendChild(document.createElement("<nobr style='"+this.OptionNobrStyle+"'></nobr>"));
	OptionTr.appendChild(OptionTd);
}

function SB_syncOptions ()
{
	var delRow = '';
	var len = Math.max(this.optionsLen, this.options.length);
	
	for (var i=0; i < len; i++) {
		if(i >= this.optionsLen) this.createOptionTr(i);
		this.OptionsTable.cells(i).setAttribute('index', i);
		
		if(i >= this.options.length) delRow = ','+i+delRow;
		else {
			if (this.OptionsTable.cells(i).childNodes(0).innerText != this.options[i].innerText)
				this.OptionsTable.cells(i).childNodes(0).innerText  = this.options[i].innerText;
		}
	}
	
	if(delRow)
	{
		var sss = delRow.substr(1).split(',');
		for(i = 0; i < sss.length; i++)
		{
			this.OptionsTable.deleteRow(parseInt(sss[i]));
		}
	}
	this.optionsLen = this.options.length;
}

function SB_handleOverTitle ()
{
	if (this.bExpanded)
		return;

	this.TitleTable.style.borderColor = this.CR.BorderActive;
	this.TitleTable.cells(1).style.display = 'none';
	this.TitleTable.cells(2).style.display = 'block';
}
function SB_handleOutTitle ()
{
	this.TitleTable.style.borderColor = this.CR.Border;
	this.TitleTable.cells(2).style.display = 'none';
	this.TitleTable.cells(1).style.display = 'block';
}
function SB_releaseTitle (event)
{
	SB_cancelEvent(event);

	if (SB_isThisEventToBeCanceled(event)) return;
	if (!this.bTitleOnAction) return;

	this.bTitleOnAction = false;
}
function SB_isThisEventToBeCanceled (event)
{
	if ('object' == typeof(event)) {
		switch (event.type) {
			case 'mousedown':
			case 'mouseup':
				if (!(event.button & 1))
					return true;
				break;
			case 'mouseout':
				//if (SB_ENV.IE_Version >= 5.5)
					return true;
				break;
		}
	}
	return false;
}
function SB_pressTitle(event)
{
	SB_cancelEvent(event);

	if (this.bTitleOnAction) return;

	this.bTitleOnAction = true;
	this.toggleOptions();
}
function SB_toggleOptions (bExpanded, bStrict)
{
	if (!bStrict && !this.bFocused) {
		this.blurExcept(this.ssID);
	}
	this.bExpanded = ('undefined'!=typeof(bExpanded)) ? bExpanded: (!this.bExpanded);
	if (this.bExpanded) {
		this.OptionsDiv.style.display = 'block';
		if (!bStrict) {
			this.toggleTitle(false);
			this.handleOverOption(this.selectedIndex);
		}
		this.handleOutTitle();
	}
	else {
		this.OptionsDiv.style.display = 'none';
		if (!bStrict) {
			this.toggleTitle(true);
		}
	}

	if (!bStrict) {
		this.bFocused = true;

		if (!this.bEventAttached) {
			document.attachEvent('onmousedown', this.blurExcept);
			document.attachEvent('ondblclick', this.blurExcept);
			this.bEventAttached = true;
		}
	}
}
function SB_toggleTitle (bReverse)
{
	this.bReverse = ('undefined'!=typeof(bReverse)) ? bReverse: (!this.bReverse);
	this.TitleTable.cells(0).style.backgroundColor = this.bReverse ? this.CR.ReverseBackground : '';
	this.TitleTable.cells(0).style.color = this.bReverse ? this.CR.ReverseText : '';
}
function SB_blurExcept (except)
{
	SB_cancelEvent(window.event);

	except = ('number'==typeof(except)) ? except : -1;

	var bHasToDetachEvent = true;
	for (var i=0; i < SB_ARR.length; i++) {
		if (-1==except && SB_ARR[i].bFocused && SB_ARR[i].bExpanded) {
			SB_ARR[i].toggleOptions(false, true);
			SB_ARR[i].toggleTitle(true);
			bHasToDetachEvent = false;
		}
		else if (i!=except) {
			if (SB_ARR[i].bExpanded)
				SB_ARR[i].toggleOptions(false, true);
			if (SB_ARR[i].bReverse)
				SB_ARR[i].toggleTitle(false);
			SB_ARR[i].bFocused = false;
		}
	}

	if (this.bEventAttached && bHasToDetachEvent) {
		document.detachEvent('onmousedown', SB_blurExcept);
		document.detachEvent('ondblclick', SB_blurExcept);
		this.bEventAttached = false;
	}
}
function SB_handleOverOption(idx)
{
	for (var i=0; i < this.options.length; i++) {
		if (i==idx)
		{
			this.OptionsTable.cells(i).style.color = this.CR.ReverseText;
			this.OptionsTable.cells(i).style.backgroundColor = this.CR.ReverseBackground;
		}
		else
		{
			this.OptionsTable.cells(i).style.color = '';
			this.OptionsTable.cells(i).style.backgroundColor = '';
		}
	}
}
function SB_moveOption (event)
{
	SB_cancelEvent(event);

	if (!(event.offsetX >= 0 && event.offsetX <= this.OptionsTable.offsetWidth)) return;

	this.handleOverOption(Math.floor(event.offsetY / this.OptionHeight));
}
function SB_releaseOption (event)
{
	SB_cancelEvent(event);

	if (SB_isThisEventToBeCanceled(event)) return;

	if (event.offsetX >= 0 && event.offsetX <= this.OptionsTable.offsetWidth) {
		this.toggleOptions(false);
		this.select.selectedIndex = Math.floor(event.offsetY / this.OptionHeight);
	}
}
function SB_handleMousewheel (event)
{
	var idx = this.selectedIndex;
	if ('mousewheel'==event.type && this.bFocused && this.bReverse) {
		for (var i=0; i < event.wheelDelta; i += 120)
			idx--;
		for (var i=0; i > event.wheelDelta; i -= 120)
			idx++;
	}
	idx = Math.max(idx, 0);
	idx = Math.min(idx, this.options.length - 1);
	this.select.selectedIndex = idx;
}
function SB_handlePropertychange ()
{
	if ('propertychange'==window.event.type && 'selectedIndex'==window.event.propertyName) {
		var oSS = window.event.srcElement.SS;
		if(oSS.select.options.length)
		{
			oSS.syncSelectedIndex();
			if (null != oSS.select.onchange) oSS.select.onchange();
		}
	}
}
function SB_syncSelectedIndex ()
{
	this.selectedIndex = this.select.selectedIndex;

	if (this.TitleTable.cells(0).childNodes(0).innerText != this.options[this.selectedIndex].innerText) {
		this.TitleTable.cells(0).childNodes(0).innerText = this.options[this.selectedIndex].innerText;
	}
	if (this.bExpanded) {
		this.toggleOptions(false);
	}
}
function SB_adjustOptionsDiv ()
{
	this.OptionsDiv.style.height = Math.min(this.options.length, this.ListMax) * this.OptionHeight + 2;
	this.OptionsWrapper.style.height = this.options.length * this.OptionHeight;
	this.OptionsDiv.style.overflowY = (this.options.length > this.ListMax) ? 'scroll' : '';
	if (!this.bListDown) {
		this.OptionsDiv.style.top = -this.height - (this.height+parseInt(this.OptionsDiv.style.height));
	}
	else {
		this.OptionsDiv.style.top = -this.height;
	}
}

function SB_rePrt()
{
	this.syncOptions();
	this.adjustOptionsDiv();
	this.syncSelectedIndex();
}	