$(function() {
	/** Test for attribute support in inputs **/
	// Placeholder
	if (!elementSupportsAttribute('input', 'placeholder')) {
		$('input').each(function() {
			var pl = $(this).attr('placeholder');
			$(this).attr('value', pl);
			
			$(this).addClass('gray');
			
			$(this).focus(function() {
				if ($(this).attr('value') == pl) {
					$(this).attr('value', '');
					$(this).removeClass('gray');
				}
			});
			
			$(this).blur(function() {
				if ($(this).attr('value') == '') {
					$(this).attr('value', pl);
					$(this).addClass('gray');
				}
			});
		});
	}
	
	/** Newsletter Signup **/
	$('#nlSubmit').click(function() {
		var email = $('#nlEmail').val();

		// Make sure there's an email
		if ($.trim(email) == '') {
			return false;
		}
		else {
			// Make sure the email's valid
			if (checkEmail(email)) {
				encoded = encodeURI(email);
				var arr = email.split('.');
				var newEmail = '';
				
				// If we don't take out the dot in the address, the url won't work 
				for(var i=0; i < arr.length; i++) {
					if (i != 0) {
						newEmail += '|' + arr[i];
					}
					else {
						newEmail += arr[i]
					}
				}
				
				Shadowbox.open({
					content: 'http://' + window.location.host + '/forms/newsletter/' + newEmail,
					player: 'iframe',
					title: 'Subscribe to Coraid News',
					width: 640,
					height: 445,
					options: {
						overlayOpacity: 0.7
					}
				});
			}
		}
	});
	
	// Special SB links to include captions
	$('.sb-caption-link').click(function() {
		var desc = $(this).nextAll('.sb-caption');
		
		var div = $('#sb-info-description');
		
		if (div.size() == 0) {
			var div = $('<div id="sb-info-description">');
			$('#sb-wrapper').append(div);
		}
		
		div.html($(desc).html());
	});
});

/**
 * Tests attribute support in an element
 */
function elementSupportsAttribute(el, attr) {
	var test = document.createElement(el);
	
	if (attr in test) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * Validate email addresses
 */
function checkEmail(emailAddress) {
	//alert(emailAddress);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * Sets up the SB object based on which page/functionality
 */
function sbSetup(which) {
	$(function() {
		var els = $('a[rel*=shadowbox]');
		
		if (els.length > 0)
		{
			switch(which) {
				case "gallery":
					Shadowbox.setup(els, {
						gallery: "gallery",
						overlayOpacity: 0.7
					});
					break;
				
				case "resources":
					Shadowbox.setup(els, {
						overlayOpacity: 0.7,
						onClose: function() {
							window.location.reload(true);
						}
					});
					break;
					
				default:
					Shadowbox.setup(els, {
						overlayOpacity: 0.7
					});
					break;
			}
		}
		else
			Shadowbox.setup();
	});
}

/**
 * Removes the signup form and replaces it with Thank You
 */
function nlThankYou() {
	$(function() {
		$('#sfdcNewsletterSignup').fadeOut(300, function() {
			$('#nlConfirm').html('<p class="center-align">Thank You for subscribing to our Newsletter!</p>');
			$('#nlConfirm').fadeIn(300);
		});
	});
}
