document.domain = 'daum.net';

/* 금융 공통 */
var finance = {};

finance.tab = function(idSet,eType,onCss,defIdx,addLinkUrl) {
	this.cnt = idSet.length;
	this.eType = eType;
	this.onCss = onCss;
	this.tab = [];
	this.tabbody = [];
	for(var i=0;i<this.cnt;++i)
	{
		var tab = document.getElementById(idSet[i][0]);
		var tabbody = document.getElementById(idSet[i][1]);
		tab.oldCss = tab.className;
		tabbody.oldCss = tabbody.className;
		this.tab.push(tab);
		this.tabbody.push(tabbody);
		if(addLinkUrl && addLinkUrl[i])tab.onclick = daum.Function.bind(this.addLink, this, i, addLinkUrl[i]);
		daum.Event.addEvent(tab,eType,daum.Function.bind(this.on, this, i));
	}
	(defIdx) ? this.on(defIdx-1) : this.on(0);
}


finance.tab.prototype = {
	on:function(n) {
		if(this.curIdx == n) return;
		for (var i = 0; i < this.cnt; ++i) {
			if (i == n) {
				this.tab[i].className = (this.tab[i].oldCss) ? this.tab[i].oldCss + " " + this.onCss : this.onCss;
				this.tabbody[i].className = (this.tabbody[i].oldCss) ? this.tabbody[i].oldCss + " " + this.onCss : this.onCss
			}
			else {
				this.tab[i].className = this.tab[i].oldCss;
				this.tabbody[i].className = this.tabbody[i].oldCss;
			}
		}
		this.curIdx = n;
	},
	addLink:function(n,url) {
		if(this.curIdx == n) GoPage(url);
	}
}


finance.paging = function(listId,maxCount,prevBtId,nextBtId,listTag) {
	this.el = document.getElementById(listId);
	this.tag = (listTag) ? listTag : 'li';
	this.maxCount = maxCount;
	this.prevBt = (prevBtId) ? document.getElementById(prevBtId) : null;
	this.nextBt = (nextBtId) ? document.getElementById(nextBtId) : null;

	if(this.prevBt) daum.Event.addEvent(this.prevBt, "click", daum.Function.bind(this.prev, this));
	if(this.nextBt) daum.Event.addEvent(this.nextBt, "click", daum.Function.bind(this.next, this));
	this.init();

}

finance.paging.prototype = {
	init : function() {
		this.list = this.el.getElementsByTagName(this.tag);
		this.totalCount = this.list.length;
		this.totalPage = Math.ceil(this.totalCount/this.maxCount);
		this.needPaging = (this.totalCount > this.maxCount);
		if(!this.needPaging) {
			if(this.prevBt) this.prevBt.style.display = "none";
			if(this.nextBt) this.nextBt.style.display = "none";
		}
		else {
			if(this.prevBt) this.prevBt.style.display = "";
			if(this.nextBt) this.nextBt.style.display = "";
		}
		this.curPage = 1;
		this.exec(1);
	},
	prev : function() {
		this.exec(--this.curPage);
	},
	next : function() {
		this.exec(++this.curPage);
	},
	exec : function(n) {
		if(n<=0)  this.curPage = this.totalPage;
		else if(n>this.totalPage)  this.curPage = 1;

		var min = this.maxCount*(this.curPage-1), max = this.maxCount*this.curPage;
		for(var i=0;i<this.totalCount;++i) {
			if(min <= i && i < max) this.list[i].style.display = "block";
			else this.list[i].style.display = "none";
		}
	}
}

//new finance.pagingTab(arrayId,btnPrev,btnNext, fstIndex)
finance.pagingTab = function(oId, btnPrev_id, btnNext_id, fstIndex) {

	if(oId.length == 0) return;
	
	this.btnPrev = daum.$(btnPrev_id);
	this.btnNext = daum.$(btnNext_id);
	if(fstIndex) this.index = fstIndex; else this.index = 0;
	this.oId = [];
	
	for(var i=0,cnt=oId.length; i<cnt; ++i) {
		this.oId.push(daum.$(oId[i]));
	}

	if(this.btnPrev) daum.Event.addEvent(this.btnPrev, "click", daum.Function.bind(this.prev, this));
	if(this.btnNext) daum.Event.addEvent(this.btnNext, "click", daum.Function.bind(this.next, this));
	
	this.init(this.index);
}

