

$(function(){
	// Turn off the top right link 
	//$.Lightbox.construct({"show_linkback":false});
	
	// Activate the gallery lightbox
	$('a.gallery_image').lightBox({fixedNavigation:true});
	zIndexHandler();
	
});

// On page load
$(function(){
	// Supersleight the system so PNGs work
	$('body').supersleight({shim: '/images/blank.gif'});
	// Apply a main pull down to the top nav
	//$('.depMain').pullDown('300');
	//$('.depSub, subSub').subNav('0');

});

$(function() {
	zIndexHandler();
});

function zIndexHandler(){
	var zIndexNumber = 10000;
	$('ul#topnav, li.depMain').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
}

$(function(){
	/* Add a change event handler to the jump menu */
	$('#jsProjectsJumpMenu').change(function(){
		/* the url is in the value of the selected item, which we can use $(this) for */
		$url = $(this).val();
		/* redirect window please, thankyou */
		window.location = $url;
	});
});
/**
 * Email validator
 * LEAVE THIS ALONE!!
 * @param str
 * @return
 */
function validateEmail(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if(str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if(str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if(str.indexOf(" ")!=-1){
		return false;
	}
	return true;		
}


/**
 * Email Validator Function
 * 
 * @param 	str
 * @return	boolean
 */
function checkValidEmailWithoutMessage(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		return false;
	}

 	return true;				
}


$(function(){
	$('#enquiryForm').submit(function(){
		// Assume we are ready
		var ready = true;
		
		// Check and enquiry type has been selected
		$('select.required_dropdown').each(function(index){
			
			// If this input is blank
			if($(this).val() == " "){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		
		
		// Cycle through each of the input fields
		$('input.required_input').each(function(index){
			
			// If this input is blank
			if($(this).val() == ""){
				// Set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$(this).addClass('error');

				
			}else{
				// Otherwise do nothing as we are assuming ready and don't want to reset it
				
				// Make sure we remove any error classes
				$(this).removeClass('error');
			}
		});
		
		// We need to check for a valid email address
		if(ready == true){
			
			// Pass the email address to the valid email checking thingy
			if(checkValidEmailWithoutMessage($('input#email').val()) == true){
				// Has passed don't do anything
				
				// Make sure we remove any error classes
				$('input#email').removeClass('error');
				
			}else{
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('input#email').addClass('error');
			}
		}
		
		if(ready == true){
			
			// We need to check the textarea
			if($('textarea#message').val() == ""){
				// If we are blank set ready to false
				ready = false;
				
				// Highlight the field with an error class (this can have a border colour of red)
				$('textarea#message').addClass('error');
	
				
			}else{
				// we don't need to change anything
				
				// Make sure we remove any error classes
				$('textarea#message').removeClass('error');
			}
		}
		if(ready == true){
			
			// Allow the form to submit
			return true;
			
		}else{
			// Alert user and don't let the form submit
			alert('Please fill in all the required fields, and ensure you have entered a valid email address');
			// Return false
			return false;
		}
	});
});
