﻿/* Helper Functions */

function MM_jumpMenuGo(objId,targ,restore){ //v9.0
  var selObj = null;  with (document) { 
  if (getElementById) selObj = getElementById(objId);
  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0; }
}

/* Plugin Summary: Checks if a jQuery object exists in the DOM, 
by checking the length of its child elements. It returns a boolean. 

Usage:

if ($("#someid").elementExists()) {
	do something here.
}*/

(function($) {
	$.fn.elementExists = function() {
		return jQuery(this).length > 0;
	};
}) (jQuery);


/* Ticker Functions */

$(document).ready(function() {	
	if ($("ul.news").elementExists()) {
		var speed = 700;
		var pause = 5000;
		function newsticker() {
			last = $('ul.news li:last').hide().remove();// get last element, remove it from the list
			$('ul.news').prepend(last);// add to first position with hidden style
			$('ul.news li:first').slideDown("slow");// slideDown the new first element, then continue
		}
		interval = setInterval(newsticker, pause);
		$("ul.news").hover(function() {
		  clearInterval(interval); // pause on mouseover
		}, function() {
		  interval = setInterval(newsticker, pause); //resume on mouseout
		});
	}
});


/* Main Navigation Functions */

var timeOut = null;
var onlineMenuFlag = 0;

function nav_setOnlineMenuFlag(flag) {
	onlineMenuFlag = flag;
}

function nav_hideAllMenus() {
	$("#navigation-wrapper .menu").css("visibility", "hidden");
	$("#main-nav a").removeClass("active");
	$("#online-nav a").removeClass("active");
	// Show iframes in the content area when the menus are closed
	$("#content").find("iframe.avoidNavigation").css({"position": "relative", "top": "0px"});
}

function nav_showMenu(menuID) {
	if (document.getElementById(menuID)) {
		nav_canceltimer();
		nav_hideAllMenus();
		document.getElementById(menuID).style.visibility = "visible";
		// Hide iframes in the content area when the menus are open
		$("#content").find("iframe.avoidNavigation").css({"position": "relative", "top": "300px"});
	}
}

function nav_timer() {
	if (onlineMenuFlag == 0) {
		nav_canceltimer();
		timeOut = window.setTimeout('nav_hideAllMenus()', 750);
	}
}

function nav_canceltimer() {
	if (timeOut !== null) {
		window.clearTimeout(timeOut);
	}
}

function changeNavButton(menuButton) {
	if (document.getElementById(menuButton)) {
		document.getElementById(menuButton).className="active";
	}
}

$(document).ready(function() {
	// Hide all navigation menus on page load
	nav_hideAllMenus();
	// Add a mouseover and mouseout event to all links inside the #main-nav section
	$("#main-nav a").each(function() {
		$(this).mouseout(function() {
			nav_timer(); // Start the timer that will delay hiding the drop down menu
		}).mouseover(function() {
			var menuButton = $(this).attr("id"); // Get the ID of the current link in #main-nav
			var menuID = menuButton.replace(/^nm/, "dd"); // Change the first two characters of the ID from nm to dd to select the drop down menu associated with the link in #main-nav
			nav_showMenu(menuID); // Show the current menu
			changeNavButton(menuButton); // Change the associated link in #main-nav to active so that it matches the drop down menu
		});
	});
	// Add a mouseover and mouseout event to all links inside the #online-nav section
	$("#online-nav a").each(function() {
		$(this).mouseout(function() {
			nav_timer(); // Start the timer that will delay hiding the drop down menu
		}).mouseover(function() {
			var menuButton = $(this).attr("id"); // Get the ID of the current link in #online-nav
			var menuID = menuButton.replace(/^nm/, "dd"); // Change the first two characters of the ID from nm to dd to select the drop down menu associated with the link in #online-nav
			nav_showMenu(menuID); // Show the current menu
			changeNavButton(menuButton); // Change the associated link in #online-nav to active so that it matches the drop down menu
		});
	});
	// Add a mouseover and mouseout event to all divs with a class name of .menu inside the #navigation-wrapper section
	$("#navigation-wrapper .menu").each(function() {
		$(this).mouseout(function() {
			nav_timer(); // Start the timer that will delay hiding the drop down menu
		}).mouseover(function() {
			var menuID = $(this).attr("id"); // Get the ID of the current drop down menu
			var menuButton = menuID.replace(/^dd/, "nm"); // Change the first two characters of the ID from dd to nm to select the associated link in #navigation-wrapper
			nav_showMenu(menuID); // Show the current menu
			changeNavButton(menuButton); // Change the associated link in #navigation-wrapper to active so that it matches the drop down menu
		});
	});
	$("#loginAccountSelection").each(function() {
		$(this).focus(function() {
			nav_setOnlineMenuFlag(1);
		}).blur(function() {
			nav_setOnlineMenuFlag(0);
			nav_timer();
		})
		
	});
});

/* Branch Locator */

function openMap() {
	var myzip = document.locatorForm.zip.value;
	if(!myzip || isNaN(myzip)) {
	   alert('Please enter a valid zipcode.');
	   return false;
	}
	var bmurl='http://www.branchmap.com/mapserver.php?client=firstunited&zip='+myzip;
	window.open(bmurl,'branchmap','width=875,height=585,top=0,toolbar=no,personalbar=no,location=no,directories=no,statusbar=no,menubar=no,status=no,resizable=yes,left=100,screenX=100,top=100,screenY=100');
}
