﻿/*
 * CjMenuUI 객체
 * 
 * Template의 상태를 현재 메뉴에 맞게 유지시켜주는 역할을 함.
 * 그외 공통적으로 모든화면에서 보여지는 Template의 각종 기능을 제어함.
 * 
 * Lee Won-Gyoon <richscript@gmail.com>, <@richscript>, <www.richscript.net>
 * 사이트 메인 content제외한 부분은 삭제
 * e-motion Corp.
*******************************************************************************/

var CjMenuUI = {
	instanceName : "CjMenuUI",
	menuInstanceName : "CjMenu",
	userSession : {},
	event : new $js.lib.ObjectEvent(),
	currentMenu : null,
	currentSectionCode : "",
	currentSectionIndex : -1,     
	msg : {
		  BAD_FRAME : "올바른 프레임 구조로 접근하지 않았습니다.\n올바른 경로로 접근해 주시기 바랍니다."
		, NO_CURRENT_MENU : "메뉴코드가 누락되었거나, 잘못된 메뉴코드입니다.\n다시 시도해 주십시오.\n\n※ 올바른 프레임 구조로 접근하지 않은 경우일 수 있습니다."
	},
	section : [],
	func : [],
	config : {
		   contentsHeight : 670
		, maxStandByTime : 3000
	},
	vars : {
		prevOverSectionIndex : 0
		, loadingTimer : null
		, resizeTimer : null
		, sitemapIndex : -1
	},
	status : {
		isHeaderClosed : true
		, isSubMenuHeightChecked : false
		, isLoading : false
	},
	ids : {
//		header : "#header"
///		, headerBody : "#header-body"
//		, headerOpenButton : "#header-open-button"
//		, headerCloseButton : "#header-close-button"
//		, gnbCurrent : "#gnb-current"
//		, gnbCurrentSectionTitle : "#gnb-current-section-title"
//		, gnbCurrentMenu : "#gnb-current-menu"
//		, gnbCurrentMenuTitle : "#gnb-current-menu-title"
//		, gnbSectionBg : "#gnb-section-bg"
//		, gnbSectionFocus : "#gnb-section-focus"
//		, gnbSectionMenu : "#gnb-section-menu"
//		, gnbSub : "#gnb-sub"
//		, gnbSubMenu : "#gnb-sub-menu"
//		, gnbUtil : "#gnb-util"
//		, footerHistoryMenu : "#footer-history-menu"
		  contents : "#content"
		, mainCover : "#cover"
		, mainCoverLoading : "#cover-loading"
//		, footer : "#footer"
//		, userSessionBefore : ".user-session-before"
//		, userSessionAfter : ".user-session-after"
//		, sitemap : "#sitemap"
//		, sitemapSectionContents : ".sitemap-section-contents"
//		, footerCopyright : "#footer-copyright"
	},

	/**
	* CjMenuUI 객체가 생성되었는지 여부를 알려준다.
	* 프레임을 잘못찾거나 객체가없으면 Exception을 발생시킨다.
	* @return boolean
	*/
	isExist : function() {
		return true;
	},


	/**
	* 다음 실행할 작업이 있으면 실행한다.
	* @return Void
	*/
	doNext : function() {
		if (this.func.length>0) {
			try {
				this.func[0]();
			} catch(e) {}
			this.func[0] = null;
			this.func = this.func.compact();
		}
	},

	/**
	* 다음 실행할 작업을 등록한다.
	* @param _func 다음 실행할 Function
	* @return Void
	*/
	addNext : function(_func) {
		this.func.push(_func);
	},

	/**
	* 다음 실행할 작업을 초기화 한다.
	* @param _func 다음 실행할 Function
	* @return Void
	*/
	resetNext : function(_func) {
		for (var i=0; i<this.func.length; i++) {
			this.func[i] = null;
		}
		this.func = [];
		this.func.push(_func);
	},


	/**
	* 지정한 컨텐츠를 불러온다.
	* @return Void
	*/
	loadContents : function(_url) {
		$(window).unbind(".eachContents");
		var url = (_url==undefined||_url=="") ? this.currentMenu.url : _url;
		//url = url.appendParameter("menuCode=");
		var cmdParam = this.currentMenu ;
		this.event.onEvent(this, "loadContents", cmdParam, url);
		var frameName = "F"+(new Date()).getTime();
		var html = '';
		html += '<div style="width:100%;height:auto;padding:0;"><iframe';
		html += ' style="position:relative;left:0;top:0;background-color:#fff;"';
		html += ' name="'+frameName+'"';
		html += ' id="'+frameName+'"';
		html += ' src="about:blank"';
		html += ' width="100%"';
		html += ' height="'+this.config.contentsHeight+'"';
		html += ' marginwidth="0"';
		html += ' marginheight="0"';
		html += ' hspace="0"';
		html += ' vspace="0"';
		html += ' scrolling="no"';
		html += ' frameborder="0"';
		html += '></iframe></div>';
		if ($(this.ids.contents).children().length>1) {
			for (var i=1; i<$(this.ids.contents).children().length; i++) {
				$(this.ids.contents).children().eq(i).remove();
			}
		}
		$(this.ids.contents).append(html);		
		try {
			frameName.location.href=url;
			//window.frames[frameName].location.replace(url);
		} catch(e) {
			
			open(url, frameName, "");
		}
		
		var tempHeight = $(this.ids.contents).children().eq(0).outerHeight();
		$(this.ids.contents).children().eq(0).css({
			position : "absolute"
			, zIndex : 2
			, height : tempHeight
			, overflow : "hidden"
			, paddingRight : "4px"
			, backgroundColor : "#ffffff"
		//	, background : "url(contents.shadow.bg.png) repeat-y"
			, backgroundPosition : "right top"
		});
		$(this.ids.contents).css({height:tempHeight});
		$('html, body').animate({scrollTop:0}, 'slow');
	},	
	displayNewContents : function() {
		if (this.vars.loadingTimer) {
			clearTimeout(this.vars.loadingTimer);
		}
		if ($(this.ids.contents).children().length>1) {
			var ui = this.instanceName;
			var tarX = (-1)*$(this.ids.contents).children().eq(0).outerWidth();
			$(this.ids.contents).children().eq(0).stop().animate({left:tarX}, 0, "easeInExpo", function() {
				/*
				var frameName = $(this).children().eq(0).attr("name");
				if (frameName!=undefined&&frameName!="") {
					try {
						window.frames[frameName].location.replace("about:blank");
					} catch(e) {
					}
				}
				*/			
				$(window[ui].ids.contents).css({height:"auto"});
				$(this).remove();
				window[ui].hideMainCover();
				window[ui].doNext();
			});
		} else {
			this.doNext();
		}
	},

	resizeMainCover : function() {
		$(this.ids.mainCover).css({width : $js.browser.maxW()-22, height : $js.browser.maxH()-22});
		$(this.ids.mainCoverLoading).css({width : $(this.ids.contents).outerWidth(), height : Math.min($(this.ids.contents).outerHeight()+this.config.gnbSectionHeight,$js.browser.screenH()), left : ($js.browser.maxW()-$(this.ids.contents).outerWidth())/2 });
	},
	
	showMainCover : function() {
		this.resizeMainCover();
		$(this.ids.mainCover).show();
		$(this.ids.mainCoverLoading).show();
		var ui = this.instanceName;
		this.vars.loadingTimer = setTimeout(function() {
			window[ui].displayNewContents();
		}, this.config.maxStandByTime);
	},
	
	hideMainCover : function() {
		$(this.ids.mainCover).hide();
		$(this.ids.mainCoverLoading).hide();
		this.status.isLoading = false;

	}
};
var $loading = {
	getL : function() {
		var s = '';
		s += '<div style="';
		s += '	width:75px;';
		s += '	height:74px;';
		s += '	background:url(/images/loading_bg.png) transparent;';
		s += '	_background-image:none;';
		s += '	_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/images/loading_bg.png, sizingMethod=scale);';
		s += '	background-repeat:no-repeat;';
		s += '">';
		s += '<div style="padding:10px 3px 0 0;">';
		s += '<img src="/images/loadingREV.gif" width="50" height="50" border="0" />';
		s += '</div>';
		s += '</div>';
		return s;
	},
	getS : function() {
		var s = '';
		s += '<div style="';
		s += '	width:48px;';
		s += '	height:48px;';
		s += '	background:url(/img/richscript/ui/template/loading.small.bg.png) transparent;';
		s += '	_background-image:none;';
		s += '	_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=/img/richscript/ui/template/loading.small.bg.png, sizingMethod=scale);';
		s += '	background-repeat:no-repeat;';
		s += '">';
		s += '<div style="padding:12px 0 0 0;">';
		s += '<img src="/img/richscript/ui/template/loading.small.gif" width="24" height="24" border="0" />';
		s += '</div>';
		s += '</div>';
		return s;
	},
	printL : function() {
		document.write(this.getL());	
	},
	printS : function() {
		document.write(this.getS());	
	}
};

jQuery(document).ready(function($) { 		
     $.history.init(function(hash) {
			if (hash == "" ){
				hash =  "/index.asp"}
			CjMenuUI.showMainCover();	
			CjMenuUI.loadContents(hash);			
            });							
	});
function menuOpen(depth,sub_depth, url, mcode){
	url = url.replace(/^.*#/, '');		
	if (mcode != ""){$.cookie('mcode', mcode);}
	$.history.load(url);
}
