var CONS = new Object();
CONS.IE_Version = new Number(((window.navigator.appVersion.split('; '))[1].split(' '))[1]);
CONS.CR = new Object();

// ÆîÄ§ ¸Þ´º ¸ñ·ÏÀÇ °¢ ³ôÀÌ
CONS.DefaultHeight = 19;
// ÆîÄ§ ¸Þ´º ¸ñ·ÏÀÇ »ï°¢Çü ¾ÆÀÌÄÜÀÇ ³Êºñ
CONS.ButtonWidth = 13;
// ÆîÄ§ ¸Þ´º ¸ñ·Ï¿¡ ¸¶¿ì½º ¿À¹ö½Ã ¹Ù²î¾î Áö´Â ÅØ½ºÆ® ¹è°æ »ö»ó
CONS.CR.ReverseBackground = '#8C8B8B';
// ÆîÄ§ ¸Þ´º ¸ñ·Ï¿¡ ¸¶¿ì½º ¿À¹ö½Ã ¹Ù²î¾îÁö´Â ÅØ½ºÆ® »ö»ó
CONS.CR.ReverseText = 'white';
// ÆîÄ§ ¸Þ´º ¸ñ·Ï Å×µÎ¸® »ö»ó
CONS.CR.Border = '#A8A8A8';
CONS.CR.BorderActive = '#A8A8A8';
// ÆîÄ§ ¸Þ´º ¸ñ·Ï ¾ÆÀÌÄÜÀÌ µé¾î ÀÖ´Â °æ·Î ¼³Á¤
CONS.ImgPrefix = './images';
CONS.OptionsDivStyle = "\
  display:none;\
  z-index:10;\
  position:absolute;\
  border:1 solid "+ CONS.CR.Border+";\
  background-color:#A8A8A8;\
  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;";
CONS.OptionNobrStyle = "\
  font-size:9pt;\
  font-color:#ffffff;\
  font-family:±¼¸²;";
var VAR = new Object();
VAR.DivDummy = document.createElement("DIV");
VAR.SelectList = new Array();
VAR.bEventAttached = false;

var CreatedElements = new Object();

function unloadObjects()
{
  try {
    if (VAR && VAR.SelectList)
    {
      for (key in VAR.SelectList)
      {
	if (VAR.SelectList[key])
	{
	  try {
	   VAR.SelectList[key].select.setAttribute('SS', 0);
	  } catch (e) {};
	  delete VAR.SelectList[key];
	}
      }
    }
  } catch (e) {};
}

attachEvent("onunload", unloadObjects);

function create (srcHTML, ListMax, bAutoDetect)
{
  this.ssID = VAR.SelectList.length;
  this.bOriginalSelect = (bAutoDetect && CONS.IE_Version < 5.5);
  this.select = createElement(srcHTML);
  this.selectedIndex = this.select.selectedIndex;
  this.options = this.select.options;
  this.width = parseInt(this.select.style.width);
  this.height = (this.select.style.height) ? parseInt(this.select.style.height) : CONS.DefaultHeight;
  this.OptionHeight = this.height - 4;
  this.bListDown = (ListMax && '-'==ListMax.toString().substr(0, 1)) ? false : true;
  this.ListMax = (!isNaN(parseInt(ListMax))) ? Math.abs(ListMax) : 100;

  this.Table;
  this.TitleDiv;
  this.TitleTable;
  this.TitleWrapper;
  this.OptionsDiv;
  this.OptionsWrapper;
  this.OptionsTable;
  this.bFocused = false;
  this.bExpanded = false;
  this.bReverse = false;

  this.isThisEventToBeCanceled = isThisEventToBeCanceled;
  this.toggleTitle = toggleTitle;
  this.syncSelectedIndex = syncSelectedIndex;
  this.toggleOptions = toggleOptions;
  this.turnOnOption = turnOnOption;
  this.turnOffOption = turnOffOption;
  this.handleMousewheel = handleMousewheel;
  this.handleOverTitle = handleOverTitle;
  this.handleOutTitle = handleOutTitle;
  this.handleOverOption = handleOverOption;
  this.createTable = createTable;
  this.createTitleDiv = createTitleDiv;
  this.checkOptionsDiv = checkOptionsDiv;
  this.createOptionsDiv = createOptionsDiv;
  this.createOptionTr = createOptionTr;
  this.adjustOptionsDiv = adjustOptionsDiv;
  this.syncOptions = syncOptions;
  this.pressOption = pressOption;
  this.moveOption = moveOption;
  this.releaseOption = releaseOption;
  this.pressTitle = pressTitle;
  this.releaseTitle = releaseTitle;


  this.display = display;
  this.insertOption = insertOption;
  this.deleteOption = deleteOption;
  this.changeOption = changeOption;

  this.createTable();
  this.select.setAttribute('SS', this);
  if (!this.bOriginalSelect)
    this.select.onpropertychange = handlePropertychange;
  VAR.SelectList[this.ssID] = this;
}
function display ()
{
  document.write("<div id=TempDiv></div>\n");
  document.all.TempDiv.appendChild(this.Table);
  document.all.TempDiv.removeNode();
}
function listArr (srcHTML, ListMax, bAutoDetect)
{
  var oSS = new create(srcHTML, ListMax, bAutoDetect);
  oSS.display();
  return oSS;
}
function insertOption (value, innerText, idx)
{
  this.checkOptionsDiv();

  var NewOption = document.createElement("OPTION");
  CreatedElements[CreatedElements.length] = NewOption;
  this.options.add(NewOption, idx);
  NewOption.innerText = innerText;
  NewOption.value = value;

  if (!this.bOriginalSelect)
    this.createOptionTr(idx);
  this.syncOptions();
  this.adjustOptionsDiv();
  this.syncSelectedIndex();
}
function deleteOption (idx)
{
  this.checkOptionsDiv();

  this.options.remove(idx);
  if (!this.bOriginalSelect)
    this.OptionsTable.deleteRow(idx);
  this.syncOptions();
  this.adjustOptionsDiv();
  this.syncSelectedIndex();
}
function changeOption (idx, value, innerText)
{
  this.checkOptionsDiv();

  this.options[idx].value = value;
  this.options[idx].innerText = innerText;
  this.syncOptions();
  this.syncSelectedIndex();
}

