/****************************************************************************
**
**		file: gallery.js
**		version: 1.0
**		author: Ben Belcourt
**		creation date: 07.13.2008
**		dependencies: "jquery-latest.js, common.js"
**
**		This file contains the image gallery functionality for the
**		Building James website
**
****************************************************************************/
	
	/* Image Gallery functions */
	var showGalleries = function (root) {
		var feed = root.feed;
		var entries = feed.entry || [];
		var imageList = [];
		
		if(entries.length > 0){
			for (var i = 0; i < entries.length; i++) {
				var entry = entries[i];
				var title = entry.title.$t;
				var description = entry.media$group.media$description.$t;
				var galleryFeed = entry.link[0].href;
				var galleryID = 'gal'+ entry.id.$t.match(/albumid\/([^?]*)\?/)[1];
				
				imageList.push('<li id="'+ galleryID +'"><h3>' + title + '</h3><p>' + description + '</p></li>');
				
				var galleryImages = $.getJSON(galleryFeed + '&callback=?', buildGallery);
			
			}
		}else{
			$('ul#imageBlock').append('<li>No images at this time.</li>');
		}
		
		$('ul#imageBlock').append(imageList.join(''));

	};
	
	
	var buildGallery = function (root) {
		var feed = root.feed;
		var entries = feed.entry || [];
		var title = feed.title.$t;
		var albumID = feed.id.$t.match(/albumid\/([^?]*)/)[1];
		var galleryID = 'gal'+ albumID;
		var outputStr = '';
		
		if(entries.length > 0){
			//outputStr = '<ul>';
			for (var y = 0; y < 1; y++) {
				var entry = entries[y];
				var thumb = entry.media$group.media$thumbnail[0];
				var image = entry.media$group.media$content[0];
				var imgStr = image.url.match(/(.*\/)([^.]*.jpg)/i);
				var imgURL = imgStr[1] + 's400/' + imgStr[2];
				var tempIndex = '';
				
				/*if ((y % 3) == 0) {
					tempIndex = y+3;
					outputStr += '<li>'
				};*/
					//outputStr += '<a href="'+ imgURL +'"  class="thickbox" title="'+ title +'" rel="'+ galleryID +'"><img alt="" src="'+ thumb.url +'" /></a>';
					outputStr += '<a href="slideshow.php?albumID='+ albumID +'&imgURL='+ imgURL +'&title='+ title +'&KeepThis=true&TB_iframe=true&height=555&width=795"  class="thickbox" title="'+ title +'"><img alt="" src="'+ imgURL +'" /></a>';
				//if (y == tempIndex) { outputStr += '</li>' };
			}
			//outputStr += '</ul>';
		}else{
			$('#'+galleryID).append('<p>No images at this time.</p>');
		}
		
		$('#'+galleryID).append(outputStr);
	};
