$(function(){

	/**
	 * SLIDESHOWS
	 * -------------------------------------------
	 */

	// Client slideshow
	if($('.clientSlideshow').size()){
		$('.clientSlideshow').slideshow({
			count : 9,
			path : '/wp-content/themes/bodylab/public/images/clients/',
			height : 337,
			classes : 'border'
		});
	}
	// Process slideshow
	if($('.processSlideshow').size()){
		$('.processSlideshow').slideshow({
			count : 11,
			path : '/wp-content/themes/bodylab/public/images/process/',
			height : 337,
			classes : 'border'
		});
	}
	// Matt slideshow
	if($('.mattSlideshow').size()){
		$('.mattSlideshow').slideshow({
			count : 6,
			path : '/wp-content/themes/bodylab/public/images/matt/',
			height : 376,
			classes : 'border'
		});
	}
	// Todd slideshow
	if($('.toddSlideshow').size()){
		$('.toddSlideshow').slideshow({
			count : 6,
			path : '/wp-content/themes/bodylab/public/images/todd/',
			height : 376,
			classes : 'border'
		});
	}

	/**
	 * LOGIN LABELS
	 * -------------------------------------------
	 */

	$('.inline input[type=text]').each(function(){

		var $field = $(this),
				$label = $('label[for='+this.id+']');

		$label.css('display','none');

		setTimeout(function(){
			if($field.val().length == 0){
				$label.fadeIn('fast');
			}
		},1000);

		$(this)
			.focus(function(){
				$label.fadeOut('fast');
			})
			.blur(function(){
				if($field.val().length == 0){
					$label.fadeIn('fast');
				}
			});
	});

	/**
	 * HANDLE NEWSLETTER SIGNUP
	 * -------------------------------------------
	 */

	$('#newsletter').submit(function(){
		var email = $('#email').val();
		if(email != ''){
			if(!isValidEmail(email)){
				alert('The email address you entered is not valid...');
				return false;
			}
		} else {
			alert('Please enter your email address before submitting...');
			return false;
		}
	});

	/**
	 * SETUP SURVEY
	 * -------------------------------------------
	 */

	if($('#signup_email').size() && getQueryVariable('email')){
		$('#signup_email').val(getQueryVariable('email'));
		$('#newsletter_signup input').attr('checked','checked');
	}

});

/**
 * Retreive variable from URL
 */

var getQueryVariable = function(variable){
	for(var vars=window.location.search.substring(1).split('&'), i=vars.length-1; i>=0; i--){
		var pair = vars[i].split('=');
		if(pair[0] == variable){
			return decodeURIComponent(pair[1]);
		}
	}
	return false;
};

/**
 * Validate Email
 */

var isValidEmail = function(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
};

/**
 * SLIDESHOW PLUGIN
 * -----------------------------------------------
 */

(function($){

	$.fn.slideshow = function(options) {

		// Set globals
		var settings = $.extend({}, $.fn.slideshow.defaults, options);

		if(!settings.count || !settings.height){
			return;
		}

		return this.each(function() {

			// First image
			var $origImage = $(this);

			// Create slideshow object
			var $slideshow = $origImage
				.addClass('active')
				.wrap('<div class="slideshow_wrapper"/>')
				.closest('.slideshow_wrapper')
				.css('height',settings.height);

			// Add images
			for(var n=2; n<=settings.count; n++){
				$('<img/>')
					.addClass(settings.classes)
					.css('display','none')
					.attr('src',settings.path+'image'+n+'.'+settings.format)
					.appendTo($slideshow);
			}

			setInterval(function(){

				// Get current image
				var $active = $slideshow.find('img.active');

				// Find next image
				if(!settings.random){
					// Load in order
					var $next =	$active.next().length ? $active.next() : $slideshow.find('img:first');
				} else {
					// Load randomly
					var $sibs	= $active.siblings();
					var rndNum = Math.floor(Math.random() * $sibs.length );
					var $next	= $($sibs[rndNum]);
				}

				// Set previous image
				$active.addClass('last-active');

				// Set next image & animate
				$next
					.css({
						opacity : 0.0,
						display : 'inline'
					})
					.addClass('active')
					.animate({opacity: 1.0}, 1000, function() {
						$active.removeClass('active last-active');
					});
			},settings.delay);
		});
	};

	// Set defaults
	$.fn.slideshow.defaults = {
		count : false,
		delay : 3000,
		path : false,
		random : false,
		height : false,
		width : 'auto',
		format : 'jpg',
		classes : ''
	};

})(jQuery);