// Javascript Global Functions
function ShowDiv(idName) {
	var idObject= document.getElementById(idName);
	if (idObject) {
		idObject.style.display = "block";
	}
}
function HideDiv(idName) {
	var idObject= document.getElementById(idName);
	if (idObject) {
		idObject.style.display = "none";
	}
}
function ShowVisib(idName) {
	var obj = document.getElementById(idName);
	if (obj) {
		obj.style.visibility = "visible";
	}
}
function HideVisib(idName) {
	var obj = document.getElementById(idName);
	if (obj) {
		obj.style.visibility = "hidden";
	}
}
function ShowModal(idName) {
	ShowVisib("overlayBkgdDiv");
	ShowDiv(idName);
}	
function HideModal(idName) {
	HideDiv(idName);
	HideVisib("overlayBkgdDiv");
}


/*------------------------------------------------------------------------------
Function: Open Window
Simple Popup Window

Parameters:
uri - string
width - number (of pixels)
height - number (of pixels)
options - string
name - string
------------------------------------------------------------------------------*/
function MM_openBrWindow(theURL, width, height, opt , name ) {
  poppedWin = window.open(theURL, (name || "OutsideWindow"), "width="+(width || 714)+",height="+(height || 536)+","+(opt || "scrollbars=yes,menubar=yes,toolbar=yes,location=yes,status=yes,resizable=yes"));
  
}

function CloseNow() {
	if (poppedWin.closed) {
		self.location.href="/My-Documents/My-Documents/Check-User-Status.aspx";
	} else {
		CloseAndRedirect();
	}
}


function CloseAndRedirect()	{
	setTimeout("CloseNow()",1000);
}

function CloseNowDD() {
	if (poppedWin.closed) {
		self.location.href="/Deposits/Sign-up-for-DepositDirect/Check-Sign-Status.aspx";
	} else {
		CloseAndRedirectDD();
	}
}


function CloseAndRedirectDD()	{
	setTimeout("CloseNowDD()",1000);
}

function OpenWindow( url, width, height, opt , name ) {
	window.open( url, (name || "OutsideWindow"), "width="+(width || 714)+",height="+(height || 536)+","+(opt || "scrollbars=yes,menubar=yes,toolbar=yes,location=yes,status=yes,resizable=yes")).focus();
}


// ---------------------------------------------------------------------------------------
// Predefined Popup Windows

function PopHowToPrint(url) {
	OpenWindow(url,550,230,"scrollbars=yes,menubar=no,toolbar=no,location=no");
}
function PopHowToPrintSave() {
	OpenWindow("/Templates/DMP/How-to-Print-and-Save.aspx",550,490,"scrollbars=yes,menubar=no,toolbar=no,location=no");
}
function PopCheck() {
	OpenWindow('/Templates/DMP/Check.aspx',400,400,'scrollbars=no,menubar=no,toolbar=no,location=no')
}
function PopCoupon(url){
    OpenWindow(url,640,480,'scrollbars=no,menubar=no,toolbar=no,location=no')
}

// ---------------------------------------------------------------------------------------
// Get Query String
function getQuery() {
	return window.location.href.slice(window.location.href.indexOf('?') + 1);
}


// ---------------------------------------------------------------------------------------
// BEGIN: Show Specific Tab in a page using URL Query
function HideAllToggle() {
	$(".toggle_display").each(function(){
		if ($(this).html() == "Hide") {
			$(this).html("Expand");
			$(this).parent().next().removeClass('showing');
		}
	});
}
function ShowMatchingQueryTab(QueryString) {
	if (QueryString) {
		$("#"+QueryString+" .toggle_display").html("Hide");
		$("#"+QueryString).next().addClass('showing');
		location.href = "#" + QueryString;
	}
}
// END: Show Specific Tab in a page using URL Query



// ---------------------------------------------------------------------------------------
// BEGIN: JW Player
function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
	swfobject.removeSWF(thePlayerId);
	var tmp=document.getElementById(theWrapper);
	if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
}
function createPlayer(thePlaceholder, thePlayerId, theFile, width, height) {
	var flashvars = {
		file:theFile, 
		autostart:"true"
	}
	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always"
	}
	var attributes = {
		id:thePlayerId,  
		name:thePlayerId
	}
	swfobject.embedSWF("/_JS/JWPlayer/player-licensed.swf", thePlaceholder, width, height, "9.0.115", false, flashvars, params, attributes);
}
function initPlayer(theFile, width, height) { 
	deletePlayer('wrapper', 'placeholder1', 'player1'); 
	createPlayer('placeholder1', 'player1', theFile, width, height);
}
// END: JW Player






