$(document).ready(function() {
	$('#nav_crib').click(function() { window.location = 'index.php'; });
	$('#nav_whatsgood').click(function() { window.location = 'whatsgood.php'; });
	$('#nav_findus').click(function() { window.location = 'findus.php'; });
	$('#nav_whatsdoin').click(function() { window.location = 'whatsdoin.php'; });
	$('#nav_parties').click(function() { window.location = 'parties.php'; });

	var vDays   = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
		vMonths = [ 'January', 'February', 'March', 
					'April', 'May', 'June', 
					'July', 'August', 'September', 
					'October', 'November', 'December'],
		vOrdFunc = function (n) {
			n=n+'';
			var l=n.length, r=parseInt(n.substring(l-2,l)), i = n % 10;
			return ((r < 11 || r > 19) && (i < 4)) ? ['th','st','nd','rd'][i] : 'th';
		},
		vLinkHTML = function(rTxt) {
			var vIdx = (rTxt && rTxt.indexOf('http://')),
				vURL, vLink;
			if (vIdx && vIdx !== -1)
			{
				vURL  = rTxt.substr(vIdx);
				vLink = $('<a>').attr('href', rTxt.substr(vIdx)).html('(map)');
				console.log('vLink', vLink);
				console.log( 'cmon', vURL, vLink[0], rTxt.replace( vURL , vLink[0]));
				return vLink[0];
			}
			
		};

	$.ajax({
        url  : "http://api.twitter.com/1/statuses/user_timeline.json",
        data : {screen_name : 'eatcurbside'},
        dataType : 'jsonp',
        success : function(rResponse)
        {   
            console.log('tweets', arguments)
			if (rResponse && rResponse.length)
			{
				var $vUL = $('<ul>'), $vLI, $vDL, $vDT, $vDD, vDate;

				$.each(rResponse, function(vKey) {
					console.log(this, arguments);
					vDate = new Date(this.created_at);
					$vLI = $('<li>');

					$vDL = $('<dl>');
					$vDT = $('<dt>').html(vDays[vDate.getDay()] 
											+ ', ' + vMonths[vDate.getMonth()] 
											+ ' ' + vDate.getDate() + vOrdFunc(vDate.getDate()));
					$vDD = $('<dd>').html(this.text);
					//$vDD.append(vLinkHTML(this.text));

					$vDL[0].appendChild($vDT[0]);
					$vDL[0].appendChild($vDD[0]);

					$vLI[0].appendChild($vDL[0]);
					$vUL[0].appendChild($vLI[0]);
					
					// Just the latest one for now.
					return false;
				});

				$('#loading').hide();
				$('#twitter').append($vUL);
			}
        }
    });
});