finance.pagingTab.prototype = {
	init : function(index) {
		this.clearClassName();
		this.oId[index].className = "on";
	},
	prev : function() {
		this.clearClassName();
		if(this.index == 0) { this.index = this.oId.length -1 } else { --this.index };
		this.oId[this.index].className = "on";
	},
	next : function() {
		this.clearClassName();
		if(this.index == this.oId.length -1 ) { this.index = 0 } else { ++this.index };
		this.oId[this.index].className = "on";
	},
	clearClassName : function () {
		for(var i=0,cnt=this.oId.length; i<cnt; ++i) {
			this.oId[i].className = "";
		}
	}
}

finance.parentTop = function() {
	parent.scrollTo(0,0);
}

//onload=finance.iframeResizing(this,'id')
finance.iframeResizing = function(frameObj, frame_contents_id) {
	try {
		var doc = frameObj.contentDocument;
		if(!doc) doc = frameObj.contentWindow.document;
		frameObj.height = doc.getElementById(frame_contents_id).offsetHeight+3;
		//commu.parentTop();
	}catch(e) {}
}


/* javascript call */
finance.callSrc = function(id, url, type) {
	var obj = document.getElementById(id);
	var parent;
	if(obj) {
		parent = obj.parentNode;
		parent.removeChild(obj);
	}
	else {
		parent = document.getElementsByTagName('head')[0];
	}
	
	obj = document.createElement('script');
	var cashTime = new Date() - Date.UTC(2008,5,0);
	if(type && type == "day") cashTime = Math.floor(cashTime/86400000);
	
	var param = (url.indexOf('?')<0) ? '?':'&';
	param += "d="+ cashTime;
 
	obj.src = url + param;
	obj.id = id;
	obj.type = "text/javascript";
	parent.appendChild(obj);
}

var callRightTypeId = "";

finance.getRightQnaClick = function(type, ele , opt){
	callRightTypeId = type;
	// 이미 불러 온 데이타가 있는 경우 다시 call 하지 않음
	if(daum.$(ele).innerHTML != "" ) return; 

	var option = {count:"5", maxLength:"25"};
	daum.extend( option, opt );

	var callUrl = "http://finance.daum.net/community/qna/popular.json?category_id=" + type + "&element=" + ele + "&menuType=invest&callback=finance.getRightQnaData&count=" + option.count + "&maxLength=" + option.maxLength;
	finance.callSrc("qnaList", callUrl);
}

finance.getRightQnaData = function(_data){
	try	{
		var ulTemp = '<ul class="dotList">$li</ul>'; 
		var liTemp = '<li><a href="$url" title="$title" target="_top">$cutTitle</a></li>';
		var result = "";
		var element = _data.element ; 
		var list = _data.list; 
		for( var i=0; i< list.length; i++){
			var temp = liTemp ;
			temp = temp.replace(/\$url/gi, list[i].url);
			temp = temp.replace(/\$title/gi, list[i].title);
			temp = temp.replace(/\$cutTitle/gi, list[i].cutTitle);
			result += temp; 
		}

		if(callRightTypeId == "QDB002") {
			daum.$("btnRightQnaMore").href = "http://stock.daum.net/community/itemConsult.daum";
		} else if(callRightTypeId == "QFQ003") {
			daum.$("btnRightQnaMore").href = "http://realestate.daum.net/community/qna.daum?Id=QFQ003";
		}

		daum.$(element).innerHTML = ulTemp.replace(/\$li/gi, result); 

	} catch (e){}
}


/* 250 확장배너 용 */
function rightAd(type) {
	var rand = Math.random().toString(); 
	var ordval = rand.substring(2,rand.length);
	switch(type) {
		case 250 :
				document.writeln("<div id=\"EXTENSIBLE_BANNER_WRAP\"><div id=\"EXTENSIBLE_BANNER\"></div></div><iframe src=\"http://amsv2.daum.net/ad/adview?secid=00867\" width=250 height=250 border=0 frameborder=0 scrolling=no marginheight=0 marginwidth=0 id=AMS_250exp></iframe>");
			break;
		case 130250 :
			try	{
				if(window.screen.width >= 1152) {
					var bannerObj = document.createElement("div");
					bannerObj.setAttribute("id","RIGHT_SPACE_BANNER");
				
					if(document.getElementById(rsTargetId)) {
/*
						document.getElementById(rsTargetId).style.position = "relative";
*/
						document.getElementById(rsTargetId).appendChild(bannerObj);

						bannerObj.innerHTML = "<iframe src=\"http://amsv2.daum.net/ad/adview?secid=00887\" width=130 height=250 border=0 frameborder=0 scrolling=no marginheight=0 marginwidth=0 id=fin_AMS></iframe>";
					}
				}
			}
			catch (e){}
			break;
		case 6090 :
			document.writeln("<iframe src=\"http://amsv2.daum.net/ad/adview?secid=00890\" width=60 height=90 border=0 frameborder=0 scrolling=no id=stock_AMS></iframe>");
			break;
		default :
			document.writeln("<iframe src=\"http://amsv2.daum.net/ad/adview?secid=00867\" width=250 height=250 border=0 frameborder=0 scrolling=no marginheight=0 marginwidth=0 id=finance_AMS></iframe>");
		break;
	}		
}

