/*
 * CjPopup 객체
 * 
 * 레이어 팝업.
 * 
 * Lee Won-Gyoon <richscript@gmail.com>, <@richscript>, <www.richscript.net>
 * e-motion Corp.
*******************************************************************************/

var $popup = CjPopup = {
	instanceName : "CjPopup",
	rootFrame : null,
	isRootFrame : false,
	status : {
		isOpened : false
	},
	config : {
		rootContentsWidth : 880
		, defaultWidth : 400
		, defaultHeight : 300
	},
	
	isExist : function() {
		return true;
	},
	
	open : function(_url) {
		if (!this.rootFrame) {
			this.checkRootFrame();
		}
		if (this.rootFrame) {
			if (!this.isRootFrame) {
				this.rootFrame[this.instanceName].open(_url);
			} else {
				this.status.isOpened = true;
				this.print();
				this.loadContents(_url);
			}
		} else {
			//alert("올바른 접근이 아닙니다.");
			window.open("/?", "_top", "");
		}
	},
	
	close : function() {
		if (!this.rootFrame) {
			this.checkRootFrame();
		}
		if (this.rootFrame) {
			if (!this.isRootFrame) {
				this.rootFrame[this.instanceName].close(_url);
			} else {
				$(window).unbind("."+this.instanceName);
				$(this.ids.root).html("");
				this.status.isOpened = false;
			}
		} else {
			//alert("올바른 접근이 아닙니다.");
			self.close();
		}
	},
	
	resizeCover : function() {
		var tarW = $js.browser.maxW();
		var tarH = $js.browser.maxH();
		$(this.ids.cover).css({width:tarW,height:tarH});
	},
	
	resizePopup : function(_width, _height) {
		$(this.ids.loading).hide();
		var width = Math.toInt(_width, this.config.defaultWidth);
		var height = Math.toInt(_height, this.config.defaultHeight);
		$(this.ids.shadowTop).css({width:width});
		$(this.ids.shadowLeft).css({height:height});
		$(this.ids.shadowRight).css({height:height});
		$(this.ids.shadowBottom).css({width:width});
		$(this.ids.contents).css({width:width, height:height});
		$(this.ids.contents+" > iframe").css({width:width, height:height});
		this.movePopup();
	},
	
	movePopup : function() {
		var popupW = $(this.ids.popup).width();
		var popupH = $(this.ids.popup).height();
		//var tarX = Math.max((this.config.rootContentsWidth-popupW)/2,50);
		var tarX = Math.max(($js.browser.screenW()-popupW)/2,50);
		var tarY = Math.max($js.browser.scrollY()+($js.browser.screenH()-popupH)/2-20,10);
		if (tarY+popupH>$js.browser.maxH()) {
			tarY = $js.browser.maxH() - popupH;
		}
		if (tarY<0) {tarY = 0;}
		$(this.ids.popup).css({left:tarX,top:tarY});
	},
	
	loadContents : function(_url) {
		var frameName = "PCF"+ (new Date()).getTime();
		var url = _url;
		url = url.appendParameter("_temp="+(new Date()).getTime());
		var s = '';
		s += '<iframe';
		s += ' name="'+frameName+'"';
		s += ' id="'+frameName+'"';
		s += ' src="about:blank"';
		s += ' width="1';
		s += ' height="1';
		s += ' marginwidth="0"';
		s += ' marginheight="0"';
		s += ' hspace="0"';
		s += ' vspace="0"';
		s += ' scrolling="no"';
		s += ' frameborder="0"';
		s += ' style="width:1px;height:1px;"';
		s += '></iframe>';
		$(this.ids.contents).html(s);
		try {
			window.frames[frameName].location.replace(url);
		} catch(e) {
			open(url, frameName, "");
		}
	},
	
	getCoverHTML : function() {
		var s = '';
		s += '<div id="'+this.ids.cover.substring(1)+'" style="';
		s += ' position:absolute;';
		s += ' z-index:100;';
		s += ' left:0;top:0;';
		s += ' width:1px;top:1px;';
		s += ' background:url(/img/richscript/ui/popup/cover.bg.png);';
		s += ' _background-image:none;';
		s += ' _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/cover.bg.png, sizingMethod=scale);"></div>'
		return s;
	},
	
	getPopupHTML : function() {
		var s = '';
		s += '<table cellpadding="0" cellspacing="0" border="0"';
		s += ' id="'+this.ids.popup.substring(1)+'"';
		s += ' style="position:absolute;z-index:105;left:0;top:0;width:auto;height:auto;visibility:hidden">\n';
		s += '	<tr>\n';
		s += '		<td><div style="width:15px;height:5px;background:url(/img/richscript/ui/popup/shadow.top.left.bg.png) no-repeat;_background-image:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.top.left.bg.png, sizingMethod=scale);"></div></td>\n';
		s += '		<td>';
		if (!$js.browser.isIE6) {
			s += '<div><img';
			s += ' id="'+this.ids.shadowTop.substring(1)+'"';
			s += ' src="/img/richscript/ui/popup/shadow.top.bg.png"';
			s += ' style="width:'+this.config.defaultWidth+'px;height:5px;" border="0" />';
			s += '</div>';
		} else {
			s += '<div';
			s += ' id="'+this.ids.shadowTop.substring(1)+'"';
			s += ' style="_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.top.bg.png, sizingMethod=scale);width:'+this.config.defaultWidth+'px;height:5px;">';
			s += '</div>';
		}
		s += '</td>\n';
		s += '		<td><div style="width:15px;height:5px;background:url(/img/richscript/ui/popup/shadow.top.right.bg.png) no-repeat;_background-image:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.top.right.bg.png, sizingMethod=scale);"></div></td>\n';
		s += '	</tr>\n';
		s += '	<tr>\n';
		s += '		<td>';
		if (!$js.browser.isIE6) {
			s += '<div><img';
			s += ' id="'+this.ids.shadowLeft.substring(1)+'"';
			s += ' src="/img/richscript/ui/popup/shadow.left.bg.png"';
			s += ' style="width:15px;height:'+this.config.defaultHeight+'px;" border="0" />';
			s += '</div>';
		} else {
			s += '<div';
			s += ' id="'+this.ids.shadowLeft.substring(1)+'"';
			s += ' style="_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.left.bg.png, sizingMethod=scale);width:15px;height:'+this.config.defaultHeight+'px;">';
			s += '</div>';
		}
		s += '</td>\n';
		s += '		<td bgcolor="#ffffff" valign="top">';
		s += '			<table cellpadding="0" cellspacing="0" border="0"';
		s += '			 id="'+this.ids.loading.substring(1)+'"';
		s += '			 width="'+this.config.defaultWidth+'"';
		s += '			 height="'+this.config.defaultHeight+'"';
		s += '			 style="position:absolute;"';
		s += '			><tr><td align="center" valign="middle"><img src="/img/richscript/ui/template/loading.small.gif" width="24" height="24" border="0" /></td></tr></table>';
		s += '			<div id="'+this.ids.contents.substring(1)+'" style="width:1px;height:1px;overflow:hidden;"></div>';
		s += '		</td>\n';
		s += '		<td>';
		if (!$js.browser.isIE6) {
			s += '<div><img';
			s += ' id="'+this.ids.shadowRight.substring(1)+'"';
			s += ' src="/img/richscript/ui/popup/shadow.right.bg.png"';
			s += ' style="width:15px;height:'+this.config.defaultHeight+'px;" border="0" />';
			s += '</div>';
		} else {
			s += '<div';
			s += ' id="'+this.ids.shadowRight.substring(1)+'"';
			s += ' style="_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.right.bg.png, sizingMethod=scale);width:15px;height:'+this.config.defaultHeight+'px;">';
			s += '</div>';
		}
		s += '</td>\n';
		s += '	</tr>\n';
		s += '	<tr>\n';
		s += '		<td><div style="width:15px;height:15px;background:url(/img/richscript/ui/popup/shadow.bottom.left.bg.png) no-repeat;_background-image:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.bottom.left.bg.png, sizingMethod=scale);"></div></td>\n';
		s += '		<td>';
		if (!$js.browser.isIE6) {
			s += '<div><img';
			s += ' id="'+this.ids.shadowBottom.substring(1)+'"';
			s += ' src="/img/richscript/ui/popup/shadow.bottom.bg.png"';
			s += ' style="width:'+this.config.defaultWidth+'px;height:15px;" border="0" />';
			s += '</div>';
		} else {
			s += '<div';
			s += ' id="'+this.ids.shadowBottom.substring(1)+'"';
			s += ' style="_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.bottom.bg.png, sizingMethod=scale);width:'+this.config.defaultWidth+'px;height:15px;">';
			s += '</div>';
		}
		s += '</td>\n';
		s += '		<td align="right" valign="bottom">\n';
		s += '			<div style="width:15px;height:15px;background:url(/img/richscript/ui/popup/shadow.bottom.right.bg.png) no-repeat;_background-image:none;_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/popup/shadow.bottom.right.bg.png, sizingMethod=scale);"></div>\n';
		s += '			<div style="position:relative;right:0;bottom:0;width:1px;height:1px;">\n';
		s += '				<a href="?" id="'+this.ids.closeButton.substring(1)+'" style="position:absolute;right:15px;bottom:15px;display:block;width:40px;height:40px;background:url(/img/richscript/ui/popup/btn.close.bg.gif) no-repeat;" title="닫기"></a>\n';
		s += '			</div>\n';
		s += '		</td>\n';
		s += '	</tr>\n';
		s += '</table>\n';
		return s;
	},
	
	print : function() {
		$(this.ids.root).html(this.getCoverHTML()+this.getPopupHTML());
		this.resizeCover();
		this.movePopup();
		$(this.ids.popup).css("visibility", "visible");
		
		
		var ui = this.instanceName;
		$(this.ids.closeButton).click(function(e) {
			e.preventDefault();
			window[ui].close();
		});
		$(this.ids.cover).click(function(e) {
			e.preventDefault();
			window[ui].close();
		});
		
		$(window).bind("resize."+this.instanceName, function() {
			window[ui].resizeCover();
		});
		/*
		$(window).bind("scroll."+this.instanceName, function() {
			window[ui].movePopup();
		});
		*/
	},
	
	printPopupArea : function() {
		if (!document.getElementById(this.ids.root.substring(1))) {
			$("body").append('<span id="'+this.ids.root.substring(1)+'"></span>');
		}
	},
	
	checkRootFrame : function() {
		try {
			if (parent[this.instanceName].isExist()) {
				this.rootFrame = parent;
			}
		} catch(e) {
			try {
				if (parent.parent[this.instanceName].isExist()) {
					this.rootFrame = parent.parent;
				}
			} catch(e2) {
				try {
					if (parent.parent.parent[this.instanceName].isExist()) {
						this.rootFrame = parent.parent.parent;
					}
				} catch(e3) {
					try {
						if (parent.parent.parent.parent[this.instanceName].isExist()) {
							this.rootFrame = parent.parent.parent.parent;
						}
					} catch(e4) {
						
					}
				}
			}
		}
	},
	
	initialize : function() {
		this.ids = {
			root : "#"+this.instanceName+"-root"
			, cover : "#"+this.instanceName+"-cover"
			, popup : "#"+this.instanceName+"-popup"
			, loading : "#"+this.instanceName+"-loading"
			, contents : "#"+this.instanceName+"-contents"
			, closeButton : "#"+this.instanceName+"-close-button"
			, shadowTop : "#"+this.instanceName+"-shadow-top"
			, shadowLeft : "#"+this.instanceName+"-shadow-left"
			, shadowRight : "#"+this.instanceName+"-shadow-right"
			, shadowBottom : "#"+this.instanceName+"-shadow-bottom"
		};
		
		if (top==self) {
			this.rootFrame = window;
			this.isRootFrame = true;
		}
		
		if (this.isRootFrame) {
			this.printPopupArea();
			var ui = this.instanceName;
			if (CjMenu) {
				CjMenu.event.on("openMenu", function(_menu, _option) {
					if (window[ui].status.isOpened) {
						window[ui].close();
					}
				});
			}
		}
	}
};



$(function() {
	CjPopup.initialize();
});

$(window).bind("load", function() {
});



