// Main Menu Navigation functions
function show_menu(element) {
	element.addClass('active');
	$('.sub', element).slideDown('fast');
}

function hide_menu(element) {
	element.removeClass('active');
	$('.sub', element).slideUp('fast');
}

// Weather Widget functions
var current_weather_zip;
function set_current_weather_zip(zip) {
	current_weather_zip = zip;
}

function show_weather_form() {
	$('#weather > .current').fadeOut('fast');
	$('#weather > .form').fadeIn('fast');
	weather_form_to_default();
}

function hide_weather_form() {
	$('#weather > .form').fadeOut('fast');
	$('#weather > .current').fadeIn('fast');
}

function weather_form_to_default() {
	var weather_zip = $('#weather #weather_zip').val();
	var default_weather_zip = 'Zip Code';
	if(weather_zip==current_weather_zip) {
		$('#weather #weather_zip').addClass('default');
	} else if(weather_zip=='' || weather_zip==default_weather_zip) {
		$('#weather #weather_zip').val(default_weather_zip).addClass('default');
	}
}

function weather_form_focus() {
	$('#weather #weather_zip').val('').removeClass('default');
}

$(document).ready(function() {
	if(jQuery().datepicker) {
		$('.datepicker').datepicker({ dateFormat:'yy-mm-dd' });
	}
	// Main menu navigation
	$('#menu > li').hover(function() {
		show_menu($(this));
	}, function() {
		hide_menu($(this));
	});
	
	// Weather Widget
	$('#weather #change_zip').click(function() {
		show_weather_form();
	});
	
	$('#weather #cancel_zip').click(function() {
		hide_weather_form();
	});
	
	$('#weather #weather_zip').focus(function() {
		weather_form_focus();
	}).blur(function() {
		weather_form_to_default();
	});
	
	$("body").delegate('a', 'click', function(event) {
		var reCSBFinancialAdvisors = /csb-financial-advisors/gi;
		var reCSBInsurance = /csb-insurance/gi;
		if(this.hostname && this.hostname !== location.hostname) {
			return confirm('Community State Bank does not provide, and is not responsible for, the product, service or overall website content available at linked websites. Community State Bank\'s privacy policies do not apply to linked websites. Viewers should consult the privacy disclosures on linked websites for further information.');
		} else if(this.pathname.search(reCSBFinancialAdvisors) >= 0 && location.pathname.search(reCSBFinancialAdvisors) < 0) {
			return confirm('You are now being directed to a section of the CSB website containing products that are not FDIC insured, contain no Bank guarantee, and may go down in value.');
		} else if(this.pathname.search(reCSBInsurance) >= 0 && location.pathname.search(reCSBInsurance) < 0) {
			return confirm('You are now being directed to a section of the CSB website containing products that are not FDIC insured, contain no Bank guarantee, and may go down in value.');
		}
		return true;
	});
	
});
