

function vec2()
{
	this.x=0;
	this.y=0;
}

function user()
{
	this.imgurl="http://s3.amazonaws.com/twitter_production/profile_images/79745077/DigitalToolsTile_bigger.png";
	this.name="hundemann!";
	this.nickname="hundemann!";
	this.pos=new vec2();
	this.id="";
	this.followers;
	this.friends;
	this.status="???";

}

var startposy=80;
var startposx=60;

var lastopen;
var movedUsers=new Array();
var users=new Array();
var currentDetail="";

var widthMax=250;



function init()
{
	updateAPILimit();
	createUsers("pandrr");
}

function startLoading()
{
	$('#loading').css("display","block");
}
function stopLoading()
{
	$('#loading').css("display","none");
}


function getUserByID(id)
{

	for(var i=0;i<users.length;i++)
	{

		if(users[i].id=="#"+id)return i;
	}
}



function repaint()
{

	for(var i=0;i<users.length;i++)
	{
		var content='<div class="ublock" id="test'+i+'">';
		content+='<img style="float:left;" src="'+users[i].imgurl+'" class="uimage" width="48" height="48"/>';
		
		content+='<div class="ucontent">';
		content+=users[i].content;
		content+='</div>';
		
		content+="</div>";

		$(content).appendTo("#content");

		$('#test'+i).animate(
		{ 
			top:users[i].pos.y,
			left:users[i].pos.x,
		}, 500 ,"swing",function(){} );

		
		
		$('#test'+i).mouseover(function() 
		{
			if(!$('#bar').isAnimated)
			{
				var tp=$(this).css("top");
				var lf=$(this).css("left");
				$('#bar').animate( { top:tp,left:lf }, {queue:false,duration:100, easing:'swing'} );
				//300 ,"swing",	function(){} );
				
			}
		});
		
		
		$('#test'+i).click(function() 
		{
			if(lastopen!=undefined || $(this).css("width")==widthMax+"px" )
			{

				for(var i=0;i<movedUsers.length;i++)
				{
					if(!$(users[movedUsers[i]].id ).isAnimated)
					{
						$(users[movedUsers[i]].id ).animate({ left:(users[movedUsers[i]].pos.x)+"px" }, {queue:false,duration:(users[movedUsers[i]].pos.x/90)*30+50, easing:'swing'}  ,function(){} );
					}
					else
					movedUsers.splice(i, 1);

				}
				lastopen.animate({ width:"52px"	}, 200 ,"swing",function(){} );

			}
			
			if($(this).css("width")==widthMax+"px")
			{
				lastopen=undefined;
			}
			else
			{
				lastopen=$(this);

				$(this).animate({ width:widthMax+"px"	}, {queue:false,duration:240, easing:'swing'},function(){} );

				
				
				// details user
				$("#userdetails").empty();
				
				var detailuser=users[getUserByID( $(this).attr("id")) ];
				var detailcontent='<div id="detail'+detailuser.arraynum+'" style="background-color:#ddd;position:absolute;z-index:1111;right:-450px;padding:10px;width:280px;top:150px;">'
				detailcontent+='<img width="48" height="48" src="'+detailuser.imgurl+'" style="border:5px solid white;float:left;" />';
				detailcontent+='<div style="float:right;margin-right:10px;width:200px;border:0px solid red;overflow:hidden;">';
				detailcontent+="<b>"+detailuser.nickname+"</b><br/>";
				detailcontent+=detailuser.name+"<br/>";
				
				if(detailuser.url!=null)
				{
					detailcontent+='<a href="'+detailuser.url+'">'+detailuser.url+'</a><br/>';
				}
	
				detailcontent+='</div><div style="clear:both;"/>';

				detailcontent+='<div style="background-color:#eee;margin-top:10px;padding:10px;">&#8220; '+detailuser.status+' &#8221;';
				detailcontent+="</div>";
								
				detailcontent+='<a onClick="createUsers(\''+detailuser.nickname+'\');" style="cursor:pointer;display:block;margin-top:20px;margin-bottom:20px;padding:10px;text-decoration:none;background-color:#ff0000;color:#fff;font-weight:bold;">browse '+detailuser.nickname+'</a>';

				detailcontent+='<a href="http://twitter.com/'+detailuser.nickname+'" target="_blankk" style="cursor:pointer;display:block;margin-top:20px;margin-bottom:20px;padding:10px;text-decoration:none;background-color:#444;color:#fff;font-weight:bold;">visit '+detailuser.nickname+' on twitter</a>';
				
				
				
				currentDetail="#detail"+detailuser.arraynum;
				
				$(detailcontent).appendTo("#userdetails");
				$("#detail"+detailuser.arraynum).animate({ right:"10px" }, {queue:false,duration:200, easing:'swing'} , function(){} );
				
				////////////////////
				

				//alert($(this).attr("id"));

				var kk=users[getUserByID( $(this).attr("id")) ].pos.x;//parseInt($(this).css("left").replace("px",""));

				var count=movedUsers.length;
				for(var i=0;i<users.length;i++)
				{
					var kk2=users[i].pos.x;//parseInt($('#test'+i).css("left").replace("px",""));

					//if( $(this).css("top") == $('#test'+i).css("top")  && kk2>kk )
					if( $(this).css("top") == $(users[i].id).css("top")  && kk2>kk )
					{
						movedUsers[count++]=i;
						$(users[i].id).animate({ left:(kk2+widthMax-52)+"px"	}, {queue:false,duration:kk2/90*30+50, easing:'swing'} , function(){} );
					}
				}
			}		
		});

	}

}

