if(!yarikuri) {
	$(function() {
		$(window).bind("hashchange", function(e) {
			var args = location.hash.split("/");
			var hashtype = args[0].replace("#", "");

			if(hashtype.length) {
				try {
					eval("onHashChange_" + hashtype)(args);
				} catch(e) {
					switch(hashtype) {
						case "board":
							yarikuri_board.load_contents(args); break;
					}
				}
			}

			yarikuri.set_title();
		});

		$(window).trigger("hashchange");

		yarikuri.set_title();
	});

	$(document).mousedown(function(e) {
		window.eventPointer = e;
	});

	var yarikuri = new function() {
		this.subContentId = "sub_contents";
		this.time = null;
		this.buffer = null;
		this.onloadView = null;
		this.set_title = function() {
			if(!$.browser.msie) return;

			setTimeout(function() {
				document.title = yarikuri.title;
			}, 100);
		}

		this.include_view = function(ctrl, params, id, url) {
			var retVal;

			if(!url) {
				url = location.href;
			}

			if(params == null || typeof params != "object") {
				params = {};
			}

			params["ctrl"] = ctrl;
			params["_key"] = "include_view/"+ ctrl +"/"+ (new Date()).getMilliseconds();

			var logObject = {params:params, id:id, url:url};

			$.ajax({type: "POST", url:url, data:params, success:function (res) {
				retVal = $.trim(res);
				logObject.content = res;
				logObject.success = true;
			}, error:function(xhr, textStatus, errorThrown) {
				logObject.msg = textStatus;
				logObject.error = true;
			}, async:false});

			if(id != "" && id != undefined) {
				$("#"+ id).html(retVal);

				if(params["_onload"] != false && typeof this.onloadView == "function") {
					setTimeout(this.onloadView, 5);
				}
			} else {
				return retVal;
			}
		}

		this.req_action = function(ctrl, params, postAction, url) {
			var ret;

			if(!url) {
				url = location.href;
			}

			if(params == null || typeof params != "object") {
				params = {};
			}

			params["ctrl"] = ctrl;
			params["_key"] = "req_action/"+ ctrl +"/"+(new Date()).getMilliseconds();

			var logObject = {params:params, url:url};

			$.ajax({type: "POST", url:url, data:params, success:function(res) {
				ret = yarikuri.res_handle(res, postAction);
				logObject.content = res;
				logObject.success = true;
			}, error: function(xhr, textStatus, errorThrown) {
				logObject.msg = textStatus;
				logObject.error = true;
			}, async: false});

			if(typeof postAction != "function" && ret != "") {
				return ret;
			}
		}

		this.res_handle = function(res, postAction) {
		 	if(res.length == 4) return;

			val = res.substr(res.indexOf("") + 1);

			try {
				eval("var obj = " + val);
			} catch(e) {
				return pub_alert("잘못된 응답형식 입니다.");
			}

			if(obj.msg != undefined && obj.msg != "") {
				if(obj.dialog != undefined) {
					pub_alert(obj.msg, obj.dialog);
				} else {
					pub_alert(obj.msg);
				}
			}

			if(obj.javascript != undefined) {
				eval(obj.javascript);
			}

			if(obj.isClose == "1") {
				window.close();
			}

			if(obj.location != undefined) {
				location.href = obj.location;
			}

			if(obj.isReload == "1") {
				location.reload();
			}

			if(obj.profile != undefined) {
				eval(obj.profile);
			}

			if(obj.ret != undefined) {
				if(typeof postAction == "function") {
					postAction(obj.ret);
				} else {
					return obj.ret;
				}
			}
		}
	}
}

