$(document).ready(function() {

	/* banner controls */
	if( $('#homePageBanners').length > 0 ) {
		$('#homePageBanners').oscillate({navigationHolderID: 'homePageBannerNav', automaticDelay: 12000, afterClickDelay: 15000 });
	}
	
	/* main navigation controls */
	$("#mainNavigation li").hover(function() {
		$(this).find("a.main").addClass("hovered");
		$(this).find(".activeArrow").hide();
		$(this).find(".sub").stop().fadeTo('fast',1).show();
	}, function() {
		$(this).find("a.main").removeClass("hovered");
		$(this).find(".activeArrow").show();
		$(this).find(".sub").stop().fadeTo('fast',0).hide();
	});
	/* end of main navigation controls */
	
	/* search form submission */
	$("#searchBar").submit(function() {
		var searchValue = $("#search").val();
		var action = $(this).attr('action');
		
		// strip the searchValue
		var reg = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
		searchValue = searchValue.replace( reg, '');
		searchValue = searchValue.replace( /[\s]+/g, '+' );
		
		window.location = action + '?s='+searchValue;
		
		return false;
	});
	
	/* scroll to functionality */
	$('.scrollTo').click(function() {
		var elementClicked = $(this).attr("href");
		var destination = $(elementClicked).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({
			scrollTop:destination-20
		}, 800 );
		return false;
	});	
	
	//apply tippy on internal pages with a
	//slightly shorter topOffset
	if( $("#internetBankingSm").length > 0 ) {
		$("#internetBankingSm").tippy({
			topOffset: 50,
			message: "This link will open in a new window."
		});
	}
	
	//apply tippy on home page with a
	//a larger offset
	if( $("#internetBankingLrg").length > 0 ) {
		$("#internetBankingLrg").tippy({
			topOffset: 60,
			message: "This link will open in a new window."
		});
	}
	
	//any links with a rel='blank' need to open in a new window
	$("a").each(function() {
		if( $(this).attr('rel') == 'blank' ) {
			$(this).click(function() {
				window.open( $(this).attr('href') );
				return false;
			});
		}
	});
	
	//attach any interimAlert() calls to the 
	//header. This implementation isn't my favorite, 
	//but it works.
	$.interimAlert = function( alert ) {
		$("#header").interim({
			action: 'now',
			message: alert
		});
	}
	
	//	branch offices
	$('#viewPhotoMainBranch').tippy({
		topOffset: 20,
		leftOffset: -40
	});
	$('#viewPhotoLakeviewBranch').tippy({
		topOffset: 20,
		leftOffset: -40
	});
	$('#viewPhotoSouthKlamathBranch').tippy({
		topOffset: 20,
		leftOffset: -40
	});
});
