$(document).ready(function () {


	$("input[type='text']").setupFormField();
	$("textarea").setupFormField();
	$("#nav li").setupHover();
	
	$("input[type='password']").setupPasswordField();
	
	//setWrapperClicks();
	setBackgroundImageSize();
	setMainContentHeight();
	
	if (($('body').hasClass('reveal')) && ((typeof document.body.style.maxHeight != "undefined"))) {
		setTimeout("navSlide()", 1000);
	} else {
		$('#nav').css('top','0');
		$('#mainContent').css('display','block');
	}

	$('form.validate').setupValidation();
	$('.accordion').accordion();

		$("ul.sf-menu").superfish({ 
			pathClass:  'current' 
		}); 
    
});

navSlide = function() {
	$("#nav").animate({"top": "+=65px"}, 500, 'jswing', fadeContent);
}
fadeContent = function() {
	 $('#mainContent').fadeIn(700);
	 //$('#mainContent').slideDown(1000);
}


window.onresize = function() {
	setBackgroundImageSize();
	setMainContentHeight();
}

setBackgroundImageSize = function() {

	var docHeight = $(document).height();
	var docWidth = $(document).width();
		
	// preserve aspect ratio
	var imgHeight = (docWidth/1024)*768
	
	$('#background img').css({width:docWidth,height:imgHeight,margin:"0 auto"});

}

setMainContentHeight = function() {
	
	$('#mainContent').height('auto')

	var docHeight = $(document).height() - 200;
	var mainContentHeight = $('#mainContent').height();
	
	if (mainContentHeight < docHeight) {
		$('#mainContent').height(docHeight);
	} else {
		$('#mainContent').height(mainContentHeight);	
	}

}

setWrapperClicks = function() {

	$('#foreground').css('opacity', '0.5');
	
	$('#nav_secondary4').click(function () {
		$('#nav_secondary4 > a').css({'background-position': '-329px bottom'});
		$('#nav_secondary4').css({'z-index': '1000'});
		$('#nav_secondary4 ul').css('display', 'block');
		if (typeof document.body.style.maxHeight != "undefined") {//if NOT IE 6
			$('#foreground').fadeIn('slow');
		}
		return false;
	});
	$('#nav_secondary5').click(function () {
		$('#nav_secondary5 > a').css({'background-position': '-736px bottom'});
		$('#nav_secondary5').css({'z-index': '1000'});
		$('#nav_secondary5 ul').css('display', 'block');
		if (typeof document.body.style.maxHeight != "undefined") {//if NOT IE 6
			$('#foreground').fadeIn('slow');
		}
		return false;
	});
	$('#foreground, #nav, #container').click(function () {
		$('#nav_secondary4 > a').css({'background-position': '-329px top'});
		$('#nav_secondary5 > a').css({'background-position': '-736px top'});
		$('#nav_secondary4').css({'z-index': ''});
		$('#nav_secondary5').css({'z-index': ''});
		$('#nav_secondary5 ul').css('display', 'none');
		$('#nav_secondary4 ul').css('display', 'none');
		if (typeof document.body.style.maxHeight != "undefined") {//if NOT IE 6
			$('#foreground').fadeOut();
		}
	});
}

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.setupHover = function() {
	return this.each(function() {
	
		$(this).hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		});
	
	});
};

jQuery.fn.accordion = function() {
	return this.each(function() {
	
		//initialize
		$(this).find('dd').hide();
		$(this).find('dt.expanded').next('dd').show();
		
		$(this).find('dt').each(function() {
			$(this).click(
			function() {
				if ($(this).hasClass('expanded')) {
					$(this).removeClass('expanded').next('dd').slideUp(300);
				} else {
					$(this).addClass('expanded').next('dd').slideDown(300);
				}
			});
		});
	});
}; 

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");
			}
		});
		
		// if email exists
		if ($(".email").length > 0) {
		
			// 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);
			return false;
		} else {
			// alert("submitting");
			return true;
		}
	});
};
