var dSelects = []; //ÀüÃ¼ dSelect°´Ã¼ÀÇ UID¸¦ key·Î, ÇØ´ç°´Ã¼¸¦ value·Î ´ã°í ÀÖ´Â º¯¼ö
var curSelectID = null; //ÇöÀç option box°¡ ¿­·ÁÀÖ´Â select id
var overBgColor = "#eaf2fc";
var overFtColor = "#204ba5";
var outFtColor = "#666";
var liHeight = "22";
/*******************************************************
# dSelect : ·¹ÀÌ¾î·Î µÇ¾îÁø select box
# parameter : 
	1. id - ¹Ì¸® »ý¼ºµÈ div id
	2. options - JSON¹æ½ÄÀÇ option¹è¿­ (value, text·Î ÀÌ·ç¾îÁü. ¾øÀ¸¸é option list´Â »ý¼º¾ÈµÊ)
	3. arrImgLink - È­»ìÇ¥ style¹®ÀÚ¿­
	4. bgColor - ¼±ÅÃµÇ¾îÁø Àü°æ»ö
	5. ftColor - ¼±ÅÃµÇ¾îÁø ±ÛÀÚ»ö
	6. widthValue - select box ÀÇ width°ª
	7. selectedIndex - select ¼Ó¼ºÀ» ÁÙ option index (¾øÀ¸¸é 0)
********************************************************/

function dSelect(id,options,arrImgLink,bgColor,ftColor,widthValue,selectedIndex)
{
	dSelects[id] = this;
	this.uid = id;
	this.selectBox = document.getElementById(id);
	setStyle(this.selectBox,"font:12px ±¼¸²,dotum;border:1px solid #cdcdcd;background-color:#fff;vertical-align:middle;color:#666;width:"+ widthValue + "px");

 
	if(getStyle(this.selectBox,"line-height") == "normal") 
	{
		setStyle(this.selectBox,"line-height:".concat(getStyle(this.selectBox,"height")));
	}

 
	setStyle(this.selectBox,"cursor:pointer;text-indent:3px;overflow:hidden"); //ÅëÀÏµÈ style	
	
	//µå·¡±×ÇÒ ÇÊ¿ä¾øÀ½
	this.selectBox.ondragstart = function() {return false;};
	this.selectBox.onselectstart = function() {return false;};
	
	//¼±ÅÃµÈ option text
	this.defText = document.createElement('span');
	setStyle(this.defText,"float:left;background-color:#fff");
	this.selectBox.appendChild(this.defText);
	//¼±ÅÃµÇ¾îÁø Àü°æ»ö/±ÛÀÚ»ö 
	selBgColor = bgColor; selFtColor = ftColor;

	//È­»ìÇ¥ Setting
	var arr = document.createElement('div')
	arr.id = this.uid + "=arr";
	setStyle(arr,"background:url(" + arrImgLink + ") no-repeat;float:right;width:17px;height:18px;margin-top:1px;margin-right:1px;display:inline");
	this.selectBox.appendChild(arr);
	
	//¿É¼ÇListBox ¸¸µé±â
	var optBox = document.createElement('ul');
	optBox.id = this.uid + "=optBox";
	this.optBox = optBox;	
	this.selectedIndex = (selectedIndex) ? selectedIndex : 0;
	
	//styleÁ¶Á¤ ¸ÕÀúÇÒ°Í (optionsÀÌ ¾øÀ» °æ¿ìµµ ÀÖÀ¸´Ï±î)	
	setStyle(this.optBox,"background-color:#fff;text-indent:3px;text-align:left;list-style-type:none;display:none;position:absolute;z-index:10000;margin:0;padding:0;font:12px ±¼¸²,dotum;color:#888;");
	setStyle(this.optBox,"width:"+getStyle(this.selectBox,"width"));			
	setStyle(this.optBox,'border:1px solid #cdcdcd;');
	
	//optionÀÇ °¹¼ö°¡ 5°³ ÀÌ»óÀÏ °æ¿ì ½ºÅ©·Ñ¹Ù »ý¼º - Ãß°¡
	if( options.length >= 5 ){
		setStyle(this.optBox,"overflow-y:auto;height:90px;");
		setStyle(this.optBox,'scrollbar-base-color:#cdcdcd;scrollbar-face-color:#fff;scrollbar-track-color:#fff;scrollbar-highlight-color:#fff;scrollbar-3dlight-color:#fff;scrollbar-shadow-color:#cdcdcd;scrollbarDarkShadowColor:#fff;scrollbar-arrow-color:#6d6d6d;');
	}

 	for(var i=0;i<options.length;++i) {
		var li = this.add(options[i].value, options[i].text);
		setStyle(li,"cursor:pointer");
		setStyle(li,"height:".concat(parseInt(getStyle(this.selectBox,"height")) + "px"));
		setStyle(li,"line-height:".concat(parseInt(getStyle(this.selectBox,"line-height")) + "px"));
		if( getNavigatorTypeChk() == 2 ){
			setStyle(li,"height:".concat(parseInt(getStyle(this.selectBox,"height"))+2 + "px"));
		}
		if(i==this.selectedIndex) {
			this.value = options[i].value;
			this.defText.innerHTML = options[i].text;
			OptionSetBG(li);
		}
	}
	this.selectBox.parentNode.appendChild(this.optBox);

	this.selectBox.onclick = function() {
		var CurSelect = GetCurSelect(this.id);
		if(getStyle(CurSelect.optBox,'display') == 'none') 
		{
			var offsetValue = CurSelect.getOffset();
		 
			OptionSetBG(CurSelect.optBox.childNodes[CurSelect.selectedIndex]);
			if( getNavigatorTypeChk() == 2 ){ //ff
				setStyle(CurSelect.optBox,"top:".concat(offsetValue[0]+parseInt(getStyle(CurSelect.selectBox,'height')) + 1+"px"));	
				setStyle(CurSelect.optBox,"left:".concat(offsetValue[1])+"px");
			} 
			else{
				setStyle(CurSelect.optBox,"top:".concat(offsetValue[0]+parseInt(getStyle(CurSelect.selectBox,'height'))+"px"));	
				setStyle(CurSelect.optBox,"left:".concat(offsetValue[1]+1)+"px");
			}
			Show(CurSelect.optBox);
			curSelectID = this.id; //ÇöÀç »ç¿ëÁßÀÎ select id¸¦ Àü¿ªº¯¼ö¿¡ ´ã´Â´Ù.
		}
		else {
			Hide(CurSelect.optBox);
			curSelectID = null;
		}
	};
}

