<!--
$(document).ready(function(){
	var twitterq = 'iPhone4';
   	var last_query = '';
	var last_id = '';
	var element = '';
	var limit;
	var played = 1;
	var newQuery = 1;
	var si; // Interval of calling search script
   	var msg = $("#message");
   	var createMsg = "<div id='message' style='display:none'></div>";


	function addTweets(){
		$("#Loading").hide();
		element = $("#t_container > div");
		$(element).each(function(i){
			if($(this).attr("id") > last_id) {
				last_id = $(this).attr("id");
			}
			$(this).slideDown("slow");
			if(i == limit-1) {
				callSearch();
			}
		});
		if($(".spamStats").size() > 0) {
			var st = window.setTimeout(function(){
				$(".spamStats").slideUp('1000');
				$(".spamStats").queue(function(){
					$(this).remove();
					$(this).dequeue();
				});
			},5000);
		}
	}

	function wipeTweets(){
		element = $("#t_container > div");
		$(element).each(function(i){
			$(this).slideUp('100');
			$(this).queue(function(){
				$(this).remove();
				$(this).dequeue();
			});
		});
	}

	function callSearch() {
		si = window.setTimeout(function(){
			goSearch(last_query);
		},10000);
	}
	
	function goSearch(twitterq){
    	window.clearTimeout(si);
    	if(last_query != twitterq) {
    		newQuery = 1;
    		$("#Loading").show();
			wipeTweets();
			last_id = '';
			
    	}
    	else {
    		newQuery = 0;
    		$("#Loading").hide();
    	}
    	last_query = twitterq;
		$.ajax({
			type: "POST",
			url: "search.php",
			cache: false,
			data: "twitterq=" + last_query + "&since_id=" + last_id,
			success: function(html){
				$("#cache").html(html);
				limit = $("#cache > div").size();

				if(limit > 0) {
					if(newQuery == 1) {
						$("#t_container").html(html);
					}
					else {
						if($(".noTweets").size() > 0){
							wipeTweets();
						}
						$("#t_container").prepend(html);
					}
					addTweets();
				}
				else {
					if(newQuery == 1) {
						$("#Loading").hide();
						$("#t_container").html("<div id='0' class='noTweets' style='display:none;'>No tweets found.</div>");
						addTweets();
					}
					$("#Loading").hide();
					callSearch();
				}
			}
		});
		return false;
		
	}
	
	function msgCtrl(html,displayTime) {
    	msg.remove();
		$(createMsg).insertAfter("#Loading");
		msg = $("#message");
		msg.html(html);
		msg.fadeIn(200).delay(displayTime).fadeOut(600);
	}
	
	
	goSearch(twitterq);
    
    $("#stopButton").click(function(){
    	if(this.innerHTML == 'Stop') {
	    	window.clearTimeout(si);
	    	this.innerHTML = 'Play';
	    	played = 0;
	    	msgCtrl("Streaming paused.<br />Thanks for your green mind.",2000);
	    	window.clearInterval(init);
    	}
    	else {
    		this.innerHTML = 'Stop';
    		played = 1;
    		init = window.setInterval(function(){
				scrollBg();
			},scrollSpeed);
	    	msgCtrl("Happy tweet again, hooray!",2000);
    		goSearch(last_query);
    		$("#Loading").show();
    	}
    });

    $("#lastID").click(function(){
    	msgCtrl("The Latest Tweet ID is <p class='small'>"+last_id+"</p>",2000);
    });

    $("#lastQuery").click(function(){
    	msgCtrl("The Latest query is <p class='small'>"+last_query+"</p>",2000);
    });

    $("#Played").click(function(){
    	msgCtrl("$played is "+played+"<p class='small'>0 - Stream/background stopped<br />1 - Streaming, background moving<br />Otherwise, something went wrong!</p>",5000);
    });

	$(".navi_menu").hover(
		function(){
			$(".drop_menu", this).hide();
			$(".drop_menu", this).slideDown("50");
		},
		function(){
			$(".drop_menu", this).fadeOut("50");
		}
	);

	$(".drop_menu > a").click(function(){
		if(played == 0) {
    		init = window.setInterval(function(){
				scrollBg();
			},scrollSpeed);
		}
		played = 1;
		goSearch($(this).attr('qid'));
		$("#stopButton").html("Stop");
		$(this).parent().fadeOut("50");
	});
	
	$("#debug_switch > a").click(function(){
		if($("#debug_menu").css("margin-left") != "0px") {
			/*@cc_on 
			@if (@_jscript)
			$("#debug_menu").animate(function(){
				marginLeft:"0px";
			}, 250);
			@else */ 
			$("#debug_menu").animate({
				marginLeft:"0px",
			}, 250);
			/* @end @*/
			$(this).text('-');
		}
		else {
			/*@cc_on 
			@if (@_jscript)
			$("#debug_menu").animate(function(){
				marginLeft:"-550px";
			}, 250);
			@else */ 
			$("#debug_menu").animate({
				marginLeft:"-550px",
			}, 250);
			/* @end @*/
			$(this).text('+');
		}
	});


});

-->
