var api_key = 'e4b47a8ee3c12e26c4b1f263dd9414bb';
var id_quique = '35648822@N08';
var set_id;
var set_title;

$(function(){
	
	$('#thumbs').html('<img src="img/ajax-loader.gif" alt="Loading..." id="loading" /></div>');
	
	// llamada para conseguir todas las fotos del usuario con el tag "inicio"
	$.getJSON("http://api.flickr.com/services/rest/?jsoncallback=?", {
		api_key: api_key,
		method: 'flickr.photos.search',
		format: 'json',
		per_page: '100',
		user_id: id_quique,
		tags: 'inicio',
		media: 'photos',
		//sort: 'date-posted-desc',
		}, function(data){
			$('#thumbs').html('');
			
			//por cada foto hacemos una nueva llamada para obtener el id del set al que pertenece
			$.each(data.photos.photo, function(i,item){
				var img_url = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret;
				var img_prev ='<a href="#'+ item.id +'" title="'+item.title+'"><img src="'+img_url+'_t.jpg" alt="'+item.title+'" /></a>';
				$('#thumbs').append(img_prev);
			});
		}
	);
	
	$('#thumbs a').live('click',function(){
		// al hacer click sobre una foto hacemos llamada para buscar todas las fotos de ese set
		
		var foto_id = $(this).attr('href').substring(1);
		
		$.getJSON("http://api.flickr.com/services/rest/?jsoncallback=?", {
			api_key: api_key,
			photo_id: foto_id,
			method: 'flickr.photos.getAllContexts',
			format: 'json'
		    },
		    function(data){
		    	if(data.set){
		    		$.each(data.set, function(e,set){
						set_id = set.id;
						set_title = set.title;
					});
					
					gerSet(set_id , set_title);
					
		    	} else {
		    		alert('no')
		    	}
		    }
		);
		
		
	})
	
	$('#close').click(function(){
		$(this).hide();
		$('#project').html("");
		$('#description').html("");
	})
	
});



function gerSet(set_id , set_title){
	
	$('#project').html('<img src="img/ajax-loader.gif" alt="Loading..." id="loading" /></div>');
	$('#description').html("");
	$('#close').hide();
	
	
	
	//hacemos una llamada para obtener las fotos del set
	$.getJSON("http://api.flickr.com/services/rest/?jsoncallback=?", {
		api_key: api_key,
		photoset_id: set_id,
		method: 'flickr.photosets.getPhotos',
		format: 'json'
	}, 
	function(data){
		$('#project').html("");
		$.each(data.photoset.photo, function(e,item){
			// construimos la imagen con el link a su set
			var img_url = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret;
			var img_prev ='<img src="'+img_url+'.jpg" alt="'+item.title+'" /><br />';
			$('#project').append(img_prev);
		});
		
		
		$.getJSON("http://api.flickr.com/services/rest/?jsoncallback=?", {
			api_key: api_key,
			photoset_id: set_id,
			method: 'flickr.photosets.getInfo',
			format: 'json'
		}, 
		function(data){
			$.each(data.photoset.description, function(e,item){
				// construimos la imagen con el link a su set
				var set_description = item;
				$('#description').html('<h2>'+set_title+'</h2><p>'+set_description+'</p>');
				$('#close').show();
				$('#project').append('<a href="#">&uarr; arriba</a>');
			});

		});
		
		
		
	});
}