dSelect.prototype.getOffset = function()
{
	var offsetValue = [this.selectBox.offsetTop,this.selectBox.offsetLeft];
	var parent = this.selectBox.offsetParent;
	while(parent != document.body) {
		if(getStyle(parent,"position") != "relative" && getStyle(parent,"position") != "absolute") {
			offsetValue[0] += parent.offsetTop;
			offsetValue[1] += parent.offsetLeft;
		}
		parent = parent.offsetParent;
	}
	return offsetValue;
}
		
dSelect.prototype.add = function(value, text)
{
	var idx = this.optBox.childNodes.length;
	var li = document.createElement('li');
	li.id = this.uid + "=li" + idx;
	li.idx = idx;
	li.innerHTML = li.text = text;
	li.tag = value;
	li.onmouseover = OptionMouseOver;	//event
	//li.onmouseout = OptionMouseOut;
	li.onclick = OptionClick;	//event
	setStyle(li,"width:100%;list-style-type:none;margin:0;padding:0");
	this.optBox.appendChild(li);
	return li;
}

dSelect.prototype.remove = function(idx)
{
	var optBox = this.optBox;
	//idx¿¡ ÇØ´çÇÏ´Â li¸¦ »èÁ¦
	optBox.removeChild(optBox.childNodes[idx]);
	if(idx == this.selectedIndex) {
		optBox.childNodes[0].onclick();
	}
	//ÀüÃ¼ liÀÇ idx¸¦ ´Ù½Ã ¸Å±ä´Ù.
	var cnt = optBox.childNodes.length;
	for(var i=0; i<cnt; ++i) {
		optBox.childNodes[i].idx = i;
	}	
}

dSelect.prototype.onselect = function(func)
{
	var lists = this.optBox.childNodes;
	var cnt = lists.length;
	for(var i=0; i<cnt; ++i) {
		AddEvent(lists[i],"click",func);
	}	
}

