var days = [
    "Sun",
    "Mon",
    "Tue",
    "Wed",
    "Thu",
    "Fri",
    "Sat"    
];

var months = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sept",
    "Oct",
    "Nov",
    "Dec"
];

function setupTweet(tweet) {
    tweet = tweet.replace(/#([A-Za-z0-9]+)/g, "<a href=\"http://twitter.com/search?q=%23$1\">#$1</a>");  
    tweet = tweet.replace(/(https?:\/\/\S+)/g, "<a href=\"$1\">$1</a>");
    return tweet.replace(/@([A-Za-z0-9]+)/g,  "<a href=\"http://twitter.com/$1\">@$1</a>");
}

function flickrDataLoaded(data) {
    var imageContainer = $("#flickr .first");
    for(var i in data.photos.photo) {
        var photoData = data.photos.photo[i];
        var title = photoData.title + " &mdash; " + photoData.description._content;
        title = title.replace(/<a.*?href="\S*".*?>(.*?)<\/a>/g, "$1");
        var newImage = $("<a class=\"lightbox\" title=\"" + title + "\" href=\"" + (photoData.url_l || photoData.url_z) + "\"><img src=\"" + photoData.url_sq + "\" height=\"54\" width=\"54\"/></a>");
        imageContainer.append(newImage);
        imageContainer = imageContainer.next();
    } 
    
    $("#flickr a.lightbox").lightBox();
}

$(document).ready(function() {
    var getData = {
        "method": "flickr.people.getPublicPhotos",
        "api_key": "88458c11783d1d2bfefc4ae2e21d077e",
        "user_id": "40034008@N05",
        "extras": "url_sq,url_l,url_z,description",
        "per_page": 5 ,
        "format": "json",
        "jsoncallback": "flickrDataLoaded"  
    };
        
    $.ajax({
        url: "http://api.flickr.com/services/rest/",
        data: getData,
        dataType: "jsonp",
        type: "get"
    });  
    
    
     
    $.getJSON(
       "http://twitter.com/status/user_timeline/jayebirdblue.json?count=1&callback=?",
       function(data) {
            $("#twitter .tweetText").html(setupTweet(data[0].text));
    
            var create_date = new Date(data[0].created_at);
            
            var hours = create_date.getHours();
            if (hours > 11) {
                var ampm = "pm"   
            } else {
                var ampm = "am"
            }
            
            hours = hours % 12;
            
            if (hours == 0) {
                hours = 12;
            }
            
            var minutes = create_date.getMinutes();
            if (minutes < 10) {
                minutes = "0" + minutes;   
            }
            
            $("#twitter .date").text(days[create_date.getDay()] + ", " +
                                     months[create_date.getMonth()] + " " +
                                     create_date.getDate() + " @ " +
                                     hours + ":" + minutes + " " + ampm);
}
    ); 
});
