(function($){

    $.fn.flickr = function(options)
    {
        var o = $.extend($.fn.flickr.defaults, options);

        return this.each(function(){
            var thisCache = this;

            // Request the JSON and process it
            $.ajax({
                type:'GET',
                url:"http://api.flickr.com/services/feeds/photos_public.gne",
                data:"id="+o.flickrID+"&lang=en-us&format=json&jsoncallback=?",
                success:function(feed) {
                    // Create an empty array to store images
                    var thumbs = [];

                    // Loop through the items
                    for(var i=0, l=feed.items.length; i<l; ++i)
                    {
                        // Manipulate the image to get thumb and medium sizes
                        var title = feed.items[i].title,
                            hide = i>=o.displayNum ? ' style="display: none;"' : '';
                            markup = feed.items[i].media.m.replace(
                                    /^(.*?)_m\.jpg$/,
                                    '<li' + hide
                                        + '><a href="$1.jpg">'
                                        + '<img src="$1_s.jpg" alt="'
                                        + title + '" /></a></li>'
                                );

                        // Add the new element to the array
                        thumbs.push(markup);
                    }

                    // Display the thumbnails on the page
                    $(thisCache).html(thumbs.join(''));

                    // Fire the callback
                    o.callback(thisCache);
                },
                dataType:'jsonp'
            });
        });
    }

    $.fn.flickr.defaults = {
        "flickrID" : "29080075@N02",
        "displayNum" : 9,
        "callback" : function(el){}
    };

})(jQuery)

