var input_history = new Array();
var hit_count = 0;
//var current_game_id;
var state= 0;   // 0: playing bubble; 1: view result; 2: search when viewing
function sendToFlash(game_id) {
	var tag_str = $$('tag_input').value.toLowerCase();
	// I need to check "stop words" here, and give hints about the stop words
	if (tag_str.length>1) {
		$$('flash_id').receiveTagFromHtml(tag_str); 

		$$('tag_input').value = '';
		input_history.push(tag_str);
		//if (input_history.length>=3) {
		//$$('next_game').disabled=false;
		// next_game commented for obama
		//$$('next_game').style.visibility="visible";
		//reverse_tags = input_history.reverse()
		//$$('input_history').value = reverse_tags.join(",");
		//}
		if (state==1) state=2;
		content = "tags="+tag_str + "&game_id="+game_id + "&state="+state;
		post_to_server('sharetags', content); 
	}
	$$('tag_input').focus();
}
//function sendToFlashShareSurprise() { $$('flash_id').receiveShareSurpriseFromHtml(input_history.join(",")); }

function next_game(curr_game_id) {
	input_history = [];
	request = "current_game_id="+curr_game_id;
	// see the processing in post_to_server() after the response from the server
	post_to_server('nextgame', request);  
}

// added Feb 5, 2009
function view_word_cloud(game_id) {
  label_open_full_view = 'open full view';
  if (state==0) {
	state = 1;
	//post_to_server('view_cloud');  
	var tags_to_share = input_history.join(",");
	tags_to_share = tags_to_share.replace(/\s+/,' ');
	//if (tags_to_share.length>=1) {
		//content = "tags="+tags_to_share + "&game_id="+game_id + "&state="+state;
		content = "tags=" + "&game_id="+game_id + "&state="+state;
		post_to_server('sharetags', content); 
		input_history = [];
	//}
	app_domain = $$('app_domain_id').innerHTML;
	$$('flash_id').viewTagCloudFromHtml(game_id,app_domain); 
	$$('tag_input').focus();
/* 	if ($$('show_full_view_id').innerHTML == '1') { 
		$$('view_word_cloud_id').innerHTML = label_open_full_view;
		$$('view_word_cloud_id').style.color = 'red';
	}
*/
	$$('view_word_cloud_id').style.display = 'none';
  }
/*  else if ($$('view_word_cloud_id').innerHTML == label_open_full_view) {
  //else if (state==1) {
	//window.location = "/s/ig.swf?id="+game_id+"&show=1"
	//window.open("/gallery?id="+game_id);	// open a new window or tab
	//window.open("/s/ig.swf?id="+game_id+"&show=1");
	window.open("/s/ig.swf?id="+game_id+"&show=1&clustering="+game_id);
  }
 */
}

// added Feb 8, 2009
function view_gallery(game_id) {
	window.open("/gallery?id="+game_id);
// show a list of clustering with their "title/name/time/comments/" (and votes) 
// when clicking a title, go to: /s/ig.swf?id=game_id & show=1 & clustering=clustering_id 
}

function fromFlash_view_cloud(game_status) { 
	if (game_status==0) {
		$$('guess_panel').style.display = 'none';
		$$('save_panel').style.display = 'block';
		$$('game_title').focus(); // not work?
	} else {
		$$('tag_input').focus(); // not work?
		$$('embed_panel').style.display="block";
		$$('embed_panel2').style.display="block";
	}

	game_id = $$('game_id').innerHTML;
	post_the_input_history(game_id);
	//alert(game_id);

	$$('question_id').innerHTML="search this cloud";
}

function save_game(game_id) {
	title = $$('game_title').value;
	creator = $$('creator_name').value;
	taboo = $$('taboo_tags').value;
	taboo = taboo.split(/\W+/).join(',');
	alert(taboo);
	request = "game_id="+game_id+"&title="+title + "&creator=" + creator + "&tags=" + input_history.join(",") + "&taboo="+taboo;
	result = post_to_server('savegame', request); 

	$$('embed_panel').style.display="block";
	$$('embed_panel2').style.display="block";
}


function key_press_proc(evt,game_id) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13) { sendToFlash(game_id); return false; }
	return true;
}


function get_top_word_freq(paste_text) {
	var tag_shown_max = 300;
	var wf_text = '';
	var word_freq = compute_freq_words(paste_text);
	words = hash2key(word_freq);
	sorted_words = sortByValue(words, word_freq);
	var cnt = tag_shown_max < words.length ? tag_shown_max : words.length;
	for (var i=0;i<cnt;i++) {
		var w = sorted_words[i];
		if (w.length<50) {  // in case there is a word that is exceptionally long.
			var f = word_freq[w];
			wf_text += w + ':' + f + ',';
		}
	}
	return wf_text.slice(0,-1);  // -1: remove the last ','
}

function post_to_server(action, content) {
	path = '/rpc';
	var req = AE_CreateXMLHttpRequest();
	req.open("POST", path, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send('action=' + action + '&' + content);
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200) {
			if (action == 'creategame') {
				result = req.responseText;
				//$$("game_url").innerHTML = " <font color=#5076BB>&#9654;</font><a style='text-decoration:none' href=/game?id=" +result + ">Test your game</a>";
				$$("game_url").innerHTML = " <font color=#5076BB>&#9654;</font><a style='text-decoration:none' href=/s/ig.swf?id=" +result + ">Test your game</a>";
			} 
			else if (action == 'nextgame') {
				next_game_id = req.responseText;
				window.location = "/game?id="+next_game_id;
			}
		}
	}
	//req.send(null);
}
function post_the_input_history(game_id) {
	//var user_name = $$('user_name').value;
	var tags_to_share = input_history.join(",");
	tags_to_share = tags_to_share.replace(/\s+/,' ');
	if (tags_to_share.length>=3) {
		//content = "tags="+tags_to_share+"&user_name="+user_name+"&game_id="+game_id;
		content = "tags="+tags_to_share + "&game_id="+game_id;
		post_to_server('sharetags', content); 
		//sendToFlashShareSurprise();
	}
}

function post_the_pasted_text() {
	var paste_text = $$("paste").value;
	var word_freq = get_top_word_freq(paste_text);

	if (word_freq.length>1) {
		//game_title = $$("game_title").value;
		question = $$("question").value;
		image_url = $$("image_url").value;
		the_thing= $$("the_thing").value;
		result = post_to_server('creategame', "word_freq="+word_freq+"&question="+question+"&image_url="+image_url+"&the_thing="+the_thing); 
	}
}

