// JavaScript Document

//homepage image fade
$(function() {
	$("#imagefadelist").innerfade({timeout:10000,containerheight: 235});
});

// initialize dropdowns
$(function() { 
	$("#subnav").superfish({ 
		animation:   {opacity:'show'},  // fade-in and slide-down animation 
        autoArrows:  false
	});
}); 

$(function() {
	// Make default form text grey on page load
	$("#s").val("Search").addClass("defaultformtext");
	$("textarea").addClass("defaultformtext");

	// If form inputs have a default value, clear on focus
	$("#s, textarea").focus(function() {
		if(this.value == this.defaultValue) {
			this.value = "";
			$(this).removeClass("defaultformtext");
		}
	})
	// Restore default text
	.blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).addClass("defaultformtext");
		}
	});
});

// Calculator Table Functions
$(function() {
	// table striping
	$(".calcresults tr:nth-child(odd)").css("background", "#eee");
	
	// show/hide more info panel
	$("#moreinfolink").click(function() {
		$(this).hide();
		$("#moreinfopanel").slideDown("slow");
		return false;
	});
	$("#lessinfolink").click(function() {
		$("#moreinfopanel").slideUp("slow", function() {
			$("#moreinfolink").show();
			});
		return false;
	});
});

// sidecalc form validation
$(function() {
	$("#sidecalc form").validate(
		{
		  rules: {
		    zip_code: {
		      required: true,
		      range: [1000, 99999]
		    }
		  }
		}
	);		 
});

// sidecalc form cleaning
$(function() {
	$("#sidecalc #average_monthly_electric_bill").change(function(){$(this).val($(this).val().replace(/[^\d\.]/gi,""));});
	$("#sidecalc #zip_code").change(function(){$(this).val($(this).val().replace(/[^\d]/gi,""));});
});

// Get Solar Now form validation
$(function() {
	$("#getsolarnowform").validate();		 
});

$.metadata.setType("attr", "validate");

// z-index dropdown nav fix for ie6 and ie7
$(function() {
	var zIndexNumber = 1000;
	$('#subnav .page-item-23').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});

// image map for 'how solar power works'
$(function() {
	$("area").hover(function() {
		var thisone = "#solarnumber" + $(this).attr("href");
		$(thisone).css("background-position", "bottom");
	},
	function() {
		var thisone = "#solarnumber" + $(this).attr("href");
		$(thisone).css("background-position", "top");
		});
		
	$("area").click(function() {
		return false;
	});
});

// faq show/hide
$(function() {
	// prep an anchor
	var h6a = $("<a></a>").attr("href", "#");
	// wrap the h6 contents with the anchor, hide the lists (answers)
	$("h6").wrapInner(h6a).next("ul").hide();
	// on click hide all answers, then open the right one
	$("h6 a").click(function() {
		$("h6").next("ul").slideUp();
		$(this).parent().next("ul").slideDown();
		return false;
	});
});