$(document).ready(function(){
	
	$("#tastenotes_box_inside").corner();
	
	
	$("#submit").click(function (){
		if ($("#q").length == 0)
		{
		
		}
		else
		{
			
			$("#tweetSearch").fadeIn("slow");
			$("#search_results_feed").html("<div class='loading'><img src='/images/loader.gif></div>");
			var args = $("#search").serialize();
			$.ajax({
				url: "/twitter_stream/ttl_search.php",
				type: "GET",
				data: args,
				dataType: "json",
				error: function(html)
				{
					alert("Oops! Something went horribly wrong!. Please refresh the page and try again.");
				},
				success: function(html)
				{
					$(".loading").remove();
					
					if (html.results.length == 0)
					{
						$("#search_results_feed").html("<li>No Results Found!</li>");
					}
					else
					{
						$((html.results).reverse()).each(function(i, items){
						
							if (items.replyID == 0)
							{
								var profile_url = web_root + "users/" + items.screen_name;
								var reply = "";		
							}
							else
							{
								var profile_url = web_root + "users/" + items.screen_name;
								var user_profile = web_root + "users/" + items.replyUser;
								var reply = 'in reply to <a href="'+user_profile+'">' + items.replyUser + '</a></span></div>';
							}
						
							var id_of_li = "searchstatus_"+items.id;
						
							var actions = "";
							
							/*if (logged)
							{
								actions = '<ul class="actions"><li><a href="#" class="reply"><img src="images/reply.png" align="absmiddle" /><span>Reply</span></a></li><li><a href="#" class="retweet"><img src="images/retweet.png" align="absmiddle" /><span>Retweet</span></a></li></ul>';
							}*/
							
							var time_stamp = items.timestamp;
							
							var time_stamp_json = '{"timestamp":"'+items.timestamp+'"}';
				
						
							var html = '<li id="'+ id_of_li + '" class="search_status">'+
									 '<span class="tweet_avatar">'+
										'<a href="'+profile_url+'"><img height="49" width="49" src="'+items.profile_image_url+'"></a>'+
									'</span>'+
									'<span class="body_text">' +
									 '<span class="tweet_user"><a href="'+profile_url+'">'+
										 '<strong>' + items.screen_name + '</strong></a>'+
									  '</span>'+
									  '<span class="tweet_text">'+items.text+'</span>'+
									  '<span class="tweet_info">'+
										'<span class="tweet_timestamp" data="'+time_stamp+'">'+relativeTime(items.timestamp)+'</span>'+
										 '<span class="source"> from '+items.source+' ' + reply + '</span>'+
										'</span>'+
										'</span>'+
									 '</li>';
				
							$("#search_results_feed").prepend(html);
					
						});
						
						var total_returned = html.total_returned;
						
						var args = $("#search").serialize();
						
						var targetpage = "search?"+args;
						
						var argss = "targetPage="+targetpage+"&totalPage="+total_returned;
						
						$.ajax({
							url: "../data/displayPaginate.php",
							type: "GET",
							data: argss,
							error: function(html)
							{
								// error goes here
							},
							success: function(html)
							{
								$(".paginagte_here").html(html);
							}
						});
					}
					
					$(".tweet_timestamp").each(function (){
						var start_time = $(this).attr("data");
						var dr = relativeTime(start_time);
						$(this).html(dr);
					});	
					
					newWindow();
					
					var term = $("#q").val();

				}
			});
			
			return false;
		}
	});
	
	
	
});
