﻿/*
 * jQuery Flickr - jQuery plug-in
 * Beta 1, Released 2008.02.26
 *
 * 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_url: null,
        callback: null,
        api_key: "65a0e4cc7fc89e2c51611592196a6c19",
        type: null,         		// allowed values: 'photoset', 'search'
        photoset_id: null,
        user_id: "26104137@N05",
        group_id: null,
        tags: null,         		// comma separated list
        tag_mode: null,     		// allowed values: 'any' (OR), 'all' (AND)
        text: null,					// free text search				
        sort: null,         		// date-posted-asc, date-posted-desc, date-taken-asc, date-taken-desc, interestingness-desc, interestingness-asc, relevance
        thumb_size: '',    			// allowed values: s (75x75), t (100x?), m (240x?)
        size: null,         		// default: (500x?), allowed values: m (240x?),  o (original)
        per_page: null,     		// default: 100, max: 500
        page: null,     			// default: 1
        litebox: false,     		// boolean, if true requires jquery.litebox.js
		slideshow: false,			// boolean, if true return list-element <ul>
		template_directory: false,	// wordpress theme url
		link: true,
		list: true
	};
    if(o) $.extend(s, o);
	return this.each(function(){
		
		if (s.slideshow == true) {
			
			var url = $.flickr.format(s);
			var slideshow = $('<ul id="thumbs">').appendTo(this);
			
			$.getJSON(url, function(r){
				
				// CREATE ARRAY BIG & THUMB IMAGES
				var bigImg = [];
				var biggestImg = [];
				var thumbImg = [];
				var imgTitle = [];
				for (var i=0; i<r.photos.photo.length; i++){
					biggestImg.push('http://farm'+r.photos.photo[i]['farm']+'.static.flickr.com/'+r.photos.photo[i]['server']+'/'+r.photos.photo[i]['id']+'_'+r.photos.photo[i]['secret']+'_b.jpg');
					bigImg.push('http://farm'+r.photos.photo[i]['farm']+'.static.flickr.com/'+r.photos.photo[i]['server']+'/'+r.photos.photo[i]['id']+'_'+r.photos.photo[i]['secret']+'.jpg');
					thumbImg.push('http://farm'+r.photos.photo[i]['farm']+'.static.flickr.com/'+r.photos.photo[i]['server']+'/'+r.photos.photo[i]['id']+'_'+r.photos.photo[i]['secret']+'_'+s.thumb_size+'.jpg');
					imgTitle.push(r.photos.photo[i]['title']);
				}
								
				// GET THUMBNAILS
	            for (var i=0; i<thumbImg.length; i++){
					slideshow.append('<li><a href="'+bigImg[i]+'" title="'+imgTitle[i]+'" onclick="javascript: return false;"><img src="'+thumbImg[i]+'" alt="'+imgTitle[i]+'" id="thumb_'+i+'" /></a></li>');
				}
				
				slideshow.append('<p>Se alla bilder på <a href="http://www.flickr.com/photos/'+s.user_id+'/tags/'+s.tags+'" target="_blank"><img src="'+s.template_directory+'/images/logo-flickr-2.png" width="31" height="10" title="flickr" /></a></p>');
								
				// GET FIRST BIG IMAGE
				$.slideshow.swapImg(biggestImg[0], bigImg[0], imgTitle[0]);
			
				// SET MENU
				$('#thumb_0').click(function () {
					$.slideshow.swapImg(biggestImg[0], bigImg[0], imgTitle[0]);
				});
				$('#thumb_1').click(function () {
					$.slideshow.swapImg(biggestImg[1], bigImg[1], imgTitle[1]);
				});
				$('#thumb_2').click(function () {
					$.slideshow.swapImg(biggestImg[2], bigImg[2], imgTitle[2]);
				});
				$('#thumb_3').click(function () {
					$.slideshow.swapImg(biggestImg[3], bigImg[3], imgTitle[3]);
				});
				$('#thumb_4').click(function () {
					$.slideshow.swapImg(biggestImg[4], bigImg[4], imgTitle[4]);
				});
				$('#thumb_5').click(function () {
					$.slideshow.swapImg(biggestImg[5], bigImg[5], imgTitle[5]);
				});
				$('#thumb_6').click(function () {
					$.slideshow.swapImg(biggestImg[6], bigImg[6], imgTitle[6]);
				});
				$('#thumb_7').click(function () {
					$.slideshow.swapImg(biggestImg[7], bigImg[7], imgTitle[7]);
				});
				$('#thumb_8').click(function () {
					$.slideshow.swapImg(biggestImg[8], bigImg[8], imgTitle[8]);
				});
				$('#thumb_9').click(function () {
					$.slideshow.swapImg(biggestImg[9], bigImg[9], imgTitle[9]);
				});
				
			});
						
		} else {
		
			var url  = $.flickr.format(s);
		
			if(s.list != false){
				var list = $('<ul>').appendTo(this);
			} else {
				var list = $(this);
			}
						    
			$.getJSON(url, function(r){
		        if (r.stat != "ok"){
		        
		        	for (i in r){
						$('<li>').text(i+': '+i[r]).appendTo(list);
		                list.append('<li>'+i+': '+i[r]+'</li>');
		            }
		        
		        } else {
			
					if (s.type == 'photoset'){
			        	r.photos = r.photoset;
			        }
			        for (var i=0; i<r.photos.photo.length; i++){
			        	var photo = r.photos.photo[i];
			            var t = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+'/'+photo['id']+'_'+photo['secret']+'_'+s.thumb_size+'.jpg';
			            var h = 'http://farm'+photo['farm']+'.static.flickr.com/'+photo['server']+'/'+photo['id']+'_';
			            if (!s.size) h += photo['secret']+'.jpg';
			            else if (s.size == 'o') h += photo['originalsecret']+'_o.'+photo['originalformat'];
			            else h += photo['secret']+'_'+s.size+'.jpg';
						if(s.link == false){
							list.append('<img src="'+t+'" alt="'+photo['title']+'" />');
						} else {
							list.append('<li><a href="'+h+'"><img src="'+t+'" alt="'+photo['title']+'" /></a></li>');
						}
					}
			        if (s.litebox) list.litebox();
		            
		        }
			});
			
		}
		
    });
};
$.flickr = {
    format: function(s){
        if (s.url) return s.url;
        if (!s.callback) s.callback = '?';
        var url = 'http://api.flickr.com/services/rest/?format=json&jsoncallback='+s.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';
                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;
                if (s.sort) url += '&sort=' + s.sort;
                break;
            default:
                url += '&method=flickr.photos.getRecent';
        }
        if (s.per_page) url += '&per_page=' + s.per_page;
        if (s.page) url += '&page=' + s.page;
        if (s.size == 'o') url += '&extras=original_format';
        return url;
    }
};
$.slideshow = {
	swapImg: function(big, img, title){
		$('#imagecontainer-big').empty();
		$('#imagecontainer-big').append('<a href="'+big+'" title="'+title+'" target="_blank"><img src="'+img+'" alt="'+title+'" /></a>');
		$('#imagecontainer-big img').width(411);
		/* $('#imagecontainer-big').append('<p>'+title+'</p>'); */
	}
};
})(jQuery);
