/*
	picasaViewer v1.0 - mooTools script to embed a picasa gallery in your website
	Lennart Pilon (http://pilon.nl)	
*/

window.addEvent('domready', function() {

	var username	= 'fbe.school';

	/*
	The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
	These images are available as both cropped(c) and uncropped(u) sizes by appending c or u to the size. As an 
	example, to retrieve a 72 pixel image that is cropped, you would specify 72c, while to retrieve the uncropped 
	image, you would specify 72u for the thumbsize or imgmax query parameter values.

	32, 48, 64, 72, 144, 160
	*/
	var thumbsize	= '144c';
	
	/*
	The following values are valid for the thumbsize and imgmax query parameters and are embeddable on a webpage. 
	These images are available as only uncropped(u) sizes by appending u to the size or just passing the size 
	value without appending anything.

	200, 288, 320, 400, 512, 576, 640, 720, 800

	*/
	var imgmax = '800';

	var url = (Browser.getQueryStringValue('albumid')) ? 'http://picasaweb.google.com/data/feed/base/user/'+ username +'/albumid/'+ Browser.getQueryStringValue('albumid') +'?category=photo&alt=json&callback=viewPhotoList&thumbsize=' + thumbsize +'&imgmax='+imgmax : 'http://picasaweb.google.com/data/feed/base/user/' + username + '?category=album&alt=json&callback=viewAlbumList&access=public&thumbsize=' + thumbsize;
	new Element('script', {'src': url}).inject($('photos'));	

});

function viewAlbumList(list) {
	
	href  = list.feed.author[0].uri.$t;
	user = list.feed.author[0].name.$t;
	
	$('navigate').adopt(new Element('a', {'href': href, 'html': '', 'title': 'View '+user+'\'s gallery at Picasa', 'class': 'picasalink'}));

	list.feed.entry.each(function(item) {
		var title	= item.title.$t;
		var summary = item.summary.$t;
		var exp = "<font color=\"#333333\">";/*pattern to find*/
		
		var position_of_exp = summary.lastIndexOf(exp); 
		position_of_exp = position_of_exp + 22; /* position of the first numeric char*/
		var number = summary.charAt(position_of_exp); /*getting first numeric char*/
		var last_number_of_photoes = number.toString(); /*converting to string*/
		/* now the variable "last_number_of_photoes" has the string value - the first numeric char of the number of photoes in the album */
		

		var next_pos = position_of_exp + 1; /* now we move to the next possible numeric char*/
		var next_pos_plus3 = position_of_exp+3; /* now we move to the last possible numeric char*/
		//alert(next_pos);
		//alert(next_pos_plus3);
		
		var quant_pat = /^\d$/; /*pattern of the numeric value*/ 
		for (var i=next_pos; i<next_pos_plus3; i++)
		{
			//alert(i);
			possible_number = summary.charAt(i);
			if ( (possible_number.search(quant_pat)==-1) )
			{
				//alert('not numeric');
			}
			else
			{
				last_number_of_photoes += possible_number;
			}
		}
		//alert(last_number_of_photoes);
		var thumb	= item.media$group.media$thumbnail[0].url;
	
		id_begin	= item.id.$t.indexOf('albumid/')+8;
		id_end		= item.id.$t.indexOf('?');
		var id		= item.id.$t.slice(id_begin, id_end);
		var href	= Browser.setQueryStringValues({'albumid': id});
		d			= item.published.$t;
		var date	= d.substr(8,2) + '-' + d.substr(5,2) + '-' + d.substr(0,4);

		$('photos').adopt(new Element('div', {'class': 'image-container'}).adopt(new Element('a', {'href': href, 'title': title}).adopt(new Element('img', {'src': thumb, 'alt': title}), new Element('p', {'html': title}))));
	}); 
}

function viewPhotoList(list) {
	
	var album = list.feed.title.$t;
	var href  = Browser.setQueryStringValues({'albumid': ''});
	
	$('navigate').adopt(new Element('a', {'href': href, 'html': 'Назад к альбомам', 'class': 'backlink'}), new Element ('br'), new Element('h1', {'html': album}));
	
	list.feed.entry.each(function(item, size) {

		var title	= item.title.$t;
		var link	= item.media$group.media$content[0].url;
		size		= item.media$group.media$content[0].width;
		var thumb	= item.media$group.media$thumbnail[0].url;
		
		id_begin	= item.id.$t.indexOf('albumid/')+8;
		id_end		= item.id.$t.indexOf('?');
		var id		= item.id.$t.slice(id_begin, id_end);
		
		$('photos').adopt(new Element('a', {'href': link, 'title': title, 'class': 'lightbox', 'rel': 'lightbox-album'}).adopt(new Element('img', {'src': thumb, 'alt': title})));
	});

	$$("a").filter(function(el) {
		return el.href && el.firstChild && el.firstChild.src;
	}).slimbox({}, function(el) {	
		return [el.href, (el.title || el.firstChild.alt) + '<br /><a href="' + el.href + '">Album: '+ album +' </a>'];
	});
}