// ---------------------------------------------------------------------------------------
/**
* hoverIntent is similar to jQuery's built-in "hover" function except that
* instead of firing the onMouseOver event immediately, hoverIntent checks
* to see if the user's mouse has slowed down (beneath the sensitivity
* threshold) before firing the onMouseOver event.
* 
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* hoverIntent is currently available for use in all personal or commercial 
* projects under both MIT and GPL licenses. This means that you can choose 
* the license that best suits your project, and use it accordingly.
* 
* // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
* $("ul li").hoverIntent( showNav , hideNav );
* 
* // advanced usage receives configuration object only
* $("ul li").hoverIntent({
*	sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
*	interval: 100,   // number = milliseconds of polling interval
*	over: showNav,  // function = onMouseOver callback (required)
*	timeout: 0,   // number = milliseconds delay before onMouseOut function call
*	out: hideNav    // function = onMouseOut callback (required)
* });
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($) {
	$.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				$(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				$(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Jquery
$(document).ready(function() {
	
	// ---------------------------------------------------------------------------------------
	// Browse Topic box HOVER
	$(".roundbox_active").hover(
	function(){
		$(this).addClass("roundbox_type2");
	},function(){
		$(this).removeClass("roundbox_type2");
	});
	
	// ---------------------------------------------------------------------------------------
	// Browse Topic box HOVER
	$(".addhover").hover(
	function(){
		$(this).addClass("over");
	},function(){
		$(this).removeClass("over");
	});
	
	// ---------------------------------------------------------------------------------------
	// Shows Calendar
	$(".datepicker").datepicker();
	
	
	// ---------------------------------------------------------------------------------------
	// Open ups correct tab
	$(".tab_hd a").click(function () {
		if ( $(this).attr('rel') != "") {
			$(this).parents(".tab_area").find(".tab_container").hide();
			$($(this).attr('rel')).show();
			$(this).parents(".tab_area").find(".tab_hd a.on").removeClass('on');
			$(this).addClass('on');
		}
	});
	
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: Dropdown Menu
	function addHoverHide(linkClass, layerId, t) {
		$("."+linkClass).hover( function(){
			clearTimeout(t);
			$(".prinav_menu").each(function(){
				if ($(this) != $("."+layerId)) {
					$(this).hide();
				}
			});
			$("."+layerId).show();
		},function() {
			t = setTimeout(function(){$("." + layerId).hide()}, 600);
		});
		
		$("."+layerId).hover( function(){
			clearTimeout(t);
			$("."+layerId).show();
		},function() {
			t = setTimeout(function(){$("." + layerId).hide()}, 200);
		});
	}
	
	function clearTimeoutAll() {
		clearTimeout(nav2timer);
		clearTimeout(nav3timer);
		clearTimeout(nav4timer);
		clearTimeout(nav5timer);
	}
	// END: Dropdown Menu
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: Dropdown Menu Initialize
	var nav1timer;	// Account Overview (no sub items)
	addHoverHide("nav1", "navmenu1", nav1timer);
	var nav2timer; // Creditor Accounts
	addHoverHide("nav2", "navmenu2", nav2timer);
	var nav3timer; // eStatements
	addHoverHide("nav3", "navmenu3", nav3timer);
	var nav4timer; // Deposits
	addHoverHide("nav4", "navmenu4", nav4timer);
	var nav5timer; // My Documents
	addHoverHide("nav5", "navmenu5", nav5timer);
	
	/* Utilities Menus */
	var utilnav1timer; // My Account
	addHoverHide("util1", "utilmenu1", utilnav1timer);
	var utilnav2timer; // My Account
	addHoverHide("util2", "utilmenu2", utilnav2timer);
	// END: Dropdown Menu Initialize
	
	
	
	// ---------------------------------------------------------------------------------------
	//form highlights
	$(".formcell").click( function() {
		$(".form_selected").removeClass('form_selected');
		$(this).addClass('form_selected');
	});
	
	
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: add color to ODD rows in tables
	$(".tbl_data_rows").each(function() {
		$(this).find("tr:even").addClass('odd')
	});
	
	$(".tbl_alt_rows").each(function() {
		$(this).find("tr:even").addClass('odd')
	});
	// END: add color to ODD rows in tables
	
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: toggle_display
	/* Clicks on link Hide/Show */
	$(".toggle_display").toggle(function(){
		if ($(this).html() == "Expand") {
			$(this).html("Hide");
		} else {
			$(this).html("Expand");
		}
		$(this).parent().next().slideToggle();
	},function(){
		if ($(this).html() == "Expand") {
			$(this).html("Hide");
		} else {
			$(this).html("Expand");
		}
		$(this).parent().next().slideToggle();
	});
	
	/* Clicks on Header to Hide/Show */
	$(".toggle_display_hd").toggle(function(){
		if ($(this).parent().find(".toggle_display").html() == "Expand") {
			$(this).parent().find(".toggle_display").html("Hide");
		} else {
			$(this).parent().find(".toggle_display").html("Expand");
		}
		$(this).parent().next().slideToggle();
	},function(){
		if ($(this).parent().find(".toggle_display").html() == "Expand") {
			$(this).parent().find(".toggle_display").html("Hide");
		} else {
			$(this).parent().find(".toggle_display").html("Expand");
		}
		$(this).parent().next().slideToggle();
	});
	
	/* Clicks on Header to Hide/Show */
	$(".expander_hd").click(function(){
		if ($(this).attr('class').split(' ').slice(-1) == "minus") {
			$(this).removeClass("minus");
		} else {
			$(this).addClass("minus");
		}
		$(this).next('.collapsable').slideToggle();
	});
	// END: toggle_display
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: accordion
	/* Clicks on link Hide/Show */
	$(".accordion h3").click(function(){
		if ($(this).find(".hd_link").html() == "Expand") {
			$(".accordion h3").find(".hd_link").html("Expand");
			$(this).find(".hd_link").html("Hide");
			$(".accordion .collapsable").slideUp();
			$(this).next().slideDown();
		} else {
			$(this).find(".hd_link").html("Expand");
			$(this).next().slideUp();
		}
	});
	// END: accordion
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: Clear form input values
	$('.clear_button').click(function() {
		var $thisForm = $(this).parents('fieldset');
		$thisForm.find(':text').val('');
		$thisForm.find('textarea').val('');
		$thisForm.find('select').each(function(){
				$(this).find('option:first').attr('selected', 'selected').parent('select');
		  });
		return false;
	});
	// END: Clear form input values
	 
	// ---------------------------------------------------------------------------------------
	// BEGIN: Clear Header Search field's default text
	$(".headsearchtxt").focus(function(){
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
			$(this).removeClass("off");
		 }
		 
	}).blur(function(){
		if($(this).val() == "") {
			$(this).val($(this).attr("title"));
			$(this).addClass("off");
		}
	});
	// END: Clearn Header Search field's default text
	
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: modal window
	function getDimensions() {
		winWidth = $(window).width();
		winHeight = $(window).height();
		
		pageWidth = $(document).width();
		pageHeight = $(document).height();
		
		wrapperWidth = $(".wrapper").width();
		
		modalWidth = $(".modal").outerWidth();
		modalHeight = $(".modal").outerHeight();
	}
	function sizeOverlay() {
		if (winWidth <= wrapperWidth) {
			$(".overlayBkgd").width( wrapperWidth );
		} else {
			$(".overlayBkgd").width( winWidth );
		}
		$(".overlayBkgd").height( pageHeight );
	}
	function positionModal() {
		/* Top */
		modalTop = ( (winHeight-(modalHeight+100))/2 )/2 ;
		if (modalTop <= 0) {
			modalTop = $(window).scrollTop() + 10;
		} else {
			modalTop = $(window).scrollTop() + 100;
		}
		$(".modal").css("top",modalTop+"px");
		
		/* Left */
		modalMarginLeft = modalWidth/2;
		if (winWidth <= wrapperWidth) {
			$(".modal").css("margin-left","-"+modalMarginLeft+"px");
			$(".modal").css("left", (wrapperWidth/2)+"px");
		} else {
			$(".modal").css("margin-left","-"+modalMarginLeft+"px");
			$(".modal").css("left","50%");
		}
	}
	function modalInit() {
		getDimensions();
		sizeOverlay();
		positionModal();
	}
	$(window).resize(function(){
		var Mask = document.getElementById("overlayBkgdDiv");
		if (Mask) {
			if (Mask.style.visibility == "visible") {
				modalInit();
			}
		}
	});
	
	//select all the a tag with name equal to modal
	$('.modal_link').click(function() {
		modalInit();
	});
	// END: modal window
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: select_all / unselect_all
	$('.select_all').click(function() {
		var $thisForm = $(this).parents('fieldset');
		$thisForm.find(':checkbox').attr("checked","checked");
		return false;
	});
	$('.unselect_all').click(function() {
		var $thisForm = $(this).parents('fieldset');
		$thisForm.find(':checkbox').removeAttr("checked")
		return false;
	});
	// END: select_all / unselect_all
	
	
	
	
	// ---------------------------------------------------------------------------------------
	// BEGIN: Use ellipsis max characters
	var slicePoint = 95;
	$('.trunkit').each(function() {
		var allText = $(this).html();
		if (allText.length > slicePoint) {
			var startText = allText.slice(0,slicePoint).replace(/\w+$/,'');
			startText = startText + "...";
			$(this).html(startText);
		}
	});
	// END: Use ellipsis max characters
	
	
});
