// reTwitter By David Rencher

function relative_time(time_value) {
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      if(delta < 60) {
      return 'less than a minute ago';
      } else if(delta < 120) {
      return 'about a minute ago';
      } else if(delta < (45*60)) {
      return (parseInt(delta / 60)).toString() + ' minutes ago';
      } else if(delta < (90*60)) {
      return 'about an hour ago';
      } else if(delta < (24*60*60)) {
      return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
      } else if(delta < (48*60*60)) {
      return '1 day ago';
      } else {
      return (parseInt(delta / 86400)).toString() + ' days ago';
      }
    }
	
function thistime(){
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	if (minutes < 10){
	minutes = "0" + minutes
	}
	
	if(hours > 11){
		day_part = "pm";
	} else {
		day_part = "am";
	}
	
	if (hours > 12) {
   hours = hours - 12;
   }

	return ""+month+"-"+day+" "+ hours + ":" + minutes + " "+day_part+"";
}	

function BadWordFilter(sText) {
	var reBadWords = /fuck|shit|asshole|wtf|asskisser|asswipe|b1tch|bastard|bitch|blowjob|crap|cracker|clit|fag|fagget|fagging|faggit|faggot|faggs|fagot|fagots|fags|nigga|nigga|nigger|niggers|pussy|pusse|pussies|pussys|slut|sluts|snatch|sphincter|spic|spic|spick|spik|splooge|spunk|teets|testicles|tits|titties|titty|twat|twaty|spic|spic|spick|spik|wetback|whoar|whore|wop/gi;
	return sText.replace(reBadWords, "****");
}

function create_urls(input){
    return input
    .replace(/(http):\/\/[\S]+(\b|$)/gim, '<a href="$&" target="_blank">$&</a>')
    .replace(/([^\/])(www[\S]+(\b|$))/gim, '$1<a href="http://$2" target="_blank">$2</a>');
} 


function reTwitter(div_id,limit,refresh_time,q,geocode,since_id){


	$.getJSON('http://search.twitter.com/search.json?lang=en&rpp='+limit+'&q='+q+'&geocode='+geocode+'&since_id='+since_id+'&callback=?',
		function (data) {
		the_id = since_id;
		
			if (data.results) {
				
				$.each(data.results, function(i,item){
					
					the_text = BadWordFilter(item.text);
					the_text = create_urls(the_text);
											  
					if(i==0){
						the_id = item.id;
					}
					
					city_state = "";
					if(geocode){
					city_state = item.location;
					}
				
					//date_time = relative_time(item.created_at);
					date_time = thistime();
					from_user = item.from_user;
					
					html = '';
					if(from_user != 'FOX40NOW' && from_user != '16WAPTNews'){
					html = '<div class="tweets" id="'+div_id+'_tweet_'+item.id+'" style="background-color:#FDFEE9;"><div class="tweets_name">'+item.from_user+' <span class="location">'+city_state+'</span>  <span class="tweet_date">'+date_time+'</span></div><a href="http://twitter.com/'+item.from_user+'" rel="nofollow"><img src="'+item.profile_image_url+'" alt="" /></a>'+the_text+'<div class="spacer"></div></div>';	
					}
					
					$('#'+div_id).prepend(html);
					$('#'+div_id+'_tweet_'+item.id+'').animate({backgroundColor: "white"}, {duration: 6000, queue: true });
				});
				
				jQuery.each($("#"+div_id+" .tweets"), function(i) { 
					if(i>limit-1){
						$(this).remove(); 
					}			
				});
		
			}
			
		setTimeout("reTwitter('"+div_id+"','"+limit+"','"+refresh_time+"','"+q+"','"+geocode+"','"+the_id+"')",refresh_time);
	});
}


function re_poll(){
 $(".PDS_Poll a").bind("click", function(){
  setTimeout("re_poll()", 1000);
 });
 $(".pds-return-poll").bind("click", function(){
  window.location.href=window.location.href
 });
}
$(window).load(function () {
 $(".PDS_Poll a").bind("click", function(){
  setTimeout("re_poll()",1000);
 });
});



function mmJax(){
	var then = new Date();
	var themsum = then.getTime();
	var div = 'mmpoll';
		$.ajax({
			type: "GET",
			url: '/wjtv_mascot_vote/poll/'+themsum+'/',
			cache: true,
			  success: function(html){
			    $('#'+div).html(html);
			  }
		});
}
$(window).load(function () {
	var randomnumber=Math.floor(Math.random()*5000);
	setTimeout("mmJax()",randomnumber);
});

(function($){
	$.fn.reorder = function() {
		function randOrd() { return(Math.round(Math.random())-0.5); }
		return($(this).each(function() {
			var $this = $(this),
					$children = $this.children(),
					childCount = $children.length;
			if (childCount > 1) {
				$children.remove();
				var indices = [];
				for (i=0;i<childCount;i++) { indices[indices.length] = i; }
				indices = indices.sort(randOrd);
				$.each(indices,function(j,k) { $this.append($children.eq(k)); });
			}
		}));
};
})(jQuery);



$(function() {
	$(".ad_notice").each(function(){$(this).text('Advertisement');});
});
