$( function() {
	//文件上传
	$("#file_upload").omFileUpload({
		action :"image.upload",
		fileExt :"*.jpg;*.png;*.gif;*.jpeg;*.bmp",
		fileDesc :"Image Files",
		fileDataName :"myFile",
		sizeLimit :200 * 1024,
		onError : function(ID, fileObj, errorObj, event) {
			if (errorObj.type == "File Size") {
				alert("上传图片的大小不能超过200KB");
			}
		},
		autoUpload :true,
		swf :window.allBasePath + "image/admin/swf/om-fileupload.swf",
		method :"GET",
		onComplete : function(ID, fileObj, response, data, event) {
			var jsonData = eval("(" + response + ")");
			if (jsonData.error == "filetype") {
				alert("文件上传格式只能支持：" + jsonData.allowtype);
				return false;
			}
			// alert(jsonData.fileUrl);
			$(".cover .i-img").attr("src", window.allBasePath + jsonData.fileUrl).show();
			$("#imgArea").show().find(" #img").attr("src", window.allBasePath + jsonData.fileUrl);
			$("#coverurl").val(jsonData.fileName);
			// alert(jsonData.fileName);
			$(".default-tip").hide();
		}
	});
	
	$(".msg-editer #title").bind("keyup", function() {
		$(".i-title").text($(this).val());
	});

	$(".msg-editer #summary").bind("keyup", function() {
		$(".msg-text").html($.htmlEncode($(this).val()));
	});

	$("#desc-block-link").click( function() {
		$("#desc-block").show();
		$(this).hide();
	});

	$("#url-block-link").click( function() {
		$("#url-block").show();
		$(this).hide();
	});

	$("#chain-block-link").click( function() {
		$("#chain-block").show();
		$(this).hide();
	});

	$("#delImg").click( function() {
		$(".default-tip").show();
		$("#imgArea").hide();
		$("#coverurl").val("");
		$(".cover .i-img").hide();
	});

	$("#cancel-btn").click( function(event) {
		event.stopPropagation();
		location.href = "imagetextinfo_doList.action";
		return;
	});

	var validator = $("#appmsg-form").validate({
		rules : {
			title : {
				required :true,
				maxlength :64
			},
			summary : {
				maxlength :120
			},
			source_url : {
				url :true
			},
			chain : {
				url :true
			}
		},

		messages : {
			title : {
				required :"请输入标题",
				maxlength :"标题不能超过64个字"
			},
			summary : {
				maxlength :"标题不能超过120个字"
			},
			source_url : {
				url :"必须输入正确的url格式"
			},
			chain : {
				url :"必须输入正确的url格式"
			}
		},

		showErrors : function(errorMap, errorList) {
			if (errorList && errorList.length > 0) {
				$.each(errorList, function(index, obj) {
					var item = $(obj.element);
					item.closest(".control-group").addClass("error");
					item.attr("title", obj.message);
				});
			} else {
				var item = $(this.currentElements);
				item.closest(".control-group").removeClass("error");
				item.removeAttr("title");
			}
		},

		submitHandler : function() {
			if ($("#coverurl").val() == "") {
				alert("必须上传一张图片");
				return false;
			}

			var editorContent = msg_editor.getContent();
			var chain = $("#chain").val();
			var len = editorContent.length;
			if (chain == "" && len == 0) {
				alert("正文内容或者外链至少要填写一个。");
				msg_editor.focus();
				return false;
			}

			if (len > charLimit) {
				alert("正文的内容不能超过" + charLimit + "个字");
				msg_editor.focus();
				return false;
			}

			var $form = $("#appmsg-form");
			var $btn = $("#save-btn");

			if ($btn.hasClass("disabled")) {
				return;
			}

			var submitData = {
				title :$("input[name='title']", $form).val(),
				summary :$("textarea[name='summary']", $form).val(),
				coverurl :$("input[name='coverurl']", $form).val(),
				source_url :$("input[name='source_url']", $form).val(),
				chain :$("#chain", $form).val(),
				link :$("input[name='link']", $form).val(),
				rid :$("input[name='rid']", $form).val(),
				action :$("input[name='action']", $form).val(),
				maincontent :editorContent
			};

			$btn.addClass("disabled");

			$.post(window.allBasePath + "admin/material/addOrUpdate", submitData, function(data) {
				$btn.removeClass("disabled");
				if (data !=null && data == true) {
					alert("保存成功");
					location.href = window.allBasePath + "admin/material/list";
				} else {
					alert("保存失败");
					/*if ("1" == data.errorCode) {
						alert("图文条数已经达到上限!");
					} else {
						alert("保存失败");
					}*/
				}
			}, "json");
			return false;
		}
	});

	window.msg_editor = new UE.ui.Editor({
		initialFrameWidth :498
	});
	window.msg_editor.render("editor");

	function computeChar() {
		var len = msg_editor.getContent().length;
		if (len > charLimit) {
			$(".editor-bottom-bar").html(
					"<span style='color:red;'>你输入的字符个数（" + len
							+ "）已经超出最大允许值！</span>");
		} else {
			$(".editor-bottom-bar").html(
					"当前已输入<span class='char_count'>" + len
							+ "</span>个字符, 您还可以输入<span class='char_remain'>"
							+ (charLimit - len) + "</span>个字符。");
		}
	}

	window.msg_editor.addListener("keyup", function(type, evt) {
		computeChar();
	});

});