/**
 * Namespace "Subclass"
 * @class Subclass
 * @desc Subclass of Base
 */
Base.Flickr = function() {
	// Variables & functions defined here are private
	
	function showImage(elem) {
		var img = $(elem).find('img');
		var album = $(elem).closest('ul.photos');
		var album_size = $(album).find('a').length-1;
		var img_index = $(album).find('a').index($(elem));
		var img_size = $(elem).attr('class').split(','); 
		
		$(elem).closest('li').addClass('active').siblings().removeClass('active');
		
		$('.photo-enlarged h3').text( $(img).attr('title') );
		$('.photo-enlarged .img').html( '<img src="' + $(elem).attr('href') + '" width="' + img_size[0] + '" height="' + img_size[1] + '" />' );
		
		
		$('.photo-enlarged .prev, .photo-enlarged .next').addClass('disabled');
		
		if (img_index+1 <= album_size) {			
			preloadImage($(album).find('li:eq(' + (img_index+1) + ') a'));
			$('.photo-enlarged .next').removeClass('disabled');
		}
		if (img_index-1 >= 0) {			
			preloadImage($(album).find('li:eq(' + (img_index-1) + ') a'));
			$('.photo-enlarged .prev').removeClass('disabled');
		}
		$(window).trigger('resize');
	}
	
	function preloadImage(elem) {
		var img	= new Image();
		img.src	= $(elem).attr('href'); 
		return img;
	}
	
	function initPhotos() {
		$('ul.photos').each(function(i, album){	
			$(album).find('a').bind('click', function(e){
				e.preventDefault();
				
				infoWindow('<div class="close" title="Sluiten"></div><div class="photo-enlarged"><h3></h3><div class="img"></div><div class="img-nav"><a href="#" class="btn prev"><span><span>&lt;</span></span></a> <a href="#" class="btn next"><span><span>&gt;</span></span></a><p class="copyright"><a href="http://www.flickr.com/">Powered by Flickr</a></p></div></div>');
				
				$('.photo-enlarged .prev').bind('click', function(e){
					e.preventDefault();
					if(!$(album).find('.active').closest('li').is(':first-child')) {
						showImage($(album).find('.active').prev().find('a'));
						$(this).trigger('blur');
					}
				});
				$('.photo-enlarged .next').bind('click', function(e){
					e.preventDefault();
					if(!$(album).find('.active').closest('li').is(':last-child')) {
						showImage($(album).find('.active').next().find('a'));
						$(this).trigger('blur');
					}
				});
				
				showImage($(this));
			});
		});
	}

	return {
  	// Variables & functions defined here are public
		init: function() {
			initPhotos();
		}
	};
}();

/* Initialize this class */
Base.register(Base.Flickr.init);

