/*
	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, imageCaptions: false, slimBox: true, slideShow: true, titleMatch: '', albumList: [], showTitle: true }, options);
	if( options.titleMatch ) {
		conLog( 'Matching albums with: "'+options.titleMatch+'"' );
		var regExp = eval( '/'+options.titleMatch+'/i');
	}
	
	$.picasa.albums( options.picasaUser, options, function( albums )
	{
		$.each( albums, function( i, album )
		{
			////conLog( album.title +' - ' + album.id );
			if( options.albumList.length > 0 && 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 id="'+galleryId+'" class="galleries" style="background-image: url('+album.thumb+')"></div>' );
			if( options.showTitle ) {
				if( options.thumbsize ) var titleWidth = options.thumbsize; else var titleWidth = $("#"+galleryId).width();
				$("#"+galleryId).after('<div class="albumTitle" id="'+galleryId+'_title" style="position: absolute; z-index: 1001; width: '+titleWidth+'px;">'+album.title+'</div>');
				var thisOffset = $("#"+galleryId).offset();
//				conLog( $("#"+galleryId).css("top"));
				$("#"+galleryId+"_title").css( { left: Math.round(thisOffset.left)+"px" } );
			}
			options.albumId = album.id;
			picasaBox( galleryId, options );
		});
	});
}

// Stuff a bunch of images into target div so the thumb fits
function picasaBox( divId, options )
{
	var options = $.extend({},{ crop: false, slimBox: true, slideShow: true, titleMatch: '', albumList: [] }, options);
	var picDiv = $('#'+divId);
	picDiv.css( { overflow: 'hidden', padding: '0px', position: 'relative',	cursor: 'pointer', 'background-position': 'center', 'background-repeat': 'no-repeat', 'overflow': 'hidden' } );
	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( maxThumb > 160 ) maxThumb = 160;
	if( !options.thumbsize ) {
		options.thumbsize = Math.round(maxThumb);
		if( options.crop ) options.thumbsize += 'c';
		else options.thumbsize += 'u';
	}
	else {
		picDiv.css( { width: options.thumbsize+'px', height: options.thumbsize+'px' } );
	}
	// If picasaBox called directly, place caption after 
	if( options.imageCaptions ) {
		var offSet = picDiv.offset();
		picDiv.parent().after( '<div id="imageCaptions_'+options.albumId+'" class="captionStyle" style="position: absolute; z-index: 1000; width: '+Math.round(picDiv.innerWidth())+'px; display: none; left: '+Math.round(offSet.left)+'px; top: '+Math.round(offSet.top+picDiv.height()-60)+'px;"></div>' );
		
	}
	
	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 );
		}
	});
}

// Keep trying to start slideshow for selecta images until it detects the right number of images within
function startSlides( divId, images, options )
{	
	if( $('#'+divId + ' img').length < images ) {
		//console.log( divId + ' only has ' + $(divId + ' img').length + ' images - retrying ..' );
		setTimeout( "startSlides( '" + divId + "', '"+ images + "', '"+options+" )", 500 );
	} else {
		if( options.slideShow ) {
			$('.ims_'+divId).css({visibility: 'visible'});
			var cycleOptions = { fx: 'fade', slideResize: 0, fit: 0, containerResize: 0 };
			if( options.imageCaptions ) cycleOptions = $.extend(
					{},
					{ after: function() { $('#imageCaptions_'+options.albumId).html("<span>"+this.alt+"</span>"); $('#imageCaptions_'+options.albumId).show() } },
					cycleOptions 
					);
			$('#'+divId).cycle( cycleOptions );
		}
		if( options.slimBox ) {
			if( options.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' } );
		}
	}
}