/* 확장배너용 */
function extBannerOver() {
	try	{ document.getElementById('EXTENSIBLE_BANNER').style.overflow = 'visible';} catch (e){}
}
function extBannerOut() {
	try	{ document.getElementById('EXTENSIBLE_BANNER').style.overflow = 'hidden';} catch (e){}	
}

function AmsFlash(f,w,h,options){
     var param={ id:"UIswf_"+f,quality:'high',bgcolor:'#ffffff',allowScriptAccess:'always',allowFullScreen:'true'}
     Object.extend(param, options);
 
     var id='id="'+param.id+'"';
     var name = 'name="'+param.id+'"';
     var p='',e=''; 
 
     for(i in param) 
     {
          if(i=='id')continue;
          p+='<param name="'+i+'" value="'+param[i]+'">\n';
          e+=i+'="'+param[i]+'" ';
     }
     var s='<object '+id+' width="'+w+'" height="'+h+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">';
     s+='<param name="movie" value="'+f+'">'+ p; 
     s+='<embed '+name+' src="'+f+'" width="'+w+'" height="'+h+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" '+e+'/>';
     s+='</object>';
     if(param.containerId) {
          document.getElementById(param.containerId).innerHTML = s;
     } else {
      document.write(s);
     }
     return s;
};

function DaumFlash(src,fv,width,height,div,id){
	var obj = new Object();
	obj.type = 'application/x-shockwave-flash';
	obj.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
	obj.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0';
	obj.width = width;
	obj.height = height;
	
	if (id != undefined){
		obj.id=id;
		obj.name=id;
	}

	var param = [		
		['movie',src],
		['src',src],
		['quality','high'],
		['wmode','transparent'],//
		['allowScriptAccess','always'],
		['bgcolor','#FFFFFF'],
		['showLiveConnect','true'],
		['FlashVars',fv]
	];
	obj.param = param;
	daumActiveX(obj,div);
}

function daumActiveX(obj,div){
	// generate html code
	// for ie obejct
	var html='<object ';
	if (!obj.id && !obj.name){
		var r=Math.round(Math.random()*100);
		//html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
		html += 'id="daumActiveXObject'+r+'"" ';
	} else {
		if (obj.id) html += 'id="'+obj.id+'" ';
		else html += 'id="'+obj.name+'" ';
		/*if (obj.name) html += 'name="'+obj.name+'" ';
		else html += 'name="'+obj.id+'" ';
		*/
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.classid) html += 'classid="'+obj.classid+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	if (obj.codebase) html += 'codebase="'+obj.codebase+'" ';
	// append events
	for (var i in obj.events){
		if (obj.events[i]){
			html += obj.events[i][0]+'="'+obj.events[i][1]+'" ';
		}
	}
	// end of object tag
	html += '>\n';
	// append params
	for (var i in obj.param){
		html += '<param name="'+obj.param[i][0]+'" value="'+obj.param[i][1]+'"/>\n';
	}

	// for ns embed
	html += '<embed ';
	if (!obj.id && !obj.name){
		var r=Math.round(Math.random()*100);
		//html += 'id="daumActiveXObject'+r+'" name="daumActiveXObject'+r+'" ';
		html += 'name="daumActiveXObject'+r+'" ';
	} else {
		//if (obj.id) html += 'id="'+obj.id+'" ';
		if (obj.name) html += 'name="'+obj.name+'" ';
	}
	if (obj.type) html += 'type="'+obj.type+'" ';
	if (obj.width) html += 'width="'+obj.width+'" ';
	if (obj.height) html += 'height="'+obj.height+'" ';
	// append params
	for (var i in obj.param){
		if (obj.param[i]){
			if (obj.param[i][0]=='movie' || obj.param[i][0]=='src'){
				var _src=obj.param[i][1];
			}
			html += obj.param[i][0]+'="'+obj.param[i][1]+'" ';
		}
	}
	html += '></embed>\n';
	html += '</object>';

	var isIE=(document.all)?true:false;
	if (isIE){
		document.getElementById(div).innerHTML=html;
	} else if (obj.type=='application/x-shockwave-flash' || obj.classid.toLowerCase()=='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'){
		// ie activex flash
		document.getElementById(div).innerHTML=html;
	} else if (navigator.platform.indexOf('Win')>=0 && obj.classid.toLowerCase()=='clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95'){
		// Windows Media Player - windows platform
		document.getElementById(div).innerHTML=html;
	}
}


