addLoadEvent(init);

var count=0;
var one_percent;

function init(){
	
	var button = document.getElementById('search_button');
	button.onclick = function(){
		askForTweets(
			document.getElementById('site').value,
			document.getElementById('text_input_1').value,
			document.getElementById('text_input_2').value,
			document.getElementById('text_input_3').value,
			document.getElementById('text_input_4').value,
			document.getElementById('text_input_5').value
		);
	}
}

function selectThis(elem){

	elems = getElementsByClassName(document, 'a', 'selected');
	elems[0].setAttribute('class', 'notselected');
	elems[0].setAttribute('className', 'notselected'); // For IE
	elem.setAttribute('class', 'selected');
	elem.setAttribute('className', 'selected'); // For IE

	var site = document.getElementById('site');
	site.value = elem.innerHTML;
		
		if(
			(document.getElementById('text_input_1').value) ||
			(document.getElementById('text_input_2').value) ||
			(document.getElementById('text_input_3').value) ||
			(document.getElementById('text_input_4').value) ||
			(document.getElementById('text_input_5').value)
		){			
			askForTweets(
				document.getElementById('site').value,
				document.getElementById('text_input_1').value,
				document.getElementById('text_input_2').value,
				document.getElementById('text_input_3').value,
				document.getElementById('text_input_4').value,
				document.getElementById('text_input_5').value
			);
		}
}

function askForTweets(site, search_phrase_1, search_phrase_2, search_phrase_3, search_phrase_4, search_phrase_5){
		
	var output_graph = document.getElementById('output_graph');
	output_graph.innerHTML = "<div class='loading e'><img alt='' src='images/wait.gif' /> Getting Stuff...</div>";	
	
	ajax.post('actions/results.php', 
		listenToTweets, 
		'site='+site+'&'+
		'search_phrase_1='+search_phrase_1+'&'+
		'search_phrase_2='+search_phrase_2+'&'+
		'search_phrase_3='+search_phrase_3+'&'+
		'search_phrase_4='+search_phrase_4+'&'+
		'search_phrase_5='+search_phrase_5
	);	
}

function listenToTweets(text){
	
	revealSendToFriend();
	
	// Reset
	var output_graph = document.getElementById('output_graph');
	var output_data = document.getElementById('output_data');
	output_data.innerHTML = '';
	count=0;
	
	// Slip up results and start printing
	var results = text.split(',');
	
	var largest=0;
	var realResults=0; // get count of 'real results'
	for(var i=0; i<results.length; i++){
		
		if(results[i] != 'nan'){
			results[i] = parseInt(results[i]);
			realResults++; // increment real results
		}
		if( (results[i] > largest) && (results[i] != 'nan') ){
			largest = results[i];
		}
	
	}
	
	var width = Math.round( 700 / realResults ); // set collum width to max / num of real results

	for(var i=0; i<results.length; i++){
		if(results[i] != 'nan'){
			var ti = 'text_input_'+(i+1);
			output_data.innerHTML = output_data.innerHTML+ "<div class='left' style='text-align: center; width: "+ width +"px;'>"+document.getElementById(ti).value+"<br />"+results[i]+"</div>";
		}
	}
	
	one_percent = Math.round(largest/100);
	if(one_percent==0){ one_percent=1; }
	output_graph.innerHTML = "";
	printResults(output_graph, results, width);	
}

function printResults(output_graph, results, width){
	if(results[count] != 'nan'){
		
		var height = Math.round(results[count] / one_percent) *2.6;
		
		if( !height ){height=2;} // a little error handling, errors in math, my fault, ya know zero's and stuff
		
		// 68a232 green
		// 9f0077 pink
		// da9d14 orange
		// 310076 purple
		// 318fa3 blue
		
		var color;
		
		switch(count){
			case 0 : color = '68a232'; break;
			case 1 : color = '9f0077'; break;
			case 2 : color = 'da9d14'; break;
			case 3 : color = '310076'; break;
			case 4 : color = '318fa3'; break;
		}
		
		var bar =
			"<div class='left'>"+
			"<div id='hidden"+count+"'>"+
			"<div id='' style='width: "+ width +"px;'>"+
			"<div id='hidden"+count+"_margin' style='margin-top: 323px;'>"+
			"<div id='hidden"+count+"_height' class='bar' style='height: 0px; background: #"+color+";' >"+
				"<a id='tooltip_"+count+"' href='javascript://' onmouseover='Effect.Fade(\"output_container\", { duration: 0.2, afterFinish: function(){ Effect.Appear(\"result_container\"); } });'></a>"+
			"</div>"+
			"</div>"+
			"</div>"+
			"</div>"+
			"</div>";
	}
	else {
		var bar =
			"<div class='left'>"+
			"<div id='hidden"+count+"'>"+
			"<div id='' style='width: 0px'>"+
			"<div class='emptybar'>"+
			"</div>"+
			"</div>"+
			"</div>"+
			"</div>";	
	}	
		output_graph.innerHTML = output_graph.innerHTML + bar;
		count++;
		revealBar(output_graph, results, count, width, height);	
}

function revealBar(output_graph, results, count, width, height){
	
	var cur_height = 0;
	var end_height = height;
	var cur_margin = 323;
	var end_margin = (323 - height);
	
	var thisbar_height = document.getElementById('hidden'+(count-1)+'_height');
	var thisbar_margin = document.getElementById('hidden'+(count-1)+'_margin');

	growthisbar(cur_height, end_height, cur_margin, end_margin, thisbar_height, thisbar_margin, output_graph, results, count, width);
	
}

function growthisbar(cur_height, end_height, cur_margin, end_margin, thisbar_height, thisbar_margin, output_graph, results, count, width){

	if(cur_height <= end_height){
		cur_height+=4;
		cur_margin-=4;
		thisbar_height.style.height = cur_height+'px';
		thisbar_margin.style.marginTop = cur_margin+'px';
		
		setTimeout(function(){ 
			growthisbar(cur_height, end_height, cur_margin, end_margin, thisbar_height, thisbar_margin, output_graph, results, count, width);
		}, 
		1  // timeout
		);
	}
	else {	
		if( count < (results.length) ){
			printResults(output_graph, results, width);
		}
	}
}

function revealSendToFriend(){
	// FIX SINGLE AND DOUBLE QUOTES, CONVERT TO HTML EQUIVALENTS
	var elem = document.getElementById('sendtofriend');
	site = document.getElementById('site').value;
	input_1 = document.getElementById('text_input_1').value;
	input_2 = document.getElementById('text_input_2').value;
	input_3 = document.getElementById('text_input_3').value;
	input_4 = document.getElementById('text_input_4').value;
	input_5 = document.getElementById('text_input_5').value;
	elem.innerHTML = "<a id='sendtofriend_link' href=\"sendtofriend.php?site="+site+"&amp;search_phrases="+input_1+","+input_2+","+input_3+","+input_4+","+input_5+"\">Send this chart to a Friend!</a>";
}

function addslashes(str) {
	str = str.replace(/'/g,"&#39;");
	return str;
}