function cancelEvent (event)
{
  event.cancelBubble = true;
  event.returnValue = false;
}
function isThisEventToBeCanceled (event)
{
  if ('object' == typeof(event)) {
    switch (event.type) {
      case 'mousedown':
        if (!(event.button & 1)) return true;
        break;
      case 'mouseup':
        if (!(event.button & 1)) return true;
        if (CONS.IE_Version >= 5.5 && event.srcElement != this.srcElementOfLastMousedown && this.srcElementOfLastMousedown != null) {
          this.srcElementOfLastMousedown = null;
          return true;
        }
        break;
      case 'mouseout':
        if (!(CONS.IE_Version < 5.5 && event.srcElement == this.srcElementOfLastMousedown))
          return true;
        break;
      case 'mousemove':
        if (CONS.IE_Version >= 5.5 && event.srcElement != this.srcElementOfLastMousedown && this.srcElementOfLastMousedown != null)
          return true;
        break;
    }
  }
  return false;
}
function createElement (html)
{
  VAR.DivDummy.insertAdjacentHTML('afterBegin', html);
  var oEl = VAR.DivDummy.children(0);
  while (VAR.DivDummy.children.length > 0) {
    VAR.DivDummy.removeChild(VAR.DivDummy.children(0));
  }
  return oEl;
}
function blurExcept (except)
{
  cancelEvent(window.event);

  except = ('number'==typeof(except)) ? except : -1;

  var bHasToDetachEvent = true;
  for (var i=0; i < VAR.SelectList.length; i++) {
    if (-1==except && VAR.SelectList[i].bFocused && VAR.SelectList[i].bExpanded) {
      VAR.SelectList[i].toggleOptions(false, true);
      VAR.SelectList[i].toggleTitle(true);
      bHasToDetachEvent = false;
    }
    else if (i!=except) {
      if (VAR.SelectList[i].bExpanded)
        VAR.SelectList[i].toggleOptions(false, true);
      if (VAR.SelectList[i].bReverse)
        VAR.SelectList[i].toggleTitle(false);
      VAR.SelectList[i].bFocused = false;
    }
  }

  if (VAR.bEventAttached && bHasToDetachEvent) {
    document.detachEvent('onmousedown', blurExcept);
    document.detachEvent('ondblclick', blurExcept);
    VAR.bEventAttached = false;
  }
}
function syncSelectedIndex ()
{
  this.selectedIndex = this.select.selectedIndex;

  if (this.bOriginalSelect) return;

  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 toggleTitle (bReverse)
{
  this.bReverse = ('undefined'!=typeof(bReverse)) ? bReverse: (!this.bReverse);
  this.TitleTable.cells(0).style.backgroundColor = this.bReverse ? CONS.CR.ReverseBackground : '';
  this.TitleTable.cells(0).style.color = this.bReverse ? CONS.CR.ReverseText : '';
}
function checkOptionsDiv ()
{
  if (!this.OptionsDiv) {
    this.createOptionsDiv();
    this.Table.cells(0).appendChild(this.OptionsDiv);
  }
}
function toggleOptions (bExpanded, bStrict)
{
  this.checkOptionsDiv();

  if (!bStrict && !this.bFocused) {
    blurExcept(this.ssID);
  }
  this.bExpanded = ('undefined'!=typeof(bExpanded)) ? bExpanded: (!this.bExpanded);
  if (this.bExpanded) {
    this.adjustOptionsDiv();
    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 (!VAR.bEventAttached) {
      document.attachEvent('onmousedown', blurExcept);
      document.attachEvent('ondblclick', blurExcept);
      VAR.bEventAttached = true;
    }
  }
}
function handlePropertychange ()
{
  if ('propertychange'==window.event.type && 'selectedIndex'==window.event.propertyName) {
    var oSS = window.event.srcElement.SS;
    oSS.syncSelectedIndex();

    if (null != oSS.select.onchange)
      oSS.select.onchange();
  }
}
function 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 handleOverTitle ()
{
  if (this.bExpanded)
    return;

  this.TitleTable.style.borderColor = CONS.CR.BorderActive;
  this.TitleTable.cells(1).style.display = 'none';
  this.TitleTable.cells(2).style.display = 'block';
}
function handleOutTitle ()
{
  this.TitleTable.style.borderColor = CONS.CR.Border;
  this.TitleTable.cells(2).style.display = 'none';
  this.TitleTable.cells(1).style.display = 'block';
}
function handleOverOption (idx)
{
  for (var i=0; i < this.options.length; i++) {
    if (i==idx)
      this.turnOnOption(i);
    else
      this.turnOffOption(i);
  }
}
function turnOnOption (idx)
{
  this.OptionsTable.cells(idx).style.color = CONS.CR.ReverseText;
  this.OptionsTable.cells(idx).style.backgroundColor = CONS.CR.ReverseBackground;
}
function turnOffOption (idx)
{
  this.OptionsTable.cells(idx).style.color = '';
  this.OptionsTable.cells(idx).style.backgroundColor = '';
}
function adjustOptionsDiv ()
{
  if (this.bOriginalSelect) return;

  this.OptionsDiv.style.width = this.width;
  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' : '';

  var top = this.Table.offsetTop;
  var left = this.Table.offsetLeft;
  for (var El = this.Table.offsetParent; 'BODY'!=El.tagName && 'absolute'!=El.style.position && 'relative'!=El.style.position; El = El.offsetParent) {
    if ('TABLE' != El.tagName) {
      top += El.clientTop;
      left += El.clientLeft;
    }
    top += El.offsetTop;
    left += El.offsetLeft;
  }
  this.OptionsDiv.style.top = (this.bListDown) ? (top + this.height) : (top - parseInt(this.OptionsDiv.style.height));
  this.OptionsDiv.style.left = left;

  this.TitleWrapper.style.top = 0;
  this.TitleWrapper.style.left = 0;
}
function syncOptions ()
{
  if (this.bOriginalSelect) return;

  for (var i=0; i < this.options.length; i++) {
    this.OptionsTable.cells(i).setAttribute('index', i);
    if (this.OptionsTable.cells(i).childNodes(0).innerText != this.options[i].innerText)
      this.OptionsTable.cells(i).childNodes(0).innerText = this.options[i].innerText;
  }
}
function pressTitle (event)
{
  cancelEvent(event);

  this.srcElementOfLastMousedown = event.srcElement;

  this.toggleOptions();
}
function releaseTitle (event)
{
  cancelEvent(event);

  if (this.isThisEventToBeCanceled(event)) return;

  this.srcElementOfLastMousedown = null;
}
function pressOption (event)
{
  cancelEvent(event);

  this.srcElementOfLastMousedown = event.srcElement;
}
function moveOption (event)
{
  cancelEvent(event);

  if (this.isThisEventToBeCanceled(event)) return;
  if (!(event.offsetX >= 0 && event.offsetX <= this.OptionsTable.offsetWidth)) return;

  this.handleOverOption(Math.floor(event.offsetY / this.OptionHeight));
}
function releaseOption (event)
{
  cancelEvent(event);

  if (this.isThisEventToBeCanceled(event)) return;

  this.srcElementOfLastMousedown = null;

  if (event.offsetX >= 0 && event.offsetX <= this.OptionsTable.offsetWidth) {
    this.toggleOptions(false);
    this.select.selectedIndex = Math.floor(event.offsetY / this.OptionHeight);
  }
}
function createTable ()
{
  this.Table = createElement("\
    <table border=0 cellpadding=0 cellspacing=0 style='table-layout:fixed; cursor:default'>\
    <tr><td></td></tr>\
    </table>"
  );
  if (!isNaN(this.width))
    this.Table.style.width = this.width;
  this.Table.style.height = this.height;

  if (!this.bOriginalSelect) {
    this.createTitleDiv();
    this.Table.cells(0).appendChild(this.TitleDiv);
  }
  else {
    this.Table.cells(0).appendChild(this.select);
  }
}
function createTitleDiv ()
{
  this.TitleDiv = createElement("\
    <div style='position:relative; top:0; left:0;'>\
      <table border=0 cellpadding=0 cellspacing=1\
        height="+this.height+"\
        bgcolor=#A8A8A8\
        style='table-layout:fixed; border:1 solid "+CONS.CR.Border+";'\
        onmouseover='VAR.SelectList["+this.ssID+"].adjustOptionsDiv()'\
      >\
      <tr>\
        <td><nobr style='text-oveflow:hidden;"+CONS.OptionNobrStyle+"'></nobr></td>\
        <td width="+CONS.ButtonWidth+" align=center style='word-wrap:normal'></td>\
        <td style='display:none' width="+CONS.ButtonWidth+" align=center style='word-wrap:normal'></td>\
        <td style='display:none'></td>\
      </tr>\
      </table>\
    </div>"
  );
  this.TitleTable = this.TitleDiv.childNodes(0);
  this.TitleTable.cells(0).childNodes(0).innerText = this.options[this.selectedIndex].innerText;
  this.TitleTable.cells(1).innerHTML = "<img src='"+CONS.ImgPrefix+"/btn_down.gif' border=0 align=absmiddle>";
  this.TitleTable.cells(2).innerHTML = "<img src='"+CONS.ImgPrefix+"/btn_down_s.gif' border=0 align=absmiddle>";
  this.TitleTable.cells(3).appendChild(this.select);
  this.TitleWrapper = document.createElement(""
    + "<img src='"+CONS.ImgPrefix+"/blank.gif'"
    + "  style='position:absolute; top:0; left:0; z-index:2; width:100%; height:"+this.height+";'"
    + "  onmouseover='VAR.SelectList["+this.ssID+"].handleOverTitle()'"
    + "  onmouseout='VAR.SelectList["+this.ssID+"].handleOutTitle(); VAR.SelectList["+this.ssID+"].releaseTitle(window.event);'"
    + "  onmousedown='VAR.SelectList["+this.ssID+"].pressTitle(window.event)'"
    + "  ondblclick='VAR.SelectList["+this.ssID+"].pressTitle(window.event); VAR.SelectList["+this.ssID+"].releaseTitle(window.event);'"
    + "  onmouseup='VAR.SelectList["+this.ssID+"].releaseTitle(window.event)'"
    + "  onmousewheel='VAR.SelectList["+this.ssID+"].handleMousewheel(window.event)'"
    + "  ondragstart='cancelEvent(window.event)'"
    + ">"
  );
  CreatedElements[CreatedElements.length] = this.TitleWrapper;
  this.TitleDiv.appendChild(this.TitleWrapper);
}
function createOptionsDiv ()
{
  this.OptionsDiv = createElement("\
    <div style='"+CONS.OptionsDivStyle+"'\
      onscroll='VAR.SelectList["+this.ssID+"].moveOption(window.event)'\
      onmousedown='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='"+CONS.ImgPrefix+"/blank.gif'"
    + "  style='position:absolute; top:0; left:0; width:100%;'"
    + "  onmousedown='VAR.SelectList["+this.ssID+"].pressOption(window.event)'"
    + "  onmousemove='VAR.SelectList["+this.ssID+"].moveOption(window.event)'"
    + "  onmouseup='VAR.SelectList["+this.ssID+"].releaseOption(window.event)'"
    + "  onmouseout='VAR.SelectList["+this.ssID+"].releaseOption(window.event)'"
    + "  ondragstart='cancelEvent(window.event)'"
    + ">"
  );
  CreatedElements[CreatedElements.length] = this.OptionsWrapper;
  this.OptionsDiv.appendChild(this.OptionsWrapper);
}
function createOptionTr (idx)
{
  idx = ('undefined'!=typeof(idx)) ? idx : this.options.length - 1;
  var OptionTr = this.OptionsTable.insertRow(-1);
  var OptionTd = document.createElement("<td height="+this.OptionHeight+"></td>");
  CreatedElements[CreatedElements.length] = this.OptionsTd;
  OptionTd.appendChild(document.createElement("<nobr style='"+CONS.OptionNobrStyle+"'></nobr>"));
  OptionTr.appendChild(OptionTd);
}
