//*** Place any JavaScript to be loaded in the <head> area here. ***
$(document).ready(function() {

	//Append the InputFocus() and InputBlur() functions to any elements with the class "clearfocus"
	$(".clearpassword").focus(function() { PasswordFocus(this); });
	$(".clearpassword").blur(function() { PasswordBlur(this); });
	$(".clearfocus").focus(function() { InputFocus(this); });
	$(".clearfocus").blur(function() { InputBlur(this); });
	$(".resetfield").each(function() { $(this).val( this.defaultValue ); });

	//preload CSS images
	$.preloadCssImages();
	
	//make footer stay at bottom on short pages
	FixShortPages();
	
	//set navigation background
	if ($('.nav li.current').length > 0) {
		SetNavBackground($('.nav li.current').get(0));
	}
	
	//init navigation animation
	$('.nav li a').mouseenter(function() {
		$(this).stop().animate({
			'padding-top': '3px'
		}, 200);
		if ( ! $(this).parent().is('.current') ) {
			$('.nav li.current').addClass('current-inactive').removeClass('current');
			MoveNavBackground(this);
		}
	});
	$('.nav li a').mouseleave(function() {
		$(this).stop().animate({
			'padding-top': '0px'
		}, 200);
		if ($('.nav .current-inactive').length > 0) {
			MoveNavBackground($('.nav .current-inactive').get(0), true);	//back to current button if it exists and isn't already current
		}
	});
	
	//flash or jquery/static banners?
	if ($.flash.available == true) {
		//top header - on every page
		$('.header .hero').addClass('flash').flash({
			swf: 'banners/flash/hic_top_banner.swf',
			width: '1000',
			height: '327',
			wmode: 'transparent',
			quality: 'high',
			allowScriptAccess: 'always'
		});
		//home page hero banner (under top header)
		$('.home-banners .flash').flash({
			swf: 'banners/flash/hero_august2011.swf',
			width: '780',
			height: '212',
			wmode: 'transparent',
			quality: 'high',
			allowScriptAccess: 'always'
		});
	} else {
		//show the home page hero jquery rotator
		$('.home-banners .rotator').show();
		$('.home-banners .banner-rotate	').cycle({ 	//set up banner rotator cycle
			fx:      'fade', 
			timeout:  4000 
		});
	}
	
	
});

$(window).resize(function() {
	FixShortPages();
});


//*** Custom Functions ***

function FixShortPages() {
	//reset content area height so it doesn't affect our calculations
	$('.content').height('auto');

	//make footer stay at bottom on short pages
	MakeElementFillVisibleSpace(".middle");
	
	//fix the content area height
	if ($('.content').length == 1) {
		var content_offset_top = $('.content').get(0).offsetTop;
		if ( $('.content').height() < ($('.middle').height() - content_offset_top) ) {
			var fixed_height = $('.middle').height() - content_offset_top;
			$('.content').height(fixed_height);
		}
	}
}

function SetNavBackground(element) {
	var button_offset = element.offsetLeft;
	var button_width = element.offsetWidth;
	var right_offset = 35;
	$('.nav .background-left').css('left', button_offset);
	$('.nav .background-right').css('left', button_offset+right_offset).css('width', button_width-right_offset);
}

function MoveNavBackground(element, setcurrent) {
	var button_offset = element.offsetLeft;
	var button_width = element.offsetWidth;
	var right_offset = 35; //a little less than the width of the left background makes the animation smoother
	var speed = 350;
	$('.nav .background-left').stop().animate({
		'left': button_offset
	},speed);
	$('.nav .background-right').stop().animate({
		'left': button_offset+right_offset,
		'width': button_width-right_offset
		},speed, function() {
			if (setcurrent == true) {
				$('.nav li.current-inactive').removeClass('current-inactive').addClass('current');
			}
	});
}


//*** Generic Functions ***

function InputFocus(element) {
	if (element.value == element.defaultValue) {
		element.value = "";
		$(element).addClass('filled');
	}
}
function InputBlur(element) {
	if (element.value == "") {
		$(element).removeClass('filled');
		element.value = element.defaultValue;
	}
}
function PasswordFocus(element) {
	if (element.value == element.defaultValue) {
		if ( ! $.browser.msie) { element.type = "password"; }
		element.value = "";
		$(element).addClass('filled');
	}
}
function PasswordBlur(element) {
	if (element.value.length == 0) {
		$(element).removeClass('filled');
		if ( ! $.browser.msie) { element.type = "text"; }
		element.value = element.defaultValue;
	}
}

//Function to make an element, e.g. content area, fill the visible space so a footer appears at the bottom. Wrapper can be a container or body element.
function MakeElementFillVisibleSpace(element_to_adjust, wrapper_element) {
	if (wrapper_element == undefined) { wrapper_element = 'body'; }
	$(element_to_adjust).height('auto');
	if ( $(wrapper_element).height() < $(window).height() ) {
		height_difference = $(window).height() - $(wrapper_element).height();
		new_height = $(element_to_adjust).height() + height_difference;
		$(element_to_adjust).height(new_height);
	}
}

//Make single element the same height as another single element - good for having two columns the same height
function MatchHeight(elementToAdjust, elementToCompare, adjust_offset, compare_offset) {
	if (isNaN(adjust_offset)) {
		adjust_offset=$(elementToAdjust).outerHeight(true) - $(elementToAdjust).height();
	};
	if (isNaN(compare_offset)) {
		compare_offset=$(elementToCompare).outerHeight(true) - $(elementToCompare).height();
	};
	if ( $(elementToAdjust).height()+adjust_offset < $(elementToCompare).height()+compare_offset ) {
		$(elementToAdjust).height( $(elementToCompare).height()+compare_offset-adjust_offset );	//adjust the height of the adjust element to the compare element
	}
}

//get a querystring by name
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}
