/**
 * Base class, extended by specialised JS objects that may or may not be included
 * 
 * Namespace "Base"
 * @class Base
 * @desc Base class of this project
 */
var Base = function() {
	// VARIABLES DEFINED HERE ARE PRIVATE
	var arr_init = []; // Array containing all registered init-functions of subclasses

	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"> <input type="button" class="prev" value="Vorige" /> <input type="button" class="next" value="Volgende" /> <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));
			});
		});
	}
	
	
	// FUNCTIONS DEFINED HERE ARE PRIVATE
	
	/**
	 * Check and execute registered subclass functions
	 */
	function check_register() {
		for (var i=0; i<arr_init.length; i++) {
			arr_init[i]();
		}
	}
	
	
	return { // Public area
		// VARIABLES DEFINED HERE ARE PUBLIC
		ie6	: ($.browser.msie && ( parseInt($.browser.version)==6 ) ) ? true : false,
		
		// FUNCTIONS DEFINED HERE ARE PUBLIC
		
		/**
		* Register (initialization) calls from subclasses
		* @param (obj_function) Function to initialize subclass
		*/
		register: function(obj_function) {
			arr_init.push(obj_function);
		},
		
		
		/**
		* Initialize this class
		*/
		init: function() {
			check_register();
			initPhotos();
		}
	}
}();

$(document).ready(function(){
	Base.init();
});
