
function collapseHidden() {
	$(".collapseMe").hide();
	$(".collapseMe").each(function () {
		if ($(this).prev().is("ul, ol")) {
			var linkPrefixE = '<li class="expandButton">';
			var linkSuffixE = '</li>';
			var insideAhrefE = '';
			var linkPrefixC = '<li class="collapseButton">';
			var linkSuffixC = '</li>';
			var insideAhrefC = '';
			}
		else {
			var linkPrefixE = ' ';
			var linkSuffixE = '';
			var insideAhrefE = ' class="expandButton"';
			var linkPrefixC = ' ';
			var linkSuffixC = '';
			var insideAhrefC = ' class="collapseButton"';
			}
		
		var titleExpand = $(this).attr('title');
		
		if (!titleExpand) titleExpand = 'Tell me more &rarr;';
		
		$(this).prev().append(linkPrefixE+'<a href="javascript:void(0)"'+insideAhrefE+'>'+titleExpand+'</a>'+linkSuffixE);
		$(this).prev().append(linkPrefixC+'<a href="javascript:void(0)"'+insideAhrefC+'>Hide &larr;</a>'+linkSuffixC);

		$(this).removeAttr('title');
		});
	}

function expandHidden() {
	$(".expandButton").click(function (){ 
		$(this).fadeOut(100, function (){
			$(this).parents().next(".collapseMe").slideDown(300, function(){ 
				$(this).css({ 'display': "block" });
				/* 
				  because internet explorers work in mysterious ways... Table contained in the .collapseMe blinks and disappears
				  when the animation finishes--unless opacity is forced--but forced opacity breaks font anti-alliasing when on 
				  normal text blocks, causing weird black artifacts around some letters.
				*/
				if ($.browser.msie && parseInt ($.browser.version) == 7 && $(this).children("table").length > 0) $(this).css({"opacity" : "1.0"});
				});
			$(this).next(".collapseButton").fadeIn(100);
			});		
		});
	}

function collapseExpanded() {
	$(".collapseButton").click(function (){ 
		$(this).fadeOut(100, function (){
			$(this).parents().next(".collapseMe").slideUp(300);
			$(this).prev(".expandButton").fadeIn(100);
			});		
		});
	}

function dummyTextSearchBox() {
	$("#queryStringBox").focus(function () {
		if($(this).val() == 'Search Jefferson Bank') {
			$(this).val('');
			$(this).css({'color': "#01392d", 'font-style': "normal"});
			}
		});
	$("#queryStringBox").blur(function () {
		if($(this).val() == '') {
			$(this).val('Search Jefferson Bank');
			$(this).css({'color': "#646464", 'font-style': "italic"});
			}
		});
	}

function replaceHeading(){
	$("h1").each(function (){
		if ($.browser.msie && parseInt($.browser.version) < 8) $(this).css({ "height" : "39px", "position" : "relative", "left": "-17px" });
		else $(this).css({ "height" : "39px", "margin-left": "-19px" });
		h1Content = $(this).text();
		$(this).flashembed({ src: "/assets/flash/hRep.swf", wmode: "transparent" }, { headingText: encodeURIComponent(h1Content) });
		});
	}

function zebraTheTables(){
	$("#copyCopy table").each(function(){ 
		$(this).find("tr:even").addClass("evenRow");
		$(this).find("tr").each(function(){ $(this).find("td:last, th:last").addClass("lastOnTheRight"); })
		});
	}

function FormatMoneyAmount(starting_string, ending_string) {
  //check validity of input (true = invalid, false = valid)
  var valid_exp = new RegExp ('[^0-9,.$]', 'gi');
  input_invalid = (typeof(ending_string) == 'undefined' && valid_exp.test(starting_string));
  
  //check if more than 2 digits follow decimal or no decimal
  decimal_invalid = typeof(ending_string) == 'undefined' && (starting_string.indexOf('.') > -1) && ((starting_string.length - starting_string.indexOf('.')) > 3);

  if (input_invalid || decimal_invalid) {
    ending_string = starting_string;
  } else {
    //remove commas, dollar signs
    var replace_exp = new RegExp ('[,$]', 'gi');
    starting_string = starting_string.replace(replace_exp, '');

    //remove decimal if ending string not set, save for adding on later
    var decimal_substring = '';
    if (typeof(ending_string) == 'undefined' && starting_string.indexOf('.') > -1) {
      decimal_substring = starting_string.substring(starting_string.indexOf('.'), starting_string.length);
      remaining_string = starting_string.substring(0,starting_string.indexOf('.'));
    } else {
      remaining_string = starting_string;
    }
    
    //if string is already 3 characters or less, do nothing
    if (remaining_string.length > 3) {
      //separate last 3 characters of string from rest of string
      var final_three = remaining_string.substring(remaining_string.length - 3, remaining_string.length);
      remaining_string = remaining_string.substring(0, remaining_string.length - 3);
      
      //if not first group of 3, add new group before old group with comma, else set to new group
      ending_string = (typeof(ending_string) == 'undefined') ? final_three + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring) : final_three + ',' + ending_string;
      
      //call function again if more than 3 digits remaining to process, else add to end string
      if (remaining_string.length > 3) {
        ending_string = FormatMoneyAmount(remaining_string, ending_string);
      } else {
        ending_string = remaining_string + ',' + ending_string;
      }
    } else {
      ending_string = (typeof(ending_string) == 'undefined') ? remaining_string : remaining_string + ',' + ending_string + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring);
    }
  }
  return ending_string;
}

var safeList = new Array('jeffersonbank.com','netteller.com','cm.netteller.com');

// Check whether links are external:
// (Only works with elements that have href):
$.extend($.expr[':'], {
    external: function(a, i, m) {
        if (!a.href) return false;
        if (a.hostname && a.hostname !== window.location.hostname) {
            var fixedHostname = a.hostname.replace(/^\s+|\s+$/g, '').replace('www.', '').toLowerCase();
            if (jQuery.inArray(fixedHostname, safeList) >= 0) return false;
            else return true;
        	}
        else return false;
		}
	});

$(document).ready(function(){
	zebraTheTables();
	replaceHeading();
	collapseHidden();
	expandHidden();
	collapseExpanded();
	dummyTextSearchBox();
    $('a:external:not(.clearToLeave)').click(function(e) {
		if (confirm('You are now leaving the Jefferson Bank website. The site you are about to enter may be less secure and may have a privacy statement that differs from Jefferson Bank. The products and services offered on this third party website are not guaranteed by Jefferson Bank.\n\nSelect OK to leave our site. \n\nSelect CANCEL to remain at our site.') != true) e.preventDefault();
		});
	$(".formatNumberOnLoadPlz").each(function (){
		$(this).val( FormatMoneyAmount($(this).val()) ); });
	$("#homepageFlash").flashembed({ src: "/assets/flash/jbank_flash_v4.swf", wmode: "opaque" }); 
	$("#menuPrimary li a span").css({ 'opacity': "0.0", 'display': "block" });
	$("#menuPrimary li a span").hover(function(){$(this).fadeTo(5, 1.0);}, function(){$(this).fadeTo(300, 0.0);} );
});