// JavaScript Document

function getTwitterPost(){
	$.getJSON("http://search.twitter.com/search.json?q=from%3AbulldogNE&rpp=1&callback=?", 
		function(data){
			handleTwitterJSONResponse(data);
		}
	);
}

function handleTwitterJSONResponse(data){
	// populate post
	if(data.results[0].text){
		$("#twitter_post").html(data.results[0].text);
	}
	if(data.results[0].created_at){
		var __date = new Date(data.results[0].created_at);
		var __dateString = __date.toDateString() + " - " + __date.toLocaleTimeString();
		$("#twitter_timestamp").html(__dateString);
	}
}

function clearText(thefield){
	if (thefield.defaultValue == thefield.value){
		thefield.value = "";
	}
}

function getFoursquareMetrics(){
	$.getJSON("http://www.thebulldognortheast.com/proxy/foursquare/", 
		function(data){
			handleFoursquareJSONResponse(data);
		}
	);
}

function handleFoursquareJSONResponse(data){
	
	if(window.console != undefined){
		window.console.log(data);
	}
	
	
	var __special = "";
	if(data.venue.specials[0] != undefined && data.venue.specials[0].message != ""){
		__special = data.venue.specials[0].message;
	}
	
	var __checkins = "tons of";
	if(data.venue.stats.checkins != undefined && data.venue.stats.checkins != ""){
		__checkins = '<a href="http://foursquare.com/venue/31611" target="_blank">' + data.venue.stats.checkins + '</a>';
	}
	
	var __mayorImageURL = "";
	if(data.venue.stats.mayor.user.photo != undefined && data.venue.stats.mayor.user.photo != ""){
		__mayorImageURL = data.venue.stats.mayor.user.photo;
	}
	
	var __mayorName = "";
	if(data.venue.stats.mayor.user.firstname != undefined && data.venue.stats.mayor.user.firstname != ""){
		__mayorName = data.venue.stats.mayor.user.firstname;
	}
	
	var __string = "";
	
	if(__special != ""){
		__string += "<p>" + __special + "</p>";
	}
	
	__string += "<p>We've had " + __checkins + " check-ins so far!";
	
	if(__mayorImageURL != ""){
		__string += " Use your mobile device to check-in and try to oust our current mayor";
		
		if(__mayorName != ""){
			__string += ", " + __mayorName;
		}
		
		__string += ':</p><img src="' + __mayorImageURL + '" width="75" height="75" alt="' + __mayorName + '" title="' + __mayorName + '"/>';
	}
	else{
		__string += "</p>";
	}
	
	$("#foursquare_message").html(__string);
}