   $(function() 
   {
       $("#search_more").click(function() 
       {
       	 
	         var element 	= 	$(this);
	         var msg 			= 	element.attr("href");
	         msg 					= 	msg.replace("#","");
	         var st_or_count  =   msg.split('_');
	         var st_or_count_query = "";
	         msg					=		st_or_count[0];
	         //For all states:
	         var href_val = "_all";
	         var right_url = "/ajax_more";
	         var right_data = "start="+ msg;
	         if (st_or_count[1]!="all")
	         {
	         	 //need to check whether it's numeric or not -> numeric -> county, non-numeric -> state
	         	 if (!isNaN(parseFloat(st_or_count[1])))
	         	 {
	         		  st_or_count_query = "&county_id=" + st_or_count[1];
	         	 }
	         	 else
	         	 {
	         	 		st_or_count_query = "&state=" + st_or_count[1] + "&county_id=All";
	         	 }
	           href_val = "_"+st_or_count[1];
	           right_url = "/ajax_counties";
	           right_data = "start="+ msg + st_or_count_query;
	          // alert(right_data);
	          
	         }	         
	         
	        // alert(st_or_count[0]+',' + st_or_count[1]);
	         if (msg =="") msg=0; 
	         //$("#morebutton").html('<img src="/js/images/ajax-loader.gif" />');
	         $("#search_more").html('Showing more results...');
	           $.ajax(
	           {
	               	type: "POST",
	           			url: right_url,
	           			data: right_data,
	           			cache: false,
	           			success: function(html)
	           			{
										$(".tweetresults").append(html);
	           				$("#search_more").html('more');
	           				start = parseInt(msg)+10;
	           				$("#search_more").attr("href", "#"+start+href_val);

	         				}
	      		});
	       		return false;
      	});
      	
      	$("#filter").click(function() 
        {
        	
        	var state_name  = $('#state option:selected').text();
        	var state_id		= $('#state option:selected').val();
        	var county_name = $('#county option:selected').text();
        	var county_id		= $('#county option:selected').val();
        	//alert(state_id);
        	switch (county_id)
        	{
        		//All States are selected, we're not running the dropdown function
        		case "":
        		  break;
        		
        		//All Counties is selected (All States could also be selected)
        		case "All":
        		  
      	 			 $("#search_more").html("Filtering by All Counties in " + state_name);
        		
        		//A county is selected in a specific state
        		default: 
        		   //\u00a0 => unicode representation of non-breakable space (&nbsp;) => we're removing all instances of it from our string
        		   $("#search_more").html("Filtering by " + county_name.replace(/\u00a0/g,'') + " in " + state_name);
        		
        	}
        	
        	var element 	= 	$("#search_more");
				  var msg 			= 	element.attr("href");
        	//start = parseInt(msg)+10;
					msg 					= 	0;
					var mydata 		= "start=1&state=" + state_id + "&county_id=" + county_id;
					//alert(mydata);
        	switch (state_id)
        	{
		        		case "All":		         
					           $.ajax(
					           {
					               	type: "POST",
					           			url: "/ajax_more",
					           			data: "start="+ msg,
					           			cache: false,
					           			success: function(html)
					           			{
														$(".tweetresults").html(html);
					           				$("#search_more").html('more');  
					           				//start = parseInt(msg)+10; 
					           				$("#search_more").attr("href", "#10_all");				
					         				}
					      		});
					       		return false;
			       	 break;
		       	   	
            default:
			      	 		 $.ajax(
				           {
				               	type: "POST",
				           			url: "/ajax_counties",
				           			data: mydata,
				           			cache: false,
				           			success: function(html)
				           			{
				           				//alert(html2);
				           				
				           				//alert(mydata);
				           				
				           				$(".tweetresults").html(html);
													//$(".tweetresults").append(html);
													if (state_id=="All")
													{
													    $("#search_more").html('more');
													    $("#search_more").attr("href", "#10_all");
													}
													else
													{
														  var loc = county_name.replace(/\u00a0/g,'') + ' in '; 
														  if (county_id=="All")
														  {
														  	loc = "state of ";
														  	$("#search_more").attr("href", "#10_"+state_id);
															}
															else
														  {
														  	$("#search_more").attr("href", "#10_"+county_id);
														  }
															$("#search_more").html('more from ' + loc  + state_name);
													}
				           			 
				         				}
				      		});
				       		return false;
            }
        });
      	
		});

