/****************************************

	Food Drop Map
 
 ****************************************/

var map_bullet = new Array();
var i = 0;
var now = new Date();
var next = null;
var last = null;
var cur_stop = null;

$(document).ready(function(){

	$("div.map_bullet").each(function(){
						
		map_bullet[i] = {
			pos : {
				left : $(this).find("span.pos_x").html(),
				top : $(this).find("span.pos_y").html()
			},
			date : $(this).find("span.date").html().replace(/&nbsp;/gi," "),
			dateObj: new Date($(this).find("span.date").html().replace(/&nbsp;/gi," ")),
			location : {
				city : $(this).find("span.city").html(),
				state : $(this).find("span.state").html()
			},
			nbr_trucks: $(this).find("span.nbr_trucks").html(),
			body: $(this).find("span.body").html()
		}		
		
		if(Date.parse(map_bullet[i].date) < Date.parse(now.toString())){
			$(this).addClass("past");
			
			if(last == null || Date.parse(map_bullet[i].date) > Date.parse(map_bullet[last].date)){
				$("div.last").removeClass("last");
				$(this).addClass("last");
				last = i;
			}
			
		} else {
			if(next == null || Date.parse(map_bullet[i].date) < Date.parse(map_bullet[next].date)){
				$("div.next").addClass("future").removeClass("next");
				$(this).addClass("next");
				next = i;
				cur_stop = i;
			} else {
				$(this).addClass("future");
			}
		}
		
				
		$(this).find(".month").html(monthToStr(map_bullet[i].dateObj.getMonth())+", "+map_bullet[i].dateObj.getFullYear());
		$(this).find(".families").html(map_bullet[i].nbr_trucks*400);
						
		$(this).css("left",map_bullet[i].pos.left);
		$(this).css("top",map_bullet[i].pos.top);
		$(this).css("z-index",(map_bullet[i].pos.top+100));
		$(this).fadeIn("slow");
					
		i++;
		
	});
	

	$("div.map_bullet").live("hover", function(){
		$(this).find("div.bubble").show();
		$(this).addClass("active");
	});
	
	$("div.map_bullet").live("mouseout", function(){
		$(this).find("div.bubble").hide();
		$(this).removeClass("active");
	});
	
	
	if($("div.map_bullet").length > 0){
		/* Generate Latest Stop stats*/
		$("#stop_stats").find(".city").html(map_bullet[next].location.city);
		$("#stop_stats").find(".state").html(map_bullet[next].location.state);
		$("#stop_stats").find(".stats").append("<li>"+map_bullet[next].nbr_trucks*400+" families to be helped</li>");
		$("#stop_stats").find(".stats").append("<li>"+map_bullet[next].nbr_trucks+" trucks</li>");
		$("#stop_stats").find(".stats").append("<li>"+monthToStr(map_bullet[next].dateObj.getMonth())+", "+map_bullet[next].dateObj.getFullYear()+"</li>");
		
		/* Previous and next buttons */
		$("#latest_stop a.prev").live("click", function(){																
			if(cur_stop > 0){
				$("div.map_bullet.current").removeClass("current");
				cur_stop--;
				$("#stop_stats").find(".city").html(map_bullet[cur_stop].location.city);
				$("#stop_stats").find(".state").html(map_bullet[cur_stop].location.state);
				$("#stop_stats").find(".stats").html("<li>"+map_bullet[cur_stop].nbr_trucks*400+" families to be helped</li><li>"+map_bullet[cur_stop].nbr_trucks+" trucks</li><li>"+monthToStr(map_bullet[cur_stop].dateObj.getMonth())+", "+map_bullet[cur_stop].dateObj.getFullYear()+"</li>");
				$("div.map_bullet:eq("+cur_stop+")").addClass("current");
				
				
				// <img src="http://www.feedthechildren.org/images/afa/home/header-were_coming.gif" />
				
				
				if(cur_stop < next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-previous_city.gif\" />");
				}
				else if(cur_stop == next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-were_coming.gif\" />");
				}
				else if(cur_stop > next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-upcoming_city.gif\" />");
				}
				
				
			}
		});
		$("#latest_stop a.next").live("click", function(){
			if(cur_stop < map_bullet.length-1){
				$("div.map_bullet.current").removeClass("current");
				cur_stop++;
				$("#stop_stats").find(".city").html(map_bullet[cur_stop].location.city);
				$("#stop_stats").find(".state").html(map_bullet[cur_stop].location.state);
				$("#stop_stats").find(".stats").html("<li>"+map_bullet[cur_stop].nbr_trucks*400+" families to be helped</li><li>"+map_bullet[cur_stop].nbr_trucks+" trucks</li><li>"+monthToStr(map_bullet[cur_stop].dateObj.getMonth())+", "+map_bullet[cur_stop].dateObj.getFullYear()+"</li>");
				$("div.map_bullet:eq("+cur_stop+")").addClass("current");
				
				
				if(cur_stop < next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-previous_city.gif\" />");
				}
				else if(cur_stop == next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-were_coming.gif\" />");
				}
				else if(cur_stop > next){
						$("#stop_stats").find(".header").html("<img src=\"http://www.feedthechildren.org/images/afa/home/header-upcoming_city.gif\" />");
				}
				
			}
		});
	}
	
	
});



