$(function() {
	
	
	
	// Slideshow
	if ($('#feature ul.slides li').length > 1) {
		$('#feature ul.slides').cycle();
	} else {
		$('#feature ul.slides li').css({display:'block'});
	}
	
	
	
	$('#primary-nav.menu > ul > li').hover(
		function(){
			$('> ul',this).show();
		},
		function(){
			$('> ul',this).hide();
		});
	
	
	
	// Forms
	
	// Form :: Email Subscription
	if ($('form#em-sub').length) {
		
		var emSubValidate = {
			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:'em-sub',
						'em-sub':'1',
						jssub:'1',
						fname:$('input#em-sub-fname',f).val(),
						lname:$('input#em-sub-lname',f).val(),
						email:$('input#em-sub-email',f).val(),
						zip:$('input#em-sub-zip',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h1>Thank You!</h1><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h1>There was a problem subscribing.</h1><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h1>There was a problem subscribing.</h1></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">* required field</span>');
				}
			}
		};
		$('form#em-sub').validate(emSubValidate);
	}
	
	// Form :: Contact
	if ($('form#contact').length) {
		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) {
				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(),
						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(),
						email:$('input#contact-email',f).val(),
						'em-sub':$('input#contact-em-sub:checked',f).length,
						phone:$('input#contact-phone',f).val(),
						comments:$('textarea#contact-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h1>Your submission has been received. Thanks for your interest!</h1><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h1>There was a problem sending your message.</h1><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h1>There was a problem sending your message. Please try again later.</h1></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">* required field</span>');
				}
			}
		};
		$('form#contact').validate(contactVal);
	}
	
	// Make Buttons Submit Forms (IE <= 7 Only)
	if ($.browser.msie && $.browser.version <= 7 && $('form button.sub').length) {
		$('form button.sub').click(function() {
			$(this).parents('form').submit();
		});
	}
	
	// Additional Form Functions
	function formShade(el,notify) {
		$('div.alert',el).remove();
		$(el).fadeOut(400, function() {
			if (notify !== undefined && notify != '') {
				$(el).before('<div class="notify">'+notify+'</div>');
			}
		});
	}
	function formReveal(el,str,kf) {
		$('div.notify').remove();
		$(el)
			.fadeOut(200,function() {
				if (kf===true) {
					$(this).prepend(str);
				} else {	
					$(this).html(str);
				}
			})
			.fadeIn(400);
	}
	
	
})
