/*
 * CjCommand 객체
 * 
 * 브라우저의 기본 history 기능을 사용할 수 없는 구조로 인해
 * 본 객체가 History 기능을 담당한다.
 * 
 * IE는 iframe을 사용하여 History를 구현하고
 * 나머지 브라우저는 location.hash를 사용하여 구현한다.
 * 
 * Lee Won-Gyoon <richscript@gmail.com>, <@richscript>, <www.richscript.net>
 * e-motion Corp.
*******************************************************************************/

var CjCommand = {
	instanceName : "CjCommand",
	menuInstanceName : "CjMenu",
	command : [],
	path : {
		command : "/contents.command.html"
	},
	current : {
		index : -1
		, hash : ""
	},
	status : {
		historyCall : false
	},

	/**
	* CjMenu 객체를 가져온다.
	* @return ShMenu 객체
	*/
	getMenuInstance : function() {
		return window[this.menuInstanceName];
	},
	
	getHash : function() {
		return (""+location.hash).trim().substring(1);
	},
	
	set : function() {
		if ($js.browser.isIE) {
			if (!this.status.historyCall) {
				this.current.index++;
				if (this.command[this.current.index]!=undefined) {
					for (var i=0; i<this.command[this.current.index].length; i++) {
						this.command[this.current.index][i] = null;
					}
					this.command[this.current.index][i] = null;
				}
				this.command[this.current.index] = arguments;
				this.saveHistory(this.current.index);
			}
			this.status.historyCall = false;
		}
	},
	
	execute : function(_index) {
		var args = this.command[_index];
		window[args[0]][args[1]].apply(window[args[0]], Array.prototype.slice.apply(args,[2]));
		args = null;
	},
	
	history : function(_index) {
		if (this.current.index!=_index) {
			this.status.historyCall = true;
			this.current.index = _index;
			this.execute(_index);
		}
	},
	
	saveHistory : function(_index) {
		if (document.getElementById(this.ids.commandFrame)) {
			this.loadCommandFrame(_index);
		} else {
			this.printCommandFrame(_index);
		}
	},
	
	loadCommandFrame : function(_index) {
		var url = this.path.command.appendParameter("_temp="+(new Date()).getTime()).appendParameter("c="+_index);
		try {
			if (_index>0) {
				window.frames[this.ids.commandFrame].location.href = url;
			} else {
				window.frames[this.ids.commandFrame].location.replace(url);
			}
		} catch(e) {
			open(url, this.ids.commandFrame, "");
		}
	},
	
	getCommandFrameHTML : function() {
		var html = '';
		html += '<iframe';
		html += ' style="position:absolute;z-index:-1;left:-100px;top:-100px;width:1px;height:1px;overflow:hidden;"';
		html += ' name="'+this.ids.commandFrame+'"';
		html += ' id="'+this.ids.commandFrame+'"';
		html += ' src="about:blank"';
		html += ' width="1"';
		html += ' height="1"';
		html += ' marginwidth="0"';
		html += ' marginheight="0"';
		html += ' hspace="0"';
		html += ' vspace="0"';
		html += ' scrolling="no"';
		html += ' frameborder="0"';
		html += '></iframe>';
		return html;
	},
	
	printCommandFrame : function(_index) {
		$(this.ids.root).html(this.getCommandFrameHTML());
		this.loadCommandFrame(_index);
	},
	
	printCommandFrameArea : function() {
		$("body").append('<span id="'+this.ids.root.substring(1)+'"></span>');
	},
	
	observeHash : function() {
		var hash = this.getHash();
		if (hash!=this.current.hash) {
			if (hash!="") {
				var prevMenuCode = this.current.hash.split("/")[0];
				var menuCode = hash.split("/")[0];
				var option = hash.replace(menuCode+"/","");
				if (option.indexOf("?")==0) {
					option = option.substring(1);
				}
				if (this.getMenuInstance().getMenuFromMenuCode(menuCode)) {
					//if (prevMenuCode!=menuCode) {
						this.getMenuInstance().openMenuFromMenuCode(menuCode, option, true);
					//}
				} else {
					this.getMenuInstance().openHome(true);
				}
			} else {
				this.getMenuInstance().openHome(true);
			}
			this.current.hash = hash;
		}
		var that = this.instanceName;
		setTimeout(function() {
			window[that].observeHash();
		}, 500);
	},
	
	initialize : function() {
		this.ids = {
			root : "#" + this.instanceName + "root"
			, commandFrame : this.instanceName + "commandFrame"
		};
		var that = this.instanceName;
		this.getMenuInstance().event.on("openMenu", function(_menu, _option, _historyCall) {
			if (_historyCall!==true) {
				
				var hash = _menu.menuCode+"/";
				if (typeof(_option)=="string"&&_option.trim()!="") {
					hash = hash.appendParameter(_option);
				}
				window[that].current.hash = hash;
				location.hash = hash;
			}
		});
		if ($js.browser.isIE) {
			this.getMenuInstance().event.on("openMenu", function(_menu, _option, _historyCall) {
				window[that].set(window[that].menuInstanceName, "openMenu", _menu, _option);
			});
			this.printCommandFrameArea();
		} else {
			this.observeHash();
		}
	}
};

$(function() {
	CjCommand.initialize();
});