if(!yarikuri_board) {
	var yarikuri_board = new function() {
		var _last_list;

		this.load_contents = function(arr) {
			var args = {
				type : arr[0],
				act : (arr[1] || ''),
				bserial : (arr[2] || 0),
				isgroup : (arr[3] || 'N'),
				page	 : (arr[4] || ''),
				ano : (arr[4] || 0),
				no : (arr[5] || 0),
				findtype : (arr[5] || 0),
				keyword : (arr[6] || ''),
				rbserial : (arr[6] || 0)
			};

			switch (args.act) {
				case "list" :
					yarikuri_board.list_ex(args);
					_last_list = location.hash;
					break;
				case "read" :
					yarikuri_board.read_ex(args);
					break;
				case 'post':
				case 'reply':
				case 'modify':
					yarikuri_board.post_ex(args);
					break;
			}
		}

		this.list = function(bserial, page, isgroup, findtype, keyword) {
			var args = {};
			args.bserial = bserial;
			args.isgroup = isgroup;
			args.page = (page == "") ? 1 : page;

			if(findtype != undefined) {
				args.findtype = findtype;
			}

			if(keyword != undefined) {
				args.keyword = keyword;
			}

			yarikuri.include_view("board:board_list", args, yarikuri.subContentId);
		}

		this.list_ex = function(obj) {
			obj = (obj || {});
			this.list(obj.bserial, obj.page, obj.isgroup, obj.findtype, obj.keyword);
		}

		this.btn_search = function(bserial, isgroup) {
			if($("#keyword").val().length < 2) {
				pub_alert("검색어를	2자이상 입력해 주세요.");
			} else {
				location.hash = "#board/list/" + bserial + "/" + isgroup + "/1/" + $("#findtype").val() + "/" + $("#keyword").val();
			}
		}

		this.go_list = function(list_hash) {
			location.hash = (_last_list) ? _last_list : list_hash;
		}

		this.read = function(bserial, ano, isgroup, no, rbserial) {
			var args = {};
			args.bserial = bserial;
			args.ano = ano;
			args.isgroup = isgroup;
			args.no = no;
			args.rbserial = rbserial;

			yarikuri.include_view("board:board_read", args, yarikuri.subContentId);

			try {
				$("body, html, document").animate({
					scrollTop:$("#sub_contents").offsetParent().position().top + 70
				}, (($.browser.safari) ? 1 : 0));
			} catch(e) {
			}
		}

		this.read_ex = function(obj) {
			obj = (obj || {});

			this.read(obj.bserial, obj.ano, obj.isgroup, obj.no, obj.rbserial);
		}

		this.side_article = function (side, bserial, ano, isgroup, no, group_srl) {
			if(side == "prev" || side == "next") {
				var args = {};
				args.bserial = bserial;
				args.ano = ano;
				args.side = side;
				args.group_srl = group_srl;

				if(isgroup == "Y") {
					args.isgroup = "Y";
				}

				yarikuri.req_action("board:get_side_ano", args, function(ret) {
					if(ret.ret == "OK") {
						no = (no || 0);

						if(side == "next") {
							no += 1;
						} else if(side == "prev" && no > 0) {
							no -=1;
						}

						location.hash = "#board/read/"+ ret.bserial +"/"+ isgroup +"/"+ ret.ano +"/"+ no + (group_srl != "" ? "/"+ group_srl : "");
					} else if(ret.ret == "NO_SRL") {
						pub_alert("더이상 글이 없습니다.");
					}
				});
			}
		}

		this.post = function(bserial, act, isgroup, ano, rbserial) {
			var args = {bserial:bserial, act:act, isgroup:isgroup, ano:ano, rbserial:rbserial};
			yarikuri.include_view('board:board_post', args, yarikuri.subContentId)
		}

		this.post_ex = function(obj) {
			obj = (obj || {});
			this.post(obj.bserial, obj.act, obj.isgroup, obj.ano, obj.rbserial);
		}

		this.write_article = function(postAction) {
			var frm = document.fo_board_post;

			if(bInProgress) return pub_alert("글을 등록중 입니다.");

			if(frm.subject.value == "") return pub_alert("제목을 입력하지 않았습니다.");

			var editor = gamepub.editor;
			var str = editor.getHTML();

			bInProgress = true;

			frm.action = action_url;
			frm.ctrl.value = "board:write_article";

			var result = up.uploadformhtmlfile(frm, str, upfile);
			bInProgress = false;

			if(result == "") return pub_alert("알수없는 오류가 발생했습니다.<br>문제가 지속되면 고객센터로 문의해 주세요.");

			yarikuri.res_handle(result, function(ret) {
				postAction(ret);
			});
		}

		this.delete_article = function(bserial, ano, postAction) {
			if(!pub_confirm("해당 글을 삭제하시겠습니까?")) return;

			var frm = document.fo_board_del;
			frm.bserial.value = bserial;
			frm.ctrl.value = "board:delete_article";

			if(ano == undefined) {
				if ($(":checkbox:checked").length == 0) {
					pub_alert("선택된 글이 없습니다."); return;
				}
				frm.ano.value = $(":checkbox:checked").serialize();
			} else {
				frm.ano.value = ano;
			}

			if(typeof postAction != "function") {
				postAction = function(ret) {
					location.hash = "#board/list/"+ bserial +"/N/1";
				};
			}

			var dl = document.location;
			var sub_dm = dl.hostname.split(".")[0];

			var last_char = frm.action.charAt(frm.action.length-1);
			var pmang = (last_char != "/") ? "/pmang/" : "pmang/";

			frm.action = frm.action + pmang + sub_dm + dl.pathname;
			var result = document.getElementById("up").uploadformhtml(frm, "");
			yarikuri.res_handle(result, postAction);
		}

		this.comment = function(bserial, ano, page) {
			yarikuri.include_view("board:board_comment", {bserial:bserial, ano:ano, cmt_page:page}, "comment_view");
		}

		this.write_comment = function(bserial, ano , tail, postAction) {
			var args = {};
			args.bserial = bserial;
			args.ano = ano;
			args.tail = tail;

			this.write_comment_ex(args, postAction);
		}

		this.write_comment_ex = function(args, postAction) {
			if(args.tail == undefined || args.tail == null) {
				args.tail = $("#tail, [name=tail]").val();
			}

			if(args.extra1 == undefined && $("#extra1, [name=extra1]").length > 0) {
				args.extra1 = $("#extra1, [name=extra1]").val();
			}

			if(args.extra2 == undefined && $("#extra2, [name=extra2]").length > 0) {
				args.extra2 = $("#extra2, [name=extra2]").val();
			}

			if(args.extra3 == undefined && $("#extra3, [name=extra3]").length > 0) {
				args.extra3 = $("#extra3, [name=extra3]").val();
			}

			if(args.extra4 == undefined && $("#extra4, [name=extra4]").length > 0) {
				args.extra4 = $("#extra4, [name=extra4]").val();
			}

			if(args.extra5 == undefined && $("#extra5, [name=extra5]").length > 0) {
				args.extra5 = $("#extra5, [name=extra5]").val();
			}

			if(typeof postAction != "function") {
				postAction = function(ret) {
					if(ret == "OK") yarikuri_board.comment(args.bserial, args.ano);
				};
			}

			yarikuri.req_action("board:write_comment", args, function(ret) {
				postAction(ret);
			});
		}

		this.delete_comment = function(bserial, ano, tsrl, postAction) {
			if(!pub_confirm("꼬리글을 삭제하시겠습니까?")) return;

			var args = {};
			args.bserial = bserial;
			args.ano = ano;

			if(tsrl == undefined) {
				if($(":checkbox:checked").length == 0) return pub_alert("선택된 꼬리글이 없습니다.");
				args.tsrl = $(":checkbox:checked").serialize();
			} else {
				args.tsrl = tsrl;
			}

			if(typeof postAction != "function") {
				postAction = function(ret) {
					if(ret == "OK") yarikuri_board.comment(bserial, ano);
				};
			}

			yarikuri.req_action("board:delete_comment", args, postAction);
		}

		this.copyBoardUrl = function(board_url) {
			board_url = (board_url || "");

			if($.browser.msie) {
				clipboardData.setData("Text", board_url);
				pub_alert("이 게시물의 주소가 복사되었습니다.\n게시판이나 메신저 창에서 Ctrl+V를 누르시면 해당 게시물의 주소를 쉽게 옮길 수 있습니다.");
			} else {
				prompt("이 게시물의 주소입니다.\nCtrl+C를 누르시면 복사할 수 있습니다.", board_url);
			}
		}
	}
}
