﻿$(document).ready(function(){
	var manage;
	var theCookie = $.cookie('manage');
	//alert(theCookie);
	if (theCookie == 'login'){
		manage = true;
	}else{
		manage = false;
	}
	if (manage){
		$("#list li").mouseover(function(){
			//alert($("#list li .post-action > a").length);
			$(".post-action",this).css("display","inline");
			
		}).mouseout(function(){
			$(".post-action",this).css("display","none");
		});
		
	}
	
	$("button",document.forms[0]).click(function(){
		//alert('hello');
		var title = $("#note_title").val();
		var content = $("#content").val();
		var noteid = $("#noteid").val();
		var edit = $("#edit").val();
		if (title == '' || content =='')
		{
			alert('Error:\nPlease enter title and content!')
			$("#note_title").focus();
			return false;
		}
		var pass = window.prompt("Please enter post password","password");
		if(!pass) return false;
		$.ajax({ 
			url:'/inc/post.asp', 
			type:'post', //数据发送方式 
			dataType:'html', //接受数据格式
			data:'act=note&title='+encodeURIComponent(title)+'&content='+encodeURIComponent(content)+'&pass='+encodeURIComponent(pass)+'&noteid='+encodeURIComponent(noteid)+'&edit='+encodeURIComponent(edit),  //要传递的数据 
			error: function(){  //失败
				alert('Error loading document');
			},
			success: function(msg){ //成功
				msgArr = msg.split("|");
				var theMsg = msgArr[0];
				var theTime = msgArr[1];
				var theID = msgArr[2];
				alert( "Data Saved: " + theMsg );
				if (theID){
					if (edit == 1)
					{
						var titleObjr = "#title_"+theID;
						var contentObjr = "#content_"+theID;
						$(titleObjr).html(title);
						$(contentObjr).html(content);
					}else{
						$("#list").prepend('<li style="display:none;"><h3 class="post-title">'+title+'</h3><h5 class="post-date">'+theTime+'</h5><p class="post-action"><a onclick="editNote('+theID+')" href="#write" title="Edit this note">Edit</a>&nbsp;&nbsp;<a onclick="delNote('+theID+')" href="javascript:void(0)" class="del" title="Delete this note">Delete</a></p><p class="post-content">'+content+'</p><p class="post-footer"><a href="/notes/?/'+theID+'">Comments</a>(0)</p>');
						$("#list >li:first").fadeIn("slow");
					}
				}// 确认成功返回
			}
		});
	});
	//show login
	$(".profile > h2").dblclick(function(){
		var manageDisplay = $("#manage").css('display'); 
		if(manageDisplay == 'none') {
			$("#manage").show("slow");
		}else {
			$("#manage").hide("slow");
		}
	})
	//create
	$("#create").click(function(){
		$.ajax({ 
			url:'/inc/post.asp', 
			type:'post', //数据发送方式 
			dataType:'html', //接受数据格式
			data:'act=create',  //要传递的数据 
			error: function(){  //失败
				alert('Error loading document');
			},
			success: function(msg){ //成功
				alert(msg);
			}
		});
	})
	//message
	$("#msgSub").click(function(){
		var uname = $("#uname").val();
		var uemail = $("#uemail").val();
		if (uname == '' || uemail == ''){
			alert('Error:\nYour name and your email Is not null!');
			$("#uname").focus();
			return false;
		}
		var usite = $("#usite").val();
		var msgcontent = $("#msgcontent").val();
		$.ajax({ 
			url:'/inc/post.asp', 
			type:'post', //数据发送方式 
			dataType:'html', //接受数据格式
			data:'act=msg&uname='+encodeURIComponent(uname)+'&uemail='+encodeURIComponent(uemail)+'&usite='+encodeURIComponent(usite)+'&content='+encodeURIComponent(msgcontent),  //要传递的数据 
			error: function(){  //失败
				alert('Error loading document');
			},
			success: function(msg){ //成功
				alert(msg);
			}
		});
	})
	//comment
	$("#replySub").click(function(){
		var c_uname = $("#c_uname").val();
		var c_uemail = $("#c_uemail").val();
		var c_usite = $("#c_usite").val();
		var c_content = $("#c_content").val();
		var noteid = $("#noteid").val();
		if (c_uname == '' || c_uemail == '')
		{
			alert('Error:\nPlease enter your name and your email!');
			$("#c_uname").focus();
			return false;
		}
		$.ajax({ 
			url:'/inc/post.asp', 
			type:'post', //数据发送方式 
			dataType:'html', //接受数据格式
			data:'act=comment&noteid='+noteid+'&c_uname='+encodeURIComponent(c_uname)+'&c_uemail='+encodeURIComponent(c_uemail)+'&c_usite='+encodeURIComponent(c_usite)+'&c_content='+encodeURIComponent(c_content),  //要传递的数据 
			error: function(){  //失败
				alert('Error loading document');
			},
			success: function(msg){ //成功
				msgArr = msg.split("|");
				theMsg = msgArr[1];
				c_uemail = msgArr[0];
				c_date = msgArr[2];
				alert(theMsg);
				if ($("#clist >li:first").html() =='No Comments!')
				{
					$("#clist >li:first").hide();
				}
				$("#clist").append('<li style="display:none" class="alt"><img alt="'+c_uname+'\'s header" src="http://www.gravatar.com/avatar/'+c_uemail+'?s=32&amp;d=http%3A%2F%2Fwww.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&amp;r=G" class="avatar" /><h4><a href="'+c_usite+'">'+c_uname+'</a>&nbsp;Says:</h4><h5>'+c_date+'</h5><p>'+c_content+'</p></li>');
				$("#clist >li:last").fadeIn("slow");
			}
		});
	})
})
//notes action
function editNote(id){
	var titleObj = "#title_"+id;
	var contentObj = "#content_"+id;
	$("#note_title").val($(titleObj).html());
	$("#content").val($(contentObj).html());
	$("#noteid").val(id);
	$("#edit").val("1");
	//alert($("#edit").val());
}
function delNote(id){
	var noteid = id;
	if (confirm("You confirm to delete it？")){
		$.ajax({ 
			url:'/inc/post.asp', 
			type:'post', //数据发送方式 
			dataType:'html', //接受数据格式
			data:'act=delnote&noteid='+noteid,  //要传递的数据 
			error: function(){  //失败
				alert('Error loading document');
			},
			success: function(msg){ //成功
				var sunObj = "#title_"+noteid;
				$(sunObj).parent().fadeOut("slow",function(){alert(msg)});
			}
		});
	}
}