var UserList = new Array; // Users in the list
var StatusList = new Array; // Status of users (Shown below)
var LoaderList = new Array; // window list of <iframe> written
var FriendList = new Array; // Users' friends list
var IconList = new Array; // Users' friends list
var userid = 0;
var use_icon = true; // Show users with icon

/*
	Status of users
	0: Downloading
		[Cancel] -> 3: Stopped
	1: OK!
		[Exclude] -> 4: Excluded
	2: Failed
		[Reload] -> 0: Loading
	3: Canceled
		[Reload] -> 0: Loading
	4: Exclude
		[Include] -> 1: OK!
*/

// get status name
function singleton_button(onclick_action, message){
	return "<input type='button' onclick='"+onclick_action+"' value='"+message+"'>";
}

function status_window(id, status){
	switch(status){
	case 0:
		return ("<td style='color:black'>Downloading...</td><td class='userbtn'>"+
			singleton_button("cancel_download("+id+")", "Cancel")+"</td>");
	case 1:
		return ("<td style='color:green'>Succeeded!</td><td class='userbtn'>"+
			singleton_button("switch_counted("+id+")", "Exclude")+"</td>");
	case 2:
		return ("<td style='color:red'>Downloading Failed...</td><td class='userbtn'>"+
			singleton_button("start_download("+id+")", "Reload")+"</td>");
	case 3:
		return ("<td style='color:fuchsia'>Downloading Canceled</td><td class='userbtn'>"+
			singleton_button("start_download("+id+")", "Reload")+"</td>");
	case 4:
		return ("<td style='color:gray'>Excluded from counting</td><td class='userbtn'>"+
			singleton_button("switch_counted("+id+")", "Include")+"</td>");
	default:
		return "<td>[Error]</td><td></td>";
	}
}

// add temporary buffer
var buffer_id = 0;

function add_buffer(html){
	var pos = document.getElementById("tmp_buffer_"+buffer_id);
	pos.innerHTML = ("<div id='tmp_buffer_main_"+buffer_id+"'>"+html+
		"</div><div id='tmp_buffer_"+(buffer_id+1)+"'></div>");
	var newbuf = "tmp_buffer_main_"+buffer_id;
	buffer_id++;
	return document.getElementById(newbuf);
}

// decide whether name is valid for twitter user
function valid_user(name){
	if(name.match(/^[0-9A-Z_a-z]+$/)){
		return true;
	}else{
		return false;
	}
}

// find an item from an array
function find(array, query, default_value){
	var i;
	for(i in array){
		if(array[i] === query){
			return i;
		}
	}
	return default_value;
}

