/*
 * jQuery flickerGallery 0.2.1 - Awesome? Yes.
 * 
 * This is only for people who cant use a better method (like dynamically 
 * retrieving and caching)
 *
 * Copyright (c) 2008 Greater Good, Inc (wearegreatergood.com)
 *
 */
;(function($){
  var API_KEY = 'dcb0c1a2374c0f02f0b4c7dc1efdea5f';
  var API_URL = 'http://api.flickr.com/services/rest/?format=json&jsoncallback=?&api_key=' + API_KEY;
  var USER_ID = '32626067@N08';
  var SET_ID  = '72157609439034635';
  
  $.fn.flickrGallery = function(){
    var gallery = this;
    var call = API_URL + '&method=flickr.photosets.getPhotos&per_page=500&user_id=' + USER_ID + '&photoset_id=' + SET_ID;
    
    // grab public photos for the selected user:
    $.getJSON(call, function(photosetData){
      
      // get details on each photo
      $.each(photosetData.photoset.photo.reverse(), function(i, photo){
        call = API_URL + '&method=flickr.photos.getInfo&photo_id=' + photo.id + '&secret=' + photo.secret;
        
        var thumb       = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_s.jpg';
        var image       = thumb.replace(/_s/g, '');
        var description = '';
        
        // grab the desc
        $.getJSON(call, function(photoData){
          var description = photoData.photo.description._content.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '');
        }); // end json call
        
        // build the gallery
        $(gallery).prepend($('<a />').attr({
          href: image,
          title: description,
          'class': 'lightbox',
          rel: 'lightbox'
        }).html($('<img />').attr('src', thumb)));

        $('#gallery a').lightbox();        
      }); // end each
    }); // end json call
  } // end $.fn.flickrGallery
})( jQuery );
