// SaySelect Environment Constants
var DD_ENV = new Object();
DD_ENV.IE_Version = new Number(((window.navigator.appVersion.split('; '))[1].split(' '))[1]);
DD_ENV.CR = new Object();
DD_ENV.CR.ReverseBackground = '#f7f7f7';
DD_ENV.CR.ReverseText = '#444444';
DD_ENV.CR.Border = '#BCBCBC';
DD_ENV.CR.BorderActive = '#BCBCBC';
DD_ENV.ImgPrefix = 'http://img.sbs.co.kr/vobos/service/common/icon/';
DD_ENV.DefaultHeight = 15;
DD_ENV.ButtonWidth = 15;
DD_ENV.OptionsDivStyle = ''
 + '  padding:0 5 0 5;'
 + '  display:none;'
 + '  z-index:10;'
 + '  position:absolute;'
  + ' border-left:1 solid '+ DD_ENV.CR.Border+';'
  + ' border-right:1 solid '+ DD_ENV.CR.Border+';'
  + ' border-bottom:1 solid '+ DD_ENV.CR.Border+';'
  + ' background-color:#FFFFFF;'
  + ' scrollbar-face-color:#ffffff;'
  + ' scrollbar-shadow-color:#ffffff;'
  + ' scrollbar-highlight-color:#F6F5F4;'
  + ' scrollbar-3dlight-color:#ffffff;'
  + ' scrollbar-darkshadow-color:#D3D3D3;'
  + ' scrollbar-track-color:#F6F5F4;'
  + ' scrollbar-arrow-color:#f7f7f7;';
DD_ENV.MouseStyle = 'cursor:hand;'//¹Ú½ºÀ§¿¡¿Ã¶ó°¬À»¶§ÀÇ ¸¶¿ì½º¸ð¾ç
DD_ENV.OptionNobrStyle =''
  + ' font-size:11px;'
  + ' font-family:µ¸¿ò;'
// SaySelect Variables
var DD_VAR = new Object();
DD_VAR.DivDummy = document.createElement("DIV");
DD_VAR.SelectList = new Array();
DD_VAR.bEventAttached = false;

var DD_CreatedElements = new Object();

function unloadObjects()
{
  try {
    if (DD_VAR && DD_VAR.SelectList)
    {
      for (key in DD_VAR.SelectList)
      {
	if (DD_VAR.SelectList[key])
	{
	  try {
	   DD_VAR.SelectList[key].select.setAttribute('SS', 0);
	  } catch (e) {};
	  delete DD_VAR.SelectList[key];
	}
      }
    }
  } catch (e) {};
}

//attachEvent("onunload", unloadObjects);

if (window.addEventListener)
    window.addEventListener("unload", unloadObjects, false);
else if (window.attachEvent)
    window.attachEvent("onunload", unloadObjects);
else if (document.getElementById)
    window.onload=unloadObjects;

