// JavaScript Document

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );

function cmxform(){
	// Hide forms
	$( 'form.cmxform' ).hide().end();
	
	// Processing
	$( 'form.cmxform' ).find( 'li>label' ).not( '.nocmx' ).each( function( i ){
		var labelContent = this.innerHTML;
		var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
		var labelSpan = document.createElement( 'span' );
			labelSpan.style.display = 'block';
			labelSpan.style.width = labelWidth;
			labelSpan.innerHTML = labelContent;
		this.style.display = '-moz-inline-box';
		this.innerHTML = null;
		this.appendChild( labelSpan );
		} ).end();	
	
	// Show forms
	$( 'form.cmxform' ).show().end();
}

$(document).ready(function(){ 
	 $('.sf-menu').superfish({ 
            animation: {height:'show'},   // slide-down effect without fade-in 
            delay:     1200               // 1.2 second delay on mouseout 
        }); 
	 
	/* Wrap nested legend tags in cmxform with p tags to prevent the text from flowing beyond the boundaries of the fieldset.
		This allows for page validation and works with the design */
	$( 'form.cmxform' ).find( 'li>fieldset>legend' ).not( '.nocmx' ).each( function( i ){
		$(this).wrap('<p></p>');
		
	}).end();
	
	$.localScroll();
	
	/* Ajax Validation and Submission of Contact Form */
	$('#contactform').validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit( {
				target: '#response',
				url: 'eml.php?ajax=true',
				success: function() {
					$('#response').show('normal');
					$('#name').attr('disabled','disabled');
					$('#eml').attr('disabled','disabled');
					$('#msg').attr('disabled','disabled');
					$('#submitted').attr('disabled','disabled');
				}
			});
			return false;
		},
		errorElement: "div",
		errorClass: "invalid",
		errorPlacement: function(error, element) {
			error.insertAfter( element );
		}
	});
							  	
});



