/*
	albumDiv = id of div container albums will be put into
	picasaUser = user account
	options:
		albumList = array of albums; format = "[ [albumId, authKey ], ... ]"
		titleMatch = Regular expression to match against album title
*/

function showAlbums( albumDiv, options ) {
	var galleries = $('#'+albumDiv);
	var galIds = new Array();
	var options = $.extend({},{ crop: false, slimBox: true, slideShow: true, titleMatch: '', albumList: [] }, options);
	if( options.titleMatch ) {
		console.log( 'Matching albums with: "'+options.titleMatch+'"' );
		var regExp = eval( '/'+options.titleMatch+'/i');
	}
	
	$.picasa.albums( options.picasaUser, options, function( albums )
	{
		$.each( albums, function( i, album )
		{
			console.log( album.title +' - ' + album.id );
			if( !options.albumList.length || jQuery.inArray( album.id, options.albumList ) == -1 ) return; // We specifiy albums and this isn't one of them
			if( options.titleMatch && !album.title.match( regExp ) ) return;  // Title filter specified and this does not match it
			var galleryId = 'gallery_'+album.id;
			// Create album div
			galleries.append(
			'<div class="galbox"><div id="'+galleryId+'" class="galleries" style="background-image: url('+album.thumb+')"></div><span>'+album.title+'</span></div>\n'
			);
			options.albumId = album.id;
			picasaBox( galleryId, options );
		});
	});
}

// Stuff a bunch if images into target div so the thumb fits
function picasaBox( divId, options )
{
	var options = $.extend({},{ crop: false, slimBox: true, slideShow: true, titleMatch: '', albumList: [] }, options);
	console.log(options);
	var picDiv = $('#'+divId);
	picDiv.addClass('gallery');
	picDiv.css( { overflow: 'hidden', padding: '0px', position: 'relative' } );
	var maxThumb = picDiv.innerWidth();
	if( options.crop ) {
		if( maxThumb < picDiv.innerHeight() ) {
			maxThumb = picDiv.innerHeight();
		} 
		maxThumb = Math.round(maxThumb);
		
	} else if( maxThumb > picDiv.innerHeight() ) {
		maxThumb = picDiv.innerHeight();
	}
	if( !options.thumbsize ) {
		options.thumbsize = Math.round(maxThumb);
		if( options.crop ) options.thumbsize += 'c';
		else options.thumbsize += 'u';
	}
	
	var slideshowLength = 0;
	
	$.picasa.images( options.picasaUser, options.albumId, options, function(images)
	{
		var slideshowLength = images.length;
		$.each( images, function( i, im ) {
			var leftGap = Math.round( (picDiv.innerWidth() - im.thumbX)/2);
			var topGap = Math.round( (picDiv.innerHeight() - im.thumbY)/2);
			picDiv.append(
			'<img src="'+im.thumb+'" alt="'+im.title+'" rel="'+im.url+'" class="ims_'+divId+'" style="width: '+im.thumbX+'px; height: '+im.thumbY+'px; margin-left: '+leftGap+'px; margin-top: '+topGap+'px; "/>\n\n' );
		});
		if( options.slideShow ) {
			startSlides( divId, slideshowLength, options.slimBox, options.slideShow );
		}
	});
}

// Keep trying to start slideshow for selecta images until it detects the right number of images within
function startSlides( divId, images, slimbox, slideshow )
{	
	if( $('#'+divId + ' img').length < images ) {
		console.log( divId + ' only has ' + $(divId + ' img').length + ' images - retrying ..' );
		setTimeout( "startSlides( '" + divId + "', '"+ images + "', '"+slimbox+"', "+slideshow+" )", 500 );
	} else {
		if( slideshow ) {
			$('.ims_'+divId).css({visibility: 'visible'});
			$('#'+divId).cycle({fx: 'fade', slideResize: 0, fit: 0, containerResize: 0 });
		}
		if( slimbox ) {
			if( slideshow ) {
				$('#'+divId).css({background: ''});
			}
			$('.ims_'+divId).slimbox( { loop:true, overlayOpacity: 0.6}, function(el) { return [$(el).attr("rel"), $(el).attr("alt")]; }, 0 );
			$('#'+divId).css( { cursor: 'pointer' } );
			
		}
	}
}
