	$(document).ready(function () {

		updateLinks();
		addMouseOver();

		$("input[type='text']").setupFormField();
		$("textarea").setupFormField();

		$("input[type='password']").setupPasswordField();

		if ($('#container > .colgroup > div').length && $('.pageContent_wider').length == 0) {
			$('#container > .colgroup > div').equalHeights(14);
		}
		if ($('.col > .colgroup > div').length && $('.pageContent_wider').length == 0) {
			$('.col > .colgroup > div').equalHeights(14);
		}
		
		$('#accordion').setupAccordion();
		$('form.validate').setupValidation();

	});


	updateLinks = function() {
		// Format any external URLs to go through the exit interstitial
		$("a[rel='ext']").formatExternalURL();
		
		// Set the target of any links that should open in a new window
		$("a[rel='new']").attr('target','_blank');
		
		// Any element that has the class .clickable, watch for click and take user to first href's URL.
		$(".clickable").click(
			function () {
				document.location = $(this).find('a:first').attr('href');
			}
		);
		
	}

	addMouseOver = function() {
		// Format any external URLs to go through the exit interstitial
		$(".mouseout").hover(
			function () {
				$(this).removeClass('mouseout').addClass('mouseover');
			}, 
			function () {
				$(this).removeClass('mouseover').addClass('mouseout');
			}
		);
	
	}



	jQuery.fn.setupFormField = function(theField) {
		return this.each(function() { 

			// Save original value
			// $(this).attr('originalValue',$(this).val());
			
			// Add focus action (selecting field)
	//		$(this).focus(function() {
	//			if ($(this).val() == $(this).attr('title')) {
	//				$(this).val('');
	//			}
	//			else {
	//				$(this).select();
	//			}
	//		});
			
			// Add blur action (moving off of field)
	//		$(this).blur(function() {
	//			if ($(this).val() == '') {
	//				$(this).val($(this).attr('title'));
	//			}					  
	//		});
	
		});
	};	

	jQuery.fn.setupPasswordField = function(theField) {
		return this.each(function() { 

			// Add focus action (selecting field)
			$(this).focus(function() {
				$(this).addClass('hidepasswordlabel');
			});
			
			// Add blur action (moving off of field)
			$(this).blur(function() {
				if ($(this).val().length == 0) {
					$(this).removeClass('hidepasswordlabel');
				}					  
			});
	
		});
	};	

	jQuery.fn.formatExternalURL = function() {
		return this.each(function() { 
			 
			// rewrite URL to go through the exit page
			var currentHref = $(this).attr('href');
			$(this).attr('href', 'interstitial.html?u=' + encodeURI(currentHref) + '&TB_iframe=true&height=360&width=484');
			
		});
	};	
	
	jQuery.fn.setupAccordion = function(el) {
		$(el).find('li h3 a').click(function() {
			if (!$(this).parent().parent().hasClass('expanded')) { 
				$(this).parent().parent().siblings().removeClass('expanded');
				$(this).parent().parent().addClass('expanded');
			} else {
				$(this).parent().parent().removeClass('expanded');
			};
		});
	};	
	
	jQuery.fn.setupValidation = function() {
		
		$(".validation_notice").hide();
		$(".validation_notice li").hide();
		
		this.submit(function() { 
			// alert('submit');
			var errors = new Array();
			
		
		
			$("input, select, textarea").removeClass("invalid");
			// passwords
			// check to see if pass1 and pass2 are long enough, then make sure they match 

			if (($("#password1").length > 0) && ($("#password2").length > 0)) {
				if ( ($("#password1").val().length < 4) || ($("#password2").val().length < 4) || ($("#password1").val() != $("#password2").val()) ) {
					
				errors.push("password");
				$("#password1").addClass("invalid");				
				$("#password2").addClass("invalid");
	
				}
			}
			
			// all other required form elements
			$(".required").each(function() {
				// detect if it's been modified
				if ( $(this).attr("title") == $(this).val() || $(this).val() == "") {
					
					// alert(this.id);
					// it hasn't, so show it's related LI
					errors.push(this.id);
					$(this).addClass("invalid");				
				}
			});

			// does it have one @
			if ($(".email").val().split("@").length == 2 ) {
				
				// does it have at least one dot in the 2nd half
				if ($(".email").val().split("@")[1].split(".").length > 1 ) {
					
					//we're ok folks!
					
				} else {
					// alert("no dot");
					errors.push("email");
					$(".email").addClass("invalid");					
				}
			} else {
				// alert("not the right number of @'s in the address");
				errors.push("email");
				$(".email").addClass("invalid");					
			}



			if ( errors.length > 0 ) {
				drawErrors(errors);
				$('#container > .colgroup > div').height("auto").css("overflow", "auto").equalHeights(14);
				return false;
			} else {
				// alert("submitting");
				return true;
			}
		});
	};	
	
	
	drawErrors = function(errors) {
		var notice = "";
		notice += "	<div class=\"validation_inside\">";
		notice += "		<h3>Required fields message. lorem ipsum dolor sit amet arcu. Ad duo quando.</h3>";
		// notice += "<p>" + errors + "</p>";
		notice += "		<ul class=\"traditional\">";
		
		( jQuery.inArray("first_name", errors) >= 0 ) ? 
			notice += "<li><strong>First Name:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("last_name", errors) >= 0 ) ?	
			notice += "<li><strong>Last Name:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("phone_home", errors) >= 0 ) ? 
			notice += "<li><strong>Home Phone:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("password", errors) >= 0 ) ? 
			notice += "<li><strong>Password:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("email", errors) >= 0 ) ? 
			notice += "<li><strong>Email:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("state", errors) >= 0 ) ? 
			notice += "<li><strong>State:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("referrer", errors) >= 0 ) ? 
			notice += "<li><strong>Referrer:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("relative", errors) >= 0 ) ? 
			notice += "<li><strong>Relative:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("job_eligibility", errors) >= 0 ) ? 
			notice += "<li><strong>Job Eligibility:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("name", errors) >= 0 ) ? 
			notice += "<li><strong>Name:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("subject", errors) >= 0 ) ? 
			notice += "<li><strong>Subject:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("comments", errors) >= 0 ) ? 
			notice += "<li><strong>Comments:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
		( jQuery.inArray("verify", errors) >= 0 ) ? 
			notice += "<li><strong>CAPTCHA:</strong> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Crassit amet velit.</li>" : null ; 
	
		notice += "		</ul>";
		notice += "	</div>";
		
		$('.validation_notice').html(notice).show();
		
	};	

