// ****************************************
// The Heritage Foundation Base JavaScript (with JQuery Help)
// Created by Brian Talbot on 2009-07-19
// (c) The Heritage Foundation
// ****************************************

// ========================================
// Utility functions (Not Loaded with DOM)
// ========================================

// ToggleText
$.fn.toggleText = function(a, b) {
	return this.each(function() {
		jQuery(this).text(jQuery(this).text() == a ? b : a);
	});
};

// FadeToggle
$.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

// SlideToggle
$.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

// Form Label Overlaying
$.fn.overLabel = function() {
  this.each(function(index) {
    var label = $(this); var field;
    var id = this.htmlFor || label.attr('for');
    
    if (id && (field = document.getElementById(id))) {
      var control = $(field);
      label.addClass("overlabel-apply");
      if (field.value !== '') {
      label.css("text-indent", "-9999px");
    }
    
    control.focus(function () {label.css("text-indent", "-9999px");}).blur(function () {
      if (this.value === '') {
        label.css("text-indent", "0px");
      }
    });
    
    label.click(function() {
      var label = $(this); var field;
      var id = this.htmlFor || label.attr('for');
      
      if (id && (field = document.getElementById(id))) {
        field.focus();
      }
    });
    }
  });
};

// Fading
$.fn.fadeDown = function(speed) {  
   return $(this).animate({	opacity: 0.01	}, speed);
};
$.fn.fadeUp = function(speed) {  
   return $(this).animate({	opacity: 1 }, speed);
};

$(document).ready(function(){

// ========================================
// Utility functions (Loaded with DOM)
// ========================================
	
  // Prepping JS-conditional styles
  $("body").addClass("enhanced");
	
	// Add utility first/last/alt classes to LIs.
	$("ul li:first-child, ol li:first-child").addClass("first");
	$("ul li:last-child, ol li:last-child").addClass("last");
	$("ul li:nth-child(even), ol li:nth-child(even), table tr:nth-child(even)").addClass("alt");
	$(".listing-photos ul li:nth-child(5n)").addClass("cinco");

	// Wrap the Inside of Button Elements
	$("button, .button").each(function() {
	  var hasSpan = $(this).find("span")[0];
	  if (hasSpan = "undefined") {
	    $(this).wrapInner("<span></span>");
	  }
	});
  
  // Overlabeling with Forms
  $("#newsletter-subscribe label").overLabel();
  
  // Heritage Hat Collapse & Expand
  var masthead = $('#masthead');
  var masthead_footer = $('#wrapper-masthead-footer');
  var extra_content = masthead.find('#wrapper-masthead-content').hide();
  
  $('#masthead-control a').click(function(){
    extra_content.slideFadeToggle('slow');
    $(this).toggleClass("expanded");
    $(this).toggleText("More Information About The Heritage Foundation","Hide This Information About The Heritage Foundation");
    return false;
  });
});