﻿/* * jQuery Flickr - jQuery plug-in * Version 1.0, Released 2008.04.17 * * Copyright (c) 2008 Daniel MacDonald (www.projectatomic.com) * Dual licensed GPL http://www.gnu.org/licenses/gpl.html  * and MIT http://www.opensource.org/licenses/mit-license.php */(function($) {$.fn.flickr = function(o){var s = {    api_key: null,              // [string]    required, see http://www.flickr.com/services/api/misc.api_keys.html    type: null,                 // [string]    allowed values: 'photoset', 'search', default: 'flickr.photos.getRecent'    photoset_id: null,          // [string]    required, for type=='photoset'      text: null,                 // [string]    for type=='search' free text search    user_id: null,              // [string]    for type=='search' search by user id    group_id: null,             // [string]    for type=='search' search by group id    tags: null,                 // [string]    for type=='search' comma separated list    tag_mode: 'any',            // [string]    for type=='search' allowed values: 'any' (OR), 'all' (AND)    sort: 'relevance',          // [string]    for type=='search' allowed values: 'date-posted-asc', 'date-posted-desc', 'date-taken-asc', 'date-taken-desc', 'interestingness-desc', 'interestingness-asc', 'relevance'    thumb_size: 's',            // [string]    allowed values: 's' (75x75), 't' (100x?), 'm' (240x?)    size: null,                 // [string]    allowed values: 'm' (240x?), 'b' (1024x?), 'o' (original), default: (500x?)    per_page: 100,              // [integer]   allowed values: max of 500    page: 1,                    // [integer]   see paging notes    // --------------------------------------------    randomize: false,           // [boolean    optional, randomize the array of images (willwade at gmail.com)    cutoff: null,               // [integer]   optional, set a cut off number.. use alongside per_page (willwade at gmail.com)    show_pager: false,          // [boolean]   whether or not to show the pager    // --------------------------------------------    pikachoose: false,          // [boolean]   setup <ul> for use with pikachoose    pika_id: '',                // [string]    the name of the id tag for pikachoose ul    use_pika_dot: false,        // [boolean]   use the circle/dot for thumbnail in pikachoose (if pikachoose enabled)    pika_dot_path: '',          // [string]    path for the pikachoose circle/dot **MUST BE SET IF ABOVE IS TRUE**    pika_start_img: '',         // [string]    path for a custom start image for the list    pika_start_img_caption: '', // [string]    string for caption of start image    pika_end_img: '',           // [string]    path for a custom end image for the list    pika_end_img_caption: '',   // [string]    string for caption of end img    // --------------------------------------------    attr: '',                   // [string]    optional, attributes applied to thumbnail <a> tag    api_url: null,              // [string]    optional, custom url that returns flickr JSON or JSON-P 'photos' or 'photoset'    params: '',                 // [string]    optional, custom arguments, see http://www.flickr.com/services/api/flickr.photos.search.html    api_callback: '?',          // [string]    optional, custom callback in flickr JSON-P response    callback: null              // [function]  optional, callback function applied to entire <ul>    // PAGING NOTES: jQuery Flickr plug-in does not provide paging functionality, but does provide hooks for a custom paging routine    // within the <ul> created by the plug-in, there are two hidden <input> tags,     // input:eq(0): current page, input:eq(1): total number of pages, input:eq(2): images per page, input:eq(3): total number of images        // SEARCH NOTES: when setting type to 'search' at least one search parameter  must also be passed text, user_id, group_id, or tags        // SIZE NOTES: photos must allow viewing original size for size 'o' to function, if not, default size is shown  };  if(o) $.extend(s, o);  return this.each(function(){    // create unordered list to contain flickr images    var obj = $(this);    var obj_id = obj.attr('id');    var loading = $('<div id="flickr_loading">loading...</div>').appendTo(this);    var list = $('<ul>').appendTo(this);    var url = $.flickr.format(s);    $.getJSON(url, function(r){      if (r.stat != "ok"){        for (i in r){          $('<li>').text(i+': '+ r[i]).appendTo(list);        };      } else { // status is okay                if (s.type == 'photoset') r.photos = r.photoset;                // add randomization and cutoff        if (s.randomize) { r.photos.photo.sort(function() {return 0.5 - Math.random()}); };        if (s.cutoff) { d = s.cutoff } else { d = r.photos.photo.length; };                  // add hooks to access paging data        list.append('<input type="hidden" value="'+r.photos.page+'" />');        list.append('<input type="hidden" value="'+r.photos.pages+'" />');        list.append('<input type="hidden" value="'+r.photos.perpage+'" />');        list.append('<input type="hidden" value="'+r.photos.total+'" />');        // ~~~ if using pikachoose and have a special start image        if (s.pikachoose && (s.pika_start_img != '')) {          list.append('<li><img ref="'+s.pika_start_img+'" src="'+s.pika_dot_path+'" alt="'+s.pika_start_img_caption+'" /><span>'+s.pika_start_img_caption+'</span></li>');        }        //for (var i=0; i<r.photos.photo.length; i++){        for (var i=0; i<d; i++){          var photo = r.photos.photo[i];          // format thumbnail url          var t = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+'/'+photo['id']+'_'+photo['secret']+'_'+s.thumb_size+'.jpg';          //format image url          var h = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+'/'+photo['id']+'_';          switch (s.size){            case 'm':              h += photo['secret'] + '_m.jpg';              break;            case 'b':              h += photo['secret'] + '_b.jpg';              break;            case 'o':              if (photo['originalsecret'] && photo['originalformat']) {                h += photo['originalsecret'] + '_o.' + photo['originalformat'];              } else {                h += photo['secret'] + '_b.jpg';              };              break;            default:              h += photo['secret'] + '.jpg';          };          // --------------------------------------------          // if using with pikachoose, setup differently...          if (s.pikachoose) {            // add the id tag to the list            list.attr("id",s.pika_id);            if (s.use_pika_dot){              thumb_src = s.pika_dot_path;            } else {              thumb_src = t;            }            list.append('<li><img ref="'+h+'" src="'+thumb_src+'" alt="'+photo['title']+'" /><span>'+photo['title']+'</span></li>');          } else {            list.append('<li><a rel="group" href="'+h+'" '+s.attr+' title="'+photo['title']+'"><img src="'+t+'" alt="'+photo['title']+'" /></a></li>');          }          // --------------------------------------------        };        // ~~~ if using pikachoose and have a special end image        if (s.pikachoose && (s.pika_end_img != '')) {          list.append('<li><img ref="'+s.pika_end_img+'" src="'+s.pika_dot_path+'" alt="'+s.pika_end_img_caption+'" /><span>'+s.pika_end_img_caption+'</span></li>');        }        // wrap the list in a div for styling, unless using pikachoose        if (!s.pikachoose){          // wrap in a div for styling          list.wrap('<div id="flickr_gal"></div>');          // show a header that details the gallery photos          list.before('<div id="flickr_head"></div>');          var page_head = $('#flickr_head',obj);                    // get the photoset list if user_id is used in search and not using pikachoose          if (s.user_id != null){            var my_albums = $.flickr.albums(s);            page_head.append('<div class="fh_left">View Album: <select id="album_sel" onchange="fg_switcher(\'change\',this.value);return false;"></select></div>');            $('#album_sel',obj).append('<option value="all"'+(s.type!='photoset'?'selected="selected"':'')+'>All Albums</option>');            $.getJSON(my_albums, function (data) {              if (data.stat != 'ok') {                for (i in data){                  $('#album_sel',obj).append('<option value="'+i+'">'+i+': '+data[i]+'</option>');                };              }else{                $.each(data.photosets.photoset, function (i, set) {                  if (s.type='photoset'){                    var is_selected = s.photoset_id==set.id?'selected="selected"':'';                  }                  $('#album_sel',obj).append('<option value="'+set.id+'" '+is_selected+'>'+set.title._content+' ('+set.photos+' image'+(set.photos>1?'s':'')+')'+'</option>');                });              }            });          }                    page_head.append('<div class="fh_center">'+r.photos.total+' image'+(r.photos.total>1?'s':'')+' ('+r.photos.pages+' page'+(r.photos.pages>1?'s':'')+', '+r.photos.perpage+' per page.)</div>');          page_head.append('<div class="fh_right">Page '+r.photos.page+' of '+r.photos.pages+'</div>');          page_head.after('<div class="clear"></div>');        }                // check for showing the pager        if (s.show_pager) {          //Scan script tags and look for path to flickr plugin          var img_path = '';          $('script').each(function(i){            var scr = $(this);            if(scr.attr('src') && scr.attr('src').match(/jquery\.flickr/)){              img_path = scr.attr('src').split('jquery.flickr')[0]+'flickr/';              }          });          // calculate & set some page variables          var pagenum = parseInt(r.photos.page);          var pagetot = parseInt(r.photos.pages);          var pagesize = parseInt(r.photos.perpage);          var pagemin = 0;          var pagemax = 0;          var pageprev = 0;          var pagenext = 0;          // setup the prev/next for the pager          pageprev = pagenum == 1 ? 1 : (pagenum - 1);          pagenext = pagenum == pagetot ? pagetot : (pagenum + 1);          // setup the min/max for the pager          // if there are more than 7 pages, we always want 7 page number links displayed          if (pagetot > 7){            if (pagenum >= 4){              pagemin = pagenum-3;              pagemax = pagenum+3;              if (pagemax > pagetot){                pagemax = pagetot;              }              if (pagemin > (pagemax-6)){                pagemin = pagemax-6;              }              if (pagemin < 1){                pagemin = 1;              }            }else{              pagemin = 1;              pagemax = 7;            }          }else{            pagemin = 1;            pagemax = pagetot;          }          // build the pager          list.after('<div id="flickr_pager"></div>');          var pager = $('#flickr_pager',obj);          pager.append('<form name="pagejump" action="javascript:void();"><table><tr id="pager_row"></tr></table></form>');          $('#pager_row', pager).append('<td id="pager_first" class="previous"></td>');          $('#pager_row', pager).append('<td id="pager_prev" class="previous"></td>');          $('#pager_row', pager).append('<td id="pager_pages" class="pages"></td>');          $('#pager_row', pager).append('<td id="pager_next" class="next"></td>');          $('#pager_row', pager).append('<td id="pager_last" class="next"></td>');          $('#pager_row', pager).append('<td id="pager_jump" class="previous"></td>');          $('#pager_row', pager).append('<td id="pager_size" class="previous"></td>');          $('#pager_first', pager).append('<a href="javascript:void(0);" title="First (pg.1)" onclick="fg_switcher(\'goto\',1);return false;"><img src="'+img_path+'go-first.png" alt="First" /></a>');          $('#pager_prev', pager).append('<a href="javascript:void(0);" title="Previous (pg.'+pageprev+')" onclick="fg_switcher(\'goto\','+pageprev+');return false;"><img src="'+img_path+'go-previous.png" alt="Previous" /></a>');                    // loop through the pages, setting up the links to the next/previous pages          for(i=pagemin;i<=pagemax;i++){            if ((i==pagemin)&&(pagemin>1)){              $('#pager_pages', pager).append('<span class="pager_divider">&nbsp;&nbsp;...&nbsp;</span>');            }            num_class = (i==pagenum)?'page_sel':'page_norm';            $('#pager_pages', pager).append('<a class="'+num_class+'" href="javascript:void(0);" title="Page '+i+'" onclick="fg_switcher(\'goto\','+i+');return false;">'+i+'</a>');            if (i < pagemax){              $('#pager_pages', pager).append('<span class="pager_divider">&nbsp;&nbsp;::&nbsp;</span>');            }            if ((i==pagemax)&&(pagemax<pagetot)){              $('#pager_pages', pager).append('<span class="pager_divider">&nbsp;&nbsp;...&nbsp;</span>');            }          }                    $('#pager_next', pager).append('<a href="javascript:void(0);" title="Next (pg.'+pagenext+')" onclick="fg_switcher(\'goto\','+pagenext+');return false;"><img src="'+img_path+'go-next.png" alt="Next" /></a>');          $('#pager_last', pager).append('<a href="javascript:void(0);" title="Last (pg.'+pagetot+')" onclick="fg_switcher(\'goto\','+pagetot+');return false;"><img src="'+img_path+'go-last.png" alt="Last" /></a>');          $('#pager_jump', pager).append('Page: <input class="pageme" type="text" size="2" value="'+pagenum+'" name="cpage">&nbsp;/&nbsp;'+pagetot+'&nbsp;&nbsp;');          $('#pager_jump', pager).append('<a href="javascript:void(0);" title="Jump" onclick="fg_switcher(\'goto\',document.pagejump.cpage.value);return false;"><img src="'+img_path+'go-jump.png" alt="Jump" /></a>');          $('#pager_size', pager).append('Show <select class="pageme" id="pg_size_sel" name="perpage" onchange="fg_switcher(\'resize\',this.value);return false;"></select> per page.');          $('#pg_size_sel', pager).append('<option value="10" '+(pagesize==10?'selected="selected"':'')+'>10</option>');          $('#pg_size_sel', pager).append('<option value="20" '+(pagesize==20?'selected="selected"':'')+'>20</option>');          $('#pg_size_sel', pager).append('<option value="30" '+(pagesize==30?'selected="selected"':'')+'>30</option>');          $('#pg_size_sel', pager).append('<option value="50" '+(pagesize==50?'selected="selected"':'')+'>50</option>');          $('#pg_size_sel', pager).append('<option value="100" '+(pagesize==100?'selected="selected"':'')+'>100</option>');          $('#pg_size_sel', pager).append('<option value="200" '+(pagesize==200?'selected="selected"':'')+'>200</option>');          $('#pg_size_sel', pager).append('<option value="500" '+(pagesize==500?'selected="selected"':'')+'>500</option>');        }                // all done, hide the loading message...        loading.css('display','none');        // do the callback        if (s.callback) s.callback(list);      };    });  });};$.flickr = {  // static function to format the flickr API url according to the plug-in settings   format: function(s){    if (s.url) return s.url;    var url = 'http://api.flickr.com/services/rest/?format=json&jsoncallback='+s.api_callback+'&api_key='+s.api_key;    switch (s.type){        case 'photoset':            url += '&method=flickr.photosets.getPhotos&photoset_id=' + s.photoset_id;            break;        case 'search':            url += '&method=flickr.photos.search&sort=' + s.sort;            if (s.user_id) url += '&user_id=' + s.user_id;            if (s.group_id) url += '&group_id=' + s.group_id;            if (s.tags) url += '&tags=' + s.tags;            if (s.tag_mode) url += '&tag_mode=' + s.tag_mode;            if (s.text) url += '&text=' + s.text;            break;        default:            url += '&method=flickr.photos.getRecent';    };    if (s.size == 'o') url += '&extras=original_format';    url += '&per_page=' + s.per_page + '&page=' + s.page + s.params;    return url;  },// format  // static function to format the flickr API url to get a list of photosets for a user  albums: function(s){    if (s.url) return s.url;    var url = 'http://api.flickr.com/services/rest/?format=json&api_key='+s.api_key;    url += '&method=flickr.photosets.getList&user_id='+s.user_id+'&jsoncallback=?';    return url;  } // albums};})(jQuery);