$(document).ready(function() {
	$("#writeForm").hide();
	$("#result").hide();
	$("#writeForm").ajaxSubmit();

	$("a.newclip").click(function() {
		$("#writeForm").slideToggle("fast", function(){
			$("#result").hide();
		});
	}).attr("href", "javascript:;");

	visual();
});

$.fn.ajaxSubmit = function(e) {
	this.submit(function(){
		var oEditor = FCKeditorAPI.GetInstance('inputcontent');
		var inputData = oEditor.GetXHTML(true);
		$("#inputcontent").attr("value", inputData);

		var params = {};
		$(this).find("input[@type='submit']").attr("disabled", "disabled");
		$(this).find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
		.filter(":enabled")
		.each(function() {
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});

		$.ajax({
			type: "POST",
			dataType: "json",
			url: this.getAttribute("action") + "?call=ajax",
			data: params,
			success: function(callback) {
				var result = callback.result;
				var returnUri = callback.returnUri;
				
				if(result) {
					if(result.match("\ERROR")) {
						$("#result").text(result).fadeIn("slow").animate({opacity: 1.0}, 1000).fadeOut("slow");
					} else {
						//$("#result").html(result).fadeIn("slow");
					}

					//$("input#lock").attr("checked", "checked");
					$(this).find("input[@type='submit']").removeAttr("disabled");
					//reload();
				}
				if(returnUri) window.location.href = returnUri;
			}
		});

		return false;
	});
	
	return this;
}

function visual() {
	$("input.reset").click(function() {
		$("#writeForm").slideUp("fast");
	});

	//$("ul.list>li").fadeTo("fast", 0.6);
	$("ul.list>li").hover(function() {
		//$(this).addClass("hover").fadeTo("fast", 1);
		$(this).addClass("hover");
	}, function() {
		//$(this).removeClass("hover").fadeTo("slow", 0.6);
		$(this).removeClass("hover");
	}).click(function(){
		window.location = "/clip/" + $(this).attr("class").match("[0-9]+");
	});

	$("a.delete").click(function() {
		var isDelete = false;
		parentObj = $(this).parent().parent();

		if(confirm("정말 삭제하시겠습니까?")) {
			$.ajax({
				url: $(this).attr("href"),
				success: function(result) {
					if(result) parentObj.fadeOut("slow");
				}
			});
		}

		return false;
	});

	$("a.modify").click(function() {
		frm = $("#writeForm");
		$("#result").hide();

		$.ajax({
			url: $(this).attr("href"),
			dataType: "json",
			success: function(result) {
				if(result) {
					var oEditor = FCKeditorAPI.GetInstance('inputcontent');
						oEditor.SetData(result.contents);

					//frm.find("textarea").attr("value", result.contents);
					//frm.find("textarea").html(result.contents);
					frm.find("input#lock").attr("checked", result.isLock);
					frm.append("<input type=\"hidden\" name=\"mID\" value=\"" + result.mID + "\" />");
					frm.slideDown("fast");
				}
			}
		});

		return false;
	});
}

function reload() {
	$.ajax({
		url: "/reload",
		dataType: "html",
		success: function(result) {
			$("#content").html(result);
			visual();
		}
	});
}