/*
 *	jQuery Custom Functions
 *	By Layered Pixels
*/

var j = jQuery.noConflict();

/* Slide Show (Frontpage) */
function InitSlideshow() {
	j('#slides').cycle({
		fx:'fade',
		timeout:3500,
speed:1000,
		pager:'#slide_navigation',
		after:update_slide_caption,
		before:fade_slide_caption
	});
}

fade_slide_caption = function(next, previous) {
	caption_container = j('#project_caption')
	caption_container.fadeOut('fast')
}

update_slide_caption = function(next, previous) {
	caption_container = j('#project_caption')
	caption = j('span.slide_caption', previous)
	caption_container.fadeIn('fast')
	caption_container.html(caption.html())
}


j(document).ready(function() {
	
	/* Slideshow Init */
	InitSlideshow();
	
	/* Dropdown Navigation */
	j('div.menu ul.children').hide();
	j('div.menu ul li').hover(function(){
		j(this).find('ul').show("fast", "easeInOutQuart");
	},
	function(){
		j(this).find('ul.children').hide("fast","easeOutSine");
	})

	/* Contact Form */
	j('#contactform').submit(function() {
	
		// Disable the submit button
		j('#contactform input[type=submit]')
			.attr('value', 'Sending message…')
			.attr('disabled', 'disabled');
	
		// AJAX POST request
		j.post(
			j(this).attr('action'),
			{
				name:j('#name').val(),
				email:j('#email').val(),
				message:j('#message').val()
			},
			function(errors) {
				// No errors
				if (errors == null) {
					$('#contactform')
						.hide()
						.html('<h3>Thank you</h3><p>Your message has been sent.</p>')
						.show();
				}
	
				// Errors
				else {
					// Re-enable the submit button
					j('#contactform input[type=submit]')
						.removeAttr('disabled')
						.attr('value', 'Send your Question');
	
					// Technical server problem, the email could not be sent
					if (errors.server != null) {
						alert(errors.server);
						return false;
					}
	
					// Empty the errorbox and reset the error alerts
					j('#contactform .errorbox').html('<ul></ul>').show();
					j('#contactform li').removeClass('alert');
	
					// Loop over the errors, mark the corresponding input fields,
					// and add the error messages to the errorbox.
					for (field in errors) {
						if (errors[field] != null) {
							j('#' + field).parent('li').addClass('alert');
							j('#contactform .errorbox ul').append('<li>' + errors[field] + '</li>');
						}
					}
				}
			},
			'json'
		);
	
		// Prevent non-AJAX form submission
		return false;
	});

	
})
