$(document).ready(function(){
	// main nav
	$('#nav > li').hover(
		function(){
			if(isIE7==true){
				$(this).children('ul').css('top','34px').fadeIn();
			}else if(isIE6==true){
			}else{
				$(this).children('ul').wrap(document.createElement('div'));
				$(this).children('div').css({
					'height':49*($(this).find('li').length),
					'width':142,
					'position':'absolute',
					'top':34
				}).children('ul').slideDown('fast');
			}
		},
		function(){
			if(isIE7==true){
				$(this).children('ul').fadeOut();
			}else{
				$(this).find('ul').slideUp('fast');
				$(this).children('div').height(0);
			}
		}
	);
	
	// Picasa gallery
	$('a.picasa-source').each(function() {
		var thisSource = $(this);
		$(thisSource).wrap('<div class="picasa-gallery-container"></div>');
		var thisGalleryContainer = $(thisSource).parents('.picasa-gallery-container');
		$.ajax({ 
			url: $(this).attr('href'),
			success: function (data,status){
				var albums = data.feed.entry;
				$(thisSource).wrap('<div class="picasa-gallery"></div>').parent('.picasa-gallery').append('<div class="thumb-carousel"></div><ul class="gallery"></ul><div class="gallery-controls"></div><a class="prevSlide">Previous</a><a class="nextSlide">Next</a>').find('.gallery-controls').append('<a class="play-control">Play</a><a class="pause-control">Pause</a>').siblings('.thumb-carousel').append('<ul/>');
				var thisGallery = $(thisSource).parent('.picasa-gallery');
				$.each(albums,function(){
					$('<li class="img_'+this.gphoto$id.$t+'"><p class="caption">'+this.media$group.media$description.$t+'</p><img style="margin-left:'+(800-this.media$group.media$content[0].width)/2+'px;margin-top:'+(800-this.media$group.media$content[0].height)/2+'px;" src='+this.content.src+' alt="Image" /></li>').appendTo($(thisGallery).children('ul'));
					$('<li class="img_'+this.gphoto$id.$t+'"><img src='+this.media$group.media$thumbnail[0].url+' alt="Image" /></li>').appendTo($(thisGallery).find('.thumb-carousel > ul'));
				});
				/* Show first slide */
				$(thisGallery).find('.gallery li:first-child').addClass('animRotatorActive').fadeIn();
				$(thisGallery).find('.thumb-carousel li:first-child').addClass('animRotatorActive').fadeIn();
				/* Start animated gallery rotator */
				animatedRotator(5000,($(thisGallery).find('.gallery')));
				smartScroll(($(thisGallery).find('.thumb-carousel')));
				/* Change slide on thumbnail click */
				$(thisGallery).find('.thumb-carousel li').click(function(){
					$(thisGallery).find('.gallery li.'+$(this).attr('class')).fadeIn().addClass('animRotatorActive').siblings().fadeOut().removeClass('animRotatorActive');
					$(this).addClass('animRotatorActive').siblings().removeClass('animRotatorActive');
					$(thisGallery).find('.gallery-controls .pause-control').click();
				});
				$(thisGallery).find('.prevSlide').click(function(){
					goAdjacentSlide('previous',($(thisGallery).find('.gallery')));
					$(thisGallery).find('.gallery-controls .pause-control').addClass('clicked').click();
				});
				$(thisGallery).find('.nextSlide').click(function(){
					goAdjacentSlide('next',($(thisGallery).find('.gallery')));
					$(thisGallery).find('.gallery-controls .pause-control').click();
				});
				$('.prevSlide,.nextSlide').mousedown(function(){
					$(this).addClass('mousedown');
				});
				$('.prevSlide,.nextSlide').mouseup(function(){
					$(this).removeClass('mousedown');
				});
				$(document).keydown(function(e){ // events for keyboard actions
					if(e.keyCode == '37'){ // left arrow
						$('.gallery-active .prevSlide').trigger('click');
					} else if (e.keyCode == '39') { // right arrow
						$('.gallery-active .nextSlide').trigger('click');
					} else if (e.keyCode == '27') { // escape key
						$('.gallery-active .reduce-gallery').trigger('click');
					}
				});
			},
			timeout: 10000,
			error: showErrorMessage,
			dataType: 'jsonp'
		});
		$(thisGalleryContainer).append('<a class="expand-gallery"><span>View Gallery</span></a><a class="reduce-gallery">Hide Gallery</a>');
		$('.picasa-gallery-container .expand-gallery,.picasa-gallery-container .reduce-gallery').click(function(){
			$(this).fadeOut().siblings('a').fadeIn();
			$(this).parent('.picasa-gallery-container').add($(this).siblings('.picasa-gallery')).animate({
				'height':$(this).hasClass('reduce-gallery')?200:800,
				'width':$(this).hasClass('reduce-gallery')?420:800,
				'margin-left':$(this).hasClass('reduce-gallery')?0:-21
			}, function(){
				$(this).find('.thumb-carousel ul').fadeOut(1000);
			}).toggleClass('gallery-active');
		});
	});
});

