
//$ namespace for jquery
(function($){
	//onload function 
	$(function() {
		$('.flickr-photoset').flickrAlbum();
		
		$('.flickr-photoset a').live('click', function () {
			flickr.photo.popup($(this));
			return false;
		});
		
		$('<div id="flb-photo-wrapper"><div id="flb-photo-container"><img id="flb-photo" src="" /><p id="flb-photo-title"></p></div></div>')
			.appendTo('body');
		$('#flb-photo-wrapper').dialog({
			autoOpen: false,
			width: 'auto'
		});
	});
	
	//flickr photoset plugin
	$.fn.extend({ 
		flickrAlbum: function(options) {
			//Settings list and the default values
			var defaults = {};
			
			var opts = $.extend({}, defaults, options);
			
			return $(this).each(function () {
				$album = $(this);
				
				flickr.photoset.load($album);
			});
    	}
	});
})(jQuery);

window.flickr = window.flickr || {};

(function($) {
	var pub = flickr.photoset = {};
	
	var settings = {};
	var photosets = {};
	
	pub.load = function($album) {
		//get the photoset id. prepended with 'photoset-'
		var photosetID = $album.attr('id').substring(9);
		photosets[photosetID] = $album;
		
		pub.construct(photosetID);
	}
	
	pub.construct = function(photosetID) {
		//get the photoset/image data
		$.ajax({
			url: root + 'flickr/flickr_photoset/load/' + photosetID, 
			dataType: 'json',
			type: 'get',
			success: function (response, status) {
				
				//setup the html
				for(i=0;i<response.photoset.photo.length;i++) {
					var link = 'http://farm' + response.photoset.photo[i].farm + '.static.flickr.com/' + response.photoset.photo[i].server + '/' + response.photoset.photo[i].id + '_' + response.photoset.photo[i].secret + '.jpg';
					var url = 'http://farm' + response.photoset.photo[i].farm + '.static.flickr.com/' + response.photoset.photo[i].server + '/' + response.photoset.photo[i].id + '_' + response.photoset.photo[i].secret + '_s.jpg';
					var flink = 'http://flickr.com/photos/' + response.photoset.owner + '/' + response.photoset.photo[i].id + '';
					
					var html = '<a href="' + link + '" rel="' + flink + '"><img src="' + url + '" title="' + response.photoset.photo[i].title + '"/></a>';
					photosets[photosetID].append(html);
				}
				
				photosets[photosetID].css('height', 'auto').css('background-image', 'none');
			}
		});
	}
})(jQuery);

(function($) {
	var pub = flickr.photo = {};
	
	var settings = {};
	var current = {};
	
	pub.popup = function($photo) {
		current = $photo;

		$('#flb-photo').attr('src', ''); //unset src to prevent showing the previous image.
		$('#flb-photo').attr('src', $photo.attr('href'));
		$('#flb-photo-title').html(
			'<b>' + $photo.children('img:first').attr('title') + '</b><br /><a href="' + $photo.attr('rel') + '">View or comment on flickr.com &raquo;</a>'
		);
		
		//open or create
		$('#flb-photo-wrapper').dialog('open', {
			title: $photo.children('img:first').attr('title')
		});
	}
})(jQuery);