$(document).ready(function() {

	// makes radio and checkbox inputs pretty
	$('input[type=checkbox],input[type=radio]').prettyCheckboxes();
	$('p.note').addClass("prettyNote");
	$('div.form-checkboxes, div.form-checkboxes-error, div.form-checkboxes-required, div.form-radio, div.form-radio-error, div.form-radio-required').addClass("prettyWrap");

	// makes textarea elements expandable
	$('textarea').prettyComments();

	// convert main tier2 nav, depending on the content
	$('#menu > li').each(function(j, val) {

		$(this).children("ul").css("display", "block");
		$(this).children("ul").css("visibility", "hidden");

		var liTotalWidth = 0;
		$(this).find("li").each(function(k, val) {
			liTotalWidth += $(this).width();
		});
		
		$(this).children("ul").css("display", "");
		$(this).children("ul").css("visibility", "");		
		
		var mainNavWidthInitial = $(this).parent().width();
		$(this).parent().css("width", "auto");
		var mainNavWidthAuto = $(this).parent().width();
		
		var mainNavDiff = mainNavWidthInitial - mainNavWidthAuto;
		var ulWidthInitial = $(this).children("ul").width();
		
		$(this).children("ul").css("width", liTotalWidth + "px");
		$(this).children("ul").css("padding-right", "0");
		
		var thisWidth = $(this).width();
		var thisPosition = $(this).position().left + thisWidth + mainNavDiff;
		var centerPoint = thisPosition - thisWidth/2;
		
		$(this).parent().css("width", mainNavWidthInitial + "px");

		var thisUlWidth = $(this).children("ul").width();
		var UlPosition = (ulWidthInitial) - Math.round(centerPoint + thisUlWidth/2) + (mainNavWidthInitial - ulWidthInitial + 7);
		
		if (UlPosition < 4)
		{
			UlPosition = 4;
			$(this).children("ul").css("width", ulWidthInitial - UlPosition + "px");
			
			$(this).children("ul").prepend('<li class="left-spacer"></li>');
			$(this).find(".left-spacer").css("width", ulWidthInitial - thisUlWidth - UlPosition + "px");
			$(this).find(".left-spacer").css("float", "left");
			
			$(this).children("ul").css("width", (liTotalWidth + (ulWidthInitial - thisUlWidth - UlPosition)) + "px");
		}
		else if (UlPosition + thisUlWidth > ulWidthInitial)
		{
			UlPosition = ulWidthInitial - thisUlWidth;
		}
		else
		{
			$(this).children("ul").prepend('<li class="left-spacer"></li>');
			$(this).find(".left-spacer").css("width", ulWidthInitial - thisUlWidth - UlPosition + "px");
			$(this).find(".left-spacer").css("float", "left");
			
			$(this).children("ul").css("width", (liTotalWidth + (ulWidthInitial - thisUlWidth - UlPosition)) + "px");
		}
		
		$(this).children("ul").css("padding-right", UlPosition + "px");
	});
	
	// 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=\"assets/images/ui_framework/bg_quoteright.gif\" width=\"15\" height=\"11\" /></span>";		
		var midQuote = "";
		var lastQuote = "";
		//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 items in a select drop-down list
	$("select option:first-child").addClass("first");
	
	// handle form selection class change when an item is selected
	$("select").change(function() {
		if (this.selectedIndex == 0)
		{
			$(this).removeClass();
		}
		else
		{
			$(this).addClass("active");
		}
	});
	
	// handle select style on page load
	function initSelectCurrent () {
		var selectArray = $("select");
		for (var i = 0; i < selectArray.length; i++)
		{
			if (selectArray[i].selectedIndex != 0)
			{
				selectArray[i].className = "active";
			}
		}
	}
	
	// handle radio button click event
	$(":radio").click(function() {
		$(this).siblings("label").removeClass("current");
		$(this).siblings("label:last").addClass("last");
								
		var thisId = this.getAttribute('id');
		$(this).siblings("label[for='"+thisId+"']").addClass("current");
	});
	
	// make sure current radio buttons, select ddls, and checkboxes get the current class
	initSelectCurrent();
	
	// 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 = $("fieldset.form1 :text, fieldset.form1 :password, fieldset.form1 textarea");
		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 "form2" 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 = $("fieldset.form2 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 = $("fieldset.form2 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();
	// end "form2" form functionality
	
	$("button").hover(
		function() {
			if ($(this).hasClass("btn_small"))
			{
				$(this).addClass("btn_bluehover_small");
			}
			else
			{
				$(this).addClass("btn_bluehover");					
			}
		},
		function() {
			if ($(this).hasClass("btn_small"))
			{
				$(this).removeClass("btn_bluehover_small");
			}
			else
			{
				$(this).removeClass("btn_bluehover");					
			}
		}
	);

   DD_belatedPNG.fix('.header, .navigation-zipcode, .content, .sub-bottom');
   
});
