flickr = {
	init : function() {	
		photoset_id = $('flickr').getProperty('title');
		
		data = new MooPix().callFlickrUrl({
		    method: 'flickr.photosets.getPhotos',
		    photoset_id: photoset_id
		});						
		// Handle the default callback method from Flickr
		jsonFlickrApi = function(rsp){
			if (rsp.stat == 'ok' ){
				if (rsp){				
					flickr.album(rsp);
				}
			}
		};		
	},	
	
	// local functions	
	album : function(data) {		
		flickr.photoset_data = data; // storing photoset data		
		data.photoset.photo.each(function(value, key) {
				// console.log(value);
				flickr.get_photo_details(value, key);
		});				
	},			
	
	get_photo_details : function(data, key) {
		data = new MooPix().callFlickrUrl({
		    method: 'flickr.photos.getSizes',
		    photo_id: data.id
		});	
							
		jsonFlickrApi = function(rsp){
			if (rsp.stat == 'ok' ){
				if (rsp){				
					// alert(rsp);
					flickr.add_photo(rsp, key);
				}
			}
		};
	},
	
	add_photo : function(data, key) {
		var thumbContainer = {};
		var thumbLink = {};
		var thumbImage = {};		
		
		// Create links to large photos
		thumbContainer[key] = new Element('li', {'class':'loading'}).injectInside('flickr');
			
		thumbLink[key] = new Element('a',{
			'link': data.sizes.size[3].source, //storing value in a temporary property
			'class' : 'galleryImage',			
			'rel' : 'lightbox[gallery]'			
		}).injectInside(thumbContainer[key]);
    

		thumbImage[key] = new Element('img', {
			'height': '75',
			'width': '75',
			'src': data.sizes.size[0].source
		}).injectInside(thumbLink[key]);
		
		
		flickr.init_viewer();
	},

	
	init_viewer : function() {
		// start prettyPhoto view only if number of item equals the photoSet length
		if($('flickr').getElements('li').length == flickr.photoset_data.photoset.photo.length) {			
			$('flickr').getElements('li').each(function(e) { $(e).removeClass('loading'); });			
			$('flickr').getElements('li a').each(function(e) { e.setProperty('href', e.getProperty('link')); }); //fixing HREF
			Lightbox.init();
		}
	}
	
};