/**
 * daumFlashChart
 *
 * src = flash url
 * width = size (width)
 * height  = size (height)
 * div = object id
 * value = parameter
 */
function daumFlashChart(src,width,height,div,value){
	var obj = new Object();
	obj.type = 'application/x-shockwave-flash';
	obj.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
	obj.codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0';
	obj.wmode = 'transparent';
	obj.width = width;
	obj.height = height;

	var param = [
		['movie',src],
		['src',src],
		['quality','high'],
		['wmode','transparent'],
		['bgcolor','#FFFFFF'],
		['flashvars',value],
	];
	obj.param = param;
	daumActiveX(obj,div);
}

function tvpotSWF(src,w,h){
	width = (w)? w : 502;
	height = (h)? h : 438;

	src=src.replace("flvPlayer","mloader");
	src+='&stype=m';
	var tvpot = '<object type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" align="middle" id="V000165992" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">';
	tvpot += '<param name="movie" value="'+src+'"/>';
	tvpot += '<param name="allowScriptAccess" value="always"/>';
	tvpot += '<param name="allowFullScreen" value="true"/>';
	tvpot += '<param name="loop" value="false"/>';
	tvpot += '<param name="menu" value="false"/>';
	tvpot += '<param name="wmode" value="transparent"/>';
	tvpot += '<param name="swLiveConnect" value="true"/>';
	tvpot += '<embed src="'+src+'" width="'+width+'" height="'+height+'" loop="false" menu="false" swLiveConnect="true" wmode="transparent" allowScriptAccess="always" type="application/x-shockwave-flash" allowFullScreen="true" nowow="true"/>';
	tvpot += '</object>';
	document.write(tvpot);	
};

//link
function GoPage(url,target)
{
	if(target) target.location.href=url;
	else window.location.href=url;
}

function GoPageNew(url)
{
	window.open(url);
}

function clearSearch() {
	try	{ document.getElementById("query1").value = "";}catch (e){}
}

function checkGNBSearch() {

	var form = document.search;

	if( form.q.value.length == 0 || form.q.value.trim() == "") {
		alert("검색할 단어를 입력해 주세요.");
		form.q.focus();
		return false;
	} else {

		if (form.GNB_s_Type.value == "stock" ) {
			if(isNaN(form.q.value)) {
				top.location.href="http://stock.daum.net/search/search.daum?name="+encodeURIComponent(form.q.value);
			} else {
				top.location.href="http://stock.daum.net/item/main.daum?code="+form.q.value;
			}
		} else if (form.GNB_s_Type.value == "estate") {	//부동산 매물
			top.location.href = "http://realestate.daum.net/search/total/?query=" + form.q.value;
		} else if (form.GNB_s_Type.value == "fund" ) {	//펀드명
			top.location.href = "http://finance.daum.net/fund/search/condition.daum?query=" + encodeURIComponent(form.q.value);
		} else if (form.GNB_s_Type.value == "ins" ) {	//보험
			top.location.href = "http://finance.daum.net/insurance/search.daum?query=" + encodeURIComponent(form.q.value);
		} else if (form.GNB_s_Type.value == "loan" ) {	//대출
			top.location.href = "http://finance.daum.net/loan/search.daum?query=" + encodeURIComponent(form.q.value);
		} else if (form.GNB_s_Type.value == "saving" ) {	//예적금
			top.location.href = "http://finance.daum.net/saving/search.daum?query=" + encodeURIComponent(form.q.value);
		}

		return false;
 	}

	return false;
}