// escape HTML special characters
function HTMLescape(str){
	var buf = str;
	buf = buf.replace(/&/g, "&amp;");
	buf = buf.replace(/</g, "&lt;");
	buf = buf.replace(/>/g, "&gt;");
	buf = buf.replace(/"/g, "&quot;"); //"//
	return buf;
}

// cancel downloading
function cancel_download(num){
	switch(StatusList[num]){
	case 0:
		StatusList[num] = 3;
		break;
	default:
		return;
	}
	
	LoaderList[num].innerHTML = '';
	refresh_userlist();
}

// start downloading
function start_download(num){
	switch(StatusList[num]){
	case 0: case 2: case 3:
		StatusList[num] = 0;
		break;
	default:
		return;
	}
	
	LoaderList[num].innerHTML = ("<iframe id='ifr"+num+"' onload='end_load("+num+
		")' src='intersect.cgi?user="+HTMLescape(UserList[num])+"'></iframe>");
	refresh_userlist();
}

// switch whether to be counted
function switch_counted(num){
	switch(StatusList[num]){
	case 1:
		StatusList[num] = 4;
		break;
	case 4:
		StatusList[num] = 1;
		break;
	default:
		return;
	}
	show_current_result();
	refresh_userlist();
}

// switch whether to show icon
function switch_icon(b){
	use_icon = document.forms.iconswitch.useicon.checked;
	show_current_result();
}

// ----------
var popup_used = false;
function nopopup(stat){
	if(stat) document.getElementById("friend_info").innerHTML = "";
	popup_used = !stat;
}

function show_about(user, all_follow){
	if(!popup_used) return;
	
	var buf = "";
	var tmp;
	var i;
	var counted_user = 0;
	var encuser = HTMLescape(user);
	var follow = new Array;
	var nofollow = new Array;
	
	// if not followed by all specified users, check following or not
	for(i = 0; i < UserList.length; i++){
		if(StatusList[i] != 1) continue;
		counted_user++;
		
		if(!all_follow){
			tmp = find(FriendList[i], user, "");
			if(tmp === ""){
				nofollow.push(HTMLescape(UserList[i]));
			}else{
				follow.push(HTMLescape(UserList[i]));
			}
		}
	}
	
	// show to a window
	buf += "<div style='border:1px solid #008000;background:#CCFFCC;padding:3px'>";
	buf += ("<strong><a href='http://twitter.com/"+encuser+"' target='_blank'>"+
		"<img border='0' class='usericon' src='"+HTMLescape(IconList[user])+
		"' alt='"+encuser+"'>"+encuser+"</a>:</strong><br>");
	
	if(!all_follow){
		if(follow.length > 0){
			buf += "<strong>Followed by:</strong><br>";
			buf += follow.join(" ");
			if(nofollow.length > 0) buf += "<br>";
		}
		if(nofollow.length > 0){
			buf += "<strong>Not followed by:</strong><br>";
			buf += nofollow.join(" ");
		}
	}else{
		buf += "Followed by all "+counted_user+" users";
	}
	document.getElementById("friend_info").innerHTML = buf;
}

// Show count result
function show_current_result(){
	// count users with result
	var validusers = new Array;
	var i, j;
	for(i = 0; i < StatusList.length; i++){
		if(StatusList[i] == 1) validusers.push(UserList[i]);
	}
	if(validusers.length < 2){
		nopopup(true);
		return;
	}else{
		nopopup(false);
	}
	
	// Show title
	var buf = "";
	var i, j;
	
	buf += "<h2>Friends of "
	for(i = 0; i < UserList.length; i++){
		if(StatusList[i] != 1) continue;
		
		buf += ("<a href='http://twitter.com/"+HTMLescape(UserList[i])+"' target='_blank'>"+
			HTMLescape(UserList[i])+"</a>");
		buf += ", ";
	}
	buf = buf.slice(0,-2);
	buf += "("+(validusers.length)+")</h2>";
	buf += "<p>Link to this "+(validusers.length)+" users' friends: <input type='text' value='";
	buf += HTMLescape(location.protocol) + "//";
	buf += HTMLescape(location.host) + HTMLescape(location.pathname);
	buf += HTMLescape("?" + validusers.join("."));
	buf += "' readonly='readonly' size='30' onfocus='this.select()'></p>"
	
	// group friends by followed specified users
	var fluserlist = new Array;
	var countlist = new Array;
	
	for(i = 0; i < UserList.length; i++){
		if(StatusList[i] != 1) continue;
		
		for(j = 0; j < FriendList[i].length; j++){
			if(!fluserlist[FriendList[i][j]]) fluserlist[FriendList[i][j]] = 0;
			fluserlist[FriendList[i][j]]++;
		}
	}
	for(i in fluserlist){
		if(!countlist[fluserlist[i]]) countlist[fluserlist[i]] = new Array;
		countlist[fluserlist[i]].push(i);
	}
	for(i = 0; i < countlist.length; i++){
		if(countlist[i]) countlist[i] = countlist[i].sort();
	}
	
	// print grouped users
	var val, encval;
	
	for(i = countlist.length - 1; i > 1; i--){
		if(!countlist[i]) continue;
		
		if(countlist[i].length > 0){
			buf += "<h3>"+i+" users follow in common ("+(countlist[i].length)+"):</h3>";
			if(use_icon){
				buf += "<p>";
			}else{
				buf += "<p style='font-family:monospace;font-size:smaller'>";
			}
			for(j = 0; j < countlist[i].length; j++){
				val = countlist[i][j];
				encval = HTMLescape(val);
				
				buf += "<a href='http://twitter.com/"+encval+"' target='_blank' onmouseover='show_about(\""+encval+"\",";
				buf += (i == validusers.length ? "true" : "false");
				buf += ")'>";
				if(use_icon){
					buf += "<img border='0' class='usericon' src='"+HTMLescape(IconList[val])+"' alt='"+encval+"'>";
				}else{
					buf += encval + " ";
				}
				buf += "</a>";
			}
			buf += "</p>"
		}
	}
	
	// print the link to this page
	document.getElementById("ints_result").innerHTML = buf;
}

function end_load(usernum){
	var ifr, buf_f, buf_i, arricon;
	var i;
	try{
		ifr = document.getElementById("ifr"+usernum).contentWindow;
		buf_f = ifr.document.getElementById("friends").innerHTML;
		buf_i = ifr.document.getElementById("icons").innerHTML;
	}catch(e){
		StatusList[usernum] = 2;
		refresh_userlist();
		return;
	}
	
	if(buf_f.length == 0 || buf_i.length == 0){
		// alert(NameList[usernum]+": user name or friend list not found");
		StatusList[usernum] = 2;
		refresh_userlist();
		return;
	}
	
	LoaderList[usernum].innerHTML = '';
	FriendList[usernum] = buf_f.split(" ");
	arricon = buf_i.split(" ");
	if(arricon.length == FriendList[usernum].length){
		for(i = 0; i < arricon.length; i++){
			IconList[FriendList[usernum][i]] = arricon[i].replace(/_normal\./, "_mini.");
		}
	}
	StatusList[usernum] = 1;
	show_current_result();
	refresh_userlist();
}

function refresh_userlist(){
	var buf = "";
	var i;
	
	buf += "<table style='width:100%;border:none'>";
	for(i = 0; i < UserList.length; i++){
		buf += "<tr>";
		buf += ("<th><a href='http://twitter.com/"+HTMLescape(UserList[i])+"' target='_blank'>"+
			HTMLescape(UserList[i])+"</a></th>");
		buf += status_window(i, StatusList[i]);
		buf += "</tr>";
	}
	buf += "</table>";
	
	document.getElementById("userlist").innerHTML = buf;
}

function add_user_main(newuser){
	var tmp = find(UserList, newuser, "");
	if(tmp === ""){
		// not added user
		UserList[userid] = newuser;
		StatusList[userid] = 0;
		LoaderList[userid] = add_buffer();
		start_download(userid);
		userid++;
	}else{
		// added user
		//alert(HTMLescape(newuser)+" is always added to the list! ("+tmp+")")
	}
}

function add_user(){
	// get input user name
	var input_user = document.forms.inputform.user.value;
	var arr_user = input_user.split(/\s+/);
	var i;
	for(i = 0; i < arr_user.length; i++){
		if(valid_user(arr_user[i])) add_user_main(arr_user[i]);
	}
	
	document.forms.inputform.user.value = "";
	document.forms.inputform.user.focus();
}

// ---------- initial actions ----------
function init_window(){
	var query = location.search;
	var usr, arr;
	if(query.substr(0, 1) === "?"){
		query = query.substr(1);
		arr = query.split(".");
		for(usr = 0; usr < arr.length; usr++){
			if(valid_user(arr[usr])) add_user_main(arr[usr]);
		}
	}
	
	document.forms.inputform.user.disabled = false;
	document.forms.inputform.submit.disabled = false;
	document.forms.iconswitch.useicon.disabled = false;
}
