$(document).ready(function(){

	var animationDuration = 750;
	var defaultEasing = 'easeInOutCubic';
	
	/* Open and close header
	-------------------------------------------------------------- */
	
	var header = $('header');
	var headerTopMargin = header.css('margin-top');
	var aboutUsToggle = $('nav li.about-us a');
	
	
	aboutUsToggle.click(function(){
		
		if (!header.hasClass('active')) {
	
			header.stop().animate({
				marginTop: '0'
			}, animationDuration, defaultEasing).addClass('active');
		
		} else {
		
			header.stop().animate({
				marginTop: headerTopMargin
			}, animationDuration, defaultEasing).removeClass('active');
		
		}
		
		return false;
	
	});
	
	
	/* Initialize homepage slideshow
	-------------------------------------------------------------- */

	var slideshowContainer = $('#slideshow');
	var slideshowNav = $('.slideshow-nav', slideshowContainer);
	var slideshowSlides = $('.slides', slideshowContainer);
	var slideshowHeader = $('.header', slideshowContainer);
	
	slideshowSlides.cycle({
		fx: 'scrollHorz',
		easing: defaultEasing,
		next: $('a.next', slideshowNav),
		prev: $('a.previous', slideshowNav),
		speed: animationDuration,
		timeout: animationDuration*5
		
	});
	
	
	if (slideshowSlides.hasClass('noadvance')) {
	
		slideshowSlides.cycle('pause');
	
	}
	
	/* Initialize tabs
	-------------------------------------------------------------- */
	
	$('#tabs').tabs();
	
	
	/* Handle feature hover animation
	-------------------------------------------------------------- */
	
	var features = $('#features');
	var featurePanelContainer = $('.panel-container', features);
	
	var featurePanels = $('.panels', featurePanelContainer);
	var featurePanelsWidth = 0;
	
	var featurePanel = $('.panel', featurePanels);
	var featurePanelInformationTop = $('.information', featurePanel).css('top');
	var featurePanelWidth = featurePanel.css('width');
	
	featurePanel.hoverIntent(
	
		function(){	
		
			$('.information', this).stop().animate({
				top: '0'
			}, animationDuration/2, defaultEasing).addClass('active');
		
		},
		function(){
		
			$('.information', this).stop().animate({
				top: featurePanelInformationTop
			}, animationDuration/2, defaultEasing).removeClass('active');
		
		}
	
	).click(function(){
	
		var href = $(this).find('a').attr('href');
		
		window.location = href;
		
		return false;
	
	});
	
	/* Handle feature pagination
	-------------------------------------------------------------- */
	
	var featureNavigationControls = $('.panel-nav a', features);
	
	/* first we need to make sure the panel container is wide enough for all panels */
	
	featurePanel.each(function(){
	
		featurePanelsWidth += $(this).width();
	
	});
	
	featurePanels.width(featurePanelsWidth);
	
	
	featureNavigationControls.click(function(){
	
		var scrollLength = parseInt(featurePanelWidth, 10);
	
		if ($(this).hasClass('previous')) {
		
			scrollDistance = '-='+scrollLength+'px';
		
		} else {
		
			scrollDistance = '+='+scrollLength+'px';
		
		}
	
		featurePanelContainer.scrollTo(scrollDistance, {
			axis: 'x',
			duration: animationDuration,
			easing: defaultEasing
		});
		
		return false;
	
	});
	
	// Hide the navigation controls if there is 3 or fewer items
	if (featurePanel.size()<=3) {
	
		featureNavigationControls.hide();
	
	}
	
	
	/* Apply form labels plugin to text fields
	-------------------------------------------------------------- */
	
	$.fn.formLabels();


	/* Scroll to target when a hash link is clicked
	-------------------------------------------------------------- */

	$('a').click(function(){
	
		var href = $(this).attr('href');
		
		if (href) {
		
			var hash = href.substr(0, 1);
			var target = href.substr(1);
	
			if (!$(this).hasClass('no-scroll') && hash==="#" && target!=="") {
		
				$.scrollTo(href, {
					axis: 'y',
					easing: defaultEasing,
					duration: animationDuration
				});
			
				return false;
			
			}
		
		}
	
	});


	/* Hide image and show text on rollover of live
	-------------------------------------------------------------- */
	
	$('#live-panel').hover(
	
		function(){
		
			$(this).find('.image').stop().hide();
			
			$(this).find('.content').stop().show();
		
		},
		
		function(){
		
			$(this).find('.image').stop().show();
		
			$(this).find('.content').stop().hide();
		
		}
	
	);
	
	
	/* Enable tipsy for upload form
	-------------------------------------------------------------- */
	
	$('#upload-form input, #upload-form textarea').tipsy({trigger: 'focus', gravity: 'w', title: 'data-tip'});
	
	
	

	
	/* Open PDFs in new window
	-------------------------------------------------------------- */
	
	$('a[href*=".pdf"]').click(function(){
		window.open(this.href);
		return false;
	});
	
	
	/* Open external links in new window
	-------------------------------------------------------------- */
	
	if (!$('html').hasClass('ie6')) {
	
		$("a").each(function(){
			if (this.href.indexOf(window.location.hostname) === -1) { $(this).attr('target', '_blank'); }
		});
	
	}
	
	
	/* LIVE popup
	-------------------------------------------------------------- */
	
	if (!$('html').hasClass('ie6')) {

		/*
		if ($.cookie('popup')==='closed') {
		
			$('#popup').hide();
		
		} else {
		
			$('#popup').show();
		
		}
		*/
		
		$('#popup').not('.newsletter').show();
	
		$('#popup a.close').click(function(){
		
			$('#popup').fadeOut();
			
			// Set a cookie
			$.cookie('popup', 'closed', { expires: 7 });
			
			return false;
		
		});
	
	}
	
	
	/* Mailchimp newsletter signup form 
	-------------------------------------------------------------- */
	
	var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var error = $('div.email_error');
	var success = $('div.email_success');
	
	// set up the options for the ajax form
	var options = { 
		url: 'http://ark.us2.list-manage.com/subscribe/post-json?u=fbe012fd25da18a857046c0dd&id=489632d990&c=?', 
		type: 'GET', 
		dataType: 'json', 
		contentType: "application/json; charset=utf-8",
		beforeSubmit: function() {

			if (regex.test($('input#mce-EMAIL').val()) == false) {
				// show the error popup
				error.fadeIn();
				return false;
			} else {
				// please wait...
				$('input#mce-EMAIL').val('Please wait...');
			}
			
		}, 
		success: function() {
			// hide the error, show the success, remove the email
			error.hide();
			success.fadeIn();
			$('input#mce-EMAIL').val('');
		}
	};
	
	// attach the options to the newsletter form
	$('#newsletter').ajaxForm(options);

	// newsletter popups
	$('.popup a.close').click(function(){
		
		$(this).closest('.popup').fadeOut();
		return false;
		
	});

});
