// Make sure jquery is loaded
if( typeof jQuery == 'function' )
{
	// On document ready:
	$(document).ready(function()
	{
		//console.log("jquery loaded");
		init();
	});

}

// Contact form validation
var validationSet =
{
	contact_text:
	{
		error: "Comment is too short or contains illegal symbols. You can only use letters, numbers, and some symbols (-\"'$%!?.,).",
		// pattern: /[\d\w\s\345\344\366\305\304\326"\$\?\.\(\)\'\!:,%\-]+/ig
		pattern: /[\d\w\s\345\344\366\305\304\326\$\?\.\(\)\!:,%\-]+/ig
	},
	contact_email:
	{
		error: "The e-mail address you entered is not valid. Please correct and send again.",
		pattern: /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i
	},
	contact_captcha:
	{
		error: "You have to pass the humanity-check. What is 3+4?",
		pattern: /7/
	}
}

function validateCommentForm(e)
{
	// Validate new comment input
	var validated = true;
	var errors = new Array();

	// Go through elements in validationSet (declared at top of script)
	for(var fieldName in validationSet)
	{
		var textEntered = $('#'+fieldName).val().replace(/^\s+|\s+$/g, ''); // trim whitespace
		var matchSet = textEntered.match( validationSet[fieldName].pattern );

		if( matchSet != null && textEntered == matchSet[0] )
		{
			// Form field OK. Remove error class, if any.
			$('#'+fieldName).removeClass('error');
		}
		else
		{
			// Not valid, pattern not matched. Set flag, alert user.
			validated = false;
			$('#'+fieldName).addClass('error');
			errors.push( validationSet[fieldName].error )
		}
	}

	if( validated )
	{
		// GO.
		//$('new_comment_form').submit();
		return true;
	}
	else
	{
		var errMsg = "There were some errors. Please correct these and resubmit:\n\n";
		for(var i in errors)
			errMsg += errors[i] + "\n\n";
		alert(errMsg);
		return false;
	}
}

// Initialize behaviors
function init()
{		
	// Contact form validation
	$('#contact_form').submit(validateCommentForm);
	
	// Lightbox Slideshows
	if($('.slideshow').length >= 1)
	{
		$(function() {
			$('a[@rel*=lightbox]').lightBox({
				//txtImage: $(this).attr('title')
			});
		});		
	}
}
