$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname != location.hostname);
};

$(function() {
	
	
	
	// To Make Buttons Submit Forms (IE <= 7 Only)
	if ($.browser.msie && $.browser.version <= 7) {
		$('form button').click(function() {
			$(this).parents('form').submit();
		});
	}
	
	
	
	// Open PDFs in New Window
	if ($('a.pdf').length) {
		$('a.pdf').attr('target','_blank');
	}
	
	
	
	// Open External Links in New Window
	$('a:external').attr('target','_blank');
	
	
	
	// Primary Nav Hover
	// $('ul#nav li').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')});
	$('ul#nav').supersubs({
		minWidth: 12,
		maxWidth: 24,
		extraWidth: 1
	}).superfish({
		autoArrows:false,
		dropShadows:false,
		delay:1000,
		animation:{opacity:'show',height:'show'},
		speed:300
		});
	
	
	
	// Section Nav Hover
	if ($('ul.section-nav').length) {
		$('ul.section-nav li:not(.here)').hover(
			function(){$(this).addClass('here');},function(){$(this).removeClass('here');}
			);
	}
	
	
	
	// Feature Rotation
	if ($('ul#feature').length) {
		$('ul#feature').cycle({
			timeout:5000,
			speed:500,
			fx:'fade',
			cleartype:!$.support.opacity,
			pager:'div#feature-anchors',
			pagerAnchorBuilder:function() {
				return '<a href="#"></a>';
			}
		});
	}
	
	
	
	// Add Image Margins on Sub Pages
	$('div.sub-content img').each(function() {
		var f = $(this).css('float');
		if (f == 'right') $(this).addClass('f-right');
		if (f == 'left') $(this).addClass('f-left');
	})
	
	
	
	// For Validating Forms (Incomplete--Needs Configuration) [[REQUIRES: jq_validate.js, jq_input_mask.js]]
	if ($('form#contact').length) {

		// Validate Contact Form
		var conSubVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				message:'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) {
				formShade(f);
				$.ajax({
					type:'POST',
					url:'/cr/php/processor.php',
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						lname:$('input#contact-lname',f).val(),
						bname:$('input#contact-bname',f).val(),
						email:$('input#contact-email',f).val(),
						phone:$('input#contact-phone',f).val(),
						addr1:$('input#contact-addr1',f).val(),
						addr2:$('input#contact-addr2',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						message:$('textarea#message',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h2>Thanks for your interest!</h2><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h2>There was a problem submitting the form.</h2><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h2>There was a problem submitting the form.</h2></div>');
					}
				});
			},
			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(conSubVal);

		// Mask Phone # Input
		$('form#contact input#contact-phone').mask("999-999-9999",{placeholder:" "});
		$('form#contact input#contact-zip').mask("99999",{placeholder:""});
	}
	
	
	
	// For Validating Forms (Incomplete--Needs Configuration) [[REQUIRES: jq_validate.js, jq_input_mask.js]]
	if ($('form#signup').length) {

		// Validate Contact Form
		var conSubVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formShade(f);
				$.ajax({
					type:'POST',
					url:'/cr/php/processor.php',
					dataType:'json',
					data:{
						action:'signup',
						jssub:'1',
						fname:$('input#signup-fname',f).val(),
						lname:$('input#signup-lname',f).val(),
						bname:$('input#signup-bname',f).val(),
						email:$('input#signup-email',f).val(),
						phone:$('input#signup-phone',f).val(),
						addr1:$('input#signup-addr1',f).val(),
						addr2:$('input#signup-addr2',f).val(),
						city:$('input#signup-city',f).val(),
						state:$('select#signup-state',f).val(),
						zip:$('input#signup-zip',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h2>Thanks for your interest!</h2><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h2>There was a problem submitting the form.</h2><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h2>There was a problem submitting the form.</h2></div>');
					}
				});
			},
			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#signup').validate(conSubVal);

		// Mask Phone # Input
		$('form#signup input#signup-phone').mask("999-999-9999",{placeholder:" "});
		$('form#signup input#signup-zip').mask("99999",{placeholder:""});
	}
	
	
	
	// Part of Our Form Validation Process
	function formShade(el) {
		$(el).fadeOut(400);
	}
	function formReveal(el,str) {
		$(el)
			.fadeOut(200,function() {
				$(this).html(str);
			})
			.fadeIn(400);
	}
	
	
	
});