//var appBase="http://localhost/comment2/"; //程序所在地址，最后以/结尾
//var appBase="http://www.ayeah.com/comment/"; //程序所在地址，最后以/结尾

//document.write("<div id=\"_ayeahCmt_tag_20080323\" style=\"font-size:9pt\"></div>");
/*
window.addStyleSheet = function(src) {
	var head = document.getElementsByTagName('head')[0];
	var style = document.createElement('link');
	style.type = "text/css";
	style.rel="stylesheet";
	//style.media = media
	style.href = src;
	head.insertBefore(style, head.lastChild.nextSibling);
}

addStyleSheet(appBase+"style/default.css");
*/
var thread = $("#_comment_tag_").attr("src").replace(/.*[?&]url=([^&]+)(&|$).*|.*/,"$1");
thread= thread!="" ? thread:escape(location.href);
$(document).ready(function(){
	if ($("#_ayeahCmt_tag_20080323").size()==1)
	{
		loadForm();
	}
});

function loadForm(){
	$("#_ayeahCmt_tag_20080323").load(appBase+"form.html",function(){
		$("#_cmt_Form #submit_tips .close").click(function(){
				$(this).parent().fadeOut(1000);
		});
		$("#_cmt_Form #cmt_content").keydown(function(eventObject){
			var eve=eventObject||window.event;
			if(eve.ctrlKey && eve.keyCode==13){
				$("#_cmt_Form").trigger("submit");
			}
		});
		$("#_cmt_Form #button1").click(function(){
			$("#_cmt_Form :input[name='reply']").val("");
			$(this).fadeOut('fast');
		}).hide();
		$("#_cmt_Form").submit(function(){
			if ($("#_cmt_Form #cmt_content").val()=="")
			{
				$("#_cmt_Form #submit_tips .content").html("请填写评论内容");
				$("#_cmt_Form #submit_tips").show();
				return false;
			}
			if ($("#_cmt_Form #cmt_content").val().length<5)
			{
				$("#_cmt_Form #submit_tips .content").html("评论内容太短，最少5个字");
				$("#_cmt_Form #submit_tips").show();
				return false;
			}
			$("#_cmt_Form #submit_tips .content").html("正在提交数据...");
			$("#_cmt_Form #submit_tips").show();

			var send_data="url="+$("#_cmt_Form :input[name='url']").val();
			send_data+="&title="+escape($("#_cmt_Form :input[name='title']").val());
			send_data+="&nickName="+($("#_cmt_Form #nickName").val()==""?escape("匿名游客"):escape($("#_cmt_Form #nickName").val()));
			send_data+="&homepage="+escape($("#_cmt_Form #homepage").val());
			send_data+="&email="+escape($("#_cmt_Form #email").val());
			send_data+="&notify="+$("#_cmt_Form #notify:checked").size();
			send_data+="&mood="+$("#_cmt_Form :radio[name='face']:checked").val();
			send_data+="&content="+escape($("#_cmt_Form #cmt_content").val());
			send_data+="&reply="+escape($("#_cmt_Form  :input[name='reply']").val());

			$.ajax({
				url:appBase+"post.asp",
				type:"post",
				data:send_data,
				dataType:"json",
				success:function(data){
					if (data.result=='post_success')
					{
						$("#_cmt_Form #submit_tips .content").html("发表评论成功！");
						$("#_cmt_Form #submit_tips").show();
						$("#_cmt_Form #cmt_content").val('');
						$("#_cmt_Form :input[name='reply']").val("");
						$("#_cmt_Form #button1").fadeOut('fast');
					}
					loadData();
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					$("#_cmt_Form #submit_tips .content").html("发表评论出错");
					$("#_cmt_Form #submit_tips").show();
				}
			});
			return false;
		});
		loadData();

	});
}

function loadData(){
	$("#_cmt_Form :input[name='url']").val(thread);
	$("#_cmt_Form :input[name='title']").val(document.title);
	$("#ayeahCmt_Main_Table").html("loading...");
	$.ajax({
		url:appBase+"comment.asp",
		data:"url="+thread+"&title="+encodeURI(document.title),
		cache:false,
		dataType:"json",
		success:function(data){
				$("#ayeahCmt_Main_Table").empty();
				//$("#ayeahCmt_Span_ShowCount").html(data.floors);
				$.each(data.comments, function(i,item){
					$(document.createElement("div"))
					  .attr("id","ayeahCmt"+item.id)
					  .addClass("comment_item")
					  .append("<div class=\"floor\">&nbsp;<span>"+item.floor+"</span>楼&nbsp;</div>")
					  .append("<div class='nickname'>"+unescape(item.postuser)+"</div>")
					  .append("<div class='mood'></div>")
					  .append("<div class='showtime'>发表于&nbsp;"+item.posttime+"</div>")
					  .append("<div class='cb'></div>")
					  .append("<div class='content'>"+unescape(item.content)+"</div>")
					  .append("<div class='morelinks'> <a href=\"javascript:voteSupport('"+item.id+"')\"><img src='"+appBase+"mood/007.gif' align=\"absmiddle\" border=0/>支持(<span class=vote1>"+item.support+"</span>)</a> <a href=\"javascript:voteAgainst('"+item.id+"')\"><img src='"+appBase+"mood/008.gif' align=\"absmiddle\" border=0 />反对(<span class=vote2>"+item.against+"</span>)</a> <a href=javascript:voteIllegal("+item.id+")>举报</a><span class=\"status\"></span></div>")
					  .append("<div class='hr'></div>")
					  .appendTo("#ayeahCmt_Main_Table");
					if (item.replyto!="")
					{
						if ($("#ayeahCmt"+item.replyto).size()==1)
						{
							$("#ayeahCmt"+item.id+" .content").prepend("<div class=\"quotetop\">引用"+$("#ayeahCmt"+item.replyto+" .nickname").text()+"["+$("#ayeahCmt"+item.replyto+" .floor span").html()+"楼]"+$("#ayeahCmt"+item.replyto+" .showtime").html()+"</div><div class=\"quotemain\">"+$("#ayeahCmt"+item.replyto+" .content").html()+"</div>");
						}
					}
					if (item.email!="")
					{
						$("#ayeahCmt"+item.id+" .nickname").addClass(item.email);
					}
					if (item.mood!="")
					{
						$("#ayeahCmt"+item.id+" .mood")
							.html("<img src='"+appBase+"mood/"+item.mood+"' align=\"absmiddle\" />");
					}
					if (item.homepage!="")
					{
						$("#ayeahCmt"+item.id+" .nickname").wrap("<a href=\""+unescape(item.homepage)+"\" target=_blank></a>");
					}
					$(document.createElement("a"))
						.attr("href","javascript:reply('"+item.id+"')")
						.html("回复")
						.addClass("replyLink")
						.prependTo("#ayeahCmt"+item.id+" .morelinks")
						.click(function(){
							$("#ayeahCmt_Textarea_Content")
								.val("回复"+item.floor+"楼：("+item.postuser+")");
							$("#ayeahCmt_Main_Table_Form_Tr div").slideDown();
						});
				});
			},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			//alert(textStatus);
			}
	});
}

