/**
 *	Javascripts for the Link site.
 *	The scripts below should be included in each page.
 *
 *	@author		Ron groenewoud <ron@7u.nl>
 *	@depends	jQuery
 *	@date		2011-04-22
 */

 

/*********************************************************************
 * UNIFORM RELATED STUFF
 ********************************************************************/

// Only proceed if uniform has been defined
if (typeof jQuery.fn.uniform != 'undefined') {

	// Start functioning when DOM is ready
	$(function() {

		$("select, input:checkbox, input:radio, input:file").uniform();
	
	});
}

/*********************************************************************
 * RANDOM IMAGES FOR THE HEADER
 ********************************************************************/

$(document).ready(function()
{
	// Get the current length of the slideshow.
	var slideLength = ($("#slideshow .slide-image").length - 1);
	
	// start positions.
	var slideToShow = 1;
	var slideToHide = 0;
	
	// Always show the first slide first.
	$(".slide-image:first").css("display", "block");
	
	// Start the slide show.
	setTimeout(function()
	{
		swapSlide();
	}, 10000);
	
	function swapSlide()
	{
		// Fade out / fade in.
		$("#slide-"+slideToShow).fadeIn(4000);
		$("#slide-"+slideToHide).fadeOut(4000);
		
		// The next slide to hide is the slide that we have just shown.
		slideToHide++;
		
		// Update the slide to show var.
		slideToShow++;
		
		// Check if we haven't reached the max.
		if(slideToShow > slideLength) slideToShow = 0;
		if(slideToHide > slideLength) slideToHide = 0;
		
		setTimeout(function()
		{
			swapSlide();
		}, 10000);
		
	}
	
	// Enforce the phone contact.
	$("#contact_phone").click();
	
	// Click on the send form button.
	$("#btn_verzenden").click(function()
	{
		// Flag to indicate that we have errors.
		formErrors = false;
	
		// Remove errors.
		$("#name_error").hide();
		$("#media_error").hide();
	
		// Check if the name was filled.
		if($("#input_name").val() == "")
		{
			$("#name_error").html('U heeft uw naam niet ingevuld');
			$("#name_error").slideDown("normal");
			formErrors = true;
		}
		
		// Check what we have to check, e-mail or phone?
		if($("#contact_phone").attr("checked"))
		{
			// Is the phone empty?
			if($("#input_media").val() == "")
			{
				$("#media_error").html('U heeft uw telefoonnummer niet ingevuld');
				$("#media_error").slideDown("normal");
				formErrors = true;
			}
		}
		else
		{
			// Is the e-mail empty?
			if($("#input_media").val() == "")
			{
				$("#media_error").html('U heeft uw e-mailadres niet ingevuld');
				$("#media_error").slideDown("normal");
				formErrors = true;
			}
			
			// Is the e-mail valid?
			if(!isValidEmail($("#input_media").val()))
			{
				$("#media_error").html('Het e-mailadres is niet geldig');
				$("#media_error").slideDown("normal");
				formErrors = true;
			}
		}
		
		// If we have no form errors, we can submit the form.
		if(formErrors == false)
		{
			$("#frm_contact").submit();
		}
	});
	
	// Check if we are clicking on the phone contact method.
	$("#contact_phone").click(function()
	{
		$("#media_label").html('Telefoon:');
		$("#input_media").attr("name", "Telefoon");
	});
	
	// Check if we are clicking on the email contact method.
	$("#contact_email").click(function()
	{
		$("#media_label").html('E-mail:');
		$("#input_media").attr("name", "Email");
	});
});

/**
 *  @desc   Method to check if the given e-mailaddress is valid.
 *  @param  string  emailAddress
 *  @return boolean
 */
function isValidEmail(emailAddress)
{
    if (emailAddress.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
    else return false;
}