//style set/get
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.currentStyle)
		var css = obj.currentStyle;
	else
		var css = document.defaultView.getComputedStyle(obj, null);
	value = css ? css[styleText] : null;
	return (value == null || value == undefined) ? "" : value;
}

/* list over/out */
function highlight(obj, isover, color)
{
	if(color == undefined) color = "#EFF1FF";
	if(!obj.oldColor) obj.oldColor = getStyle(obj,"background-color");
	obj.style.backgroundColor = (isover) ? color : obj.oldColor;
}

function clickLogger(){
	var tag = '';
	try
	{
		var ref = top.document.referrer;
		var loc = top.document.location;

		var cntArg = arguments.length;
		var sc = arguments[0];
		/*	
			c1 : 종목코드
		*/

		var rootUrl = "http://log.finance.daum.net"; 
		tag = "<img name='clickLog' id='clockLog' style='display:none' src=" + "'" +  rootUrl + "/click?";
		tag += "sc=" + sc + "&";
		for ( var n=1; n<cntArg ; n++ ) {
			tag += "c"+n+"=" + arguments[n] + "&";
		}
		tag += "ref=" + encodeURIComponent(ref) + "&loc=" + encodeURIComponent(loc) + "'" ;
		tag += "width='0' height='0'>";		
	}
	catch (e) {}
//	console.log(tag);
	return tag;
}

var resizePopup = function( obj ) { 
	if(!obj)obj = document.getElementsByTagName('HTML')[0];
	var doc = document.getElementsByTagName('HTML')[0];
	var clientW = doc.clientWidth||doc.scrollWidth;
	var clientH = doc.clientHeight||doc.scrollHeight;
	var offsetW = obj.offsetWidth||obj.scrollWidth;
	var offsetH = obj.offsetHeight||obj.scrollHeight;
	//alert( clientW + " : " + clientH + " / " + offsetW + " : " + offsetH )   
    var gapWidth = offsetW - clientW ;
    var gapHeight = offsetH - clientH;
    if(gapWidth || gapHeight){
        window.resizeBy(gapWidth,gapHeight);
    }
};

//popup
function openPop(url,winnm,width,height,etc_options)
{
	var options = "width="+width+",height="+height;
	if(etc_options) options += ","+etc_options;
	var oWindow = window.open(url,winnm,options);
	return oWindow;
}

function centerPop(url,winnm,width,height,etc_options)
{
	var OpenerLeft = (window.screenLeft != undefined) ? window.screenLeft : window.screenX;
	var OpenerTop = (window.screenTop != undefined) ? window.screenTop : window.screenY;
	var brWidth = (window.outerWidth) ? window.outerWidth : document.documentElement.clientWidth;
	var popLeft = OpenerLeft + parseInt((brWidth-width)/2);
	var popTop = parseInt((window.screen.availHeight-height)/2);
	openPop(url,winnm,width,height,'left='+popLeft+',top='+popTop+','+etc_options);
}


finance.openPopupSiseBoard = function(type, niltag){
	try	{
		var OpenerLeft = (window.screenLeft != undefined) ? window.screenLeft : window.screenX;
		var OpenerTop = (window.screenTop != undefined) ? window.screenTop : window.screenY;
		var brWidth = 1024;//(window.outerWidth) ? window.outerWidth : document.documentElement.clientWidth;
		var brHeight = 768;//(window.outerHeight) ? window.outerHeight : document.documentElement.clientHeight;
		
		if(type == "stock") {
			var boardUrl = "http://stock.daum.net/quote/allpanel.daum";
		}else if(type == "estate") {
			var boardUrl = "http://realestate.daum.net/maemul/SiseBoard.daum?datekey=Q";
		} else {
			return false;
		}
		
		if (niltag && niltag != "") {
			boardUrl = boardUrl + niltag;
		}
		
		if(type == "stock") {
			var panel = window.open(boardUrl,"ammpanel","width="+brWidth+",height="+brHeight+",left="+OpenerLeft+",top="+OpenerTop+",resizable=yes,scrollbars=yes");
			panel.focus();	
		}else if(type == "estate") {
			var board = window.open(boardUrl,"realestateSiseBoard","width="+brWidth+",height="+brHeight+",left="+OpenerLeft+",top="+OpenerTop+",resizable=yes,scrollbars=yes");
			board.focus();
		}
		return false;		
	}catch (e){}

}