$(document).ready(function(){
	var ua = navigator.userAgent,
		isMobile = ua.match(/iPod|iPhone/i),
		eventType = (isMobile) ? 'touchstart' : 'click';

	$('body').addClass('websites');
	
	if(isMobile) {
		$('#mobilenav h2').bind(eventType, function(){
			$('body').attr('class','');
			var bodyClass = this.innerHTML.toLowerCase();
			$('body').addClass(bodyClass);
		});
	}
	
	// Robert Penners easing equation (http://www.robertpenner.com/easing/).
	$.easing['easeInOutQuart'] = function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	};

	var galleryViewer = {
		carouselObj: null,
		initialized: false,
		options: {
			galleryName: null,
			startSlide: 1
		},
		galleryEl: null,
		slideNames: [],
		init: function(galleryContainer, startSlide){

			var galleryViewerInstance = this,
				galleryEl = $(galleryContainer).children('ul');

			this.options.startSlide = startSlide;
			this.galleryEl = galleryEl;
			this.options.galleryName = $(galleryContainer).attr('id');
			
			// hide this prior to reload, as LIs are sized incorrectly
			// $(galleryEl).css('visibility', 'hidden');

			// create carousel
			this.carouselObj = $(galleryEl).jcarousel({
			    scroll: 1,
				visible: 1,
				easing: 'easeInOutQuart',
				animation: 'slow',
			    initCallback: function(carousel){
					// create close button
					if($('.jcarousel-container .close').length === 0) {
						var closeButton = $('<a>',{
							href: '#',
							class: 'close',
							text: 'close'
						}).appendTo('.jcarousel-container');
					}
					$('.jcarousel-container .close').click(function(){
						galleryViewer.close();
						return false;
					});
				},
				itemVisibleInCallback: {
		    		onBeforeAnimation: function(carousel, item, idx, state) {
						// if(galleryViewerInstance.initialized === false) {
						// 	$(galleryViewerInstance.galleryEl).css('visibility','visible');
						// }
					},
		    		onAfterAnimation: function(carousel, item, idx, state) {
						if(state !== 'init') {
							document.location.hash = galleryViewerInstance.slideNames[idx-1];
						}
						galleryViewerInstance.initialized = true;
					}
				},
				reloadCallback: function(){
					// console.log("reload");
				},
				start: galleryViewerInstance.options.startSlide,
				itemFallbackDimension: 800
			});
		},
		carouselReload: function(){
			var that = this;
			setTimeout(function(){
				$(that.carouselObj).data('jcarousel').reload();
			}, 10);
		},
		jumpTo: function(index){
			// save the animation duration and temporarily set it to 0 for instant transition
			var defaultAnimation = $(this.carouselObj).data('jcarousel').options.animation;
			$(this.carouselObj).data('jcarousel').options.animation = 0;
			$(this.carouselObj).data('jcarousel').scroll(index);
			this.carouselReload();
			$(this.carouselObj).data('jcarousel').options.animation = defaultAnimation;
		},
		open: function(slideName){
			$('body').addClass(this.options.galleryName);
			
			setTimeout(function(){
				$('.gallery-artwork #gallery-artwork').css({
					opacity: 1
				});
				$('.jcarousel-container').css({
					marginTop: $(window).scrollTop() + 'px'
				});
			}, 200);
			
			var that = this;
			$(document).keyup(function(e){
				// key controls - left, right, close
				var key = e.which || e.keyChar || e.keyCode;
				if (key == 37)  {
					$(that.carouselObj).data('jcarousel').prev();
				} else if (key == 39) {
					$(that.carouselObj).data('jcarousel').next();
				} else if (key == 27) {
					galleryViewer.close();
				}
				return false;
			});
		},
		close: function(){
			document.location.hash = '';

			$('.gallery-artwork #gallery-artwork').css({
				opacity: 0
			});
			
			// timeout for opacity transition
			var galleryName = this.options.galleryName;
			setTimeout(function(){
				$('body').removeClass(galleryName);
			}, 200);
		},
		nameToIndex: function(name){
			var index = this.slideNames.indexOf(name);
			index = (index === -1) ? 0 : index + 1;
			return index + '';
		}
	};
	
	// store slide names
	$('.column.last a.page').each(function(){
		var name = this.href.split('#')[1];
		galleryViewer.slideNames.push(name);
	});

	// thumbnail triggers
	$('.column.last .page').click(function(evt) {
		var start = $(this).closest('li').index()+1;

		// if there isn't a gallery
		if(galleryViewer.initialized !== true) {
			// initialize gallery
			galleryViewer.init($('.gallery-carousel'), parseInt(start));
			galleryViewer.carouselReload();
		} else {
			galleryViewer.jumpTo(start);
		}

		galleryViewer.open();
		document.location.hash = this.href.split('#')[1];
		return false;
	});
	
	// on load if there's a hash, see if it matches a gallery and if so, init and open
	if(document.location.hash) {
		var hash = window.location.hash.slice(1),
			index = galleryViewer.nameToIndex(hash);

		galleryViewer.init($('.gallery-carousel'), parseInt(index));
		galleryViewer.carouselReload();
		galleryViewer.open();
	}
	
	// fixes Safari using cached JS when you go back using the back button
	window.onunload = function(){};	
});
