/******************************************************** 
   파일명 : lib.window.js
   설  명 : 브라우져 윈도우 관련 공통함수 모음
   작성자 : 김현배 
   작성일 : 2008.08.18 
   수정일 : 2008.08.18 
*********************************************************/ 

/**
	함수명 : windowOpenModal
	설  명 : Modal창을 연다.
	인  자 : sUrl:url주소, 
				param:파라미터, 
				cw:Dialog Width, 
				ch:Dialog Height, 
				sScroll:스크롤여부('yes' or 'no'), 
				sResize:리사이즈여부('yes' or 'no'),
				sOption:추후확장을 위한 임시변수
	리  턴 : 유효성 검사여부(true, false)
	사용법 : 
		windowOpenModal(sUrl, param, cw, ch, sScroll, sResize, sOption)
 */
function windowOpenModal(sUrl, param, cw, ch, sScroll, sResize, sOption){
	//스크린의 크기
	var sw = screen.availWidth;
	var sh = screen.availHeight;

	//창의 위치 
	var px=(sw-cw)/2;
	var py=(sh-ch)/5;

	//창을 여는부분
	var wnd = window.showModalDialog(sUrl,
										param,
										'dialogHeight:'+ch+'px;dialogWidth:'+cw+'px;dialogTop:px;dialogLeft:px;center:Yes;help:No;resizable:'+sResize+';status:Yes;scroll:'+sScroll);  
	return wnd;
}


/**
	함수명 : showTabLayer
	설  명 : 화면상의 탭의 전환을 한다.
	인  자 : tab,index, imgSrc
	사용법 : 
		showTabLayer('tabLayer_2','2', '/image/wb/mk/fm/tab_definitePayDB0')
 */
function showTabLayer(tab,index, imgSrc){
	for(i=1;i<=index;i++){
		if(tab == "tabLayer_"+i){ 
		document.getElementById("tabLayer_"+i).style.display = "";
		document.getElementById("tab"+i).src = imgSrc+i+"_on.gif"; 
		}  else {
		document.getElementById("tabLayer_"+i).style.display = "none"; 
		document.getElementById("tab"+i).src = imgSrc+i+"_off.gif"; 
		} 
	}
}

/**
	함수명 : getIEVersion
	설  명 : 사용자의 브라우져 버전을 리턴한다.
	인  자 : 
	리  턴 : 사용자의 브라우져 버전
	사용법 : 
		var ieVer = getIEVersion();
 */
function getIEVersion(){
	return "IE";
}

/**
	함수명 : getIEVersion
	설  명 : 처리중 레이어를 호출한다.
	인  자 : isShow: true or false
	리  턴 : 사용자의 브라우져 버전
	사용법 : 
		var ieVer = getIEVersion();
 */
function procLayer(isShow){
	if(isShow){
		document.all["PROC_DIV"].style.display = "";
	} else {
		document.all["PROC_DIV"].style.display = "none";
	}
}


	

//lib.window.js에서 popup의 window를 저장하고 있는다.
var dynamicPopupTarget = new Object();
//lib.window.js에서 popup의 index에 해당하는 uniqueKey를 저장하고 있는다.
var dynamicPopupIndex = new Object();
/**
	함수명 : openPopup
	설  명 : 레이어팝업 hidden으로 있는 아이프레임을 호출
	인  자 : seq : 기본 1 (아이프레임을 숨기는 div의 id명(div를 뺀 뒷 숫자) : id="div1")
			 height : div높이
			 width : div 넓이
			 src : 아이프레임에 나타날 호출 경로
			 form : 함수를 호출하는 페이지의 form name
			 cookie : 심을 쿠키값
	리  턴 : hidden아이프레임을 나타나게함
	사용법 : openPopup(seq, height, width, src, form, cookie);

			 <DIV id="div1" style="display:none; position:absolute;">
			 <iframe name="iframepopup1" width="100%" height="100%" frameborder="0" scrolling="no" style="background:#999999"></iframe>
			 </DIV>
 */
 var openPopupIndex = 0;
