// flicker jquery gallery... woot
/* 
Reads a flickr account's public photos, and creates a gallery
*/
// rj pittman phiberart@gmail.com
// October 03 2008
// polkadot peeps

// flickr args
var apiKey 	= "2ce9f192987a3f6dcc529849a85f12f2";
var userID 	= "31118919@N05";
var perPage 	= ""; // leave blank for all images... lots may cause animation lag

// ie bug calls onready 2x....
var loaded = false;

// JSON Data Containers
var jsonData;

// time to wait in between adding thumbs
var loadTimeout 	= 0;

// dom ready... 
$(document).ready(function() {
	// http://api.flickr.com/services/feeds/photos_public.gne?format=json&id=30027883@N03&jsoncallback=?
	// http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=02803d51e958d4b9c8048bc6a6c79108&user_id=30027883%40N03&api_sig=6db42326e5bdd153b05946cc0e178c45
	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key="+ apiKey +"&per_page="+ perPage +"&user_id="+ userID +"&format=json&jsoncallback=?", function(data){
		jsonData = data;
		$.preload([ "img/loading.gif" ], {
			onFinish:function(){
				if(loaded==false){
					loadImg(0);
					loaded = true;
				}
			}
		});
	});
});

// loads an image from the json array
function loadImg(id){
	var thisImg = jsonData.photos.photo[id];
	var imgURL = "http://farm"+ thisImg.farm + ".static.flickr.com/"+thisImg.server+"/" + thisImg.id + "_" + thisImg.secret;
	
	$("#flickr").append("<li id=\"img" + id + "\"></li>");
	
	$("#img" + id).append("<a href=\""+imgURL+ ".jpg"+"\" title=\""+thisImg.title+"\"></a>");
	
	$("#flickr li a").click(function(){
		return false;
	});
	
	$("#img" + id).hide().fadeIn("fast", function(){
		$("#img" + id + " a").hide().append("<img src=\""+ imgURL+ "_s.jpg" +"\" alt=\""+thisImg.title+"\" /></a>");
		$.preload([ imgURL ] , {
			onFinish:function(){
				$("#img" + id + " a").fadeIn("fast");
			}
		});		
	});

	if(eval(id+1) < jsonData.photos.photo.length && (eval(id+1) < perPage || perPage == "")){
		setTimeout(function(){
			loadImg(eval(id+1));
		}, loadTimeout);
	}else{
		$("#flickr li a").lightBox();
	}
}

