﻿/*
 * CjContents 객체
 * 
 * 모든 컨텐츠의 초기화를 담당함.
 * 모든 컨텐츠들은 본파일을 필수로 인클루드 해야함.
 * 본 파일을 인클루드 하기만 하면, 자동으로 컨텐츠의 사이즈에 맞춰 컨텐츠 프레임을 Resize시킨다.
 * 도메인이 다른 컨텐츠도 자동으로 Resize처리를 한다.
 * 
 * Lee Won-Gyoon <richscript@gmail.com>, <@richscript>, <www.richscript.net>
 * e-motion Corp.
*******************************************************************************/

var CjContents = {
	instanceName : "CjContents",
	host : "www.cj.co.kr",
	windowName : "",
	funcs : [],
	useAutoResize : true,
	isSameDomain : true,
	vars : {
		minHeight : 600,
		height : 0,
		checkTime : 500,
		checkTimer : null
	},
	path : {
		resizeCommandPage : "/contents.resize.html"
		, initCommandPage : "/contents.init.html"
	},
	
	start : function(_func) {
		this.funcs.push(_func);
	},
	
	runStartCommand : function() {
		for (var i=0; i<this.funcs.length; i++) {
			try {
				this.funcs[i]();
			} catch(e) {}
			this.funcs[i] = null;
		}
		this.funcs = [];
	},
	
	displayContents : function() {
		try {
			parent.CjMenuUI.displayNewContents();
		} catch(e) {
			try {
				if (parent!=self) {
					parent[this.instanceName].displayNewContents();
				}
				this.runStartCommand();
			} catch(e2) {
				try {
					if (parent!=self&&(location.protocol=="https:"||location.host!=this.host)) {
						this.initFrame();
					}
				} catch(e3) { }
				this.runStartCommand();
			}
		}
	},
	
	displayNewContents : function() {
		try {
			throw "이 기능이 필요한 페이지는 별도로 해당페이지에서 재정의하여 사용합니다.";
		} catch(e) {
			
		}
	},
	
	resizeFrame : function() {
		if (this.isSameDomain) {
			try {
				parent.$("iframe[name='"+this.windowName+"']").css("height", this.vars.height);
				try {
					if (parent==top) {
						top.$(top.window).trigger("iframeResize");
					}
				} catch(e2) {}
			} catch(e) {
				this.isSameDomain = false;
			}
		}
		if (!this.isSameDomain) {
			if (!document.getElementById(this.ids.resizeFrameArea)) {
				$("html > body").append('<div id="'+this.ids.resizeFrameArea+'" style="position:absolute;left:-100px;top:-100px;width:1px;height:1px;overflow:hidden;"></div>');
			}
			var frameName = "CPF"+ (new Date()).getTime();
			var url = "http://"+this.host+this.path.resizeCommandPage;
			url = url.appendParameter("_temp="+(new Date()).getTime());
			url = url.appendParameter("h="+this.vars.height);
			url = url.appendParameter("n="+this.windowName);
			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.resizeFrameArea).html(s);
			try {
				window.frames[frameName].location.replace(url);
			} catch(e) {
				open(url, frameName, "");
			}
		}
	},
	
	initFrame : function() {
		if (!this.isSameDomain) {
			if (!document.getElementById(this.ids.initFrameArea)) {
				$("html > body").append('<div id="'+this.ids.initFrameArea+'" style="position:absolute;left:-100px;top:-100px;width:1px;height:1px;overflow:hidden;"></div>');
			}
			var frameName = "CPF"+ (new Date()).getTime();
			var url = "http://"+this.host+this.path.initCommandPage;
			url = url.appendParameter("_temp="+(new Date()).getTime());
			url = url.appendParameter("n="+this.windowName);
			var s = '';
			s += '<iframe';
			s += ' name="'+frameName+'"';
			s += ' id="'+frameName+'"';
			s += ' src="'+url+'"';
			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.initFrameArea).html(s);
		}
	},

	checkHeight : function() {
		try {
			var h = $js.browser.bodyH();
			if (h<this.vars.minHeight) h = this.vars.minHeight;
			if (this.vars.height!=h) {
				this.vars.height = h;
				this.resizeFrame();
			}
		} catch(e) { }
	},
	
	clearObserver : function() {
		clearTimeout(this.vars.checkTimer);
	},
	
	observeHeight : function() {
		if (this.useAutoResize) {
			var thisName = this.instanceName;
			this.vars.checkTimer = setInterval(function() {
				window[thisName].checkHeight();
			}, this.vars.checkTime);
		}
	},
	
	initScroll : function(_x, _y) {
		var x = (_x==undefined) ? 0 : Math.toInt(_x);
		var y = (_y==undefined) ? 0 : Math.toInt(_y);
		try {
			top.scroll(x,y);
		} catch(e) {
			window.scroll(x,y);
		}
	},
	
	initialize : function() {
		this.ids = {
			resizeFrameArea : this.instanceName + "-resizeFrameArea"
			, initFrameArea : this.instanceName + "-initFrameArea"
		};
		var thisName = this.instanceName;
		this.windowName = window.name;
		this.start(function() {
			window[thisName].observeHeight();
		});
		try {
			(function() {
				var that = window[thisName];
				parent.CjMenuUI.resetNext(function() {
					that.runStartCommand();
				});
			})();
		} catch(e) { }
	}
};
CjContents.initialize();

$(function() {
	$(document).bind("click.bubble", function() {
		try {
			parent.$(parent.document).trigger("click");
		} catch(e) {
			$(document).unbind("click.bubble");
		}
	});
	if (window.AUTO_RESIZE!==false) {
		CjContents.checkHeight();
	} else {
		CjContents.useAutoResize = false;
	}
	if (window.INIT_SCROLL===true) {
		CjContents.initScroll();
	}
});
$(window).load(function(){
	CjContents.displayContents();
	
	/* <a/> 클릭 후 테두리 제거 */
	$("a").click(function(e) {
		$(this).blur();
	});
});



