$(document).ready(function() {

	$("#content").css("display","block");
	
	$.each(photos, function(key, value) {
		if (key == "image") jQuery.preLoadImages(value);
	});
	
	//slider carousel
	$('ul.jcarousel-skin-slider').jcarousel({
		'scroll':2
	});
	//slider carousel
	$('ul.jcarousel-skin-terrain').jcarousel({
		'scroll':2,
		'buttonNextHTML':'<div>Next</div>',
		'buttonPrevHTML':'<div>Prev</div>'
	});
	
	$("a.videoBox,li.vids a").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'			: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	$("a.photoBox").fancybox();
		
	/****************************************
		front page hover quotes functionality
	****************************************/

	$('.jcarousel-skin-slider .item .detail').each(function () {
		var showDistance = 0;
		var hideDistance = 150;
	    var time = 500;
	    var hideDelay = 70;
	    var hideDelayTimer = null;
	    var beingShown = false;
	    var shown = false;
        var detailDiv = $(this);
		


		// set the mouseover and mouseout on both element
	    $(this).parent('.item').hover(function () {

			// stops the hide event if we move from the trigger to the quote element
			if (hideDelayTimer) 
				clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				// reset position of quote box
				detailDiv.animate({
					top: showDistance
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
			
 			if ($(this).find("a").attr("href")) {
				if ($(this).find("a").attr("rel")) {
					$(this).click(function(){
					});
					$(this).css("cursor","pointer");
				} else {
					$(this).click(function(){
					});
					$(this).css("cursor","pointer");	
				}	
			}

	    },function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) 
				clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				detailDiv.animate({
					top: hideDistance
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					detailDiv.css({'top' : hideDistance });
				});
			}, hideDelay);
	    });
	});
	
	
	
	// Backwards navigation
	$("#home-splash-nav a[title='back']").click(function(event) {
		event.preventDefault;
		stopAnimation();
		navigate("back");
		return false;
	});
	
	// Forward navigation
	$("#home-splash-nav a[title='next']").click(function(event) {
		event.preventDefault;
		stopAnimation();
		navigate("next");
		return false;
	});
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
	};
	
	var stopAnimation = function() {
	   // Clear the interval
	   clearInterval(interval);
	};	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Clear previous img and set the background image of the new active container
		$("#home-splashImg" + activeContainer).empty();
		$('<img src="' + photoObject.image + '" width="960" height="411" />').appendTo("#home-splashImg" + activeContainer);
		$("#home-splashImg" + activeContainer).css({
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#home-splash-text").css({"display" : "none"});

		// Set Learn More link
                $('#home-splash .splash-more').attr('href', photoObject.href);

		// Set the new header text
		if (photoObject.title == "") { $("#home-splash-text p").css('display','none'); } 
		else { $("#home-splash-text p").html(photoObject.title); }
		
		
		// Fade out the current container and display the header text when animation is complete
		$("#home-splashImg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#home-splash-text").fadeIn(200);
				animating = false;
			}, 100);
		});
	};
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	
	$('#tray #tray-nav li a').click(function(event) {
		event.preventDefault;
		var currentPick = $(this).attr('id');
		
		if ($("#tray").position().left == "-800") {
			$("#"+currentPick).attr('class','on');
			$("#tray").animate({
				left: '+=800'
			}, 500, function() {
				$("#"+currentPick+"-tray").fadeIn(500);
			});
		} else {
			if ($("#"+currentPick).attr('class')=='on') {
				$('#tray #tray-nav li a').removeClass('on');
				$("#tray").animate({
					left: '-=800'
				}, 500, function() {
					$("#"+currentPick+"-tray").fadeOut(500);
				});
			} else {
				$("#tray #tray-inner>div").hide();
				$('#tray #tray-nav li a').removeClass('on');
				$("#"+currentPick).attr('class','on');
				$("#"+currentPick+"-tray").fadeIn(500);
			}
		}
		return false;
	});
	
	$('#tray #tp-tray-nav li a').click(function(event) {
		event.preventDefault;
		var currentPick = $(this).attr('id');
		if ($("#"+currentPick).attr('class')!='on') {
			$('#tray #tp-tray-nav li a').removeClass('on');
			$("#"+currentPick).attr('class','on');
			$("#tray>div>div").hide();
			$("#"+currentPick+"-tray").fadeIn(500);
		}
		return false;
	});

		
	// conditions navs pipe separator added 
	$('#conditions-nav li').not('#conditions-nav > li:last-child').append('|');
	
	$('#conditions-container #trails').show();
	$('#conditions-nav li a[title="trails"]').addClass('on');
	$('#conditions-nav li a').click(function(event) {
		event.preventDefault;
		var chosenBlock = $(this).attr('title');
		$('#conditions-nav li a').removeClass('on');
		$(this).addClass('on');
		$('#conditions-container .conditions-block').css({
			'display': 'none'
			});
		$('#conditions-container #'+chosenBlock).fadeIn(100);
		return false;
	});
	
	// Homepage tabs for Events and News
	$('.tab-content').hide();
	$('ul.tabs li:first-child a').addClass('active').show();
	$('.tab-content:first-child').show();
	
	$('ul.tabs li').click(function() {
	
		$(this).parent().find('li').find('a').removeClass('active');
		$(this).find('a').addClass('active');
		$(this).parent().parent().find('.tab-content').hide();
		
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn();
		return false
	
	});
		
});

