$(document).ready(function ()
{
	//	DOM has loaded
	
	$(function()
	{
		$.beautyOfCode.init('/javascript/jquery/syntaxhighlighter/clipboard.swf');
		$.beautyOfCode.beautifyAll();
	});
	
	if($('#commentsForm').length > 0)
	{
		$('#commentsForm').data('Comments', new Comments());
	}
	
	if($('#contactForm').length > 0)
	{
		$('#contactForm').data('ContactForm', new ContactForm());
	}

	//	init clear on focus behaviour
	$('.clearOnFocus').clearOnFocus();
});

function Comments()
{
	//	allow access to the parent object in class methods
	var parent = this;
	
	//	replace the form submit with a better button
	$('#commentsFormSubmitButton').after('<a href="#" class="iconButton addComment" id="commentsFormSubmit" title="Add Your Comment">Add</a>').remove(); 
	
	$('#commentsFormSubmit').bind('click', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	$('#commentsForm').bind('submit', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	function validateDataEntry()
	{
		$.post('/ajax/comments/add/', {
			'ajax' : 1,
			'comments' : $('#comments').val(),
			'name' : $('#name').val(),
			'captcha' : $('#captcha').val(),
			'tableName' : $('#tableName').val(),
			'tableID' : $('#tableID').val()
		},
		function(jsonData)
		{
			if(jsonData.error == false)
			{
				window.location.reload();
			}
			else
			{
				$('#commentsMessage').html('<span class="error">' + jsonData.message + '</span>');
				
				if(typeof(jsonData.script) != 'undefined')
				{
					eval(jsonData.script);
				}
			}
		}, 'json');
	}
	
	this.handleCaptchaClick = function()
	{
		var currentTime = new Date();
		
		$(this).attr({src: '/images/captcha/reload=' + currentTime.getTime()});
		
		$('#captcha').focus();
	}
	
	if($('.captcha').length > 0)
	{
		$('.captcha').unbind('click', parent.handleCaptchaClick);
		$('.captcha').bind('click', parent.handleCaptchaClick);
	}
};

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="#" class="iconButton submit" id="contactFormSubmit" title="Send Your Message">Send</a>').remove(); 

	$('#contactFormSubmit').bind('click', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	$('#contactForm').bind('submit', function(event)
	{
		event.preventDefault();
		validateDataEntry();
	});
	
	function validateDataEntry()
	{
		//	serialize the form to json object
		var formData = {};
		$('#contactForm 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)
			{
				$('#contactForm').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');
	}
}