/* START jQUERY */
$(document).ready(function(){   
	
	/* BANNER TABED CONTENT 
	$(".tab-content a").click(function(){
		$(".tab-content a").removeClass("on");
		$(this).addClass("on");
		$(".tab-content aside").hide();    
		$(this).parent().next("aside").show();
		return false;
	});  */      
	
	/* FAQ ACCORDIAN */
	var animSpeed = 300;
	var onClass = "on";
	$(".faqs h3").click(function(){
		if ($(this).hasClass(onClass)){
			$(this).next().slideUp(animSpeed);
			$(".faqs h3").removeClass(onClass);
		} else {
			$(".faqs dl").slideUp(animSpeed);
			$(this).next().slideDown(animSpeed);
			$(".faqs h3").removeClass(onClass);
			$(this).addClass(onClass);
		}
	});
	$(".faqs dt").click(function(){
		if ($(this).hasClass(onClass)){
			$(this).next().slideUp(animSpeed);
			$(this).removeClass(onClass);
			$(this).siblings("dt").removeClass(onClass);
		} else {
			$(".faqs dd").slideUp(animSpeed);
			$(this).next().slideDown(animSpeed);
			$(this).siblings("dt").removeClass(onClass);
			$(this).addClass(onClass);
		}
	}); 
	
	/* HTML5 TAGS FOR IE6 */
	// document.createElement("header");
	// document.createElement("footer");
	// document.createElement("section");
	// document.createElement("nav");
	// document.createElement("article");  
	// document.createElement("aside");  
	// document.createElement("video"); 
	// document.createElement("audio");  
	
	/* ZEBRA ROWS */
	$(".zebra tr:even").addClass("alt");
	/*$(".zebra tr").hover(
		function () {
			$(this).addClass("over");
		}, 
		function () {
			$(this).removeClass("over");
		}
	);*/   
	
	/* FADE BODY BG 
	$(document.body).click(function () {
		$(document.body).fadeOut("slow");
	});       */  	
	
	/* BUILD TOC BASED ON NUMBER OF H2.cateogory */
	if ($("#toc")) {  
		// add our initial ULs to the TOC container
		var tocCol1 = $("#toc").append('<ul></ul>').children('ul').get(0);
		var tocCol2 = $("#toc").append('<ul></ul>').children('ul').get(1);
	    // get number of H2.cateogory in the page
		var numSections = $(".main h2.category").length; 
		var insertIndex =  0;  
		if (numSections%2==0){
			 insertIndex = numSections/2;
		} else {
			 insertIndex = (numSections/2)+.5;      
		}
		// loop through H2.cateogory, add a unique ID, top link and create an LI with link to it in the TOC       
		$(".main h2.category").each(function(i) {  
			var $this = $(this);  
			// write the TOC link first before we add in the TOP anchor
			var tocLink =  '<li><a href="#header_' + i + '">' + $this.text() + '</a></li>'; 
			// check to see if it's time to start a new TOC column
			if (i >= insertIndex) {   
				$(tocCol2).append(tocLink); 
			} else {
				$(tocCol1).append(tocLink);
			}
			$this.attr('id','header_'+i);
			$this.append('<a href="#top" class="top">Back to Top</a>');   
		});        
	}                         

	/* RESCALE BACKGROUND IMAGE */
	if ($("#bkg").length > 0) {
		rescale();               
		
		/* ROTATE BKG  */  
		// $('#bkg').innerfade({
		// 	animationtype: 'fade',
		// 	speed: 400,
		// 	timeout: 1500,
		// 	type: 'sequence'
		// });   
				
		$(window).resize(function(){
			rescale();
		});
	}       
	
                                         

	/* WRITE DATE TO PAGE */
	$("#date").html(getDate());
	
	/* INITIALIZE FIELD VALUE RESET (looks for presence of "alt" attribute in the input tag) */
	$("input[alt!='']").focus(function(){
		setValue(this,"onfocus"); 
		return false;
	});
	$("input[alt!='']").blur(function(){
		setValue(this,"onblur"); 
		return false;
	});

 });
 
 
/* BEGIN DATE FUNCTIONS */
function getDate() {
	var today = new Date();
	var Year = takeYear(today);
	var Month = leadingZero(today.getMonth()+1);
	//var DayName = Days[today.getDay()];
	var Day = leadingZero(today.getDate());	
	var Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");	
	var Month = Months[today.getMonth()];	
	var retVal = ""	
	retVal = Month + " " + Day + ", " + Year;	
	return retVal;		
}  
function leadingZero(nr) {
	if (nr < 10) nr = "0" + nr;
	return nr;
}
function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}
/* END DATE FUNCTIONS */
 
 /* BEGIN SETVALUE
 setValue clear's a text input's value and resets it to the input's alt attribute if the user doesn't enter a value */
function setValue(pEl,pSwitch) {	
	if (pSwitch == "onfocus") {
		if (pEl.value == pEl.alt) {
			pEl.value = "";	
		}
	}
	if (pSwitch == "onblur") {
		if (pEl.value == "") {
			pEl.value = pEl.alt;
		}
	}
}
/* END SETVALUE */
                    
/* Rescale from Fox-land */
function rescale() {
	// alert("Blimey");
	var max_height = $(window).height();
	var max_width = $(window).width();
	var min_width = 960;
	//alert ($(window).width());

	var height = $("#bkg img").height();
	var width = $("#bkg img").width();
	var ratio = height/width;

	// If height or width are too large, they need to be scaled down
	// Multiply height and width by the same value to keep ratio constant
	ratio = max_height / height;
	height = height * ratio;
	width = width * ratio;

	if (width < max_width) {
		// alert ("image width smaller than window width")
		ratio = max_width / width;
		height = height * ratio;
		width = width * ratio;
	}

	$("#bkg img").height(height);
	$("#bkg img").width(width);
	$("#bkg").height(max_height);

	// set top/left
	var top = $("#bkg img").offset().top;
	var left = $("#bkg img").offset().left;
	top = Math.round((max_height - height)/2);
	left = Math.round((max_width - width)/2);     
	$("#bkg img").css("margin-top",top);
	$("#bkg img").css("margin-left",left);

}
