var timeline = new Object();
timeline.tweetID = 0;
timeline.firstTweetID = 0;
timeline.tweetNum = 0;
timeline.defaultValue = "";
timeline.argument = true;
timeline.latest = false;
timeline.more = false;
timeline.moreNum = 0;
timeline.moreNumMax = 10;
timeline.postFlag = false;
timeline.toMe = false;
timeline.toAll = false;

function removeTweet(tweetsHtml) {
	var s = tweetsHtml.lastIndexOf("<div class=\"tweet");
	if(s < 0)
		s = tweetsHtml.lastIndexOf("<DIV class=tweet");
	if(s < 0)
		s = tweetsHtml.lastIndexOf("<DIV class=\"tweet")
	if(s >= 0) {
		return tweetsHtml.substring(0, s);
	}
	return tweetsHtml;
}

function postTweet(form) {
	$.ajax({
		type: "POST",
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		url: timeline.postUrl,
		data: {"Tweet":form.Tweet.value},
		dataType:"json",
		success: function() {
			form.Tweet.value=timeline.defaultValue;
			timeline.more = false;
			timeline.postFlag = true;
			$(update);
			timeline.tweetID++;
			timeline.firstTweetID++;
			if(timeline.latest) {
				$(updateLatest);
				$("input#tweetBox").css("background", "#FFFFFF");
			}
		}
	});
}

function update() {
	var tempTweetID = 0;
	var date = new Date();
	var tweetID = (!timeline.more) ? timeline.firstTweetID : timeline.tweetID;
	if ((timeline.toMe || timeline.toAll) && !timeline.more && !timeline.postFlag) {
		tweetID = 0;
		timeline.firstTweetID = 0;
		timeline.tweetNum = 0;
	}
	var ctrlUrl = timeline.getUrl+(timeline.argument ? "&" : "")+"TweetID="+tweetID+"&GetMax="+timeline.tweetNumMax+"&time="+date.getTime()+(timeline.more ? "&More=1" : "")+(timeline.toMe ? "&ToMe=1" : "");
	$.getJSON(ctrlUrl,function(json){
		var tweetsHtml = $(timeline.tweetsTag).html();

		if(json.tweets[0] != null && !timeline.more) {
			tempTweetID = parseInt(json.tweets[0].seq, 10);
		}
		if(!timeline.argument && timeline.firstTweetID > 0) {
			json.tweets.reverse();
		}
		for(var i = 0; i < json.tweets.length; i++) {
			var tweet = json.tweets[i];
			if(tweet != null) {
				if(timeline.tweetNum >= timeline.tweetNumMax) {
					tweetsHtml = removeTweet(tweetsHtml);
					timeline.tweetNum--;
				}
				if(timeline.firstTweetID == 0 || timeline.more) {
					tweetsHtml += uniformTweet(tweet);
				} else {
					tweetsHtml = uniformTweet(tweet) + tweetsHtml;
				}
				timeline.tweetNum++;
				if (!timeline.postFlag) {
					timeline.tweetID = tweet.seq;
				}
			}
		}
		if(i >= timeline.tweetNumMax && timeline.moreNum < timeline.moreNumMax) {
			$("div#moreLink").html("<a href=\"#\" onclick=\"updateMore();return false;\"><img src=\"../image/tweet_more.gif\" /></a>");
		}

		if(tempTweetID > 0) {
			timeline.firstTweetID = tempTweetID
		}
		$(timeline.tweetsTag).html(tweetsHtml);

		if(!timeline.argument) {
			window.setTimeout(update, 15000);
		}
	});
}

function updateLatest() {
	var date = new Date();
	$.getJSON(timeline.getLatestUrl+"&time="+date.getTime(),function(json){
		var tweet = json.tweets[0];
		if (tweet != null) {
			var comment = tweet.msg;
		}
		$("div#comment").html(comment);
	});
}

function updateMore() {
	timeline.more = true;
	timeline.tweetNum = 0;
	timeline.moreNum++;
	timeline.postFlag = false;
	$("div#moreLink").html("");
	if(timeline.moreNum > timeline.moreNumMax) {
		return;
	}
	$(update);
}

