// Credit to: http://phpcamp.net/toolbar/a-jquery-twitter-ticker

$(document).ready(function(){
  var fileref = document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", "http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key=4a4cd7a770cd615a69080e958a339dea&user_id=35009491@N08&per_page=15&page=1&format=json&jsoncallback=flickr.ticker");
  document.getElementsByTagName("head")[0].appendChild(fileref);
});

flickr = {
  photos : [],
  counter : 0,
  container : null,
  
  ticker : function(ob) {
    this.container = $('#flickr-feed');
    this.photos = ob.photos.photo
    this.container.innerHtml = '';
    this.nextPhoto();
    setInterval("flickr.nextPhoto()", 6000);
  },
  
  nextPhoto : function() {
    var photo = this.photos[this.counter];
    this.container.append(this.createPhotoDiv(photo));
    $('#flickrphoto-'+photo.id).fadeIn('slow');
    if (this.container.children().length > 1) {
      $(this.container.children()[0]).fadeOut('slow', function() {
        $(this).remove();
      });
    }
    this.counter++;
    if (this.photos[this.counter] === undefined) { this.counter = 0; }
  },
  
  createPhotoDiv : function(photo) {
    var url = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + '_m.jpg';
    return '<div class="photo" style="display: none;" id="flickrphoto-' + photo.id + '">\
    <img src="'+url+'" />\
    </div>';
  },
};
