// function to equalize heights
jQuery.fn.equalizeCols = function(){ 
	var el, height = 0, h;
	this.each(function() {
		el = jQuery(this).css("height", "auto"); 
		h = el.height(); 
		height = (h > height) ? h : height;
	}); 
	return this.each(function() { 
		jQuery(this).css("height", height); 
	}); 
};


// ** Portal home page boxes **
// **                        **
$(function(){ // (executes when the dom is ready)
	$("#navigation li a").hover(function(){
		$("#navigation li a").removeClass("active");
		$(this).addClass("active");
		$("#box-withnav .feature-box").removeClass("feature-box-active");
		$("#"+$(this).attr("rel")).addClass("feature-box-active");
		return false;
	},function(){});
});

// ** Prompt when leaving legacy site **
// **                                 **
$(function(){ // (executes when the dom is ready)
	$("a").filter(function() {
		if (this.hostname.search(/legacytexas/) < 0) {
			return true;
		} else {
			return false;
		};
		//return this.hostname && this.hostname !== location.hostname;
	}).click(function () { 
		var x = window.confirm('You are about to leave this site; do you wish to continue?\n\nLegacyTexas does not endorse products or services provided by other organizations.  Content encountered and information collected on  \nany other website is the responsibility of the owner and outside of our control.  We encourage you to be cautious and protect your identity.');
		var val = false;
		if (x)
			val = true;
		else
			val = false;
		return val;
	});
});

// ** Bank login form        **
// **                        **
$(function(){ // (executes when the dom is ready)
	$("#bankloginform .dark-orange-button").unbind('click').click(function(){
		// submit form
		$("#bankloginform").submit();
		return false;
	})
});


// ** Change first letter backgrounds **
// **                                 **
$(function(){ // (executes when the dom is ready)
	try {
		// grab the text, find the first letter (the replace function trims spaces off the front)
		var headingText = $(".feature-box h2").html().replace(/^\s+/,"").toLowerCase();
		var headingLetter = headingText.split("",1);
		$(".feature-box h2").addClass("letter"+headingLetter);
	} catch(err) {};
	try {
		// grab the text, find the first letter (the replace function trims spaces off the front)
		var headingText2 = $("#box3 .inside .left h2").html().replace(/^\s+/,"").toLowerCase();
		var headingLetter2 = headingText2.split("",1);
		$("#box3 .inside .left h2").addClass("letter"+headingLetter2);
	} catch(err) {};
});

// ** Hide/Reveal content **
// **                     **
$.hideReveal = function() {
	$(".hide-reveal-content").not(".default-opened").hide();
	$(".hide-reveal-switch, .hide-reveal-switch-faq").unbind('click');
	$(".hide-reveal-switch, .hide-reveal-switch-faq").click(function(){
		var check1 = $(this).nextAll(".hide-reveal-content:first");
		var check2 = $(this).parent().nextAll(".hide-reveal-content:first");
		// see if popup-navigation exists, this will be used to determine if we should scroll the page or not
		var scrollPage = ($("#popup-navigation").length > 0) ? false : true;
		// hide any other opened content (except the content they want to toggle)
		$(".hide-reveal-content").not(check1).not(check2).slideUp("normal");
		$(".hide-reveal-switch-active").removeClass("hide-reveal-switch-active");
		$(".hide-reveal-switch-faq-active").removeClass("hide-reveal-switch-faq-active");
		// slide open new content
		if (check1.length > 0) {
			if ($(this).is(".hide-reveal-switch-faq")) {
				if ($(check1).is(":visible")) {
					$(this).removeClass("hide-reveal-switch-faq-active");
				} else {
					$(this).addClass("hide-reveal-switch-faq-active");
				};
			} else {
				if ($(check1).is(":visible")) {
					$(this).removeClass("hide-reveal-switch-active");
				} else {
					$(this).addClass("hide-reveal-switch-active");
				};
			};
			check1.slideToggle("normal", function(){
				if (scrollPage) {	$.scrollTo(check1, 800, {offset: {top:-150, left:0} }); };
			});
		} else if (check2.length > 0) {
			if ($(this).is(".hide-reveal-switch-faq")) {
				if ($(check2).is(":visible")) {
					$(this).removeClass("hide-reveal-switch-faq-active");
				} else {
					$(this).addClass("hide-reveal-switch-faq-active");
				};
			} else {
				if ($(check2).is(":visible")) {
					$(this).removeClass("hide-reveal-switch-active");
				} else {
					$(this).addClass("hide-reveal-switch-active");
				};
			};
			check2.slideToggle("normal", function(){
				if (scrollPage) {	$.scrollTo(check2, 800, {offset: {top:-150, left:0} }); };
			});
		} else {
			
		};
		return false;
	});
};

// ** Banking home page tabs **
// **                        **
$(function(){ // (executes when the dom is ready)
	$("#tab-nav-personal a").click(function(){
		$(this).addClass("active");
		$("#tab-nav-business a").removeClass("active");
		$("#tab-content-business").removeClass("active");
		$("#tab-content-personal").addClass("active");
		$("#right-column-box").addClass("active");
		$("#right-column-box2").removeClass("active");
		return false;
	});
	$("#tab-nav-business a").click(function(){
		$(this).addClass("active");
		$("#tab-nav-personal a").removeClass("active");
		$("#tab-content-personal").removeClass("active");
		$("#tab-content-business").addClass("active");
		$("#right-column-box2").addClass("active");
		$("#right-column-box").removeClass("active");
		return false;
	});
});

