$(document).ready(function() {
      
			var showdate = true;
			
			var first = true;
			
			var banned = ['fred', 'bob','caliasmvn', 'valenkaeraw9','colleenjuoao8']; //list of banned twitter usernames from the feed
			
			
	function parseTwitterDate($stamp)
      {		
      // convert to local string and remove seconds and year //
			
			if(showdate){
				 var currentTime = new Date();
      	var date = new Date(Date.parse($stamp))
				
				var minDiff = Math.ceil((currentTime-date)/(60*1000));
				var hourDiff = Math.ceil((currentTime-date)/(60*60*1000));
    		var dayDiff = Math.ceil((currentTime-date)/(24*60*60*1000));
				
				
				if(first && (dayDiff > 7)){
							
							showdate = false;
							
							first = false;
							
							return 'no';
				}
				
				
				
				if(minDiff < 59){
						return "About "+minDiff+" minutes ago";
        }else if(hourDiff < 24){
            return "About "+hourDiff+" hours ago";
        }else if(dayDiff < 5){
            return dayDiff+" days ago";
     		}else{
				
				/*if(minDiff < 59){
						return "about "+minDiff+" minutes ago";
        }else if(hourDiff < 24){
            return "about "+hourDiff+" hours ago";
        }else if(dayDiff < 5){
            return dayDiff+" days ago";
     		}else{*/
				
				
				date = date.toLocaleString().substr(0, 16);		
							
			// get the two digit hour //
      	var hour = date.substr(-5, 2);
      // convert to AM or PM //
      	var ampm = hour<12 ? ' AM' : ' PM';
      	if (hour>12) hour-= 12;
      	if (hour==0) hour = 12;
      // return the formatted string //
      	//return date.substr(0, 11)+' ¥ ' + hour + date.substr(13) + ampm;
				//return date.substr(0, 11)+' ¥ ' + hour + ":"+ date.substr(13) + ampm;
				return date.substr(0, 11)+' ¥ ' + hour + ":"+ date.substr(13) + ampm;
							//return Math.ceil((((currentTime - date) / 1000) /60));
				
				}
			} else {
				return 'no';
			
			}
				
				

				
				
				//'http://search.twitter.com/search.json?callback=?&rpp=5&q=from:portswoodchurch+OR+%40portswoodchurch+OR+pchurch',
				//return date;
      }
		//'http://search.twitter.com/search.json?callback=?&rpp=5&q=%40biz'
		//'http://search.twitter.com/search.json?callback=?&rpp=5&q=from:portswoodchurch+OR+%40portswoodchurch+OR+from:pwstudents+OR+%40pwstudents',
		var needbonus = false;
		
		/*$.getJSON(
			'http://search.twitter.com/search.json?callback=?&rpp=5&q=from%3Aportswoodchurch+OR+from%3Apwstudents+OR+from%3Apwhousegroups+OR+%40portswoodchurch+OR+%40pwstudents+OR+%40pwhousegroups',		
        function(data) {
           
					 
					 $.each(data, function(i, tweets){
					 							if (tweets.length != undefined){
					 								 if (tweets[0] != undefined){
					 								 		if (tweets[0].created_at != undefined){
																
																
																
																
																														
																 for(var i = 0; i < tweets.length; i++){
    																	
																			//document.wrire
																			
																if(i < 4 || needbonus){			
																	if(jQuery.inArray(tweets[i].from_user, banned) == -1){			
																				
																$('#tweeets').append("<div class='atweet'><b>@"+tweets[i].from_user + ":</b>"+tweets[i].text+"</div>");
																				
																	} else {
																	
																	needbonus = true;
																	
																	}
																}
																			
																			//$('#tweeets').append("<div class='tweet'><b><img	src='"+tweets[i].profile_image_url+"' align='left' style='margin-right: 8px; border: 1px solid black;'/>  @"+tweets[i].from_user+"</b>: "+tweets[i].text+"<br><i><small><small>"+parseTwitterDate(tweets[i].created_at)+"</small></small></i></div>");
	
																			
															
																			//$('#tweeets').append("<div class='tweet'><b><img	src='"+tweets[i].profile_image_url+"' align='left' style='margin-right: 8px; border: 1px solid black;'/>  @"+tweets[i].from_user+"</b>: "+tweets[i].text+"<br><i><small><small>"+parseTwitterDate(tweets[i].created_at)+"</small></small></i></div>");
															
															
															   }
 
					 										}
					 								 }
					 						  }
					 });
					 
					 
					 // our code to handle the data here
    		}
		);*/
		
		
		$.getJSON(
    		'http://search.twitter.com/search.json?callback=?&rpp=1&q=from:portswoodchurch',
        function(data) {
           
					 
					 $.each(data, function(i, tweets){
					 							if (tweets.length != undefined){
					 								 if (tweets[0] != undefined){
					 								 		if (tweets[0].created_at != undefined){
															
																	$('#thelatesttweetbox').slideDown();
																														
																 for(var i = 0; i < tweets.length; i++){
    																	
																			//document.wrire
																			
																			$('#thelatesttweet').append(tweets[i].text);
																			
				
															   }
 
					 										}
					 								 }
					 						  }
					 });
					 
					 
					 // our code to handle the data here
    		}
		);
		
		
		
		
});
