﻿/*******************************레이어 팝업 스크립트**************************************/
//작성자 : 클립서비스 정웅기

//레이어팝업 객체 정의
function LayerPopup() {
	//팝업 속성값
	this.width = 0;			//팝업 최소가로크기 100px
	this.height = 0;			//팝업 최소세로크기 100px
	this.isModal = true;		//팝업 모달창여부
	this.dimmingOpacity = 0.3;	//모달창 배경 투명도
	this.url = "#"; 			//내용 Url
	this.isIframePop = true;	//Iframe에 내용 표시 여부
	this.left = -1;				//팝업창 위치 Left값
	this.top = -1; 				//팝업창 위치 Top값

	//팝업 자식객체
	this.dimmer = null;
	this.wrapper = null;
	this.iframe = null;
}


LayerPopup.prototype.Init = function (url, option) {
	var target = this;

	//각각 속성 기본값 지정
	this.url = url;
	if (option.width != undefined && !isNaN(option.width)) this.width = option.width;
	if (option.height != undefined && !isNaN(option.height)) this.height = option.height;
	if (option.isModal != undefined) this.isModal = option.isModal != false;
	this.SetPosition(option);

	try {
		this.isIframePop = $("#" + url).length == 0
	}
	catch (e) {
		target.isIframePop = true;
	}

	///////////자식객체 생성
	//Modal창 배경레이어 생성
	if (this.dimmer == null) {
		if ($("#clipserviceLayerPopupDimmer").length == 0) {
			var dimmer = $(document.createElement("div"));
			dimmer.attr("id", "clipserviceLayerPopupDimmer");
			dimmer.css({
				position: "absolute"
				, zIndex: 999998
				, top: "0px"
				, left: "0px"
				, width: $(document).width()
				, height: $(document).height()
				, backgroundColor: "#000000"
				, display: "none"
			});
			dimmer.appendTo("body");
			this.dimmer = dimmer;
		}
		else {
			this.dimmer = $("#clipserviceLayerPopupDimmer").first();
		}

		this.dimmer.click(function () {
			target.Close();
		});
	}

	//레이어팝업 틀 레이어 생성
	if (this.wrapper == null) {
		var wrapper = $(document.createElement("div"));
		wrapper.appendTo("body");
		this.wrapper = wrapper;
	}
	this.wrapper.css({
		position: "absolute"
		, width: this.width + "px"
		, height: this.height + "px"
		, left: this.left
		, top: this.top
		, overflow: "auto"
		, zIndex: 999999
		, backgroundColor: "transparent"
		, margin: "0px"
		, padding: "0px"
		, display: "none"
	});

	//아이프레임형식인경우 프레임생성 및 삽입
	if (this.isIframePop) {
		if (this.iframe == null) {
			var iframe = $(document.createElement("iframe"));
			iframe.css({
				width: "100%"
				, height: "100%"
				, margin: "0px"
				, padding: "0px"
				, backgroundColor: "transparent"
				, overflow: "auto"
			});
			iframe.attr("frameborder", 0);
			//iframe.attr("scrolling", "no");
			iframe.attr("allowtransparency", true);
			iframe.appendTo(this.wrapper);
			this.iframe = iframe;
		}
		this.wrapper.css("overflow", "hidden");
		this.iframe.attr("src", this.url);
	}
	else {
		$("#" + this.url).show().appendTo(this.wrapper);
	}
}


LayerPopup.prototype.Open = function () {
	var target = this;

	if (this.isModal) {
		try { this.dimmer.fadeTo(0, this.dimmingOpacity); } catch (e) { }
	}

	target.SetPopSize();
	this.wrapper.fadeIn(300, function () {  });
}


LayerPopup.prototype.Close = function () {
	var dimm = this.dimmer

	var target = this;
	var targetWrapper = this.wrapper;
	var targetIframe = this.iframe;

	this.wrapper.fadeOut(300, function () {
		dimm.hide();

		$(targetIframe).remove();
		$(targetWrapper).remove();
		target.wrapper = null;
		target.iframe = null;

		__clipLayerPopResizePopupTarget = null;
	});
}


LayerPopup.prototype.SetPosition = function (option) {
	var target = this;

	//left 값 지정
	if (option.left != undefined && !isNaN(option.left)) {
		this.left = option.left;
	}
	else {
		var scrollLeft = $(document).scrollLeft();
		var viewWidth = $(window).width();
		var popWidth = this.width;

		this.left = scrollLeft + viewWidth / 2 - popWidth / 2;

		$(this.wrapper).css("left", this.left);
	}

	// top 값 지정
	if (option.top != undefined && !isNaN(option.top)) {
		this.top = option.top;
	}
	else {
		var scrollTop = $(document).scrollTop();
		var viewHeight = $(window).height();
		var popHeight = this.height;

		this.top = scrollTop + viewHeight / 2 - popHeight / 2;
		if (this.top < 0) this.top = 0;

		$(this.wrapper).css("top", this.top);
	}
}


LayerPopup.prototype.SetPopSize = function () {
	//아이프레임방식일경우 자동 리사이징
	if (this.isIframePop && this.height == 0) {
		__clipLayerPopResizePopupTarget = this;

		var targetIframe = this.iframe;
		var targetWrapper = this.wrapper;

		var height = $("body", targetIframe[0].contentWindow.document).height();

		$(targetIframe).height(height);
		$(targetWrapper).height(height);

		this.height = height;
		this.SetPosition({});

		if (targetWrapper.height() != height) {
			setTimeout("try{__clipLayerPopResizePopupTarget.SetPopSize();}catch(e){};", 50);
		}
	}
}



//기본적으로 모든 페이지에 팝업객체 삽입
var clipserviceLayerPopupObj = new LayerPopup();

function LayerPop(url, option, id) {
	if (id != undefined && id.length > 0) {
		eval("clipserviceLayerPopupObj_" + id + " = new LayerPopup();");
		var popObj = eval("clipserviceLayerPopupObj_" + id);
		popObj.Init(url, option);
		popObj.Open();
	}
	else {
		clipserviceLayerPopupObj.Init(url, option);
		clipserviceLayerPopupObj.Open();
	}
}

function CloseLayerPop(id) {
	if (id != undefined && id.length > 0) {
		var popObj = eval("clipserviceLayerPopupObj_" + id);
		popObj.Close();
	}
	else {
		clipserviceLayerPopupObj.Close();
	}
}
