var $j = jQuery.noConflict();
var curr_loc = ''; //This is used for correction of img url right quote marks in blockquote
$j(document).ready(function() {

	/*prepend numbers to subnav
	if (document.getElementById("sub_nav")) {
		var subNavLinks = document.getElementById("sub_nav").getElementsByTagName("a");
		for (i=1; i<subNavLinks.length+1; i++) {
			if (subNavLinks[i-1].parentNode.className != "btn") {
				var thisText = subNavLinks[i-1].firstChild.nodeValue;
				var thisNumber;
				if (i < 10) {
					thisNumber = "0" + i;
				} else {
					thisNumber = i;
				}
				subNavLinks[i-1].innerHTML = "<span>" + thisNumber + "</span>" + thisText;
			}
		}
	}*/
	
	// 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\">";
		
		
		if(curr_loc == 'home')
		{
			var lastQuote = "<img class=\"quote_rt\" src=\"assets/images/ui_framework/bg_quoteright.gif\" width=\"15\" height=\"11\" /></span>";
		} else {
			
			var lastQuote = "<img class=\"quote_rt\" src=\"../assets/images/ui_framework/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 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 select style on page load
	function initSelectCurrent () {
		var selectArray = $j("select");
		for (var i = 0; i < selectArray.length; i++)
		{
			if (selectArray[i].selectedIndex != 0)
			{
				selectArray[i].className = "active";
			}
		}
	}
	
	// handle radio button click event
	$j(":radio").click(function() {

		var siblingLabels = $j(this).siblings("label");
		for (var i=0; i< siblingLabels.length; i++)
		{
			if (siblingLabels[i].className.indexOf("last") == -1)
			{
				siblingLabels[i].className = "";
			}
			else
			{
				siblingLabels[i].className = "last";
			}
		}
								
		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();
	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 = $j("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 "form3" forms
	function initForm3 () {
		if (!document.getElementById) return;  	
		
		var labels, id, field;
		
		// Set focus and blur handlers to hide and show 
		// LABELs with 'overlabel' class names.
		textInputs = $j("fieldset.form3 :text, fieldset.form3 :password, fieldset.form3 textarea");
		for (var i = 0; i < textInputs.length; i++)
		{
			textInputs[i].onfocus = function()
			{
				this.className = 'focused';
			}

			textInputs[i].onblur = function()
			{
				this.className = 'default';
			}
		}
	};
	
	initForm3();
	// end "form3" 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 = $j("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 = $j("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
	
});