function mySort(sortf)
{

	$(currentDetail).css("right","-400px");
	$('#bar').css("top","20px");
	$('#bar').css("left","-120px");
	
	if(lastopen!=undefined)lastopen.animate({ width:"48px"	}, 200 ,"swing",function(){} );
	

	if(sortf=="followers_asc")users.sort(sort_followers_asc);
	else
	if(sortf=="followers_desc")users.sort(sort_followers_desc);
	else
	if(sortf=="friends_asc")users.sort(sort_friends_asc);
	else
	if(sortf=="friends_desc")users.sort(sort_friends_desc);
	else
	if(sortf=="unsorted")users.sort(sort_unsorted);
	else
	if(sortf=="random")users.sort(sort_random);
	else
	if(sortf=="alphabetically")users.sort(sort_alphabetically);

	
	
	

	$('#dividers').empty();

	var posy=0;
	var lastgroup=0;
	var countx=0;
	var county=0;

	if(sortf=="friends_desc" || sortf=="friends_asc") 
	{
		for(var i=0;i<users.length;i++)
		{
			users[i].group_followers="less than 50 friends";
			if(users[i].friends>50)users[i].group_followers="more than 50 friends";
			if(users[i].friends>100)users[i].group_followers="more than 100 friends";
			if(users[i].friends>200)users[i].group_followers="more than 200 friends";
			if(users[i].friends>300)users[i].group_followers="more than 300 friends";
			if(users[i].friends>400)users[i].group_followers="more than 400 friends";
			if(users[i].friends>500)users[i].group_followers="more than 500 friends";
			if(users[i].friends>1000)users[i].group_followers="more than 1000 friends";
		}
	}
	else
	if(sortf=="followers_desc" || sortf=="followers_asc") 
	{
		for(var i=0;i<users.length;i++)
		{
			users[i].group_followers="less than 50 followers";
			if(users[i].followers>50)users[i].group_followers="more than 50 followers";
			if(users[i].followers>100)users[i].group_followers="more than 100 followers";
			if(users[i].followers>200)users[i].group_followers="more than 200 followers";
			if(users[i].followers>300)users[i].group_followers="more than 300 followers";
			if(users[i].followers>400)users[i].group_followers="more than 400 followers";
			if(users[i].followers>500)users[i].group_followers="more than 500 followers";
			if(users[i].followers>1000)users[i].group_followers="more than 1000 followers";
		}
	}
	else
	if(sortf=="friendornot") 
	{
		for(var i=0;i<users.length;i++)
		{
			if(users[i].friendornot==1)users[i].group_followers="friends of "+$('#username').val()+":";
			else
			if(users[i].friendornot==2)users[i].group_followers="fans of "+$('#username').val()+":";
			else 
			users[i].group_followers=""+$('#username').val()+" follows those people, but they are not following "+$('#username').val()+":";
		}
	}
	else
	{
		for(var i=0;i<users.length;i++)
			users[i].group_followers="";

		county=1;
	}
	


	for(var i=0;i<users.length;i++)
	{
		if(countx==10)
		{
			county++;
			countx=0;
		}

		if( users[i].group_followers!=lastgroup)
		{	
		
			posy+=40;
			var y=county*60+startposy+posy+20;
		
			$('<div id="divider'+i+'" class="divider" style="position:absolute;width:700;height:20px;display:block;left:-1000px;top:'+y+'px;">'+users[i].group_followers+':</div>').appendTo("#dividers");
			$('#divider'+i).animate({ left:startposx+"px" }, {queue:false,duration:500} , function(){} );

			countx=0;
			county++;
		}
		lastgroup=users[i].group_followers;

		users[i].pos.x=countx*60+startposx;
		users[i].pos.y=county*60+startposy+posy;
		
		//$(users[i].id).css("top",users[i].pos.y);
		//$(users[i].id).css("left",users[i].pos.x);

		$(users[i].id).animate({ left:(users[i].pos.x)+"px",top:(users[i].pos.y)+"px"	}, {queue:false,duration:500} , function(){} );
  
		countx++;
		

	}
	//repaint();
	
}