function openPopupTemp(uniqueKey, width, height, src, form, cookie){
		var w = width;
		var h = parseInt(height, 10) + 5;

		var width = top.screen.availWidth;
		var height = top.screen.availHeight;
		 
		var leftpos = width/2 - w/2 - 40; 
		var toppos = height/2 - h/2 - 5 + top.document.documentElement.scrollTop; 
		if(leftpos<0 ) leftpos=0;
		if(toppos<0 ) toppos=0;
/*		document.all["div" + seq].style.width = w;
		document.all["div" + seq].style.height = h;n
		document.all["div" + seq].style.left = leftpos;
		document.all["div" + seq].style.top = toppos;
		document.all["div" + seq].style.display = "block";	

		form.target="iframepopup"+seq;
		form.action = src;
		form.submit();	*/

		top.openPopupIndex++;
		var divCtl = top.document.createElement('<DIV id="commonPopupDiv' + top.openPopupIndex + '" style="position:absolute;z-index:99;cursor:move" onmouseDown="setLayerMoveTarget(1,event,this);" onmouseout="setLayerMoveTarget(0,event,null)">');
		top.document.body.appendChild(divCtl); 
		var innerStr = ""
		innerStr = innerStr + "<table width=\"" + w + "\" height=\"25\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
		innerStr = innerStr + "  <tr>";
		innerStr = innerStr + "    <td background=\"/images/us/cm/popup_bar.gif\">&nbsp;&nbsp;<font color=\"#FFFFFF\">교보다이렉트</font></td>";
		innerStr = innerStr + "    <td align=\"right\" background=\"/images/us/cm/popup_bar.gif\"><img src=\"/images/us/cm/popup_close_off.gif\" onMouseOver=\"this.src='/images/us/cm/popup_close_on.gif'\" onMouseOut=\"this.src='/images/us/cm/popup_close_off.gif'\" width=\"15\" height=\"15\" hspace=\"5\" onClick=\"closeCommonDiv(" + top.openPopupIndex + ")\" style=\"cursor:hand\"></td>";
		innerStr = innerStr + "  </tr>";
		innerStr = innerStr + "</table>";
		innerStr = innerStr + '<iframe name="commonPopupIframe' + top.openPopupIndex + '" width="100%" height="100%" frameborder="1" scrolling="no" style="background:#999999"></iframe>';
		divCtl.innerHTML = innerStr

		divCtl.style.width = w;
		divCtl.style.height = h;
		divCtl.style.left = leftpos;
		divCtl.style.top = toppos;
		divCtl.style.display = "block";
		
		if(src != null && src.indexOf("?") != -1){
			src = src + "&openPopupIndex=" + top.openPopupIndex;
		} else {
			src = src + "?openPopupIndex=" + top.openPopupIndex;
		}

		form.target="commonPopupIframe"+top.openPopupIndex;
		form.action = src;
		form.submit();

		top.dynamicPopupTarget[top.openPopupIndex] = window;

//		dynamicPopupIndex[openPopupIndex] = uniqueKey;
		/*
		if(cookie != null){
			if(getCookie(cookie) != null){
				document.all["div" + seq].style.display = "";
			}
		}
		*/
}

var popupAddress = new Object();

