(function($){
	
	$.easing.easeOutQuart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};

	$(document).ready(function(){
		var LastImageGallerySlideshow;
		
		loadMap();
		$("a[rel^='tab']").tab();
		
		$('#ImageGallerySlideshow').serialScroll({
			items: 'li',
			offset: 16, //when scrolling to photo, stop 230 before reaching it (from the left)
			start: 0, //as we are centering it, start at the 2nd
			duration: 1200,
			force: true,
			stop: true,
			lock: false,
			cycle: false, //don't pull back once you reach the end
			easing: 'easeOutQuart', //use this easing equation for a funny effect
			exclude: 3,
			lazy: true,
			onBefore:function(e, elem, pane, items, pos){
				if(pos > 0) {
					$('#ImageGallery a.prev').removeClass('disabled');
				}
				else {
					$('#ImageGallery a.prev').addClass('disabled');
				}

				if((pos+this.exclude) >= (items.length-1)) {
					$('#ImageGallery a.next').addClass('disabled');
				}
				else {
					$('#ImageGallery a.next').removeClass('disabled');
				}
			}
		});
		
		$('#ImageGallery a.prev').click(function() {
			$('#ImageGallerySlideshow').trigger('prev');
			return false;
		});
		
		$('#ImageGallery a.next').click(function() {
			$('#ImageGallerySlideshow').trigger('next');
			return false;
		});
	});

	$.fn.tab = function() {
		var allTabs = new Array;
		
		$(this).each(function() {
			allTabs.push(this);
			
			theRel = $(this).attr('rel');
			tabNameRegExp = /\[(.*)\]/;
			tabName = tabNameRegExp.exec(theRel)[1];
			active = ($(this).parent().hasClass('active')) ? true : false;
			
			if(!active) $('#'+tabName).hide();

			$(this).bind('click',function(){
				closeAll();
				open(this);
				return false;
			});
			
		});
		
		function open(elm) {
			theRel = $(elm).attr('rel');
			tabNameRegExp = /\[(.*)\]/;
			tabName = tabNameRegExp.exec(theRel)[1];
			
			$(elm).parent().addClass('active');
			$('#'+tabName).show();
		}
		
		function close(elm) {
			theRel = $(elm).attr('rel');
			tabNameRegExp = /\[(.*)\]/;
			tabName = tabNameRegExp.exec(theRel)[1];

			$(elm).parent().removeClass('active');
			$('#'+tabName).hide();
		}
		
		function closeAll() {
			$(allTabs).each(function(){
				close(this);
			});
		}
	}
})(jQuery);