// ** Links which open popup window **
// **     
quicksearch = '';
$(function(){ // (executes when the dom is ready)
	// if they submit the form in the traditional way, trigger a click on the submit link
	$("#zipcodequicksearchform").submit(function(){
		$("#zipcodequicksearch-submit").click();
		return false;
	});
	$(".popup-window-link").click(function(){
		// see if it's zipcodequicksearch-submit
		if ($(this).is("#zipcodequicksearch-submit")) {
			quicksearch = $("#zipcodequicksearch").attr("value");
		};
		// update hash
		//parent.location.hash = $(this).attr("href");
		// scroll to top of page
		$.scrollTo($('body'), 800, {offset: {top:0, left:0} });
		// show our window
		$("#blackout, #popup-window-wrapper, #popup-window").css("display","block");
		// size height absolutely for IE 6
		if ($.browser.msie && ($.browser.version <= 6)) {
			var iebodyheight = $("body").height();
			$("#blackout").css("height",iebodyheight);
		};
		//load content
		var pageToLoad = $(this).attr("href") + "/windowed";
		$.get(pageToLoad, "", function(data){
			// refresh elements (add content and reinitialize)
			$.resetElements(data);
		}, "html");
		return false;
	});
	// function to reset our newly loaded html via some scripts
	$.resetElements = function(data) {
		$("#popup-window-inside").html(data);
		// do misc enhancements as we would when loading the dom for the first time
		$.miscFixesEnhancements();
		// make close button work
		$("#popup-window-close span").click(function(){
			$("#blackout, #popup-window-wrapper, #popup-window").css("display","none");
			$("#popup-window-inside").html("<div id='popup-activity'></div>");
			return false;
		});
		// make internal popup-nav work properly (load content inside current popupwindow)
		$("#popup-navigation a").click(function(){
			var pageToLoad = $(this).attr("href") + "/windowed";;
			//parent.location.hash = $(this).attr("href");
			$.get(pageToLoad, "", function(data){
				// refresh elements
				$.resetElements(data);
			});
			return false;
		});
	};
});

// add default text functionality to input fields function
$.defaultTextInput = function() {
	$('.input-default-text').each(function(i) {
		var defaultValue = $(this).attr('value');
		$(this).focus(function () { if (this.value == defaultValue) this.value = ''; this.style.color = '#252525' });
		$(this).blur(function () { if (this.value == '') { this.value = defaultValue; this.style.color = '#91816b' }});
	});
	$('.input-default-text-password').each(function(i) {
		var defaultValue = $(this).attr('value');
		var defaultName = $(this).attr('name');
		var defaultClass = $(this).attr('class');
		var defaultID = $(this).attr('id');
		$(this).focus(function () { 
			if (this.value == defaultValue) {
				$(this).replaceWith('<input id="'+defaultID+'" class="type-password '+defaultClass+'" type="password" name="'+defaultName+'" value="">');
				$('#'+defaultID).focus();
			};
		});
	});
};

// ** Misc **
// **      **
$.miscFixesEnhancements = function() {
	// execute hide/reveal stuff
	$.hideReveal();
	// make any links with rel="external" to open in new windows
	$("a[@rel$='external']").click(function(){
		this.target = "_blank";
	});
	// stripe table
	$("table.chart").wrap("<div class='chart-wrapper'></div>");
	$("table.form").wrap("<div class='form-wrapper'></div>");
	$("table.chart tr:even, table.form tr:even").addClass("stripe");
	// misc IE 6 stuff
	if ($.browser.msie && ($.browser.version <= 6)) {
		// give radio buttons and checkboxes "type-checkbox" and "type-radio" classes for ie
		$(":checkbox").addClass("type-checkbox");
		$(":radio").addClass("type-radio");
		$(":submit").addClass("type-submit");
		// css :first-child and :last-child support
		$("#curve-bar ul li:first").addClass("first-child");
		$("#curve-bar ul li:last").addClass("last-child");
		$("#popup-navigation li:first").addClass("first-child");
		$("#popup-navigation li:last").addClass("last-child");
	};
	// misc IE 7 stuff
	if ($.browser.msie && ($.browser.version == 7)) {
		// css :last-child support
		$("#curve-bar ul li:last").addClass("last-child");
		$("#popup-navigation li:last").addClass("last-child");
	};
	// misc IE stuff
	if ($.browser.msie && ($.browser.version <= 7)) {
		// hr's
		$("hr.hr2").replaceWith('<div class="hr2"></div>');
		$("hr").replaceWith('<div class="hr"></div>');
	};
	// misc FF 2 stuff
	if ($.browser.mozilla && $.browser.version.substr(0,3) == "1.8") {
		// hr's
		$(".orange-button, .dark-orange-button").addClass("ff2-inline-block");
	};
	// add spans inside certain elements
	$(".orange-button, .dark-orange-button").append("<span></span>");
	// add default text functionality to input fields
	$.defaultTextInput();
	// equalize front page columns
	$(".portal .column-box .inside2").equalizeCols();
};

// ** Misc **
// **      **
$(function(){ // (executes when the dom is ready)
	// misc fixes
	$.miscFixesEnhancements();
	// make logo a link to go back to home page
	$("#logo").click(function(){
		document.location = "/";
	}).css("cursor","pointer");
	// rollover stuff
	$(".navigation-bar>li").hover(function(){
		$(this).addClass("hover");
		$(this).children("a:first").addClass("hover");
	}, function(){
		$(this).removeClass("hover");
		$(this).children("a:first").removeClass("hover");
	});	
});

// ** Misc for Window load **
// **                      **
$(window).load(function(){ // (executes when the entire page is loaded (images, etc.))
	// remove background color
	$(".feature-box .finside").css("background-color","transparent");
});

/* stop IE6 flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}