﻿function openMap() {
	var zipCode = jQuery(".callout-location input[name='zip']").val();
	var searchfor = jQuery(".callout-location input[name='branch-atm']:checked").val();

	if (!zipCode || isNaN(zipCode) || zipCode.length != 5) {
		alert('Please enter a valid zipcode.');
		return;
	}

	var redirectURL = jQuery('#AllLocationsLink').attr('href').split('?')[0];
	redirectURL += '?addTXT=' + zipCode;
	if (searchfor != '') {
		//searchfor values: "atm" or "branch"
		if (searchfor == "atm") {
			redirectURL += "&ATMSF=1&ATMDP=1&FCSATM=1&FIATM=1";
		}
		else if (searchfor == "branch") {
			redirectURL += "&FCS=1&ESC=1&FSCESC=1";
		}
	}
	window.location = redirectURL;
}

(function () 
{
	jQuery(document).ready(function () {
		InitializeLocatorSearch();
		InitializeSearchBox();
		InitializePrimaryNav();
		InitializeTabs();
		ProcessExternalLinks();
		ProcessButtons();

		//AW - 02/17/2011 - moved Cufon replacement calls inside document ready in order to address a random IE issue where primary navigation font replacement wouldn't occur.
		//The Cufon site explicitly recommends not putting Cufon calls in document ready because users might actually "see" the font replacement occurring, 
		//but this does seem like the logical place to put the replacement calls- you know, when the DOM is fully loaded and ready for operations... silly Cufon.
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon('#PrimaryNavigation span', {
			hover: { color: '#336699' }
		});
	});

	function InitializeLocatorSearch()
	{
		jQuery(".callout-location input[name='zip']").unbind('blur').bind('blur', function() 
		{
			if (this.value == '') 
				this.value = 'Zip Code';

		}).unbind('focus').bind('focus', function() 
		{
			if (this.value == 'Zip Code') 
				this.value = '';

		}).keydown(function (event) {
			if (event.keyCode == '13') {
				event.preventDefault();
				openMap();
			}
		});

		jQuery(".callout-location input[name='go_btn']").unbind('click').bind('click', function() {
			openMap();
			return false;
		});		 
	}

	function InitializeSearchBox()
	{
		var searchBox = jQuery('.searchbox .search');
		var initialValue = searchBox.val();

		searchBox.unbind('blur').bind('blur', function()
		{
			if (this.value == '')
				this.value = initialValue;

		}).unbind('focus').bind('focus', function()
		{
			if (this.value == initialValue)
				this.value = '';
		});
	}

	function InitializePrimaryNav()
	{
		jQuery('#PrimaryNavigation > ul').clickMenu();
	}

	function InitializeTabs()
	{
		var tabLoc = window.location.href.indexOf("#tab");
		if (tabLoc > -1 && window.location.href.length > tabLoc + 4) {
			jQuery("ul.tabs").tabs("div.panes > div", { initialIndex: parseInt(window.location.href.substring(tabLoc + 4, tabLoc + 5)) - 1 });
		} else {
			jQuery("ul.tabs").tabs("div.panes > div");
		}		
	}

	function ProcessButtons()
	{
		jQuery("a.ctabuttons, a.back-button, button.ui-button").each(function () {
			jQuery(this).html("<span>" + jQuery(this).text() + "</span>");
		});

		jQuery(".scfSubmitButtonBorder input[type=submit]").each(function () {
			var oldFormButton = jQuery(this).hide();
			var newFormButton = jQuery("<input type='image' src='/Components/Design/Images/btn_submit.gif' />");
			newFormButton.appendTo(oldFormButton.parent()).click(function () { oldFormButton.click(); return false; });
		});
	}

	function ProcessExternalLinks()
	{	
		jQuery('a[href*="#external"]').attr('target', '_blank').live('click', HandleExternalLink).bind('externallinkclick', HandleExternalLink);
	}

	function HandleExternalLink(event)
	{
		var overlayElem = jQuery('#ExternalLinksNotification');
		var dialogTitleElem = overlayElem.find('.notificationTitle');		
		var dialogTitle = dialogTitleElem.text();
		dialogTitleElem.remove();

		var sender = jQuery(this);
		overlayElem.show().dialog({
			modal: true,
			resizable: false,
			title: dialogTitle,
			closeOnEscape: true,
			draggable: false,
			show: { effect: "fade", duration:200 },
			hide: { effect: "fade", duration:200 },
			close: function()
			{
				jQuery(this).dialog('destroy').hide();
			},
			buttons: {
				Cancel: function() {
					jQuery(this).dialog('close');
				},
				Continue: function () {
					window.open(sender.attr('href').replace('#external', ''));
					jQuery(this).dialog('close');
				}				
			}				
		});
		event.stopPropagation;
		return false;
	}
})();
