﻿
var formHTML = '<span id="tip">还可以输入<b>140</b>个字</span><form action="index.php" method="post">';
formHTML += '<textarea name="status" id="textbox"></textarea>';
formHTML += '<input type="hidden" id="in_reply_to" name="in_reply_to" value="0" />';
formHTML += '<input type="submit" id="submit_btn" title="按ctrl+enter键发送" value="发送" /></form>';	
	
function formFunc() {
	document.getElementById("submit_btn").disabled=false;	
	leaveWord();
	$("#textbox").focus();
	$("#textbox").keydown(function(){leaveWord()}).keyup(function(){leaveWord()}).keydown(function(event){
		if (event.ctrlKey && event.keyCode==13) {
			updateStatus();
		}
		});
	
	$(".submit_btn").click(function(){
		document.getElementById("submit_btn").disabled=true;	
	});
}

function updateStatus(){
	$("#sentTip").remove();
	var $sentTip = $("<div id='sentTip'></div>");
	$sentTip.prependTo("#statuses");
	$sentTip.html("发送消息中...");
	
	var text = $("#textbox").val();
	
	$.ajax({
		url: "ajax/update.php",
		type: "POST",
		data: "status=" + text + "&in_reply_to=" + $("#in_reply_to").val(),
		success: function(msg) {
			
			if (/\d+/.test($.trim(msg))) {
				$sentTip.html("发送消息成功");
				$("#textbox").val("");
				if (document.location.href.indexOf("index") > 0 || document.location.href.indexOf("all") > 0) {
					var userid = $("#sideid").text(), statusid = $.trim(msg);
					var html = "<li><span class=\"status_author\">";
					html += "<a href=\"info?id=" + userid + "\" target=\"_blank\"><img src=\"" + $("#sideimg").attr("src") + "\" /></a>";
					html += "</span>";
					html += "<span class=\"status_body\">";
					html += "<span class=\"status_id\">" + statusid + "</span>"
					html += "<span class=\"status_word\"><a class=\"user_name\" href=\"info?id=" + userid + "\">" + userid + "</a> " + text + "</span>";
					html += "<span class=\"status_info\"><a class=\"rt_btn\" href=\"a_rt.php?id=" + statusid + "\">回推</a><a class=\"favor_btn\" href=\"a_favor.php?id=" + statusid + "\">收藏</a><a class=\"delete_btn\" href=\"a_del.php?id=" + statusid + "&t=s\">删除</a>";
					html += "<span class=\"source\">通过 web</span>";
					html += "<span class=\"date\"><a href=\"https://twitter.com/" + userid + "/status/" + statusid + "\">刚刚</a></span>";
					html += "</span></span></li>";	
					$(".timeline").eq(0).prepend(html);
					$("#user_stats li:last").find(".count").html(parseInt($("#user_stats li:last").find(".count").text())+1);

					$(".replie_btn").hide();
					$(".rt_btn").hide();
					$(".favor_btn").hide();
					$(".delete_btn").hide();
				}
			} else if (msg.indexOf("empty") >= 0){
				$sentTip.html("发送消息出错,内容不能为空");
			} else {
				$sentTip.html("发送消息出错,请重试");
			}
		},
		error: function(msg) {
			$sentTip.html("发送消息出错,请重试");
		}
	});
}

function timelineFocus(){	
	var re=new RegExp("(http://twitpic.com/){1}([a-zA-Z0-9]*)");
	$(".timeline li").each(function(i,o) {
		$text = $(this).find(".status_word")
		if ($text.text().match(re)) {
			var id = $text.text().match(re)[2];
			$(this).find(".status_info").before('<div class="twitpic"><a href="http://twitpic.com/' + id + '" target="_blank"><img src="http://twitpic.com/show/mini/' + id + '"/></a></div>');
		}
	});
	
	if (/i686/.test(navigator.userAgent) && /U;/.test(navigator.userAgent)) {
	} else {
		$(".replie_btn").hide();
		$(".rt_btn").hide();
		$(".favor_btn").hide();
		$(".delete_btn").hide();
	}
	
	$(".timeline").find("li").live("mouseover", function(){
		$(this).find(".replie_btn").css("display", "inline-block");
		$(this).find(".rt_btn").css("display", "inline-block");
		$(this).find(".favor_btn").css("display", "inline-block");
		$(this).find(".delete_btn").css("display", "inline-block");
	});
	
	$(".timeline").find("li").live("mouseout", function(){
		$(this).find(".replie_btn").hide();
		$(this).find(".rt_btn").hide();
		$(this).find(".favor_btn").hide();
		$(this).find(".delete_btn").hide();
	});
}
function onFavor($this) {
	var status_id = $.trim($this.parent().parent().find(".status_id").text());
	var $sentTip = $("<div id='sentTip'></div>");
	$("#sentTip").remove();
	$sentTip.prependTo("#statuses");
	$sentTip.html("正在收藏id为" + status_id + "的消息...");
	$.ajax({
		url: "ajax/addfavor.php",
		type: "POST",
		data: "status_id=" + status_id,
		success: function(msg) {
			if (msg.indexOf("success") >= 0) {
				$sentTip.html("收藏消息成功");
			} else {
				$sentTip.html("收藏失败，请重试");
			}
		},
		error: function(msg) {
			$sentTip.html("收藏失败，请重试");
		}
	});
}
function onReplie($this){
	var replie_id = $this.parent().parent().find(".status_word").find(".user_name").text();
	$("#textbox").val($("#textbox").val() + "@" + replie_id + " ");
	$("#textbox").focus();
	$("#in_reply_to").val($this.parent().parent().find(".status_id").text());
	leaveWord();
}

function onRT($this){
	var replie_id = $this.parent().parent().find(".status_word").find(".user_name").text();
	$("#textbox").val("RT @" + replie_id + ":" + $this.parent().parent().find(".status_word").text().replace(replie_id, ""));
	$("#textbox").focus();
	leaveWord();
}

function onDelete($this, type) {
	var status_id = $.trim($this.parent().parent().find(".status_id").text());
	var confirm = window.confirm("确定要删除id为" + status_id + "的消息?");
	if (confirm) {
		$("#sentTip").remove();
		var $sentTip = $("<div id='sentTip'></div>");
		$sentTip.prependTo("#statuses");
		$sentTip.html("删除id为" + status_id + "的" + type + "中...");
		var postData = (type == "消息")? "status_id=": "favor_id=";
		$.ajax({
			url: "ajax/delete.php",
			type: "POST",
			data: postData + status_id,
			success: function(msg) {
				if (msg.indexOf("success") >= 0) {
					$this.parent().parent().parent().remove();
					$sentTip.html("删除id为" + status_id + "的" + type + "成功");
				} else {
					$sentTip.html("删除" + type + "出错，请重试");
				}
			},
			error: function(msg) {
				$sentTip.html("删除" + type + "出错，请重试");
			}
		});
	}
}
function leaveWord(num) {
	if (!num) num = 140;
	var leave = num-$("#textbox").val().length;
	if (leave < 0) {
		$("#tip").css("color","#CC0000");
		$("#tip b").css("color","#CC0000");
		$("#tip").html("已经超出<b>" + (-leave) + "</b>个字");
	} else {
		$("#tip").css("color","#000000");
		$("#tip b").css("color","#000000");
		$("#tip").html("还可以输入<b>" + leave + "</b>个字");
	}
}