// JavaScript Document


/* 	determines the value of a url parameter
*	@param actstr	||	required string	||	a url string
*	@param name		||	required string	||	the name of the variable in the string
*/
function getURLParam( href, name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
 // var results = regex.exec( window.location.href );
 var results = regex.exec( href );
  if( results == null )
    return "";
  else
    return results[1];
}


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 
        }); 
	 
	$('.rounded').corner({
	  tl: { radius: 12 },
	  tr: { radius: 12 },
	  bl: { radius: 0 },
	  br: { radius: 0 },
	  antiAlias: true,
	  autoPad: true });
	
	$('.roundbottom').corner({
	  tl: { radius: 0 },
	  tr: { radius: 0 },
	  bl: { radius: 12 },
	  br: { radius: 12 },
	  antiAlias: true,
	  autoPad: true });
	
	$('.roundall').corner({
	  tl: { radius: 12 },
	  tr: { radius: 12 },
	  bl: { radius: 12 },
	  br: { radius: 12 },
	  antiAlias: true,
	  autoPad: true });
	
	
	/* 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();
	
	/* 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 );
  		 }
		
	});
	/*
	$('a').click(function() {
		deliverContent($(this));		
		return false;
	});
	*/
	function deliverContent(ele) {
		var h = $(ele).attr('href');
		var page = getURLParam(h,'page');
		
		$("body").append("<div id='transition_overlay'></div>");
		
		$.ajax({
			type: 'GET',
			url: 'content_functions.php',
			data: 'content='+page+'&ajax=true',
			success: function(msg){
				if(page!='home') {
					if($('#content').hasClass('span-13') == false) {
						$('#content').addClass('span-13');
						$('#content').removeClass('span-11');
					}
					if($('#sidebar').hasClass('smallside') == false) {
						$('#sidebar').addClass('smallside');
					}
				}
				else {
					$('#content').addClass('span-11');
					$('#content').removeClass('span-13');
					$('#sidebar').removeClass('smallside');
				}
				$('#content').html( msg);
				
				$('#content a').each(function() {
					$(this).bind('click',function() { deliverContent(); return false; });
				});
				$("*").remove("#transition_overlay");
				
			}
			
		});
		$.ajax({
			type: 'GET',
			url: 'content_functions.php',
			data: 'side='+page+'&ajax=true',
			success: function(msg){
				
				$('#sidebar').html( msg);
			}
		});
	}
						  	
});



