// jQssp
// (c) Ricardo Tun - Interactive Cozumel
// ricardo[at]interactivecozumel.net
// This code is still in development.
// Feel free to send suggestions, bug reports and other issues.

$(document).ready(function(){
	
	var sspAlbumPhotos = [];
	var i = 0;
	
	$.ajax({
		type: 'GET',
		url: '../ssp/gallery/ssp.xml',
		dataType: 'xml',
		success: function(showGalleries){
			$(showGalleries).find('album').each(function(){
				var gallery_id = $(this).attr('id');
				var gallery_title = $(this).attr('title');
				var gallery_thumb = $(this).attr('tn');
				var gallery_desc = $(this).attr('description');
				var gallery_tnPath = $(this).attr('tnPath');
				var gallery_lgPath = $(this).attr('lgPath');
				$('<div class="galleryThumb"></div>').html('<div class="imgssp"><a href="#gallery-'+gallery_id+'" rel="'+gallery_id+'" class="galleryAlbum"><img src="../ssp/'+gallery_thumb+'" title="'+gallery_desc+'" /></a></div><div class="albumtitle"><a href="#gallery-'+gallery_id+'" rel="'+gallery_id+'" class="galleryAlbum">'+gallery_title+'</a></div>').appendTo('#galleries');
				
				$.ajax({
					type: 'GET',
					url: '../ssp/gallery/'+gallery_id+'/'+gallery_id+'.xml',
					dataType: 'xml',
					success: function(showPhotos){
						$(showPhotos).find('album').each(function(){
							$(this).find('img').each(function(){
							var photo_src = $(this).attr('src');
							sspAlbumPhotos.push(
								{album: gallery_id, tnPath: gallery_tnPath, lgPath: gallery_lgPath, photo: photo_src}
							);
							i++;
							});
						});
					}
				});
			});
		}
	});
	
	$(document).ajaxSuccess(function(){

		$('a.galleryAlbum').live('click', function(){
			$('<div class="galleryFooter"><a class="showGalleries" href="#">&laquo; Albums</a></div>').appendTo('#galleryPhotos');
			var galleryID = $(this).attr('rel');
			j=0;			
			$.each(sspAlbumPhotos, function(k, l){
				if(galleryID == sspAlbumPhotos[k].album){
					$('<div class="galleryThumb"></div>').html('<div class="imgssp"><a href="../ssp/'+sspAlbumPhotos[k].lgPath+sspAlbumPhotos[k].photo+'" class="sspColorbox" rel="gal-'+sspAlbumPhotos[k].album+'"><img src="../ssp/'+sspAlbumPhotos[k].tnPath+sspAlbumPhotos[k].photo+'" /></a></div>').appendTo('#galleryPhotos');
					j++;
				$('.sspColorbox').colorbox();
				}
			});
		
			$('<div class="galleryFooter"><a class="showGalleries" href="#">&laquo; Albums</a></div>').appendTo('#galleryPhotos');
			$('#galleries').hide();
			$('#galleryPhotos').fadeIn('slow');
			return false;
		});
		$('a.showGalleries').live('click', function(){
			$('#galleryPhotos').slideUp('slow').contents().remove();
			$('#galleries').fadeIn('slow');
			return false;
		});
	});
});
