$(document).ready(function ()
{
	//	DOM has loaded
	
	if($('#contactFormForm').length > 0)
	{
		$('#contactFormForm').data('ContactForm', new ContactForm());
	}
	
	if($('.obf').length > 0)
	{
		$('.obf').data('Obf', new Obf());
	}
	
	if($('.openInNewWindow').length > 0)
	{
		$('.openInNewWindow').data('OpenInNewWindow', new OpenInNewWindow());
	}
});

function ContactForm()
{
	//	allow access to the parent object in class methods
	var parent = this;
	
	//	replace the form submit with a better button
	$('#contactFormSubmitButton').after('<a href="#" id="contactFormSubmit" title="Send Your Message"><img src="/images/submit.gif" alt="Send Your Message" /></a>').remove(); 

	$('#contactFormSubmit').bind('click', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	$('#contactFormForm').bind('submit', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	function validateDataEntry()
	{
		//	serialize the form to json object
		var formData = {};
		$('#contactFormForm label').each(function()
			{
				formData[$(this).attr('for')] = $('#'+ $(this).attr('for') + '').val();
			}
		);
		
		$.post('/ajax/contact/send/', {
			'jsonFormData' : $.toJSON(formData),
			'ajax' : 1,
 		},
		function(jsonData)
		{
			if(jsonData.error == false)
			{
				$('#contactFormForm').html('<span class="success">' + jsonData.message + '</span>');
				
				$('#contactFormMessage').html('');
			}
			else
			{
				$('#contactFormMessage').html('<span class="error">' + jsonData.message + '</span>');
				
				if(typeof(jsonData.script) != 'undefined')
				{
					eval(jsonData.script);
				}
			}
		}, 'json');
	}
}

function Obf()
{
	//	allow access to the parent object in class methods
	var parent = this;
	
	$('.obf').after('<a href="mailto:&#115;&#121;&#110;&#99;&#104;&#64;&#115;&#121;&#110;&#99;&#104;&#116;&#97;&#110;&#107;&#46;&#110;&#101;&#116;">&#115;&#121;&#110;&#99;&#104;&#64;&#115;&#121;&#110;&#99;&#104;&#116;&#97;&#110;&#107;&#46;&#110;&#101;&#116;</a>').remove();
	
	//	encoder at
	//	http://htmlfixit.com/cgi-bin/tools/encoded_html_code.cgi
}

function OpenInNewWindow()
{
	//	allow access to the parent object in class methods
	var parent = this;
	
	$('.openInNewWindow').bind('click', function(event)
	{
		event.preventDefault();
		window.open($('.openInNewWindow').attr('href'));
	});
}