function ready() {
	$("#bg,#popups").remove();
	$("#loading").hide();
	
	
	if ($("input.error_field").length) {
		$("input:not(:disabled).error_field").eq(0).focus();
	} else if ($("input:not(:disabled)").length) {
		$("input:not(:disabled)").eq(0).focus();
	}
	else {
		$("textarea").eq(0).focus();
	}


	$("a").each(function() {
		if (this.onclick) {
			this._onclick = this.onclick;
			this.onclick = null;
		}
	});

	
	$(".file-upload input").change(function(e) { fileupload.call(this); });	
	window.scrollTo(0,0);
}




/// FILE UPLOAD
function fileupload() {
	var filename = this.value.split("\\").pop();
	
	if ($(this).hasClass("image")) {
		var ext = filename.split(".").pop().toLowerCase();
		switch(ext) {
			case "gif": case "jpg": case "jpeg": case "png":
			break;
			
			default:
				alert("gif, jpg, png 파일만 업로드 가능합니다.");
				return;
			break;
		}		
	}
	
	var $a = $('<span class="filename">'+filename+'<span class="del">x</span></span>');
	$(this).closest("td").find(".filename").remove();
	$(this).closest("label").after($a);	
}

$("span.del").live("click", function(e) {
	var $label = $(this).closest("td").find(".file-upload").html('<span>파일선택</span><input class="file" type="file" name="file[]"/>')
	var $filename = $(this).closest("td").find(".filename").remove();
	$(this).remove();
	
	$(".file-upload input").change(function(e) { fileupload.call(this); });		
});




function ajax_page_load(url) {
	$.post(url, null, function(data) {
		document.body.innerHTML = data;
		$("body script").appendTo("head");
		setTimeout(ready, 100);		
	});
}




/// CLICK
$(document).click(function(e) {
	if ($(e.target).is("a")) {
		return;
	}
	
	if (!$(e.target).closest(".layer").length) {
		$(".layer").last().find("a.back").click();
	}
});

$(document).keydown(function(e) {
	if (e.keyCode == 27) {
		if (!$("#bg:visible").length) {
			$(".layer").last().find("a.back").click();
		}
	}
});

$(".board tr td").live("click", function(e) {
	var $a = $(this).closest("tr").find("a").eq(0);
	var url = $a.attr("href").replace("http://"+window.location.hostname, "");
	var href = "http://"+window.location.hostname+"/#"+url;
	window.location = href;
});













$(document).ready(function() {
	var page_id = 0;
	var current_hash = location.hash;
	var param;
	var num_layer = $("section.layer").length;
	
	
	
	$("a:not([type=submit])").live("click", function(e) {
		if ($(this).closest("section.disabled").length) {
			e.preventDefault();		
			return;
		}

		if ($(this).closest(".comment-each").length) {
			e.preventDefault();
			return;
		}


		if (this._onclick && !this._onclick()) {
			e.preventDefault();		
			return false;	
		}		
		
		
		if (this.target) {
			return;
		}

		e.preventDefault();		
		var url = this.href.replace("http://"+window.location.hostname, "");
		if (url == "/#") {
			return;		
		}

		if (url == "/?delete") {
			if (!confirm("정말로 삭제하시겠습니까?")) {
				return;
			}
		}

		if (url.substring(0, 2) == "/?") {
			url = "/" + page_id + url.substring(1);
		}
		else {	
			page_id = parseInt(url.split("/")[1]);
		}



		window.location = "http://"+window.location.hostname+"/#"+url;
		current_hash = location.hash;
		var url = location.href.replace("#/", "");

		ajax_page_load(url);
	});




	$("form a[type=submit]").live("click", function(e) {
		
		if (this._onclick) {
			if (!this._onclick()) {
				alert("adflkjdslksfdjkladfjlkajfsdlkadfjskljafdslfds");
				e.preventDefault();
				return false;	
			}			
		}		


		
		$("#loading").show();
		
		var $form = $(this).closest("form");
		$form.attr("action", location.href.replace("#/", ""));
		$form.attr("target", "ajax-iframe");
		$("<input>").attr("type", "hidden").attr("name", $(this).attr("name")).appendTo($form);
		
		
		var iframe;
		try { iframe = document.createElement('<iframe name="ajax-iframe">'); }
		catch (ex) { iframe = document.createElement('iframe'); }
		
		iframe.id = "ajax-iframe";
		iframe.name = "ajax-iframe";
		iframe.width = 0;
		iframe.height = 0;
		iframe.marginHeight = 0;
		iframe.marginWidth = 0;
		document.body.appendChild(iframe);

		$form.submit();
		e.preventDefault();		
		
		
		function test() {
			try {			
				if (!(iframe && iframe.contentWindow && iframe.contentWindow.document.body)) {
					setTimeout(test, 100);
					return;
				}
	
				var href = iframe.contentWindow.location.href;			
				if (href=="about:blank") {
					setTimeout(test, 100);
					return;			
				}
				
				var host = location.host;
				var url = href.replace(host, host+"/#");			
				window.location.replace(url);
				document.body.innerHTML = iframe.contentWindow.document.body.innerHTML;	
				setTimeout(ready, 50);

			} catch(e) {}
		}
		setTimeout(test, 100);
	});
	
	
	
	
	
	if(location.hash) {
		document.body.innerHTML = "";
		page_id = parseInt(location.hash.split("/")[1]);

		var url = location.href.replace("#/", "");		
		ajax_page_load(url);
	}
	
	
	setInterval(function() {
		if (location.hash != current_hash) {
			current_hash = location.hash;
			var prev_id = page_id;
			page_id = parseInt(location.hash.split("/")[1]);
			
			var url = location.href.replace("#/", "");
			ajax_page_load(url);			
		}	
	}, 13);
});









