function initWGFlickrGallery()
{
	var flickrGalleryWrapper = jQuery('#WG_FlickrGallery');
	if (flickrGalleryWrapper.size() == 0) return;
	
	// read and parse XML data
	 jQuery.ajax({
		 type: "GET",
		 url: '/brian/whatgives/photos_public.php',
		 dataType: 'xml',
		 cache: false,
		 success: function(xml)
			{				
				var w = jQuery(xml);				
				var RSSentries = w.find('entry');
				var RSSEntriesNo = RSSentries.size();
				
				var flickrGallerySlideWrapper = jQuery('<ul></ul>');
				flickrGallerySlideWrapper.appendTo(flickrGalleryWrapper);
				RSSentries
					.each(function(index)
					{
						var entry = jQuery(this);	
						var entryDescription = new String(entry.find('content').text());
										
						entryDescription = entryDescription.replace(/&gt;/mig, '>');
						entryDescription = entryDescription.replace(/&lt;/mig, '<');
						
						var entryHTML = jQuery('<li class="WG_FlickrGalleryItem" style="visibility: hidden"></li>');
						entryHTML.appendTo(flickrGallerySlideWrapper);
						
						entryHTML.html(entryDescription);
						entryHTML.find('p:first').remove();
					
						entryHTML.append('<p class="WG_FlickrGalleryItemTitle">' + entry.find('title').text() + '</p>');
						
						entryHTML.find('a:has(img)')
							.attr('href', entry.find('link[rel=enclosure]').attr('href'));
						
						entryHTML.css('visibility', 'visible');
					});
					
				activateWGFlickrGallery();
					
			 },
			 error: function(err, opt, errM)
					{
						alert(opt);
						//console.log('error');
					}
	 });
}

function activateWGFlickrGallery()
{	
	initWGMiniPhotoGallery();
	initWGPhotoGallery();
}

function initWGMiniPhotoGallery()
{
	jQuery('#WG_FlickrGallery>ul')
		.cycle({
			       random: 1,
			       pause: 1,
			       timeout: 8000
		       });
}

function initWGPhotoGallery()
{	
	var galleryOverlay = jQuery('<div class="WG_PhotoGalleryOverlay" id="WG_PhotoGalleryOverlay"></div>');
	galleryOverlay
		.html('<a class="prev">prev</a><a class="next">next</a><div class="info"></div><div class="progress"></div>')
		.appendTo('body');
		
	
	var gallery = jQuery('#WG_FlickrGallery');	
	gallery.find('a:has(img)')
		.click(function(){jQuery('#WG_FlickrGallery>ul').cycle('pause');})
		.overlay({
			target: galleryOverlay,
			expose: '#f1f1f1'
		})
		.gallery({speed: 800, autohide: false, opacity:1});
}

jQuery(function(){
	initWGFlickrGallery();
})
