/**
 * @author Chris
 */
var Gallery = {
	_name:"Gallery",
	_image:null,
	_currentImage:0,
	_totalImages:null,
	init:function() {
		Gallery._image = $("<img></img>").attr({src:$(".thumbnail:first").attr('href'),
												alt:$(".thumbnail:first img").attr('alt'),
												id:"image"})
										 .css({maxHeight: "450px",
										 	   maxWidth: "700px"});
		
		$("#caption").html($(".thumbnail:first img").attr('alt'));
		
		$("#images").append(Gallery._image);
		
		$(".thumbnail").click(Gallery.thumbnailEventHandler);
		$("#right-arrow").click(Gallery.rightEventHandler);
		$("#left-arrow").click(Gallery.leftEventHandler);
		
		Gallery._totalImages = $(".thumbnail").size();
	},
	thumbnailEventHandler:function(e) {
		switch(e.type) {
			case 'click':
				$("#image").attr({src:$(this).attr('href'),
								  alt:$(this).attr('alt')});
				$("#caption").html($(e.target).attr('alt'));
				//console.log($("#caption"));
				return false;
				break;
		}
	},
	rightEventHandler:function(e) {
		switch(e.type) {
			case 'click':
				var nextIndex = Gallery._currentImage + 1;
				nextIndex = nextIndex >= Gallery._totalImages ? 0 : nextIndex;
				$(".thumbnail:eq(" + nextIndex + ")").click();
				Gallery._currentImage = nextIndex;
				break;
		}
	},
	leftEventHandler:function(e) {
		switch(e.type) {
			case 'click':
				var nextIndex = Gallery._currentImage - 1;
				nextIndex = nextIndex < 0 ? (Gallery._totalImages - 1) : nextIndex;
				$(".thumbnail:eq(" + nextIndex + ")").click();
				Gallery._currentImage = nextIndex;
				break;
		}
	}
}

Registry.add(Gallery);

