/*****		*****/
{
	
	$(function(){
		get_limit();
		$("#recheck-limit").click(function(e){
			get_limit();
		});
		$("#find").click(function(e){
			$("#mutweeps").html("");
			var me = $("#me").val();
			var him = $("#him").val();
			find_mut(me,him);
			$("#form").hide();
		});
		$("li.faq").css('display','none');
		$(".toggler").click(function(){
			$("li.faq").toggle();
		});
		get_followers();
	});

	function find_mut(me,him){
		var imFollowin = [];
		var heIsFollowin = [];
		var mutualTweeps = [];
		var api_saving_limit = 10;
//		mutualCount = [];
		
		$.getJSON('http://twitter.com/friends/ids/'+me+'.json?callback=?',function(data){
			imFollowin = data;
		});
		$.getJSON('http://twitter.com/friends/ids/'+him+'.json?callback=?',function(data){
			$.each(data,function(i,tweep){
				if ($.inArray(tweep,imFollowin) > 0){
					mutualTweeps.push(tweep);
				}
			});
			for (var i=0;i<mutualTweeps.length && i< api_saving_limit;i++){
				$.getJSON('http://twitter.com/users/show/'+mutualTweeps[i]+'.json?callback=?',function(tw_info){
					$("<a />").attr('href','http://twitter.com/'+tw_info.screen_name).attr('target','_blank').append("<img src='"+tw_info.profile_image_url+"' />").appendTo("#mutweeps");
					get_limit();
				});
			}
			
			$("#mutweeps").append("<p class='mutweeps'>"+mutualTweeps.length+" mutual tweeps</p>");
			if (mutualTweeps.length > api_saving_limit){
				var see_all = $("<a href='#see-all' class='twitter-link'>see all</a>").click(function(e){
					for (var i = api_saving_limit; i < mutualTweeps.length; i++){
						$.getJSON('http://twitter.com/users/show/'+mutualTweeps[i]+'.json?callback=?',function(tw_info){
							var img = $("<img />").attr('src',tw_info.profile_image_url).css('max-height','48px').css('padding-right','3px');
							$("<a />").attr('href','http://twitter.com/'+tw_info.screen_name).append(img).appendTo("#mutweeps");
						});
					}
					$(this).css('display','none');
				}).css('margin-left','10px');
				$(".mutweeps").append(see_all);
			}
			var tweet = mutualTweeps.length+"+tweeps+in+common+between+@"+me+"+&+@"+him+"+[using+@mutweeps]";
			var goAgain = $("<a href='#go-again' class='cool'>try again</a>").click(function(e){
				$("#tweet").hide();
				$("#form").show();
			});
			$("#tweet").html("<a href='http://twitter.com/?status="+escape(tweet)+"' class='cool'>tweet this</a>").append(goAgain);
			$("#tweet").show();
		});
	}
	function get_limit(){
		$.getJSON('http://twitter.com/account/rate_limit_status.json?callback=?',function(data){
			$("#api-limit").html('API Limit: <a id="recheck-limit" class="twitter-link" href="#recheck-limit">'+data.remaining_hits+"</a>");
			if (data.remaining_hits == 0){
				$("#find").attr("disabled","disabled");
				var reset_time = new Date();
				reset_time.setTime(data.reset_time_in_seconds*1000);
				$("#form").html("<span class='red'><b>Twitter API Limit Reached</b> [back at " + reset_time.toLocaleTimeString() + "]</span>");
			}
		});
	}
	
	function get_followers(){
		$.getJSON('http://twitter.com/users/show/mutweeps.json?callback=?',function(data){
			$("#followers").html("["+data['followers_count']+" followers]");
		});
	}
}
