/* Clear a text box if the value is the default value */
function clearBox(box) {
    if (box.value == box.defaultValue) {
        box.value = "";}}

/* Set a text box back to the default value if the value is blank */
function resetBlankBox(box) {
    if (box.value == "") {
        box.value = box.defaultValue;}}

/* Check if a string ends with another */
function endsWith(testString, endingString) {
    if (endingString.length > testString.length) return false;
    return testString.indexOf(endingString) == (testString.length - endingString.length);
}


$(document).ready(function() {

	//Apply dynamic CSS classes
	$("#sub-nav li:last").addClass("last");

	//Set the background to the month image
	var d = new Date();
	$("body").addClass("month" + (d.getMonth() + 1));

	//Highlight current page
	var pageurl = location.href.toLowerCase();
	$("#sub-nav a").each(function() {
		var hreflink = $(this).attr("href").toLowerCase();
		var aspurl = pageurl + "default.asp";
		var aspxurl = pageurl + "default.aspx";
		var htmurl = pageurl + "default.htm";
		var htmlurl = pageurl + "default.html";
		if (endsWith(pageurl, hreflink) ||
		   pageurl == hreflink ||
		   endsWith(aspurl, hreflink) ||
           endsWith(aspxurl, hreflink) ||
		   endsWith(htmurl, hreflink) ||
		   endsWith(htmlurl, hreflink)) {
			$(this).addClass("active");
		}
	});


	/*
	// It's possible, with caching, that the images could
	//  be loaded *before* this code runs.  So:
	var is_image_loaded = function(img) {
	// IE
	if(!img.complete) {
	return false;
	}
	// Others
	if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
	return false;
	}
	return true;
	}

	var first_slide = $('#rotator img:first');
	if(is_image_loaded(first_slide.get(0)) {
	$('#rotator').cycle();
	} else {
	first_slide.load(function(e) {
	//$('#loading').hide();
	$('#rotator').show().cycle();
	});
	$('#rotator').hide();
	// I'll handwave the display of a loading indicator, since
	// it's covered in the tutorial linked in the question.
	//$('#loading').show();
	}
	*/

	var slideCount = $('#rotator-controls ul li').size();
	var randNum;
	if (slideCount > 0) {
		randNum = Math.floor(Math.random() * slideCount)
	}

	//Homepage feature rotator
	$('#rotator').cycle({
		speed: 'slow',
		timeout: 8000,
		pause: 1,
		pauseOnPagerHover: 0, //Set this to 1 if you want it to pause when hovering over one of the three boxes //
		pager: '#rotator-controls',
		pagerEvent: 'click',
		random: 0,
		startingSlide: randNum,
		pagerAnchorBuilder: function(idx, slide) {
			return '#rotator-controls li:eq(' + idx + ') ';
		}

		/*speed: 'slow',     
		timeout: 8000,
		pause: 1,
		pauseOnPagerHover: 0, 
		pager: '#rotator-controls', 
		pagerEvent: 'click',
		random: 0,
		pagerAnchorBuilder: function(idx, slide) { 
		return '#rotator-controls li:eq(' + idx + ') '; 
		} */
	});
});