var $j = jQuery;
$j(document).ready(function () {
	// generateSprites arguments: 
	// 1st - parent class (the main class on the parent ul), with preceding period
	// 2nd - selected prefix (eg. for a selected class of "selected-about", use "selected-" as the value)
	// 3th - animation speed, in milliseconds (eg. 300 = 0.3 seconds)
	
	// example usage:
	// generateSprites(".navigation", "selected-", 300);
	// generateSprites(".top-nav", "position-", 200);
	// generateSprites(".sidebar-nav", "current-", 150);

	generateSprites(".nav", "current-", 250);
	setTimeout(initOverLabels, 50); //setTimeout is used to prevent labels from covering saved input values
	styleFieldsets();
	helpPopups();
	addTestimonialQuote();
});
/* BEGIN css sprites 2 nav behaviour
http://www.alistapart.com/articles/sprites2
=========================== */
function generateSprites(parent, selectedPrefix, hoverSpeed) {
	// throw the parent object's class into a variable
	var parentClass = $j(parent).attr("class");
	// start a loop that cycles through each of the li elements inside the parent element
	$j(parent).children("li").each(function() {
		// create a few variables that we'll need during this function:
		// myClass = the class of the object we're currently inspecting
		// current = what the selected class should look like for the parent of the object we're currently inspecting
		var myClass = ($j(this).attr("class"))
		var current = parent.substring(1) + " current-" + ($j(this).attr("class"));
		// turn on nav events for element this loop identifies
		attachNavEvents(parent, myClass, hoverSpeed);
		// let's hide the CSS-defined background image, but only if this isn't the currently-selected item
		if (parentClass != current) {
			$j(this).children("a").css({backgroundImage:"none"});
		}
	});
}
function attachNavEvents(parent, myClass, hoverSpeed) {
	$j(parent + " ." + myClass).mouseover(function() {
		// create pseudo-link
		$j(this).append('<div class="nav-' + myClass + '"></div>');
		// fade in the pseudo-link
		$j("div.nav-" + myClass).css({display:"none"}).fadeIn(hoverSpeed);
	}).mouseout(function() {
		// fade out & destroy pseudo-link
		$j("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
			$j(this).remove();
		});
	});
}
/* END css sprites 2
=========================== */

function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel(field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.left = (hide) ? '-32767px' : '6px';
      return true;
    }
  }
}


function styleFieldsets() {
	$j("fieldset").before("<hr />");
}

function helpPopups() {
	$j("span.help_content").css({
		display:"none",
		position:"absolute",
		top:"-40px",
		left:"200px",
		width:"300px",
		background:"#fff url(/images/bg_email_signup_footer.png) repeat-x 0 -1px",
		textAlign:"left",
		padding:"5px 10px 15px 10px",
		border:"1px solid #D8D5B3"
	});
	$j("span.help").css({
		display:"inline"
	});
	var containerDiv = document.getElementById("inner");
	var spanList = containerDiv.getElementsByTagName("span");
	var closeButton = $j("<span><b>Close</b></span>").prependTo("span.help_content");
	$j("span.help_content span").addClass("close_button");
	$j("span.help_content span.close_button b").click(function() {
		this.parentNode.parentNode.style.display = "none";
		showIE6Selects();
	});
	for(var i=0;i<spanList.length;i++) {
		if (spanList[i].className == "help") {
			spanList[i].onclick = function() {
				var helpContentSpan = this.nextSibling.nextSibling;
				helpContentSpan.style.display = "block";
				//Hide selects from IE
				$j("select").addClass("hide_from_ie6");
			}
		}
	}
	$j("body").click(function(e) {
		var obj;
		if(window.event) {
			var e = window.event;
			obj = e.srcElement;
		}
		else {
			e = e;
			obj = e.target;
		}
		if((obj.nodeName == "DIV") || (obj.nodeName == "BODY")) {
			$j("span.help_content").css("display","none");
			showIE6Selects();
		}
	});
	function showIE6Selects() {
		//Show hidden selects in IE
		$j("select").removeClass("hide_from_ie6");
	}
}
function addTestimonialQuote() {
	var divList = document.getElementsByTagName("div");
	for (var j = 0; j < divList.length; j++) {
		if(divList[j].className == "testimonial") {
			var closingQuote = document.createElement("img");
			closingQuote.src = "/images/testimonials_quote_closed.png";
			closingQuote.alt = "";
			var testimonialPara = (divList[j].getElementsByTagName("p")[0]);
			var testimonialCite = (divList[j].getElementsByTagName("cite")[0]);
			var testimonialText = testimonialPara.firstChild;
			testimonialPara.insertBefore(closingQuote,testimonialCite);
		}
	}
}