function empty(val) {

	if(val!=null || typeof(val)=="undefined") {

		return false;

	}

	return true;

}

function makeTweetBox(json) {

	data = eval("(" + json + ")");

	for(i = 0; i < data.length; i++) {

		if(page == 'user') {

			data[i]['twitter_idF'] = data[i]['twitter_id'];

		}

		addTweetBox(data[i]['id'], data[i]['img'], data[i]['twitter_idF'], data[i]['status'], data[i]['time'], data[i]['fav'], true);

	}

	if(i < 19) {

		$("input.more").hide();

	}

	$("#timeline_loading").hide();

	twitterInit();

}

function addTweetBox(tweet_id, img, twitter_idF, status, time, fav, append) {

	var box = '<table style="width:100%;" class="border5 timeline_table" tweetid="' + tweet_id + '"><tr>';

	if(!empty(img) && page != 'user') {

		box += '<td style="width:5%;"> <img src="' + img + '" width="48" height="48" /> </td>';

	}

	box += '<td class="color" style="font-size:10pt;vertical-align:top;width:95%; padding:5px;">';

	if(connected) {

		box += '<div class="hentry" tweetid="' + tweet_id + '">';

		box += '<div class="fav-action ' + ((fav != 1) ? 'non-' : '') + 'fav">';

		box += '</div>';

		if(page == 'home' && twitter_id != twitter_idF) {

			box += '<div class="reply"></div>';

		} else if(twitter_id == twitter_idF) {

			box += '<div class="del"></div>';

		}

		box += '</div>';

	}

	box += '<div style="width:95%"> ';

	if(page != 'user') {

		box += '<a href="http://twitter.com/' + twitter_idF + '" target="_blank"><b>' + twitter_idF + '</b></a> ';

	} else {

		box += '<b style="display:none">' + twitter_idF + '</b>';

	}

	box += (!empty(status)) ? status : '';

	box += '<div style="font-size:10px">' + time + '</div></div></td></tr></table>';

	if(append) {

		$("#tweets_content").append(box);

	} else {

		$("#tweets_content").prepend(box);

	}

}

function getMore() {

	var max_id = $("#max_id").val();

	$("#timeline_loading").show();

	switch(page) {

		case 'home':

			xajax_getMoreF(max_id, twitter_id);

			break;

		case 'mentions':

			xajax_getMoreM(max_id, twitter_id);

			break;

		case 'user':

			xajax_getMore(max_id, twitter_id);

			break;

		case 'fav':

			xajax_getMoreFav(max_id, twitter_id);

			break;

	}

}

function updateStatus() {

	var status = $("#status_new").val();

	$("#status_loading").show();

	xajax_updateStatus(status, twitter_id);

}

function createFav(tweet_id) {

	$(".hentry[tweetid='" + tweet_id + "'] .non-fav").removeClass("non-fav").addClass("fav-throb");

	xajax_createFav(twitter_id, tweet_id);

}

function removeFav(tweet_id) {

	$(".hentry[tweetid='" + tweet_id + "'] .fav").removeClass("fav").addClass("fav-throb");

	xajax_removeFav(twitter_id, tweet_id);

}

function reply(twitter_idF) {

	if($("#status_new").length > 0) {

		$("#status_new").val("@" + twitter_idF + " ").focus();

	} else {

		window.location.href = link_twitter + 'reply_' + twitter_idF;

	}

}

function createFollowDone() {

	$("#follow_following").show();

	$("#follow_follow").hide();

	$("#follow_remove").hide();

}

function removeFollowDone() {

	$("#follow_follow").show();

	$("#follow_following").hide();

	$("#follow_remove").hide();

}