function openPopup(uniqueKey, width, height, src, form, cookie){
//		width = width + 15;
//		height = height + 15;
		var winl = (screen.width - width)/2;
		var wint = (screen.height - height)/2;
		top.openPopupIndex++;
		
		var wnd = null;
		
		if(popupAddress[src] == null){
			// 상품비교 팝업 관련해 넣은 조건문
			if(uniqueKey=="goodsComparePopup"||uniqueKey=="popupCounsel"||uniqueKey=="privateInfo"){
				wnd = window.open("", uniqueKey, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				form.target = uniqueKey;
				form.action = src;
				form.submit();
			}else{
				wnd = window.open("", "DINAMIC_POPUP" + top.openPopupIndex, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				popupAddress[src] = wnd;
				
				if(src != null && src.indexOf("?") != -1){
					src = src + "&openPopupIndex=" + top.openPopupIndex;
				} else {
					src = src + "?openPopupIndex=" + top.openPopupIndex;
				}
	
				form.target = "DINAMIC_POPUP" + top.openPopupIndex;
				form.action = src;
				form.submit();	
			}
		} else {
			wnd = popupAddress[src];
			try{
				wnd.focus();
				//이벤트 경우 팝업이 이미 열려있다면 입력창으로 새로 바뀐다
				if(uniqueKey == "preEventPop" && top.openPopupIndex > 1){
					top.openPopupIndex = top.openPopupIndex-1;
					form.target = "DINAMIC_POPUP" + top.openPopupIndex;
					form.action = src;
					form.submit();
				}
			} catch(e) {
				wnd = window.open("", "DINAMIC_POPUP" + top.openPopupIndex, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				popupAddress[src] = wnd;
				
				if(src != null && src.indexOf("?") != -1){
					src = src + "&openPopupIndex=" + top.openPopupIndex;
				} else {
					src = src + "?openPopupIndex=" + top.openPopupIndex;
				}

				form.target = "DINAMIC_POPUP" + top.openPopupIndex;
				form.action = src;
				form.submit();
			}
		}

//		wnd.window.onunload=closeCommonDiv(1);
//		wnd.window.attachEvent('onunload', closeCommonDiv);
}

//가로 세로 위치 조절
function openPopupLeftTopSet(uniqueKey, width, height, left, top, src, form, cookie){
//		width = width + 15;
//		height = height + 15;
		var winl = screen.width - left;
		var wint = 0;
		
		if(top == "half"){
			wint = (screen.height - height)/2;
		}else{
			wint = top;
		}

		top.openPopupIndex++;
		
		var wnd = null;
		
		if(popupAddress[src] == null){
			// 상품비교 팝업 관련해 넣은 조건문
			if(uniqueKey=="goodsComparePopup"||uniqueKey=="popupCounsel"||uniqueKey=="privateInfo"){
				wnd = window.open("", uniqueKey, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				form.target = uniqueKey;
				form.action = src;
				form.submit();
			}else{
				wnd = window.open("", "DINAMIC_POPUP" + top.openPopupIndex, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				popupAddress[src] = wnd;
				
				if(src != null && src.indexOf("?") != -1){
					src = src + "&openPopupIndex=" + top.openPopupIndex;
				} else {
					src = src + "?openPopupIndex=" + top.openPopupIndex;
				}
	
				form.target = "DINAMIC_POPUP" + top.openPopupIndex;
				form.action = src;
				form.submit();	
			}
		} else {
			wnd = popupAddress[src];
			try{
				wnd.focus();
			} catch(e) {
				wnd = window.open("", "DINAMIC_POPUP" + top.openPopupIndex, "width=" + width + ", height=" + height + ", top=" + wint + ", left=" + winl + "', scrollbars=no");
				popupAddress[src] = wnd;
				
				if(src != null && src.indexOf("?") != -1){
					src = src + "&openPopupIndex=" + top.openPopupIndex;
				} else {
					src = src + "?openPopupIndex=" + top.openPopupIndex;
				}

				form.target = "DINAMIC_POPUP" + top.openPopupIndex;
				form.action = src;
				form.submit();
			}
		}

//		wnd.window.onunload=closeCommonDiv(1);
//		wnd.window.attachEvent('onunload', closeCommonDiv);
}

/**
	함수명 : openPopup
	설  명 : 레이어팝업 hidden으로 있는 아이프레임을 호출
	인  자 : seq : 기본 1 (아이프레임을 숨기는 div의 id명(div를 뺀 뒷 숫자) : id="div1")
			 height : div높이
			 width : div 넓이
			 left : left 간격
			 top : top 간격
			 src : 아이프레임에 나타날 호출 경로
			 form : 함수를 호출하는 페이지의 form name
			 cookie : 심을 쿠키값
	리  턴 : hidden아이프레임을 나타나게함
	사용법 : openPopup(seq, width, height, left, top, src, form, cookie);

			 <DIV id="div1" style="display:none; position:absolute;">
			 <iframe name="iframepopup1" width="100%" height="100%" frameborder="0" scrolling="no" style="background:#999999"></iframe>
			 </DIV>
 */
function openPopupLoc(uniqueKey, width, height, left, top, src, form, cookie){
		openPopup(uniqueKey, width, height, src, form, cookie);
}

var layerObj;
var onOff;


/**
	함수명 : closeCommonDiv
	설  명 : 레이어 팝업을 닫는다.
			 단 닫힐때 해당 iframe의 closePopupEvent() 함수를 호출하여 준다.
	인  자 : index 순번
	리  턴 : 
	사용법 : closeCommonDiv(getScriptParameter("openPopupIndex"));		
 */
function hideFrame(index){
	top.document.getElementById("commonPopupIframe" + index).style.display = "none";
}
var callCloseCommonDiv = false;
function closeCommonDiv(index){
/*	top.document.getElementById("commonPopupDiv" + index).style.display = "none";

	try{
		eval("top.commonPopupIframe" + index).closePopupEvent();
	}catch(e){}
	top.document.getElementById("commonPopupDiv" + index).innerHTML = "";*/
	if(!callCloseCommonDiv){
		try{
			callCloseCommonDiv = true;
			closePopupEvent();
		}catch(e){}
	}
	self.close();
}


/**
	함수명 : getOpener
	설  명 : 레이어 팝업을 호출한 window객체를 얻는다.
	인  자 : index 순번
	리  턴 : 레이어 팝업을 호출한 window 객체
	사용법 : getOpener(getScriptParameter("openPopupIndex")).testCall();		
 */
function getOpener(index){
/*	if(index == null){
		index = getScriptParameter("openPopupIndex");
		alert(index);
	}
	return top.dynamicPopupTarget[index];*/
	return opener;
}

//정리 
function setLayerMoveTarget(onOffFl, e, targetObj){ 
	layerObj = targetObj; 
	if(onOffFl == 1){ 
		x_margin =  e.clientX - layerObj.offsetLeft; 
		y_margin =  e.clientY - layerObj.offsetTop; 
	} 
	onOff = onOffFl; 
} 

//이동 
function layerMove(e){ 
	if(onOff == 1){ 
		x_result =  e.clientX - x_margin; 
		y_result =  e.clientY  - y_margin; 
		if(x_result > 0){layerObj.style.left = x_result;} 
		if(y_result > 0){layerObj.style.top =  y_result;} 
	}    
}

// 앵커
function moveBottom(Name) { 
	var objs = document.getElementsByName(Name); 

	if (Name == "" || Name == "top") document.body.scrollTop = 0; 
	if (objs == null || objs.length == 0) return false; 
	if (typeof moveBottom.obj == "undefined") { 
		moveBottom.obj = document.createElement("A"); 
		moveBottom.obj.href = "#"; 
		document.body.appendChild(moveBottom.obj); 
	} 

	// focus on the bottom of the page 
	moveBottom.obj.style.display = ""; 
	moveBottom.obj.focus(); 
	moveBottom.obj.style.display = "none"; 

	if (objs[0].tagName.toLowerCase() == "a") { // for IE 
		var ref = objs[0].href; 
		objs[0].href = "#"; 
		objs[0].focus(); 
		if (ref == "") objs[0].removeAttribute("href"); 
		else objs[0].href = ref; 
	} else { 
		objs[0].focus(); 
	} 
	objs[0].blur(); 

	return false; 
} 