/** 
 * name: json2flickr 
 * date: 2008OCT13
 * programmer: goon
 *      description: parse flickr JSON file and 
 *      array of flickr data to build imag
 */
function json2flickr(o) {
    var i = 0;
    var d = [];
    if (o.stat !== "ok") {
        // something broke!
        return;
    }
    // extact flickr image data from flickr JSON photo object
    // photos
    //     photo
    //         id,farm,secret,title
    // stat
    //     ok
    function ExFlickr(o, i) {
        this.id = o.photos.photo[i].id;
        this.farm = o.photos.photo[i].farm;
        this.secret = o.photos.photo[i].secret;
        this.server = o.photos.photo[i].server;
        this.title = o.photos.photo[i].title;
        // new
        this.tags = o.photos.photo[i].tags;
        this.title = this.title + ": " + this.tags;
        // new 
        this.url = "http://flickr.com/photos/bootload/" + this.id;
        this.alt = this.title + " by bootload";
        this.urlimg = "http://farm" + this.farm + ".static.flickr.com/" + this.server + "/" + this.id + "_" + this.secret + "_s.jpg";
    }  
    // flickr image data ready for to build display
    fid = [o.photos.photo.length];
    for (i = 0; i < o.photos.photo.length; i++) { 
        fid[i] = new ExFlickr(o, i);
    }
    
    return fid;
}   

function json2twit(o) {
    var i = 0;
    var data = [];
    var td = [];
    if (!o) {
        // something went wrong
        return;
    }

    // extraction of twitter data ready 
    // for display
    function ExTwit(o, i) {
        this.id = o[i].id;
        this.created = o[i].created_at;
        this.text = o[i].text; 
        this.url = "<a href='" + "http://twitter.com/bootload/statuses/" + this.id + "' title='" + this.created + " " + this.text + "'>" + "#" + "</a>";
        this.message = this.text + " " + this.url;       
    }
    td = [o.length];
    for (i = 0; i < o.length; i++) {
        td[i] = new ExTwit(o, i);
    }    
    
    return td;
}

$(document).ready(function(){
    // compact display of multiple 
    // data feeds - drops downwards
    /*
    $("#sites").accordion({
        header: 'h4.site',
        event: 'mouseover'
    });
    */
    // extract & build latest flickr images 
    // from preprocessed JSON feed
    $.getJSON("flickr.json",
        function(d) {
            var photos = json2flickr(d);             
            $.each(photos, function (i, p) {
               // console.debug(i, item.url);
                $("<a/>").attr("href",p.url).attr("title",p.title).attr("id","flickr" + i).appendTo("#images"); 
                $("<img/>").attr("src",p.urlimg).attr("alt",p.alt).attr("title",p.title).attr("height",40).attr("width",40).attr("id","flickr").appendTo("a#flickr" + i); 
            });
        });
    $.getJSON("twit.json",
        function(d) {
            var twits = json2twit(d);
            $.each(twits, function (i, t) {
                $("<div/>").attr("id","twit" + i).appendTo("#twits");
                $("div#twit" + i).prepend("<p>" + t.message + "</p>");
            });
    });
});


