function Atwitude()
{
	var me  = this.constructor.prototype;
	var brand = '';
	var results = new Array();
	var curr = 0;
	var ind = 0;
	var max_ind = 39;
	var result_counter = 0;
	var neg = 0;
	var pos = 0;
	var max_display = 100;
	var curr_display = new Array();
	var pause = false;
	var loading = false;	
	var has_results = false;
	var pause_num;
	var stop_current = false;
	
	me.hasBrand = function() { return brand != ''; };
	
	me.resetScores = function() { neg = 0; pos = 0; $('#pos').html(0); $('#neg').html(0); $('#eq').html('0%'); $('#eq').css('color', '#ABABAB')  };
	
	me.addPositive = function() { pos++; $('#pos').html(pos); $('#pos').animate({fontSize: '30px', color: '#008B00'}, 200).animate({fontSize: '20px', color: '#ABABAB'}, 100); me.updateEqual(); };
	
	me.addNegative = function() { neg++; $('#neg').html(neg); $('#neg').animate({fontSize: '30px', color: '#CC1100'}, 200).animate({fontSize: '20px', color: '#ABABAB'}, 100); me.updateEqual(); };
	
	me.updateEqual = function()
	{
		var eq;
		var color;
		if (pos > neg)
		{
			eq = Math.round(100*(pos / (pos + neg)));
			color = '#008B00';
		}
		else if (neg > pos)
		{
			eq = Math.round(100*(neg / (pos + neg)));
			color = '#CC1100';
		}
		else
		{
			eq = '50';
			color = '#ABABAB';
		}
		$('#eq').html(eq+'%');
		$('#eq').css('color', color);
	};
	
	me.setBrand = function(brand_new) { brand = brand_new; };
	
	me.resetSearch = function() { ind = 0; me.resetScores(); curr_display = new Array(); result_counter = 0; has_results = false; stop_current = false};
	
	me.startLoading = function() { loading = true; };
	
	me.stopLoading = function () { loading = false; };
	
	me.nextSearch = function() { ind = ind + 4;};
	
	me.getCurrentSearch = function() { return ind; };
	
	me.getBrand = function() { return brand; };
	
	me.moveLogo = function()
		{
			$('#spacer').animate({height: '0px'}, 'slow');
		};
	
	me.showPause = function() { $('#pause').show(); };
			
	me.isCurrent = function(time) { return (time == curr); };
	
	me.isIE = function() { return (navigator.appName == 'Microsoft Internet Explorer'); };

	me.stopSearch = function()
	{
		stop_current = true;
	};
	
	me.startSearch = function(curr_time)
		{
			if (me.isIE())
			{
				//$('#footer').hide();
			}
			me.moveLogo();
			me.showScoreArea();
		
			$('#results').empty();			
			me.startLoading();
			$.getJSON('./search.php?ind='+ind+'&brand='+escape(brand), function(data)
			{
				if (data.success == true)
				{
					me.hideLoader();
					me.stopLoading();
					results = data.results;
					has_results = true;
					me.showResults(curr_time);
				}
				else
				{
					me.hideLoader();
					me.stopLoading();
					me.destroyResults();
					me.doNextSearch(curr_time);
				}
			});
		};

	me.doNextSearch = function(curr_time)
		{
			result_counter = 0;
			me.nextSearch();
			if (ind < max_ind)
			{
				me.startLoading();
				me.showLoader();
				$.getJSON('./search.php?ind='+ind+'&brand='+escape(brand), function(data)
						{
							if (data.success = true)
							{
								me.hideLoader();
								me.stopLoading();
								results = data.results;
								has_results = true;
								me.showResults(curr_time);
							}
							else
							{
								me.hideLoader();
								me.stopLoading();
								if (data.reason == '-1')
								{	
									$('#results').html('woops');								
								}
								else
								{									
									if (me.isCurrent(curr_time))
									{
										me.destroyResults();
										me.doNextSearch(curr_time);
									}
								}
		
							}
						});
			}
			else
			{
				me.destroyResults();
				var res = document.getElementById('results').innerHTML;
				if (res == '')
				{
					$('#info').prepend('<p class="splash_text">We have failed you and we are sorry. Try another search term and we promise, we\'ll do better with our insanely complex analysis.</p>');
					$('#info').append('<p class="splash_text">Got questions? <a href="faq.php">Read our FAQ</a>.</p>');

				}
				else
				{
					$('#info').prepend('<p class="splash_text">Those are all the results available right now. Try again or try another term to see how people feel towards it. </p>');
					$('#info').append('<p class="splash_text">Got questions? <a href="faq.php">Read our FAQ</a>.</p>');
				}
			}
		};

	me.getNbResults = function() { return results.length; };

	me.showNoResults = function() { $("#results").empty(); $('#info').html('<p class="splash_text">We have failed you and we are sorry. Try another search term and we promise, we\'ll do better with our insanely complex analysis.</p>'); };

	me.showResults = function(curr_time)
		{

			if (!stop_current)
			{
				jQuery.whileAsync({
					delay: 1750,
					bulk: 0,
					test: function()
					{
						return ((result_counter < results.length) && (me.isCurrent(curr_time)) && (!pause))
					},
					loop: function()
					{
						me.appendResult(results[result_counter].result.id, results[result_counter].result.text, results[result_counter].result.profile_image_url, results[result_counter].negative, results[result_counter].result.user);
						result_counter++;
					},
					end: function()
					{
						if (!pause)
						{
							if (me.isCurrent(curr_time))
							{
								me.doNextSearch(me.getCurrent());
							}
						}
					}
				});		
		}
		};
		
	me.pauseStream = function()
	{
		pause_num = me.getCurrent();
		pause = true;
	}
	
	me.resumeStream = function()
	{
		pause = false;
		if (me.isCurrent(pause_num))
		{
			me.showResults(me.getCurrent());
		}
	}

	me.appendResult = function(id, text, avatar, neg_term, user)

		{
		
			var colour_class = '';
			if (me.stringSearch(text, neg_term) != '-1') 
			{ 
				colour_class = 'neg'; me.addNegative(); 
			} 
			else 
			{ 
				colour_class = 'pos'; me.addPositive(); 
			};
			var new_text = me.stringReplace(text, colour_class);
			$('#results').prepend('<div class="result" id="'+id+'"><a target="_blank" href="http://twitter.com/'+user+'/statuses/'+id+'"><img param="http://twitter.com/'+user+'/statuses/'+id+'" src="'+avatar+'" /></a><p>'+new_text+'</p></div>');
			$('#'+id).slideDown('slow');
			if (curr_display.push(id) > max_display)
			{
				var old_id = curr_display.shift();
				$("#"+old_id).hide('fast');
			}

		};
	
	me.destroyResults = function() { results = new Array(); };
	
	me.addCurrent = function() { curr++; };
	me.getCurrent = function() { return curr; };
	
	me.hideLoader = function() { $('.search_button').attr('src', '/assets/images/searchbutton-bluetext.jpg').css('margin-bottom', '-17px');  $('#message').hide(); };
	
	me.showLoader = function() { $('.search_button').attr('src', '/assets/images/load.gif').css('margin-bottom', '-13px'); $('#message').show(); };
	
	me.showScoreArea = function() { $('.score_area').show(); };
	
	me.stringReplace = function(txt, colour_class) { 

		var e_txt = escape(txt);
		e_txt = e_txt.replace('%26amp%3B', '%26');
		
		var r = new RegExp(escape(brand), 'i'); 

		var fix = e_txt.replace(r, '<span class="'+colour_class+'">'+brand+'</span>'); 
		return unescape(fix);
	};

	me.stringSearch = function(text, neg_term) { var r = new RegExp(neg_term, 'i'); return text.search(r); };
	
	me.isLoading = function() { return loading; };
	

}