function DD_create (srcHTML, ListMax, bAutoDetect)
{
  // property
  this.ssID = DD_VAR.SelectList.length;
  this.bOriginalSelect = (bAutoDetect && DD_ENV.IE_Version < 5.5);
  this.select = DD_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) : DD_ENV.DefaultHeight;
  this.OptionHeight = this.height;
  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;

  // private method
  this.isThisEventToBeCanceled = DD_isThisEventToBeCanceled;
  this.toggleTitle = DD_toggleTitle;
  this.syncSelectedIndex = DD_syncSelectedIndex;
  this.toggleOptions = DD_toggleOptions;
  this.turnOnOption = DD_turnOnOption;
  this.turnOffOption = DD_turnOffOption;
  this.handleMousewheel = DD_handleMousewheel;
  this.handleOverTitle = DD_handleOverTitle;
  this.handleOutTitle = DD_handleOutTitle;
  this.handleOverOption = DD_handleOverOption;
  this.createTable = DD_createTable;
  this.createTitleDiv = DD_createTitleDiv;
  this.checkOptionsDiv = DD_checkOptionsDiv;
  this.createOptionsDiv = DD_createOptionsDiv;
  this.createOptionTr = DD_createOptionTr;
  this.adjustOptionsDiv = DD_adjustOptionsDiv;
  this.syncOptions = DD_syncOptions;
  this.pressOption = DD_pressOption;
  this.moveOption = DD_moveOption;
  this.releaseOption = DD_releaseOption;
  this.pressTitle = DD_pressTitle;
  this.releaseTitle = DD_releaseTitle;

  // public method
  this.display = DD_display;
  this.insertOption = DD_insertOption;
  this.deleteOption = DD_deleteOption;
  this.changeOption = DD_changeOption;

  // initiate
  this.createTable();
  this.select.setAttribute('SS', this);
  if (!this.bOriginalSelect)
    this.select.onpropertychange = DD_handlePropertychange;
  DD_VAR.SelectList[this.ssID] = this;
}
function DD_display ()
{
  document.write("<div id=DD_TempDiv></div>\n");
  document.all.DD_TempDiv.appendChild(this.Table);
  document.all.DD_TempDiv.removeNode();
}
function DD_write (srcHTML, ListMax, bAutoDetect)
{
  var oSS = new DD_create(srcHTML, ListMax, bAutoDetect);
  oSS.display();
  return oSS;
}
function DD_insertOption (value, innerText, idx)
{
  this.checkOptionsDiv();

  var NewOption = document.createElement("OPTION");
  DD_CreatedElements[DD_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 DD_deleteOption (idx)
{
  this.checkOptionsDiv();

  this.options.remove(idx);
  if (!this.bOriginalSelect)
    this.OptionsTable.deleteRow(idx);
  this.syncOptions();
  this.adjustOptionsDiv();
  this.syncSelectedIndex();
}
function DD_changeOption (idx, value, innerText)
{
  this.checkOptionsDiv();

  this.options[idx].value = value;
  this.options[idx].innerText = innerText;
  this.syncOptions();
  this.syncSelectedIndex();
}

function DD_cancelEvent (event)
{
  event.cancelBubble = true;
  event.returnValue = false;
}
function DD_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 (DD_ENV.IE_Version >= 5.5 && event.srcElement != this.srcElementOfLastMousedown && this.srcElementOfLastMousedown != null) {
          this.srcElementOfLastMousedown = null;
          return true;
        }
        break;
      case 'mouseout':
        if (!(DD_ENV.IE_Version < 5.5 && event.srcElement == this.srcElementOfLastMousedown))
          return true;
        break;
      case 'mousemove':
        if (DD_ENV.IE_Version >= 5.5 && event.srcElement != this.srcElementOfLastMousedown && this.srcElementOfLastMousedown != null)
          return true;
        break;
    }
  }
  return false;
}
function DD_createElement (html)
{
  DD_VAR.DivDummy.insertAdjacentHTML('afterBegin', html);
  var oEl = DD_VAR.DivDummy.children(0);
  while (DD_VAR.DivDummy.children.length > 0) {
    DD_VAR.DivDummy.removeChild(DD_VAR.DivDummy.children(0));
  }
  return oEl;
}
function DD_blurExcept (except)
{
  DD_cancelEvent(window.event);

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

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

  if (DD_VAR.bEventAttached && bHasToDetachEvent) {
    document.detachEvent('onmousedown', DD_blurExcept);
    document.detachEvent('ondblclick', DD_blurExcept);
    DD_VAR.bEventAttached = false;
  }
}
function DD_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 DD_toggleTitle (bReverse)
{
  this.bReverse = ('undefined'!=typeof(bReverse)) ? bReverse: (!this.bReverse);
  this.TitleTable.cells(0).style.backgroundColor = this.bReverse ? DD_ENV.CR.ReverseBackground : '';
  this.TitleTable.cells(0).style.color = this.bReverse ? DD_ENV.CR.ReverseText : '';
}
function DD_checkOptionsDiv ()
{
  if (!this.OptionsDiv) {
    this.createOptionsDiv();
    this.Table.cells(0).appendChild(this.OptionsDiv);
  }
}
function DD_toggleOptions (bExpanded, bStrict)
{
  this.checkOptionsDiv();

  if (!bStrict && !this.bFocused) {
    DD_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 (!DD_VAR.bEventAttached) {
      document.attachEvent('onmousedown', DD_blurExcept);
      document.attachEvent('ondblclick', DD_blurExcept);
      DD_VAR.bEventAttached = true;
    }
  }
}
function DD_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 DD_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 DD_handleOverTitle ()
{
  if (this.bExpanded)
    return;

  this.TitleTable.style.borderColor = DD_ENV.CR.BorderActive;
  this.TitleTable.cells(1).style.display = 'none';
  this.TitleTable.cells(2).style.display = 'block';
}
function DD_handleOutTitle ()
{
  this.TitleTable.style.borderColor = DD_ENV.CR.Border;
  this.TitleTable.cells(2).style.display = 'none';
  this.TitleTable.cells(1).style.display = 'block';
}
function DD_handleOverOption (idx)
{
  for (var i=0; i < this.options.length; i++) {
    if (i==idx)
      this.turnOnOption(i);
    else
      this.turnOffOption(i);
  }
}
function DD_turnOnOption (idx)
{
  this.OptionsTable.cells(idx).style.color = DD_ENV.CR.ReverseText;
  this.OptionsTable.cells(idx).style.backgroundColor = DD_ENV.CR.ReverseBackground;
}
function DD_turnOffOption (idx)
{
  this.OptionsTable.cells(idx).style.color = '';
  this.OptionsTable.cells(idx).style.backgroundColor = '';
}
function DD_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 DD_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 DD_pressTitle (event)
{
  DD_cancelEvent(event);

  this.srcElementOfLastMousedown = event.srcElement;

  this.toggleOptions();
}
function DD_releaseTitle (event)
{
  DD_cancelEvent(event);

  if (this.isThisEventToBeCanceled(event)) return;

  this.srcElementOfLastMousedown = null;
}
function DD_pressOption (event)
{
  DD_cancelEvent(event);

  this.srcElementOfLastMousedown = event.srcElement;
}
function DD_moveOption (event)
{
  DD_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 DD_releaseOption (event)
{
  DD_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 DD_createTable ()
{
  this.Table = DD_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 DD_createTitleDiv ()
{
  this.TitleDiv = DD_createElement("\
    <div style='position:relative;background:#ffffff; top:0; left:0;'>\
      <table border=0 cellpadding=0 cellspacing=0\
        height="+this.height+"\
        style='table-layout:fixed; border:0 1 1 1 solid "+DD_ENV.CR.Border+";'\
        onmouseover=''\
      >\
      <tr>\
        <td style='padding:0 1 0 5'><nobr style='text-oveflow:hidden;"+DD_ENV.OptionNobrStyle+"'></nobr></td>\
        <td width="+DD_ENV.ButtonWidth+" align=center style='word-wrap:normal'></td>\
        <td style='display:none' width="+DD_ENV.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='"+DD_ENV.ImgPrefix+"/ico_search.gif' border=0 align=absmiddle>";
  this.TitleTable.cells(2).innerHTML = "<img src='"+DD_ENV.ImgPrefix+"/ico_search.gif' border=0 align=absmiddle>";
  this.TitleTable.cells(3).appendChild(this.select);
  this.TitleWrapper = document.createElement(""
    + "<img src='"+DD_ENV.ImgPrefix+"/null.gif'"
    + "  style='position:absolute; top:0; left:0; z-index:2; width:100%;height:"+this.height+";'"
    + "  onmouseover='DD_VAR.SelectList["+this.ssID+"].handleOverTitle()'"
    + "  onmouseout='DD_VAR.SelectList["+this.ssID+"].handleOutTitle(); DD_VAR.SelectList["+this.ssID+"].releaseTitle(window.event);'"
    + "  onmousedown='DD_VAR.SelectList["+this.ssID+"].pressTitle(window.event)'"
    + "  ondblclick='DD_VAR.SelectList["+this.ssID+"].pressTitle(window.event); DD_VAR.SelectList["+this.ssID+"].releaseTitle(window.event);'"
    + "  onmouseup='DD_VAR.SelectList["+this.ssID+"].releaseTitle(window.event)'"
    + "  onmousewheel='DD_VAR.SelectList["+this.ssID+"].handleMousewheel(window.event)'"
    + "  ondragstart='DD_cancelEvent(window.event)'"
    + ">"
  );
  DD_CreatedElements[DD_CreatedElements.length] = this.TitleWrapper;
  this.TitleDiv.appendChild(this.TitleWrapper);
}
function DD_createOptionsDiv ()
{
  this.OptionsDiv = DD_createElement("\
    <div style='"+DD_ENV.OptionsDivStyle+"'\
      onscroll='DD_VAR.SelectList["+this.ssID+"].moveOption(window.event)'\
      onmousedown='DD_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='"+DD_ENV.ImgPrefix+"/null.gif'"
    + "  style='position:absolute; top:0; left:0; width:100%;'"
    + "  onmousedown='DD_VAR.SelectList["+this.ssID+"].pressOption(window.event)'"
    + "  onmousemove='DD_VAR.SelectList["+this.ssID+"].moveOption(window.event)'"
    + "  onmouseup='DD_VAR.SelectList["+this.ssID+"].releaseOption(window.event)'"
    + "  onmouseout='DD_VAR.SelectList["+this.ssID+"].releaseOption(window.event)'"
    + "  ondragstart='DD_cancelEvent(window.event)'"
    + ">"
  );
  DD_CreatedElements[DD_CreatedElements.length] = this.OptionsWrapper;
  this.OptionsDiv.appendChild(this.OptionsWrapper);
}
function DD_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+" style='padding:2 0 0 2'></td>");
  DD_CreatedElements[DD_CreatedElements.length] = this.OptionsTd;
  OptionTd.appendChild(document.createElement("<nobr style='"+DD_ENV.OptionNobrStyle+"'></nobr>"));
  OptionTr.appendChild(OptionTd);
}
