$(document).ready(function()
{
	//-------------------- Elements Exists Plugin
	jQuery.fn.exists = function()
	{
		return jQuery(this).length > 0;
	}

	//------------------ Newsletter Signup
	$('#newsletter_signup_form').submit(function()
	{
		//-------------------- Validation
		var name = $('#newsletter_signup_form input[name="name"]').val();
		var email = $('#newsletter_signup_form input[name="email"]').val();
		
		var status_message = $('#newsletter_signup_form .status');
		
		status_message.html('').removeClass('error').hide();
		
		if(status_message.html != '') status_message.show();
		
		if(name == "")
		{  
			status_message.html('Please enter your name.').addClass('error');
			return false;
		}
		
		if(email == "")
		{  
			status_message.html('Please enter your email address.').addClass('error');
			return false;
		}
		
		var email_regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(email_regex.test(email) == false)
		{
			status_message.html('Please enter a valid email address.').addClass('error');
			return false;
		}

		//-------------------- AJAX
		$.ajax(
		{
			type: 'POST',
			url: 'newsletter-signup',
			data: $(this).serialize(),
			success: function(data)
			{
				if(data == 'success')
				{
					status_message.html('Thank you for signing up.').addClass('success');
					$('#newsletter_signup_form').find('input[type="text"]').val('');
				}	
			}
		});
		
		return false;
	});
	
	//------------------ Nivo Sliders
	
	// Usage: http://nivo.dev7studios.com/#usage
	
	$('#home_page_slider').nivoSlider(
	{
		effect: 'sliceUpLeft', // 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
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.7, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next' // Next directionNav text
    });
	
	$('#content_page_slider').nivoSlider(
	{
		effect: 'sliceDown', // 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
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.7, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next' // Next directionNav text
    });
	
	// Hide slider navigation if only 1 image
	if ( $('.nivoSlider img').size() <= 1 )
	{
		$('.nivo-control.active').css('display', 'none');
		$('.nivo-prevNav').css('display', 'none');
		$('.nivo-nextNav').css('display', 'none');
		$('.theme-orman.slider-wrapper').css('padding-bottom', 25);
	}
	
	//------------------ ShareThis button styles
	if ( $('#content_page_slider').exists() )
	{
		$('#share_this').addClass('with_gallery');
	}
	
	//------------------ Subnav (If there is only 1 child MODx does not add the 'last' class to the first/last li)
	$('.sub_nav li.active ul li:last').addClass('last');
	
	//------------------ Page load events
	$(window).load(function()
	{
		$('#share_this').css('visibility', 'visible'); // CSS font edit, flicker
		$('h1, h2, h3, h4').css('visibility', 'visible'); //Cufon flicker
	});
	
	//-------------------- Fancybox
	
	// Usage: http://fancybox.net/api
	
	function hide_youtube_video()
	{
		$('#youtube').addClass('hidden').find('#video').css('visibility', 'hidden');
	}
	
	function show_youtube_video()
	{
		$('#youtube').removeClass('hidden').find('#video').css('visibility', 'visible');
	}
	
	$('a.fancybox').fancybox(
	{
		'overlayShow'	: false,
		'autoScale'		: true,
		'hideOnContentClick': false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'onComplete'	: function(){ hide_youtube_video(); },
		'onClosed'		: function(){ show_youtube_video(); },
		'onCancel'		: function(){ show_youtube_video(); }
	});
	
});