/****************************************

	Facebook Status Feed
 
 ****************************************/
/*

$(document).ready(function(){

	$.spark.request({
						 
		// http://www.comsmart.org/StatusExport/rss.php?uid=100000885717740
		// http://feed43.com/feedthechildren.xml
							 
		//url : "http://feed43.com/feedthechildren.xml", 
		url : "http://www.facebook.com/feeds/page.php?format=json&id=21455818797",
		loading : function(o){		
			$("#facebook_feed .feed").html("<b>Loading...</b>");
		},
		success : function(o){
			
			if(!o || !o.rss || !o.rss.channel || !o.rss.channel.item){
				$("#facebook_feed .feed").html("Unable to load Facebook updates.");
			} else {
			
				$("#facebook_feed .feed").html("");
			
				if(is_array(o.rss.channel.item)){
					for(i = 0; i <= 1; i++){
						//var timestamp = new Date(o.rss.channel.item[i].pubDate);
						$("#facebook_feed .feed").append("<div class=\"item\">"+replaceAll(o.rss.channel.item[i].description, '<p><sub><i>-- Delivered by <a href="http://feed43.com/">Feed43</a> service</i></sub></p>', ''));
					}
				} else {
					//var timestamp = new Date(o.rss.channel.item.pubDate);
					$("#facebook_feed .feed").append("<div class=\"item\">"+replaceAll(o.rss.channel.item.description, '<p><sub><i>-- Delivered by <a href="http://feed43.com/">Feed43</a> service</i></sub></p>', ''));
				}
				
			}

		}
		
	});

});

*/


/*
$(document).ready(function(){
	
	$("#facebook_feed .feed").append("<div class=\"item\">No matter the weather, our Americans Feeding Americans Caravan still feeds families! Today, we&rsquo;re in The Windy City helping 3,200 families!</div>");
	$("#facebook_feed .feed").append("<div class=\"item\">Americans Feeding Americans Caravan stops in Chicago tomorrow! <a href=\"http://bit.ly/bGjEF1\" target=\"_blank\">http://bit.ly/bGjEF1</a></div>");
	
});
*/



/****************************************

	Twitter Status Feed
 
 ****************************************/
 
$(document).ready(function(){
	
	getTwitters('twitter_feed-feed', { 
		id: 'feedthechildren', 
		count: 2, 
		enableLinks: true, 
		ignoreReplies: true, 
		clearContents: true,
		template: '"%text%" <a href="http://twitter.com/%user_screen_name%/statuses/%id%/" class="timestamp">%time%</a>'
	});

});

/* 


*/
function replaceAll(haystack, needle, replace) {
  return haystack.replace(new RegExp(needle, 'g'),replace);
}
function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}
function monthToStr(mo, long_format){
	
	long_format = long_format || true;
	
	switch(mo)	{
		case 0:
			long = "January";
			short = "Jan.";
			break;
		case 1:
			long = "February";
			short = "Feb.";
			break;
		case 2:
			long = "March";
			short = "Mar.";
			break;
		case 3:
			long = "April";
			short = "Apr.";
			break;
		case 4:
			long = "May";
			short = "May";
			break;
		case 5:
			long = "June";
			short = "Jun.";
			break;
		case 6:
			long = "July";
			short = "Jul.";
			break;
		case 7:
			long = "August";
			short = "Aug.";
			break;
		case 8:
			long = "September";
			short = "Sept.";
			break;
		case 9:
			long = "October";
			short = "Oct.";
			break;
		case 10:
			long = "November";
			short = "Nov.";
			break;
		case 11:
			long = "December";
			short = "Dec.";
			break;

		default:
			return false;
			
	}
	
	if(long_format == true){ return long; } else { return short; }
	
	
}
