var $j = jQuery.noConflict();

$j(document).ready(function() {

	// add class names to all sprite-based navigation tier 1 children
	var tabArray = $j("ul.nav-tabs-noimage > li");
	for (j=1; j<tabArray.length+1; j++)
	{
		$j(tabArray[j-1]).addClass("nav"+j);
	}

	// add class names to all sprite-based navigation tier 1 children
	var spriteArray = $j("ul.nav-main-sprites > li");
	for (j=1; j<spriteArray.length+1; j++)
	{
		$j(spriteArray[j-1]).addClass("nav"+j);
	}

	// add class names to all superfish-based navigation tier 1 children
	var spriteArraySf = $j("ul.nav-main-sf > li");
	for (j=1; j<spriteArraySf.length+1; j++)
	{
		$j(spriteArraySf[j-1]).addClass("nav"+j);
	}

	// add class names to all superfish sprite-based navigation tier 1 children
	var spriteArraySfSprite = $j("ul.nav-main-sprites-sf > li");
	for (j=1; j<spriteArraySfSprite.length+1; j++)
	{
		$j(spriteArraySfSprite[j-1]).addClass("nav"+j);
	}

	// handle testimonial quote images
	var blockquotes = document.getElementsByTagName("blockquote");
	for (j=0; j<blockquotes.length; j++)
	{
		var thisBlockInfo = blockquotes[j].innerHTML;
		var firstQuote = "<span class=\"quote_lt\">\"</span>";
		var midQuote = "<span class=\"quote_lastword\">";
		var lastQuote = "<img class=\"quote_rt\" src=\"images/bg_quoteright.gif\" width=\"15\" height=\"11\" /></span>";
		//var thisBlockBuilder = "<span class=\"quote_lt\">\"</span>" + thisBlockInfo[0].nodeValue.substring(1, thisBlockInfo[0].nodeValue.length);
		var quoteSplit = thisBlockInfo.split(" ");
		//alert(quoteSplit[0].substring(1, quoteSplit[0].length));
		quoteSplit[0] = firstQuote + quoteSplit[0].substring(1, quoteSplit[0].length);
		quoteSplit[quoteSplit.length-1] = midQuote + quoteSplit[quoteSplit.length-1].substring(0, quoteSplit[quoteSplit.length-1].length-1) + lastQuote;

		var thisQuoteJoined = quoteSplit.join(" ");
		blockquotes[j].innerHTML = thisQuoteJoined;
	}

	// handle first and last items in a ul.pipe list
	$j("ul.pipe li:first-child").addClass("first");
	$j("ul.pipe li:last-child").addClass("last");

	// handle first and last items in a ul.nav-tabs-noimage list
	$j("ul.pipe li:first-child").addClass("first");
	$j("ul.pipe li:last-child").addClass("last");

	// handle first and last items in a ul.nav-main-sf list
	$j("ul.nav-main-sf li:first-child").addClass("first");
	$j("ul.nav-main-sf li:last-child").addClass("last");

	// handle first and last items in a ul.nav-main-sf list
	$j("ul.nav-main-sprites-sf li:first-child").addClass("first");
	$j("ul.nav-main-sprites-sf li:last-child").addClass("last");

	// add non-breaking space character to the end of all anchor tags in a superfish list.
	// this fixes a text fade glitch
	$j("ul.nav-main-sf a").append("&nbsp;");

	// apply superfish functionality to all unordered lists with a class of sf-menu

	// handle first items in a select drop-down list
	$j("select option:first-child").addClass("first");

	// handle form selection class change when an item is selected
	$j("select").change(function() {
		if (this.selectedIndex == 0)
		{
			$j(this).removeClass();
		}
		else
		{
			$j(this).addClass("active");
		}
	});

	// handle radio button click event
	$j(":radio").click(function() {
		$j(this).siblings("label").removeClass();
		var thisId = this.getAttribute('id');
		$j(this).siblings("label[for='"+thisId+"']").addClass("current");
	});

	// handle radio button style on page load
	function initRadioCurrent () {
		var thisRadioChecked = $j(":radio:checked");
		for (var i = 0; i < thisRadioChecked.length; i++)
		{
			var thisId = thisRadioChecked[i].getAttribute('id');
			$j("label[for='"+thisId+"']").addClass("current");
		}
	}

	// make sure current radio buttons get the current class
	initRadioCurrent();

	// apply functionality to all "form1" forms
	function initForm1 () {
		if (!document.getElementById) return;

		var labels, id, field;

		// Set focus and blur handlers to hide and show
		// LABELs with 'overlabel' class names.
		textInputs = $j("form.form1 :text, form.form1 :password");
		for (var i = 0; i < textInputs.length; i++)
		{
			textInputs[i].onfocus = function()
			{
				this.className = 'focused';
			}

			textInputs[i].onblur = function()
			{
				this.className = 'default';
			}
		}
	};

	initForm1();
	// end "form1" form functionality

	// apply functionality to all "form4" forms
	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 = $j("form.form4 label.overlabel");
		for (var i = 0; i < labels.length; i++)
		{
			// 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;
			}

			if (labels[i].className.indexOf("error") == -1)
			{
				// Change the applied class to hover the label
				// over the form field.
				labels[i].className = 'overlabel-apply';
			}
			else
			{
				// Change the applied class to hover the label
				// over the form field, with error
				labels[i].className = 'overlabel-apply error';
			}

			// Hide any fields having an initial value.
			if (field.value !== '')
			{
				if (labels[i].className.indexOf("error") == -1)
				{
					hideLabel(field.getAttribute('id'), true);
				}
				//labels[i].style.textIndent = '-1000px';
			}

			// Set handlers to show and hide labels.
			field.onfocus = function()
			{
				hideLabel(this.getAttribute('id'), true);
				this.className = 'focused';
			};

			field.onblur = function()
			{
				if (this.value === '')
				{
					hideLabel(this.getAttribute('id'), false);
				}
				this.className = 'default';
			};

			// 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 = $j("form.form4 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.textIndent = (hide) ? '-1000px' : '0px';
				labels[i].className = 'overlabel-apply';
				return true;
			}
		}
	}

	initOverLabels();


});

