function formHide(f) {
	var fw = $(f).innerWidth();
	var fh = $(f).innerHeight();
	var fid = $(f).attr('id');
	$(f).parent('.fwrap').remove();
	$(f).wrap(function() {
		return '<div class="fwrap '+ fid +'" style="width:'+ fw +'px;height:'+ fh +'px" />';
	});
	$(f).fadeOut(400,function() {
		if ( ! $('div.fwrap.finished').length) $('div.fwrap.'+ fid).css('background','transparent url(/cr/images/spinner.gif) no-repeat center center');
	});
}
function formShow(f,m,r,h) {
	$(f).parent('.fwrap').prepend(m).addClass('finished').css('background','none');
	r = typeof(r) !== 'undefined' ? r : false;
	if (r) $(f).show();
	h = typeof(h) !== 'undefined' ? h : false;
	if (h && !r) $(f).parent('.fwrap').animate({height:h+'px'},200);
}

$(function() {


	if ($('ul.feature li').length) {
			if ($('ul.feature li').length > 1) {
				$('ul.feature').cycle({
					timeout:6000,
					speed: '0',
					pager:'#feature-ctrl',
					pagerAnchorBuilder:function(i,s) {
						return '<li><a href="#"></a></li>';
					}
				});
			} else {
				$('ul.feature li').show();
			}
		}

	// Primary Nav Superfish
	$('#primary-nav ul').supersubs({
			minWidth: 16,
			maxWidth: 24,
			extraWidth: 1
		}).superfish({
			autoArrows:false,
			dropShadows:false,
			delay:1000,
			animation:{opacity:'show',height:'show'},
			speed:300
			});
	
	
	// Contact Form
	// Validate & Handle Contact Form
	if ($('form#contact').length) {
		
		// Define Validation
		var contactVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						lname:$('input#contact-lname',f).val(),
						email:$('input#contact-email',f).val(),
						mobile:$('input#contact-phone',f).val(),
						addr:$('input#contact-addr',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						comments:$('textarea#contact-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							//_gaq.push(['_trackEvent','Forms','Form Submitted','contact']);
						} else {
							m = '<p>'+json.message+'</p>';
							//_gaq.push(['_trackEvent','Forms','Form Error','contact, json error']);
						}
						formShow(f,m);
					},
					error:function(x,t,e) {
						console.log([x,t,e]);
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						//_gaq.push(['_trackEvent','Forms','Form Error','contact']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#contact').validate(contactVal);
		
		// Mask Phone & Zip Input
		$('form#contact input#contact-phone').mask("999 999-9999",{placeholder:" "});
		$('form#contact input#contact-zip').mask("99999",{placeholder:" "});
		
	}
	
	
});





