$(document).ready(function(){
	if( $.isFunction($.fn.jCarouselLite) ){
		var show = false;
		$('#galleryContainer .galleryImg').css({'width':960,'height':640});
		$('#captionMask .captions').css({'height':53,'overflow':'hidden'});
		$('.csContainer').hide();
		$('.captions').hide();
		$('#captionMask').show().css({'opacity':.7});
		$('#slider').show();
		$('.showCaptions').show();
		$('#captionMask .captions').empty().append($("#galleryContainer li:first .captions").clone().show());

		var photoCount = $('#slider li').length;
		var photoArray = [];

		for(i=1; i<photoCount; i++){
			photoArray.push('.p'+i);
		}

		var photoCount = $('#galleryContainer li').length;

		$("#galleryContainer").jCarouselLite({
			btnNext: "#slide-right",
			btnPrev: "#slide-left",
			mouseWheel: true,
			visible: 1,
			btnGo:photoArray,
			beforeStart: function(object){
				$('#captionMask .captions').empty();
				var index = $('#galleryContainer li').index(object);
				index = index > photoCount ? 1 : index;
				$('.p'+index).removeClass('over');
			},
			afterEnd: function(object){
				var index = $('#galleryContainer li').index(object);
				$('#captionMask .captions').append($('.captions',object).clone().show());
				index = index > photoCount ? 1 : index;
				$('.p'+index).addClass('over');
				}
			});

		$('.showCaptions').click(function(){
			if (show)
			{
				$('.captions').slideUp(400,function(){$('.showCaptions').html('SHOW CAPTIONS');});
				$('.showCaptions').removeClass('up').addClass('down');
				show = false;
			}else{
				$('.captions').slideDown(400,function(){$('.showCaptions').html('HIDE CAPTIONS');});
				$('.showCaptions').removeClass('down').addClass('up');
				show = true;

			}
			return false;
		});

		$(".side-slider").jCarouselLite({
			btnNext: ".slide-up",
			btnPrev: ".slide-down",
			mouseWheel: true,
			beforeStart: function(object){

			},
			afterEnd: function(object){
			},
			vertical:true,
			visible:3
		});
		//var photoCount = $('#slider li').length;
		$("#slider.on").jCarouselLite({
			btnNext: "#slide-right",
			btnPrev: "#slide-left",
			mouseWheel: true,
			visible: 20,
			beforeStart: function(object){

			},
			afterEnd: function(object){
			}
		});

		// Home page rotator
//		var photoCount = $('.story-rotator li').length;
//		var photoArray = [];
//		for(i=0; i<photoCount; i++){
//			var $button = $('<button class="p'+i+' ui-state-default">'+(i+1)+'</button>').hover(
//				function(){ $(this).addClass('ui-state-hover'); },
//				function(){ $(this).removeClass('ui-state-hover'); }
//			);
//
//			$('.story-rotator .slider-control').append( $button );
//			photoArray.push('.story-rotator .slider-control .p'+i);
//		}
//
//		$('.story-rotator .slider-control .p0').addClass('ui-state-focus');
//
//		$('.story-rotator').jCarouselLite({
//			visible:1,
//			btnGo:photoArray,
//			mouseWheel: true,
//			auto: 5000,
//			speed: 1000,
//			beforeStart: function(object){
//				$('.story-rotator .slider-control .'+object.attr('rel')).removeClass('ui-state-focus');
//			},
//			afterEnd: function(object){
//				$('.story-rotator .slider-control .'+object.attr('rel')).addClass('ui-state-focus');
//			}
//		});
	}

	$(window).load(function() {
		if( $.isFunction($.fn.nivoSlider)){
			$('#slider').nivoSlider({
				effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
				slices: 15, // For slice animations
				boxCols: 8, // For box animations
				boxRows: 4, // For box animations
				animSpeed: 500, // Slide transition speed
				pauseTime: 4000, // How long each slide will show
				startSlide: 0, // Set starting Slide (0 index)
				directionNav: true, // Next & Prev navigation
				directionNavHide: true, // Only show on hover
				controlNav: true, // 1,2,3... navigation
				controlNavThumbs: true, // Use thumbnails for Control Nav
				controlNavThumbsFromRel: false, // Use image rel for thumbs
				controlNavThumbsSearch: '.jpg', // Replace this with...
				controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
				keyboardNav: true, // Use left & right arrows
				pauseOnHover: true, // Stop animation while hovering
				manualAdvance: false, // Force manual transitions
				captionOpacity: 0.8, // Universal caption opacity
				prevText: 'Prev', // Prev directionNav text
				nextText: 'Next', // Next directionNav text
				randomStart: false, // Start on a random slide
				fauxBackground: '#f2f2f2', // transparent
				beforeChange: function(){}, // Triggers before a slide transition
				afterChange: function(){}, // Triggers after a slide transition
				slideshowEnd: function(){}, // Triggers after all slides have been shown
				lastSlide: function(){}, // Triggers when last slide is shown
				afterLoad: function(){} // Triggers when slider has loaded
			});
		}
	});
});
