$(document).ready(function(){
	list_items = $("#topten").children("li");
	last_item = $(list_items[list_items.length - 1]);
	
	new_last_id = last_item.children("span.tweet_id").text().replace(/^\s+|\s+$/g,"");
	new_last_num = last_item.children("span.number").text().replace(/^\s+|\s+$/g,"");
	
	$("#viewmore").unbind();
	
	$("#viewmore").click(function(){
		if(document.location.toString().indexOf("/newly_added") > 0)
		{
			nextPage(new_last_id, new_last_num, 'newly_added');
		}
		else if(document.location.toString().indexOf("/top") > 0)
		{
			nextPage(new_last_id, new_last_num, 'top');
		}
		return false;
	});
	
	$("#tw_login_btn").click(function(e){
		e.preventDefault();
		login();
	});
	
	$('#tw_cancel_btn').click(function(e){ 
		e.preventDefault(); 
		$('#lightbox_background').css('filter','alpha(opacity=50)').fadeOut('fast'); // filter is required hack for ie8 
		$('#lightbox_login').fadeOut('fast'); 
	});
});

function login()
{
	$.ajax({type: "POST", url: "/login", data:{username: $('#tw_username').val(), password: $("#tw_password").val() }, success: function(data){
		
		if(data.replace(/^\s+|\s+$/g,"") == "true")
		{
			$('#lightbox_background').css('filter','alpha(opacity=50)').fadeOut('fast'); // filter is required hack for ie8 
			$('#lightbox_login').fadeOut('fast');
			notify('Login successful! You may now vote!');
		}
		else
		{
			notify('Hmm... your login failed. Try again?');
		}
		
	}, error: function(){
		notify("Woah! Something went wrong! We're working hard to fix it!");
	} });
}

function nextPage(last_id, last_num, current_location)
{
	$.ajax({type: "POST", url: "/next_page", data: "last_id=" + last_id + "&last_num=" + last_num + "&location=" + current_location, success: function(data){
		if(data.replace(/^\s+|\s+$/g,"") == "")
		{
			notify('It looks like the arrogance has run out! Perhaps you should <span id="menusubmit"><a href="#" class="thickbox"><span>Submit a Tweet!</span><a></span>');
			$('#menusubmit a').click(function(e) {
				e.preventDefault();
				$('#lightbox_background').css('filter','alpha(opacity=50)').fadeIn('slow'); // filter is required hack for ie8
				$('#lightbox_submit').fadeIn('slow');
			});
			$("#viewmore").parent().effect("shake", 'fast');
		}
		$("#topten").append(data);
		
		list_items = $("#topten").children("li");
		last_item = $(list_items[list_items.length - 1]);
		
		new_last_id = last_item.children("span.tweet_id").text().replace(/^\s+|\s+$/g,"");
		new_last_num = last_item.children("span.number").text().replace(/^\s+|\s+$/g,"");
		
		$("#viewmore").unbind();
		
		$("#viewmore").click(function(){
			if(document.location.toString().indexOf("/newly_added") > 0)
			{
				nextPage(new_last_id, new_last_num, 'newly_added');
			}
			else if(document.location.toString().indexOf("/top") > 0)
			{
				nextPage(new_last_id, new_last_num, 'top');
			}
			return false;
		});
		
	}, error: function(){
		notify("Agh! The arrogance is just too much for our server right now! Try again in a minute...");
	} });
	return false;
}

function notify(message)
{
	if($(".message_bar").css("display") == "none" )
	{
		$(".message_bar").html(message);
		$(".message_bar").slideDown('slow', function(){
			$(".message_bar").effect("highlight", {}, 3000);
		});
		setTimeout(function(){ 
			$('.message_bar').slideUp('slow'); 
		}, 5000);
	}
}

$("#submitusername").click(function(){
	$.ajax({type: "POST", url: "/submit", data: "username=" + $("#username").val(), success: function(data){
		$("#submituser > h2").remove(); $("#submituser > img.profile_pic").remove();$("ul.lightbox_tweets").remove();
		$("#submituser").append(data);
	}, error: function(){
		notify("That user does not exist or their stream is private. Who do they think they are, anyway?");
	} });
	return false;
});

function submit_tweet(tweet_url)
{
	$.ajax({type: "POST", url: "/submit", data: "permaurl=" + tweet_url, success: function(data)
		{
			document.location = "/newly_added";
		}, error: function()
		{
			notify("We couldn't find that tweet, or it's private... Maybe it's just <i>that</i> important?");
		}
	});
}


$("#submitpermaurl").click(function(){
	submit_tweet($("#permaurl").val());
	return false;
});

function vote(vote_up, tweet_id, elem)
{
	$.ajax({type: "POST", url: "/vote", data: ({vote_up: vote_up, tweet_id: tweet_id}), success: function(data){
			if (data != "")
			{
				if(data.replace(/^\s+|\s+$/g,"") == "not_logged_in")
				{
					$('#lightbox_background').css('filter','alpha(opacity=50)').fadeIn('slow'); // filter is required hack for ie8
					$('#lightbox_login').fadeIn('slow');
				}
				else
				{
					$(elem).parent().parent().children("span.quantity").text(data.replace(/'/g, ""));
					$(elem).parent().parent().parent().children("div.tweet").effect("highlight", {}, 3000);
					if(vote_up)
					{
						$(elem).attr("class", "voted_up");
						$(elem).siblings().attr("class", "vote_down");
					}
					else
					{
						$(elem).attr("class", "voted_down");
						$(elem).siblings().attr("class", "vote_up");
					}
				}
			}
			else if(document.location.toString().indexOf("/random") == -1)
			{
				notify("Trying to cast your vote more than once? You're not <i>that</i> special.")
			}
			
			if(document.location.toString().indexOf("/random") >= 0 && data.replace(/^\s+|\s+$/g,"") != "not_logged_in")
			{
				document.location = "/random"; 
			}
		},
		error:
		function(){
			notify("Ack! Your vote failed! We're looking into it... Try again in a few minutes.");
		}
	});
}