Gallery = function(){
	return {
		init: function(){
			Flickr.init();
			
			this.USER_ID = '8056133@N02';
			this.PER_PAGE = 18;
	
			this.date();
			
		},
		
		date: function(){
			$('li.active').removeClass('active');
			$('li#date').addClass('active');
			$('#menu').html("").slideUp(200);
			this.byDate();
		},
		
		albums: function(o){
			if(!o){
				$('li.active').removeClass('active');
				$('li#albums').addClass('active');
				Flickr.call({
					method: 'flickr.photosets.getList',
					user_id: this.USER_ID,
					count: 100,
					jsoncallback: 'Gallery.albums'
				});
			} else {
				$('#menu').html("").slideDown(200);
				var albums = o.photosets.photoset;
				for(var i=0; i<albums.length; i++){
					$('#menu').append(
						'<div class="album" onclick="Gallery.byAlbum(\''
						+ albums[i].id + '\')">' 
						+ '<img src="' + Flickr.url(albums[i],'s','primary') + '"/>'
						+ ' <p>' + albums[i].title._content + '</p>'
						+ ' <h6>' + albums[i].photos + ' photos.</h6>'
						+ "</div>");
				}
				$('#menu').addClass('albums');
			}
		},

		tags: function(o){
			if(!o){
				$('li.active').removeClass('active');
				$('li#tags').addClass('active');
				Flickr.call({
					method: 'flickr.tags.getListUserPopular',
					user_id: this.USER_ID,
					count: 100,
					jsoncallback: 'Gallery.tags'
				});
			} else {	
				$('#menu').html("").slideDown(200);
				var tags = o.who.tags.tag;
				for(var i=0; i<tags.length; i++){
					this.c('menu', 'a', tags[i]._content, {
						style: 'font-size: ' + (.6+(tags[i].count/500)) + 'em;',
						href: "#",
						onclick: "Gallery.byTag('" + tags[i]._content + "')"
					});
					document.getElementById('menu').appendChild(document.createTextNode(' '));
				}
			}
		},	
		
		byDate: function(page){
			var o = {
				method: 'flickr.people.getPublicPhotos',
				user_id: this.USER_ID,
				per_page: this.PER_PAGE,
				jsoncallback: 'Gallery.listTags'
			};
			if(page){
				o.page = page;
			}
			Flickr.call(o);
			this.currentType = 'date';
		},

		byTag: function(tag, page){
			$('li.active').removeClass('active');
			$('li#tags').addClass('active');
			
			var o = {
				method: 'flickr.photos.search',
				tags: tag,
				user_id: this.USER_ID,
				per_page: this.PER_PAGE,
				jsoncallback: 'Gallery.listTags'
			};
			if(page){
				o.page = page;
			} else {
				$("#menu").slideUp(200);
			}
			Flickr.call(o);
			this.currentType = 'tag';
			this.current = tag;
		},
		
		byAlbum: function(album, page){
			var o = {
				method: 'flickr.photosets.getPhotos',
				photoset_id: album,
				user_id: this.USER_ID,
				per_page: this.PER_PAGE,
				jsoncallback: 'Gallery.listAlbums'
			};
			if(page){
				o.page = page;
			} else {
				$("#menu").slideUp(200);
			}
			Flickr.call(o);
			this.currentType = 'album';
			this.current = album;
		},

		view: function(id){
			Flickr.call({
				method: 'flickr.photos.getInfo',
				photo_id: id,
				jsoncallback: 'Gallery.display'
			});
		},

		display: function(o){
			$('#display').show();
			$('#display').html("");
			this.c('display','img',undefined,{
				src: Flickr.url(o.photo)
			});
			this.c('display','div',undefined,{
				id: 'meta'
			})
			var title = o.photo.title._content;
			if(!title.match(/\w+\.\w{3,4}/)){
				this.c('meta','h3',title);
			}
			var desc = o.photo.description._content;
			if(desc){
				this.c('meta','h5',desc);
			}
			var tags = o.photo.tags.tag;
			if(tags.length){
				this.c('meta','div',undefined,{
					id: 'display-tags'
				});
			}
			for(var i=0; i<tags.length; i++){
				this.c('display-tags','a',tags[i]._content,{
					href: '#'
				});
				$('#display-tags a').click(function(){
					Gallery.byTag($(this).html());
				})
			}
		},

		listTags: function(o){
			$('#list').html("");
			var page = o.photos.page;
			var pages = o.photos.pages;
			var p = o.photos.photo;
			if(page != 1)
				this.c('list','div', '< prev', {
					onclick: "Gallery.prevPage()"
				});
			for(var i=0; i<p.length; i++){
				this.c('list','img', undefined, {
					src: Flickr.url(p[i],"s"),
					onclick: "Gallery.view(" + p[i].id + ")"
				});
			}
			if(page < pages)
				this.c('list','div', 'next >', {
					onclick: "Gallery.nextPage()"
				});
				
			this.page = page;
			$("#nav h2").html(o.photos.total + ' photos.');
		},
		
		listAlbums: function(o){
			$('#list').html("");
			var page = o.photoset.page;
			var pages = o.photoset.pages;
			var p = o.photoset.photo;
			if(page != 1)
				this.c('list','div', '< prev', {
					onclick: "Gallery.prevPage()"
				});
			for(var i=0; i<p.length; i++){
				this.c('list','img', undefined, {
					src: Flickr.url(p[i],"s"),
					onclick: "Gallery.view(" + p[i].id + ")"
				});
			}
			
			if(page < pages)
				this.c('list','div', 'next >', {
					onclick: "Gallery.nextPage()"
				});
				
			this.page = page;
			$("#nav h2").html(o.photoset.total + ' photos.');
		},

		prevPage : function(){
			if(this.currentType == 'tag')
				this.byTag(this.current, --this.page);
			else if(this.currentType == 'album')
				this.byAlbum(this.current, --this.page);
			else if(this.currentType == 'date')
				this.byDate(--this.page);
		},
		
		nextPage : function(){
			if(this.currentType == 'tag')
				this.byTag(this.current, ++this.page);
			else if(this.currentType == 'album')
				this.byAlbum(this.current, ++this.page);
			else if(this.currentType == 'date')
				this.byDate(++this.page);
		},

		c: function(parent, tag, text, attrs){
			var t = document.createElement(tag);
			if(attrs){
				for(x in attrs){
					t.setAttribute(x, attrs[x]);
				}
			}
			if(text){
				t.appendChild(document.createTextNode(text));
			}
			document.getElementById(parent).appendChild(t);
			return t;
		}
	}
}();