var photoGallery = new function() {
	var titleCell, galleryCell;
	var galleryItems = new Array();
	var self = this;
	var galleryScriptLoaded = false;
	var carouselScriptLoaded = false;
	var topItem = 0;
	var totalItems = 0;
	var SCROLL_BY = 1;
	var ITEM_HEIGHT = 83;
	var ITEM_OVERLAP = 10;
	var MAX_VISIBLE = 4;
	var ANIM_SPEED = "fast";

	function registerGalleryItem(i) {
		if($('.galleryIntro', $(this).parent()).length == 0) {
			galleryItems.push({title : $('a', this).eq(0).text(), src : $('a', this).eq(0).attr('href'), desc : $('p', this).eq(0).text()});
		}
	}

	function buildGalleria() {
		var htmlItems = $('.galleryIntro', galleryCell);
		galleryCell.empty();
		htmlItems.appendTo(galleryCell);
		var galleryContainer = $('<div id="galleryContainer" style="position: relative;"></div>');
		var thumbnailContainer = $('<ul id="galleryList"></ul>');
		var buttonUp = $('<img id="galleryButtonUp" style="position: absolute; top: 3px; right: 7px;" src="/AM/ClubsNSWGraphics/gallery_up.gif" />');
		var buttonDown = $('<img id="galleryButtonDown" style="position: absolute; bottom: 60px; right: 7px;" src="/AM/ClubsNSWGraphics/gallery_down.gif" />');

		for(i=0; i < galleryItems.length; i++) {
			var imageContainer = $('<li></li>');
			if(i==0) {imageContainer.addClass('active');}

			var img = $('<img />');
			img.attr('src', galleryItems[i].src);
			imageContainer.append(img);

			if(galleryItems[i].desc) {
				img.attr('title', galleryItems[i].desc);
			}

			thumbnailContainer.append(imageContainer);

		}
		galleryContainer.append(thumbnailContainer);
		galleryContainer.append(buttonUp);
		galleryContainer.append(buttonDown);
		galleryContainer.append('<p class="galleryControls"><a class="galleryPrev" href="#" onclick="$.galleria.prev();return false;">&lt;&nbsp;Previous</a><a class="galleryNext" href="#" onclick="$.galleria.next();return false;">Next&nbsp;&gt;</a></p>');
		galleryCell.append(galleryContainer);
		window.setTimeout(startGallery, 100);
	}

	function startGallery() {
		if(!galleryScriptLoaded || !carouselScriptLoaded) {
			window.setTimeout(startGallery, 100);
			return;
		}
		$('#galleryList').galleria({
			history   : false,
			clickNext : false,
			onImage   : function(image){
				if($(image).width() < $(image).height()) { //portrait
   					image.css({width:'auto', height:'386px'});
				}
				image.css({display:'none'}).fadeIn();
			}
		});
		$("#galleryList").wrap('<div id="galleryListScroller" style="margin-top: 15px; height: 355px; float: left; overflow: hidden; position: relative;"></div>');
		$("#galleryList").css({position: "relative"});
		$("#galleryButtonUp").click(scrollUp);
		$("#galleryButtonDown").click(scrollDown);

	}
	
	function scrollUp() {
		topItem = Math.max(0, topItem - SCROLL_BY);
		scrollToCurrent();
	}
	
	function scrollDown() {
		topItem = Math.min(galleryItems.length - MAX_VISIBLE, topItem + SCROLL_BY);
		scrollToCurrent();
	}

	function scrollToCurrent() {
		if (topItem == 0) {
			$("#galleryList").animate({top: '0'}, ANIM_SPEED);
		}
		else if (topItem == (galleryItems.length - MAX_VISIBLE)) {
			var newTop = Math.max(0, $("#galleryList").outerHeight() - $("#galleryListScroller").height());
			$("#galleryList").animate({top: '-' + newTop + 'px'}, ANIM_SPEED);
		}
		else {
			$("#galleryList").animate({top: '-' + (topItem * ITEM_HEIGHT - ITEM_OVERLAP) + 'px'}, ANIM_SPEED);
		}
	}

	// Public Methods
	this.start = function() {
		titleCell = $('.TPHeader').eq(0);
		if(titleCell.length > 0) {
			galleryCell = titleCell.parent().next('tr').children('td').eq(0);
			$(".galleryImage", galleryCell).each(registerGalleryItem);
			if(galleryItems.length > 0) {
				$.ajax({
					type: "GET",
					url: '/AM/ClubsNSWScripts/jquery.galleria.pack.js',
					success: function(){galleryScriptLoaded = true;},
					dataType: "script",
					cache: true
				});
				$.ajax({
					type: "GET",
					url: '/AM/ClubsNSWScripts/jcarousellite_1.0.1.pack.js',
					success: function(){carouselScriptLoaded = true;},
					dataType: "script",
					cache: true
				});
				buildGalleria();
			}
		}
	}

	return this;

}();

document.write('<style type="text/css">.galleryImage{display: none;}</style>');

$('document').ready(photoGallery.start);
