/*
 * 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 = 'd0eea92430dade9fbab0bc02e6216507';
  var API_URL = 'http://api.flickr.com/services/rest/?format=json&jsoncallback=?&api_key=' + API_KEY;
  var USER_ID = '7744223@N03';
  var SET_ID  = '72157617529532637';
  
  $.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;
        
        // grab the desc and build the gallery
        $.getJSON(call, function(json){
          
          var title       = photo.title;
          var description = json.photo.description._content.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '');
          var thumb       = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_s.jpg';
          var image       = thumb.replace(/_s/g, '_b');

          // build the gallery
          $(gallery).prepend($('<div />').html('<img class="arrowed" src="images/arrow.png" alt="Arrowed!!!" />'))
            .prepend($('<div />').attr({
              id: title
            })
            .prepend($('<h6 />').html(title))
            .prepend($('<img />').attr({
              src: image,
              title: title,
              alt: description
            }))
          ); // end of gallery
                    
        }); // end json call
      }); // end each
    }); // end json call
  } // end $.fn.flickrGallery
})( jQuery );
