$(document).ready( function () {

	//mega menu
	//$('.megaContainer').hide()
	function showMega(){  
		if($(this).find('.megaContainer').length == 1){
			$(this).find('h1 a').addClass('megaMenuItemActive');
			$(this).find('.megaContainer').fadeIn('fast');
			$(this).find('.mega-chevron').hide();
		} 
	}  
	function hideMega(){  
		if($(this).find('.megaContainer').length == 1 && $(this).find('.freezeMega').length != 1){
			$(this).find('.megaContainer').fadeOut('fast');
			$(this).find('h1 a').removeClass('megaMenuItemActive');
			$(this).find('.mega-chevron').show();
		} 
	}  
	var megaConfig = {      
		interval: 100,  
		sensitivity: 4,
		over: showMega,
		timeout: 100,  
		out: hideMega  
	}; 
	$('#mainNav .mega').hoverIntent(megaConfig) 
/*
  $('#mainNav .mega').hover(function() {
    if($(this).find('.megaContainer').length == 1){
      $(this).find('h1 a').addClass('megaMenuItemActive');
      $(this).find('.megaContainer').show();
	  $(this).find('.mega-chevron').hide();
	}
  },function() {
    if($(this).find('.megaContainer').length == 1 && $(this).find('.freezeMega').length != 1){
      $(this).find('.megaContainer').hide();
	  $(this).find('h1 a').removeClass('megaMenuItemActive');
	  $(this).find('.mega-chevron').show();
	}
  });
  */
  
  
// search bar
  var originalInput = $("#searchInput").val();
  var saveInput = null;
  $("#searchInput").focus( function () {
    saveInput = $(this).val();
    if(saveInput == 'Search') {
      $(this).val("");
    }
  });
  $("#searchInput").blur( function () {
    if($(this).val() == "") {
      $(this).val('Search');
    }
  });
  $("#searchButton").click( function () {
    var currentInput = $("#searchInput").val();
    if(currentInput == "" || currentInput == 'Search') {
      alert("Please type in the text you would like to search the site for.");
      $("#searchInput").focus();
      return false;
    }
  });

 

  // blog
  $('.blogCommentButton').hover(function() {
    $(this).addClass('blogCommentButtonHover');
  },function() {
    $(this).removeClass('blogCommentButtonHover');
  });
  
  //popular posts more button
	$('#popularShowMore').click(function() {
		$('#popularShowMore').hide();
		$('.popularMore').show();
		return false;
	});
	$('#popularShowLess').click(function() {
		$('#popularShowMore').show();
		$('.popularMore').hide();
		return false;
	});
	
//hide special blog comment input fields from real visitors - available to spam bots
  $('#blogCommentFirstName').hide();
  $('#blogCommentLastName').hide();

  $('#blogCommentButtonSubmit').click(function() {
    if(!$('#blogCommentName').val() || !$('#blogCommentText').val()) {
      alert('Both your name and a comment are required');
      return false;
    }
    else {
      return true;
    }
  });
  $('#blogCommentButtonReset').click(function() {
    $('#blogCommentName').val('');
    $('#blogCommentText').val('');
    return false;
  });

//blog subnav controls
  $('.blogSubNavLink').click(function() {
    $(this).closest('.blogSubNavMonth').find('ul').toggle('fast');
    return false;
  });

  //fix embeded objects so they don't shine through
  $('object').append('<param name="wmode" value="opaque"></param>');
  
	//hide the fsf inputs (fight spam fields)
	$('.bomberCMSform-fsf').hide();

	//show custom form success popup
	if($('#custom_form_success').length ){
		alert("Thank you!\n\nYour information was submitted successfully");
	}
	if($('#custom_form_fail').length ){
		alert("There was a problem with the information you submitted\n\nPlease check it and try again");
	}
	//handle validation of custom forms
	$('.customFormSubmit').click( function () {
		var status;
		$(this).closest('.customForm').find('.require').each(function() {
			if($(this).attr('type') == 'checkbox' && $(this).attr('checked') == false) {
				status = false;
				$(this).prev().addClass('customFormFail');
				
				$(this).focus();
				alert("You must agree to the terms.\n\nPlease complete the form and try again");
				return false;
			}
			else if($(this).val() == "") {
				status = false;
				$(this).prev().addClass('customFormFail');
				$(this).focus();
				alert("A required field was left blank.\n\nPlease complete the form and try again");
				return false; 
			}
			else if($(this).attr('class') == "require number" && isNaN($(this).val())) {
				status = false;
				$(this).prev().addClass('customFormFail');
				$(this).focus();
				alert("This field only accepts numbers.\n\nPlease correct the form and try again");
				return false; 
			}
			else {
				status = true;
				$(this).prev().removeClass('customFormFail');
				return true;
			}
		});
		return status;
	});
});
