var $j = jQuery.noConflict();

$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;
			}
		}
	}*/
	
	// 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);
	}
	
	/*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\">";
		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();
	// end "form4" form functionality
	
	function setupFlashObjects () {
		
        var so = new SWFObject("assets/flash/sunstone_banner.swf", "sunstone banner", "978", "240", "9");
        so.addParam("quality", "high");
        so.addParam("wmode", "transparent");
        so.addParam("flashvars", "current_page=<?=$this->var['page_section'];?>");
		so.write("navigation");
		
		if (document.getElementById("logo-one") != null)
		{
			var so1 = new SWFObject("assets/flash/logo-rotator.swf", "logorotator1", "102", "70", "8");
			so1.addParam("quality", "high");
			so1.addParam("wmode", "transparent");
			so1.addVariable("folder", "assets/images/logos/");
			so1.addVariable("group", "group1");
			so1.write("logo-one");
	
			var so2 = new SWFObject("assets/flash/logo-rotator.swf", "logorotator2", "102", "70", "8");
			so2.addParam("quality", "high");
			so2.addParam("wmode", "transparent");
			so2.addVariable("folder", "assets/images/logos/");
			so2.addVariable("group", "group2");
			so2.write("logo-two");
	
			var so3 = new SWFObject("assets/flash/logo-rotator.swf", "logorotator3", "102", "70", "8");
			so3.addParam("quality", "high");
			so3.addParam("wmode", "transparent");
			so3.addVariable("folder", "assets/images/logos/");
			so3.addVariable("group", "group3");
			so3.write("logo-three");
		}
	}
	
	setupFlashObjects();

});