function CloseOptBoxChk(event)
{
	if(curSelectID != null) {
		var target = (window.event) ? window.event.srcElement : event.target ;
		if(target.id && GetCurSelect(target.id) && curSelectID == GetCurSelect(target.id).uid) return false;
		CloseOptBox();
	}
}

function CloseOptBox(event)
{
	if(curSelectID != null) {
		Hide(GetCurSelect(curSelectID).optBox);
	}
}

function GetCurSelect(id)
{
	var uid = id.split("=")[0];
	return dSelects[uid];
}
	
function OptionSetBG(obj)
{
	var ul = obj.parentNode;
	var cnt = ul.childNodes.length;
	for(var i=0;i<cnt;++i) {
		BGColor(ul.childNodes[i],'transparent');
		FGColor(ul.childNodes[i],outFtColor);
	}
	FGColor(obj,selFtColor);
	BGColor(obj,selBgColor);
}

function OptionMouseOver()
{
	OptionSetBG(this);
}

function OptionClick()
{
	var CurSelect = GetCurSelect(this.id);
	CurSelect.defText.innerHTML = this.innerHTML;
	CurSelect.selectedIndex = this.idx;
	CurSelect.value = this.tag;
	Hide(CurSelect.optBox);
	if(CurSelect.value != 0){
		parent.gLink(null, 'Z0TO', '1', '1');
		location.href = "http://search.daum.net/search?q=" + CurSelect.value;
	}
}

function getStyle(obj,styleText)
{
	var temp = styleText.split("-");
	var templen = temp.length;
	if(templen > 1) {
		styleText = 	temp[0];
		for(var n=1; n<templen; ++n) {
			styleText += temp[n].substr(0,1).toUpperCase() + temp[n].substr(1);
		}
	}
	if(obj == null)
		return;

	if(obj.currentStyle)
		var css = obj.currentStyle;
	else
		var css = document.defaultView.getComputedStyle(obj, null);
	value = css ? css[styleText] : null;
	return (value == null || value == undefined) ? "" : value;
}

function setStyle(obj,styles)
{
	if(obj == null)
		return;

	var styleArr = styles.split(";");
	var styleCnt = styleArr.length;
	for(var i=0; i<styleCnt; ++i) {
		var styleItem = styleArr[i].split(":");
		styleItem[1] = styleArr[i].substr(styleItem[0].length+1);
		styleItem[0] = styleItem[0].trim();
		switch(styleItem[0]) {
			case 'float' :
				if(obj.style.styleFloat != undefined) 
					obj.style.styleFloat = styleItem[1];
				else 
					obj.style.cssFloat = styleItem[1];
				break;
			default :
				if(styleItem[0].length>0 && styleItem[1].length>0) {
					var temp = styleItem[0].split("-");
					var templen = temp.length;
					if(templen > 1) {
						styleItem[0] = 	temp[0];
						for(var n=1; n<templen; ++n) {
							styleItem[0] += temp[n].substr(0,1).toUpperCase() + temp[n].substr(1);
						}
					}
					obj.style[styleItem[0]] = styleItem[1];	
				}
				break;
		}
	}
}

function Show(obj)
{
	obj.style.display = 'block';
}

function Hide(obj)
{
	obj.style.display = 'none';
}
		
function BGColor(obj, color)
{
	obj.style.backgroundColor = color;
}

function FGColor(obj, color)
{
	obj.style.color = color;
}

String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/gi, ""); 
} 

function AddEvent(element, name, func)
{
	if (element.addEventListener) {
		element.addEventListener(name, func, false);
	}
	else if (element.attachEvent) {
		element.attachEvent('on' + name, func);
	}
}

function getNavigatorTypeChk(){
	if(navigator.appName == "Microsoft Internet Explorer")
		return 1;  
	else if (navigator.appName == "Netscape")
		return 2;	
	else 
		return 0;
}

AddEvent(document,"mousedown",CloseOptBoxChk); //À©µµ¿ì ´Ù¸¥ ¿µ¿ª¿¡¼­ mousedownÇÒ¶§ option box ´ÝÈû
AddEvent(window,"resize",CloseOptBox); //È­¸é Å©±â°¡ ´Þ¶óÁ®¼­ À§Ä¡°¡ ¾î±ß³¯¶§ option box ´ÝÈû
