$(function(){
	// show / hide mailing list
	$(".join").click(function() {
		$('#signUpWrapper').slideToggle('500');
		$(this).toggleClass('over');
    });
	$(".cancel").click(function() {
		$('#signUpWrapper').slideUp('500');
		$('.join').removeClass('over');
    });
	
	// main navigation
	$("#mainNav li").mouseenter(function() {
		$(this).find('ul').stop(true, true).fadeIn(200);
		$(this).addClass('selected');
	}).mouseleave(function(){
		$(this).find('ul').stop(true, true).fadeOut(200);
		$(this).removeClass('selected');
    });
	
	// generic clickable element
	$('.clickable').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	}).click(function(){
		window.location = $(this).parent().find('a').attr('href');
	});
    
    $(".reveal").click(function(){
    	$('.hidden',this).slideToggle('slow');
	});    
    
    $(".moreInfo").click(function (){
    	var parent = $(this).parents('.featurepanel');
		$('.extraInfo', parent).fadeToggle('500');
	});
   
    
	$('.slideshow.scrollable').scrollable({
		next: '.rightArrow',
		prev: '.leftArrow',
		circular: true
	});

	var imagetotal = $(".slideshow.scrollable img").length - 2;
	if (imagetotal < 2){
		$('.leftArrow, .rightArrow').css('display', 'none');

	}
	
    $('#mailingList').click(function(){
    	if($(this).val() == 'Your Email Address') {
    		$(this).val('');
    	}
    });
    
    $('.searchfilter select').change(function(){
    	$(this).parents('form')[0].submit();
    })
    
    $('.drilldown a.close').click(function(){
    	var parent = $(this).parents('.drilldown'),
    		form = $(this).parents('form');
    	parent.find('input').val('');
    	form.submit();
    });

	function onSeek() {

	}

    function scrolly_init() {
		$('#anchor-feature').parent().removeClass('scrollable-disabled');
		$('#afeature .controls').show();
    	var zIndex=10;
        var newGallery = $('<div class="scrolly"><div class="scrolly-static"/><div class="scrolly-content"><div class="scrolly-scroller"/></div></div>');
        
		$('.scrollable .cms-component').each(function(){
	
			var component = $(this).find('.scrollLarge').clone(),
				this_class = $(this).find('.scrollLarge').attr('class'),
				info = $('.info', component).clone(),
				link = info.find('.findoutmore a').attr('href');
				component.find('.info').remove();
				info.addClass(this_class);
				component.removeAttr('id').removeClass('cms-component');
				
				$('.scrolly-static', newGallery).append(info);
				$('.scrolly-scroller', newGallery).append(component);
				

		});
		
		$('#anchor-feature').find('.cms-component').hide();
		newGallery.find('.info').eq(0).css({zIndex: 11});
		$('#anchor-feature').append(newGallery);
		
		if($('.scrolly-static > div').length > 1 || $('.scrollable.medium > div').length > 1) {
			$('.rightArrow, .leftArrow').css({'display':'block'});
		}


		
		$('.scrolly-content').scrollable({
			next: '.rightArrow',
			prev: '.leftArrow',
			circular: true,
			easing: 'linear',
			onBeforeSeek:function(e, i){
				$('.scrolly-scroller .scrollLarge').find('.credit').hide();
			},
			onSeek:function(event, index){
				this.stamp = new Date();
				var fadeId = index;
				var self = this;
				
				setTimeout(function(){
					// Dont animate if new scroll started
					if (self.getRoot().children().is(':animated')){
						return;
					}
					// Dont fade to ourself!
					if ($('.scrolly-static .info.active').index() === fadeId){
						return;
					}
					
					$('.scrolly-static .info.active').removeClass('active');
					
					$('.scrolly-static .info').eq(fadeId).css({display:'none', zIndex:++zIndex}).addClass('active').fadeIn($.browser.msie && $.browser.version < 9 ? 0 : 500);
					$('.scrolly-scroller .scrollLarge').find('.credit').css({display:'none'}).fadeIn($.browser.msie && $.browser.version < 9? 0 : 500);

				}, 40);

			}
		});
    }
    
    scrolly_init();

	var scrolly_disable = function() {
		$('#afeature .controls .leftArrow, #afeature .controls .rightArrow').hide().unbind('click');
		$('#anchor-feature .scrolly-content').remove();
		$('#anchor-feature .scrolly').remove();		
		$('#anchor-feature .cms-component').show();
		$('#anchor-feature').parent().addClass('scrollable-disabled');
	}

	$('#cms-workspace').bind('editing-enabled', function(){
		scrolly_disable()
	}).bind('editing-disabled', function(){
		if ( ! $('#anchor-feature .scrolly-content').length) {
			scrolly_init();
		}
	});

	$('.contentPanel.video-wrapper a').fancybox({
		type: 'iframe',
		href: this.href
	});
});


