$(document).ready(function(){
	
	var home = document.getElementById('home'),
			len = home ? 3 : 4;
	
	//Fancybox
	if($('a.gallery-image').length || $('a.video-link').length){
		$("a.gallery-image").fancybox();
	
		$("a.video-link").fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'width'			:  640,
			'height'		: 480,
			'href'			: $(this).find("embed").attr("src"),
			'type'			: 'swf',
			'swf'			: {
				'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
	}
	
	
	function awesometown(carousel) {
		var $dotNav = $("#dot-navigation"),
			mathFloor = Math.floor;
			
		$("#prev-link, #next-link").bind('click', function(e) {
			e.preventDefault();
			var direction = this.id.replace(/-link$/,'');
			carousel[direction]();
			
			$dotNav.find("a").each(function(){
				if ( mathFloor( carousel.last / len ) == $.jcarousel.intval( $(this).text() ) ) {
					$dotNav.find('.active').removeClass('active');
					$(this).addClass('active');
				}
			});
		});
		
		
		$dotNav.delegate("a", 'click', function(e) {
			e.preventDefault();
			$dotNav.find('.active').removeClass('active');
			$(this).addClass('active');
			var sc = $.jcarousel.intval($(this).text());
			if (sc == 1){
				carousel.scroll(1);
			} else {
				if (carousel.first > sc) {
					carousel.scroll(sc * len - (len - 1));
				} else {
					carousel.scroll(sc * len);
				}
			}
		})
	};
	
	$("div.on-the-cay ul").jcarousel({
		'visible': len,
		'scroll': len,
		'wrap' : "both",
		'buttonNextHTML': null,
		'buttonPrevHTML' : null,
		initCallback: awesometown
	});
	
	if(home) {
		$("#home #hero").cycle({
			speed:  'fast',
			timeout: 5000,
			next: '#next-hero-link',
			prev: '#previous-hero-link',
			activePagerClass: 'here',
			pager: 'ul#hero-slideshow-nav',
			pagerAnchorBuilder: function(idx, slide) {
				return '<li><a href="#"><span></span></a></li>'; 
			}
		});
	}

	if ($(".page #gallery ul#gallery-images").length) {
		
		$(".page #gallery ul#gallery-images").cycle({
			speed:  'slow',
			timeout: 0,
			pager: '#gallery-nav',
			pagerAnchorBuilder: function(idx, slide) {
			        return '<li class="li_'+idx+'"><a href="#"><img src="' + $(slide).find("img").attr("src") + '" width="57" height="46" /></a></li>';
			},
			before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				$("p.caption_" + ($(currSlideElement).index() + 1)).fadeOut(function(){
					$("p.caption_" + ($(nextSlideElement).index() + 1)).fadeIn();
				}); 
			}
		});
	}
	
	$(".field-tooltip").hide();
	
	$("#field-tooltip-link").hover(function() {
	  $(".field-tooltip").animate({opacity: "show"}, "slow");
	}, function() {
	  $(".field-tooltip").animate({opacity: "hide"}, "fast");
	});
	
	$(".validate-form").validate();

	
});