function sort_random(a, b)
{
	var d = new Date();
	
	return 0.5 - Math.random(d.getMilliseconds());
}

function sort_alphabetically(a, b)
{
	if(a.nickname.toLowerCase() > b.nickname.toLowerCase())return 1;
	else if(a.nickname.toLowerCase() < b.nickname.toLowerCase())return -1;
	else return 0;
}


function sort_unsorted(a, b)
{
	if(a.arraynum > b.arraynum)return 1;
	else if(a.arraynum < b.arraynum)return -1;
	else return 0;
}


function sort_followers_asc(a, b)
{
	if(a.followers > b.followers)return 1;
	else if(a.followers < b.followers)return -1;
	else return 0;
}


function sort_friendornot(a, b)
{
	if(a.friendornot > b.friendornot)return 1;
	else if(a.friendornot < b.friendornot)return -1;
	else return 0;
}

function sort_followers_desc(a, b)
{
	if(a.followers < b.followers)return 1;
	else if(a.followers > b.followers)return -1;
	else return 0;
}

function sort_friends_asc(a, b)
{
	if(a.friends > b.friends)return 1;
	else if(a.friends < b.friends)return -1;
	else return 0;
}
function sort_friends_desc(a, b)
{
	if(a.friends < b.friends)return 1;
	else if(a.friends > b.friends)return -1;
	else return 0;
}



function updateAPILimit()
{
	$.getJSON('http://twitter.com/account/rate_limit_status.json?callback=?', {}, function(json)
	{	
		var now= new Date();

		var minutesToReset=Math.round((json.reset_time_in_seconds-now/1000)/60)+1;
	
		$('#remaining').html(json.remaining_hits+" of "+json.hourly_limit+", resets in "+minutesToReset+" minutes");
	});
}


function createUsers(name)
{

	startLoading();
	$(currentDetail).css("right","-400px");
	$('#bar').css("top","20px");
	$('#bar').css("left","-120px");
	$('#username').val(name);
	
	for(var i=0;i<users.length;i++)
		$(users[i].id).remove();

	movedUsers=new Array();
		
	users=new Array();

	$.getJSON('http://twitter.com/statuses/friends/'+name+'.json?callback=?', {}, function(json)
	{	
		var count=0;
		var countx=0;
		var county=1;
		$.each(json, function(i,item)
		{
			//var s=JSON.stringify(item);
			//alert(s);

			countx++;
			if(countx==10)
			{county++;countx=1;}
			var u=new user();
			u.arraynum=count;
			u.nickname=item.screen_name;
			u.name=item.name;
			u.twitterid=item.id;
			u.id='#test'+count;
			u.imgurl=item.profile_image_url;
			u.url=item.url;
			
			u.followers=item.followers_count;
			u.friends=item.friends_count;
			
			if(item["status"])
			u.status=item["status"].text;

			
			u.content='<a target="_blankk" style="font-size:12px;font-weight:bold;" onClick="createUsers(\''+item.screen_name+'\')">browse '+item.screen_name+'</a> <br/> ';
			u.content+=""+u.followers+" followers / "+u.friends+" friends<br/>";

			users[count++]=u;

		});
		 mySort("unsorted");
		
		repaint();
		updateAPILimit();
		stopLoading();

	});
}

var followers=new Array();
var friends=new Array();



function getFriendsAndFollowers()
{
	startLoading();
	
	
	
	$.getJSON('http://twitter.com/followers/ids.json?screen_name='+$('#username').val()+'&callback=?', {}, function(json)
	{	
		followers=new Array();
		s=json+"";
		followers= s.split(",");
				
				
		$.getJSON('http://twitter.com/friends/ids.json?screen_name='+$('#username').val()+'&callback=?', {}, function(json)
		{	
			friends=new Array();
			s=json+"";
			friends= s.split(",");

			var test="";
			for(var i=0;i<users.length;i++)
			{
				users[i].friendornot=0;

				for(var a=0;a<followers.length;a++)
				{

					if(users[i].twitterid==followers[a])
					{

						users[i].friendornot=1;
					}
				}

				/*
				for(var j=0;j<friends.length;j++)
				{
					if(users[i].twitterid==friends[j])
					{
						users[i].friendornot=2;
					}
				}
				*/
				
				test+=users[i].friendornot+"\n";
			}

			// TODO: add "new" people to users...

			users.sort(sort_friendornot);
			
			
			updateAPILimit();
			stopLoading();
			
			mySort('friendornot');
			
			
		});
				
				
	});





}