function twitterInit(){

	if(connected && following != 2) {

		if(following) {

			$("#follow_following").show();

		} else {

			$("#follow_follow").show();

		}

		$("#follow_following").click(

			function() {

				$("#follow_remove").toggle();

			}

		);

		$("#follow_follow").click(

			function() {

				xajax_createFollow(twitter_id);

			}

		);

		$("#follow_remove").click(

			function() {

				xajax_removeFollow(twitter_id);

			}

		);

	}

	$(".timeline_table:not(.inited)").addClass('inited').hover(

					function() {

						$(this).addClass("hoverT").find("div.hentry .non-fav, div.hentry .del, div.hentry .reply").show();

					},

					function() {

						$(this).removeClass("hoverT").find("div.hentry .non-fav, div.hentry .del, div.hentry .reply").hide();

					}

				);

	$(".hentry .non-fav:not(.inited)").addClass('inited').click(

					function() {

						tweet_id = $(this).parents(".hentry").attr('tweetid');

						createFav(tweet_id);

					}

				);

	$(".hentry .fav:not(.inited)").addClass('inited').click(

					function() {

						tweet_id = $(this).parents(".hentry").attr('tweetid');

						removeFav(tweet_id);

					}

				);

	$(".hentry .reply:not(.inited)").addClass('inited').click(

					function() {

						twitter_idF = $(this).parents("table.timeline_table").find("b").html();

						reply(twitter_idF);

					}

				);

	$(".hentry .del:not(.inited)").addClass('inited').click(

					function() {

						if(confirm("Sure you want to delete this update? There is NO undo!")) {

							tweet_id = $(this).removeClass("del").addClass("del-throb").show().parents(".hentry").attr('tweetid');

							xajax_delTweet(twitter_id, tweet_id);

						}

					}

				);

	 $('#text_countdown').html(140);

	$("#status_new:not(.inited)").addClass('inited').keyup(function(){

		  if(this.value.length >= 140) {

			   //handle the over the limit part here

			   this.value = this.value.substring(0, 140);

		  }

		 $('#text_countdown').text(140-this.value.length);

	

	});

	switch(page) {

		case 'home':

			$("#link_home").addClass('active');

			break;

		case 'user':

			$("#link_home").addClass('active');

			break;

		case 'fav':

			$("#link_fav").addClass('active');

			break;

		case 'mentions':

			$("#link_mentions").addClass('active');

			break;

	}

}

function loadHome() {

	resetLink();

	$("#link_home").addClass('active').addClass('loading');

	html = $.get(link_home, function(data) {

												$("#twitter_super_content").html(data);

												page = pageH;

												twitterInit();

												$("#link_home").removeClass('loading');

										 }, 'html');

}

function loadFav() {

	resetLink();

	$("#link_fav").addClass('active').addClass('loading');

	html = $.get(link_fav, function(data) {

												$("#twitter_super_content").html(data);

												page = 'fav';

												twitterInit();

												$("#link_fav").removeClass('loading');

										 }, 'html');

}

function loadMentions() {

	resetLink();

	$("#link_mentions").addClass('active').addClass('loading');

	html = $.get(link_mentions, function(data) {

												$("#twitter_super_content").html(data);

												page = 'mentions';

												twitterInit();

												$("#link_mentions").removeClass('loading');

										 }, 'html');

}

function resetLink() {

	$("li[id^='link_']").removeClass('active');

}

$(document).ready(

		function() {

			if(page == 'home') {

				pageH = 'home';

			} else {

				pageH = 'user';

			}

			$("#link_home").click(

							function() {

								loadHome();

							}

						);

			$("#link_fav").click(

							function() {

								loadFav();

							}

						);

			$("#link_mentions").click(

							function() {

								loadMentions();

							}

						);

			twitterInit();

			avatar_img = $("#user_profile_box img.pic1");

			avatar_td = $("#user_profile_box img.pic1").parent('td');

			avatar_td.prepend('<a href="' + $('#link_to_profile').attr('href') + '"><img width="70" src="'+ avatar_img.attr('src') +'"></a>');

			avatar_img.remove();

		}

	);