/* Animated gallery rotator function
 interval = the amount of time on each slide
container = css selector for the parent element of all the slides
*/
animatedRotator = function (interval,container){
	var galleryPause = true;
	// checks whether there is more than one slide before running animation
	if ($(container).children().length > 1) {
	//randomizes first slide if ".animRotatorActive" class hasn't been given to one of them
		if ($(container).find('.animRotatorActive').length < 1) {
			var numSlides = $(container).children().length;
			var firstSlideNumber = Math.floor(Math.random()*numSlides) + 1;
			var firstSlide = $(container).children('*:nth-child(' + firstSlideNumber + ')');
			//$(container).height(firstSlide.innerHeight());
			firstSlide.addClass('animRotatorActive');
		} else {
			//$(container).height($('.animRotatorActive').innerHeight());
		}
		$(container).siblings('.gallery-controls').find('.pause-control').click(function(){
			$(this).addClass('active').siblings().removeClass('active');
			clearInterval($(container).data('animatedRotatorInterval'));
			galleryPause = true;
		});
		$(container).siblings('.gallery-controls').find('.play-control').click(function(){
			$(this).addClass('active').siblings().removeClass('active');
			if (galleryPause){
				intervalSetter(interval,container);
			}
			galleryPause = false;
		});
		$('.picasa-gallery .gallery-controls .pause-control, .picasa-gallery .gallery-controls .play-control').mousedown(function(){
			$(this).addClass('mousedown');
		});
		$('.picasa-gallery .gallery-controls .pause-control, .picasa-gallery .gallery-controls .play-control').mouseup(function(){
			$(this).removeClass('mousedown');
		});
		$('.picasa-gallery .gallery-controls .play-control').click(); /* Starts animation on initial load */
	}
};
intervalSetter = function(interval,container) {
	$(container).data('animatedRotatorInterval', setInterval(function(){
		var active = $(container).children('.animRotatorActive');
		var next = (active.next()[0])?active.next():$(container).children('*:first-child');
		active.fadeOut(1000).removeClass('animRotatorActive');
		$('.thumb-carousel li.'+active.attr('class')).removeClass('animRotatorActive');
		$('.thumb-carousel li.'+next.attr('class')).addClass('animRotatorActive');
		next.fadeIn(1000).addClass('animRotatorActive');
		/*$(container).animate({
			height:next.innerHeight()
		},1000);*/
	},interval));
}

goAdjacentSlide = function(direction,container) {
	var active = $(container).children('.animRotatorActive');
	var next = (active.next()[0])?active.next():$(container).children('*:first-child');
	var previous = (active.prev()[0])?active.prev():$(container).children('*:last-child');
	var directionSlide = direction==='previous'?previous:next;
	//if (direction === 'previous'){ var directionSlide = previous } else
	active.fadeOut(1000).removeClass('animRotatorActive');
	$('.thumb-carousel li.'+active.attr('class')).removeClass('animRotatorActive');
	$('.thumb-carousel li.'+directionSlide.attr('class')).addClass('animRotatorActive');
	directionSlide.fadeIn(1000).addClass('animRotatorActive');
}

smartScroll = function (scrollContainer) {
	scrollContainer.bind('mouseenter',function(event){
		$(this).children().stop(true,true).fadeIn();
		var windowSize = $(window).width();
		var containerSize = scrollContainer.width();
		var thumbSize = scrollContainer.children().children().outerWidth();
		var carouselSize = thumbSize*scrollContainer.children().children().length;
		var nonScrollablePercent = thumbSize/containerSize;
		var scrollablePercent = 1 - (2*nonScrollablePercent);
		scrollContainer.children().css('width',carouselSize);
		var relativePositionX = event.clientX-scrollContainer.offset().left;
		var smartScroll = Math.round((((relativePositionX - nonScrollablePercent * containerSize) / (scrollablePercent * containerSize)) * (carouselSize - containerSize)));
		scrollContainer.animate({'scrollLeft':smartScroll},400, function(){
			scrollContainer.mousemove(function(event){
				var relativePositionX = event.clientX-scrollContainer.offset().left;
				var smartScroll = Math.round((((relativePositionX - nonScrollablePercent * containerSize) / (scrollablePercent * containerSize)) * (carouselSize - containerSize)));
				scrollContainer.scrollLeft(smartScroll);
			});
		});
	});
	scrollContainer.bind('mouseleave',function(event){
		$(this).unbind('mousemove').children().fadeOut();
	});
}
smartScrollSetter = function(scrollContainer,nonScrollablePercent,containerSize,scrollablePercent,carouselSize) {
	var relativePositionX = event.clientX-scrollContainer.offset().left;
	var smartScroll = Math.round((((relativePositionX - nonScrollablePercent * containerSize) / (scrollablePercent * containerSize)) * (carouselSize - containerSize)));
}
function showErrorMessage(){
	$('a#picasa-source').wrap('<div class="picasa-gallery"></div>');
	$('.picasa-gallery').append('<p>Gallery not available.</p>');
}
