﻿// JScript 파일
IncludeCSS("/popup/css/style.css")	//팝업 스타일시트 인클루드
var Popup
Popup = function (idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize, Body) {
	this.idx = idx;
	this.pTop = pTop;
	this.pLeft = pLeft;
	this.pWidth = pWidth;
	this.pHeight = pHeight;
	this.pScrollbars = pScrollbars;
	this.autosize = autosize;
	this.Body = Body;

	this.oPopup = $("<div />");
}

Popup.prototype.showPop = function () {
	this.oPopup.attr("id", "LayerPopup");
	this.oPopup.css({
		zIndex: "100000" + this.idx,
		position: "absolute",
		top: this.pTop + "px",
		left: this.pLeft + "px",
		width: this.pWidth + "px",
		height: this.pHeight + "px",
		overflow: ((this.pScrollbars == "1") ? "auto" : "visible")
	});

	this.oPopup.html(this.Body);

	this.oPopup.appendTo($("body"));
}
	
Popup.prototype.hidePop = function () {
	this.oPopup.hide();
}

function initPopup(){
	Url = "/popup/popupcheck.asp";
	Parameters = "";
	
	$.ajax({
		url: Url,
		data: Parameters,
		dataType: "text",
		success: function (Rst) {
			if(Rst != ''){
				var tPop = eval(Rst)
				
				for(j = 0; j < tPop.length; j++){
					var pInfo = tPop[j];
					
					idx = pInfo[0];
					ptype = pInfo[1];
					pTop = pInfo[2];
					pLeft = pInfo[3];
					pWidth = pInfo[4];
					pHeight = pInfo[5];
					pToolbar = pInfo[6];
					pLocation = pInfo[7];
					pStatus = pInfo[8];
					pMenubar = pInfo[9];
					pScrollbars = pInfo[10];
					pResizable = pInfo[11];
					pClose = pInfo[12];
					autosize = pInfo[13];
					
					var pName = "Popup"+idx
					var eventCookie = getCookie(pName); 
					
					if(ptype == "1"){
						if (eventCookie != "no") 
							Wopen(idx, pTop, pLeft, pWidth, pHeight, pToolbar, pScrollbars, pStatus, pResizable, pLocation, pMenubar);
					}else if(ptype == "0"){
						if (eventCookie != "no") 
							Lopen(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize);
					}
				}
			}
		},

		error: function (xhr, textStatus, errorThrown) {
			var exceptShow = "상태 코드: " + xhr.status;
			exceptShow += ",  비정상으로 종료되었습니다.";
			alert(exceptShow);
		}
	});
}

function getCookie(name) { 
    var Found = false 
    var start, end 
    var i = 0 

    while(i <= document.cookie.length) { 
      start = i 
      end = start + name.length 
      if(document.cookie.substring(start, end) == name) { 
          Found = true 
          break 
      }
      
      i++ 
    } 

    if(Found == true) { 
      start = end + 1 
      end = document.cookie.indexOf(";", start) 
      
      if(end < start) 
          end = document.cookie.length 
      
      return document.cookie.substring(start, end) 
    } 
    return "" 
}

function setCookie( name, value, expiredays ){ 
		var todayDate = new Date(); 
		todayDate.setDate( todayDate.getDate() + expiredays ); 
		document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" 
}

function Wopen(idx, pTop, pLeft, pWidth, pHeight, pToolbar, pScrollbars, pStatus, pResizable, pLocation, pMenubar) {
	Url = "/popup/popup.asp?idx="+idx
	Name = "Popup"+idx
	Features = "top="+pTop+",left="+pLeft+",width="+pWidth+",height="+pHeight+",scrollbars="+pScrollbars+",toolbar="+pToolbar+",status="+pStatus+",resizable="+pResizable+",menubar="+pMenubar+",location="+pLocation;
	
  window.open(Url, Name, Features)
}

var popupBox = [];

function Lopen(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize){
	try{
		Url = "/popup/getPopup.asp";
		Parameters = "idx=" + idx;

		$.ajax({
			url: Url,
			data: Parameters,
			dataType: "text",
			success: function (Rst) {
				if (Rst != '') {
					if (autosize == "1") {
						var divEl = $(Rst)

						divEl.css({
							position: "absoult",
							left: "-10000px"
						});

						divEl.appendTo("body");
						
						pWidth = divEl.outerWidth();
						pHeight = divEl.outerHeight();

						divEl.remove();
					}

					popupBox[idx] = new Popup(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize, Rst);
					popupBox[idx].showPop();

					HideSelectBox();
				}
			},

			error: function (xhr, textStatus, errorThrown) {
				var exceptShow = "상태 코드: " + xhr.status;
				exceptShow += ",  비정상으로 종료되었습니다.";
				alert(exceptShow);
			}
		});
	}catch(e) {}
}

function HideSelectBox(){
	var box = document.getElementsByTagName("select")
	
	for(i = 0; i < box.length; i++){
		box[i].style.visibility = "hidden";
	}
}

function ShowSelectBox(){
	box = document.getElementsByTagName("select")
	
	for(i = 0; i < box.length; i++){
		box[i].style.visibility = "visible";
	}
}

function HidePopup(idx){
	popupBox[idx].hidePop();
	
	var Flag = true;
	var pop = document.getElementsByName("LayerPopup")
	for(i = 0; i < pop.length; i++){
		if(pop[i].style.display != "none")
			Flag = false;
	}
	
	if(Flag) ShowSelectBox();
}

function SaveCookiesAndClose(idx, name, term) {
	setCookie(name, "no", 1); // 1일동안 쿠키를 보존합니다.

	HidePopup(idx);
}

function CloseWin(){
	HidePopup(idx);
}

$(document).ready(function () {
	initPopup();
});
