/* 재테크 환율용 - 20090115 */

	var arrImgLink = "http://imgsrc.search.daum-img.net/search_all/special/070719_fortune/form_arrow.gif";

	var dSelects = []; //전체 dSelect객체의 UID를 key로, 해당객체를 value로 담고 있는 변수
	var curSelectID = null; //현재 option box가 열려있는 select id
	var overBgColor = "#eaf2fc";
	var overFtColor = "#204ba5";
	var outFtColor = "#000";
	var liHeight = "22";
	var changeFun = [];
	var curObj = null; 
	/*******************************************************
	# 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)
		8. fun - 값이 변경되었을때 호출할 내부 함수
	********************************************************/

	function dSelect(id,options,arrImgLink,bgColor,ftColor,widthValue,selectedIndex, fun)
	{
		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:#000;width:"+ widthValue + "px");
		curObj = id; 

		changeFun[curObj] = fun; 
	/*
		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;padding:4px 0 3px 0;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;zindex:2000;margin:0;padding:0;font:12px 굴림,dotum;color:#666;");
		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-x:hidden;overflow-y:auto;height:106px;");
			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, "padding:4px 0 3px 0");
			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])+"px");
				}
				Show(CurSelect.optBox);
				curSelectID = this.id; //현재 사용중인 select id를 전역변수에 담는다.
			}
			else {
				Hide(CurSelect.optBox);
				curSelectID = null;
			}
		};

		if(id == "fromExchange" ) {
			document.getElementById("unit_name").innerHTML =  k_ex[selectedIndex];
		}
	}

	dSelect.prototype.getOffset = function()
	{
		var offsetValue = [this.selectBox.offsetTop,this.selectBox.offsetLeft];
		var parent = this.selectBox.offsetParent;
		while(parent != document.body && parent) {
			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) {
			daum.Event.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);

		var str = this.id; 
		var n = str.split('=')[0]; 

		if( n != ""){    
			if( changeFun[n] != ""){
				eval(changeFun[n] + '()');
			}
		}
	}

	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;
	}


	function ChangingValue(_id, _idx, _text, _value)
	{
		var CurSelect = GetCurSelect(_id);
		CurSelect.defText.innerHTML = _text;
		CurSelect.selectedIndex = _idx;
		CurSelect.value = _value;
	}

	function getNavigatorTypeChk(){
		if(navigator.appName == "Microsoft Internet Explorer")
			return 1;  
		else if (navigator.appName == "Netscape")
			return 2;	
		else 
			return 0;
	}

	daum.Event.addEvent(document,"mousedown",CloseOptBoxChk); //윈도우 다른 영역에서 mousedown할때 option box 닫힘
	daum.Event.addEvent(window,"resize",CloseOptBox); //화면 크기가 달라져서 위치가 어긋날때 option box 닫힘


	function Comma(number) {
		var orgnum = number;
		
		if (number.length > 3) number = number + ".";
				
		arrayOfStrings = number.split('.');
		number = '' + arrayOfStrings[0];
		
		if (number.length > 3 ) {
			var mod = number.length % 3;
			var output = (mod > 0 ? (number.substring(0,mod)) : '');
			for (i=0 ; i < Math.floor(number.length / 3); i++) {
				if ((mod == 0) && (i == 0)){
					output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
				}else{
					output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
				}
			}
			
			if (orgnum.indexOf(".") > -1 )	output+= '.'+arrayOfStrings[1];
			return (output);
		
		
		}else{
			 return orgnum;
		}
	}

	// 숫자에 컴마빼고 다시 계산후 컴마찍기.
	function CheckMoney(obj){
		var a=out_comma(obj.value);
		obj.value=Comma(a)
	}

	//컴마 빼기
	function out_comma(str){
		comm_str = String(str);
		uncomm_str='';
		for(i=0; i<comm_str.length; i++)
		{
			substr=comm_str.substring(i,i+1);
			if(substr!=',')		uncomm_str += substr;
		}
		return uncomm_str;
	}

	// fromChange가 변경되었을때 호출하는 함수
	function changedIndex1() {      

		if (fromExchange.selectedIndex == 0){
			ChangingValue("toExchange", 1, '미국(USD)', ex_rate[1]);
		} else {
			ChangingValue("toExchange", 0, '대한민국(WON)', 1);
		}
		document.getElementById("unit_name").innerHTML =  k_ex[fromExchange.selectedIndex];
	}       

	function change_cal_select() {
		var toIndex = toExchange.selectedIndex;
		var fromIndex = fromExchange.selectedIndex;

		document.getElementById('ext_from_money').value = "1000";
		document.getElementById("ex_result").style.display = "none";

		ChangingValue('fromExchange', toIndex, options[toIndex].text, options[toIndex].value);
		ChangingValue('toExchange', fromIndex, options[fromIndex].text, options[fromIndex].value);
		document.getElementById("unit_name").innerHTML =  k_ex[fromExchange.selectedIndex];
//		changedIndex1();

		document.getElementById("changeExBtn").className = "typeBtn";
		daum.Event.removeEvent(document.getElementById("changeExBtn"), "click", change_cal_select);
	}

	function calculate_exchange(){
		var ext_from_money= out_comma( document.getElementById('ext_from_money').value );
		var ext_from = fromExchange.value;
		var ext_to= toExchange.value;

		if (ext_from_money=="" || isNaN(ext_from_money)){
			alert('숫자를 입력하세요');
			document.getElementById('ext_from_money').focus();
		}else{
			document.getElementById('ext_to_money').innerHTML = Comma( (ext_from_money * ext_from / ext_to).toFixed(2) );
			document.getElementById("ex_result").style.display = "block";
			document.getElementById("e_unit").innerHTML = ex[toExchange.selectedIndex];
			document.getElementById("k_unit").innerHTML = full_k_ex[toExchange.selectedIndex];

			document.getElementById("changeExBtn").className = "typeBtn changeEx";

			//RemoveEvent(document.getElementById("changeExBtn"), "click", calculate_exchange);
			daum.Event.addEvent(document.getElementById("changeExBtn"), "click", change_cal_select);

		}
	}