
	$(function(){

		jQuery.root = $(document)

		jQuery.root.find('#slideshow').innerfade({
			speed: 900,
			timeout: 4000,
			type: 'random',
			containerheight: '163px'
		})

		jQuery.root.find('#comment_form').postWithAjax({
			type: 'json'
		})

	})


	;(function($) {
		$.fn.postWithAjax = function(settings) {
			var option = $.extend({}, $.fn.postWithAjax.defaultSettings, settings)

			return this.submit(function() {
				form = $(this)

				if ($('.alert.error')[0]) $('.alert.error').slideUp(500, function() { $(this).remove() })

				if (!$('input[name=ajax]', form)[0]) form.append('<input name="ajax" type="hidden" value="true" />')

				$.post(form.attr('action'), form.serialize(), handleResponse, option.type)

				return false;
			})



			function handleResponse(data) {
				switch (data.status) {
					case 'win' :
						form.find('button').fadeOut()
						form.find('fieldset')
							.fadeOut(250, function() {
								$(this).remove()
								success_message = '<div class="alert done"><h3>Your Comment Has Been Posted</h3><p>Your comment was successfully submitted and is awaiting approval. It will appear here once it has been moderated.</p></div>';
								$(success_message).css({ display : 'none' }).insertAfter('#comment_form h3').slideDown()
							})
					break;

					case 'fail' :
						error_message = '<div class="alert error">';
						error_message+= '<h3>Oops!</h3>';
						error_message+= '<p>Something went wrong while submitting your comment. Check these errors and try again!</p>';
						error_message+= '<ul>';

						for (i = 0; i < data.errors.length; i++) {
							form_field = data.errors[i].field;
							form_error = data.errors[i].error;

							$('#' + form_field).addClass('error')

							error_message += '<li>' + form_error + '</li>';
						}

						error_message += '</ul>';
						error_message += '</div>';

						$(error_message).css({ display : 'none' }).insertAfter('#comment_form h3').slideDown()
					break;
				}
			}
		}

		$.fn.postWithAjax.defaultSettings = { type : 'html' }
	})(jQuery);


	//	Other functions

	function DESemail(emailname,emailserver) {
		document.write("<a href='mailto:" + emailname + "@" + emailserver +"'>");
		document.write(emailname + "@" + emailserver); 
		document.write("</a>"); 
	}

	// Accessible Pop Ups
	function DESopen(url,width,height,toolbar,scroll) {
		window.open(url, "", "scrollbars="+scroll+",toolbar="+toolbar+",height="+ height +",width="+width);
		return false;
	}