function scaleTo(width, height) {
// This is a total hack.  Time ran out to give the designer a logical function call
// for the Flash "close" button.  So, he used this function call from previous flash
// files, and I assumed that it was a built in function, but I should have realized
// that it was not.  So, it gave me an excuse to remove the DIV that the flash movie
// is appended to in the footer file.
	$("#demo_holder").remove();
}

function readCookie(name) {
	if (document.cookie && document.cookie != '') {
	    var cookies = document.cookie.split(';');
	    for (var i = 0; i < cookies.length; i++) {
	        var cookie = jQuery.trim(cookies[i]);
	        if (cookie.substring(0, name.length + 1) == (name+'=')) {
	            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	            return cookieValue;
	        }
	    }
	}
}

function newWindow(mainHTML, headHTML, footHTML){
	if (!mainHTML) { var mainHTML = ""; }
	if (!headHTML) { var headHTML = "&nbsp;"; }
	if (!footHTML) { var footHTML = "&nbsp;"; }
	
	if (!$("#popupcontent").length){
		$popout = $("<div id=\"popup\"></div>").addClass("popup");
	} else {
		$popout = $("#popup").empty();
		if (!$popout.hasClass("popup")) { $popout.addClass("popup"); }
	}
	
	$popup = $("<div id=\"popupcontent\"></div>");
	$("body").append($popout.append($popup));
	
	$close = $("<a>Close</a>").addClass("close").click(function(){ $("#popup").remove();});
	$popupHead = $("<div id=\"popuphead\"></div>").html(headHTML).prepend($close);
	$popupFoot = $("<div id=\"popupfoot\"></div>").html(footHTML);
	$popup.html("<div class=\"wrapper\">"+mainHTML+"</div>");
	$popout.prepend($popupHead).append($popupFoot);

	$popout.css({
		position:"absolute",
		"z-index":100,
		top:($(window).height()-$popout.outerHeight())/2+$(window).scrollTop(),
		left:($(window).width()-$popout.outerWidth())/2
	});
}

$(document).ready(function(){
// Homepage Body Banner
// set jQuery CSS to reflect appropriate #page class, then update href and title and span text to reflect campaign.
	var welcome = $('<div class="tierone-welcome"><a href="/welcome/" title="Welcome."><span>Welcome.</span></a></div>');
	var banking = $('<div class="tierone-banking"><a href="/welcome/tieroneaccountaccess/" title="Internet Banking Center Access"><span>Internet Banking Center Access</span></a></div>');
	$(".tierone_welcome #homepage_flash").append(welcome).append(banking);
	$(".tierone-banking a").click(function(ev){
		ev.preventDefault();
		
		var mainHTML = "";
		mainHTML += "<h3>Looking for Online Banking?</h3>";
		mainHTML += "<p>As of August 30th, all former TierOne Bank Online Banking users can access their accounts through the log-in area on our homepage. Just enter your new User ID and PIN at the top of the homepage under \"Account Access.\"</p>";
		mainHTML += "<p>Need help? You can still view the <a href=\"/welcome/tieroneaccountaccess/\">Account Access support page</a> for questions or instructions.</p>";
		
		newWindow(mainHTML);
	});

// Hiders class - mimics the FAQ style
	$(".hiders h3").addClass("expands").addClass("closed")
		.click(function(){
			var name = $.trim($(this).text());
			var action = "";
			if ($(this).hasClass("closed")) { action = "showing"; } else { action = "hiding"; }
			 
			$(this).toggleClass("closed");
			$(this).next().slideToggle();
			pageTracker._trackPageview("/hiders/"+name+"/"+action+"/");
		});
	$(".hiders div").hide();

//Compliance Popups.  Messages for those heading off-site
	$(".notresponsible").click(function(ev){
		ev.preventDefault();
		
		var url = $(this).attr("href");
		var title = $(this).attr("title");
		var target = $(this).attr("target");
		var mainHTML = "";
		mainHTML += "<h3>You Are Leaving Our Site</h3>";
		mainHTML += "<p>You are leaving Great Western Bank's web site. Any products and services accessed through this link are not provided, endorsed, or guaranteed by Great Western Bank. Great Western Bank is not responsible for the content, privacy policies, services, etc. of external web sites.</p>";
		mainHTML += '<p><a href="'+url+'" title="'+title+'" target="'+target+'">Click here</a> to continue.</p>';
		
		newWindow(mainHTML);
	});
	$(".partner").click(function(ev){
		ev.preventDefault();
		
		var url = $(this).attr("href");
		var title = $(this).attr("title");
		var target = $(this).attr("target");
		var mainHTML = ""
		mainHTML += "<h3>You Are Leaving Our Site</h3>";
		mainHTML += "<p>You are going to a site that is not owned by Great Western Bank.  However, the site content is maintained by the bank. Great Western Bank has partnered with this company to offer this service to its customers. Please review the site's privacy policy and security statement as they differ from Great Western Bank's.</p>";
		mainHTML += '<p><a href="'+url+'" title="'+title+'" target="'+target+'">Click here</a> to continue.</p>';
		
		newWindow(mainHTML);
	});
	$(".notresponsible_agribusiness").click(function(ev){
		ev.preventDefault();
		
		var url = $(this).attr("href");
		var title = $(this).attr("title");
		var target = $(this).attr("target");
		var mainHTML = "";
		mainHTML += "<h3>You Are Leaving Our Site</h3>";
		mainHTML += "<p>You are now leaving our site and being linked to a third party website that is not affiliated with Great Western Bank. Any products and services accessed through this link are not provided, endorsed or guaranteed by Great Western Bank. Great Western Bank is not responsible for the site's content and/or privacy policies and does not guarantee that any file or program available for download and/or execution from or via this site is free of computer viruses or other conditions which could damage or interfere with data, hardware or software. Great Western Bank will not be liable for any loss or damage resulting from your use of any aspect of this site and all such use is solely at your risk.</p>";
		mainHTML += '<p><a href="'+url+'" title="'+title+'" target="'+target+'">Click here</a> to continue.</p>';
		mainHTML += '<p>To return to the Great Western Bank website, click close.</p>';
		
		newWindow(mainHTML);
	});
	$(".notresponsible_nabagribusiness").click(function(ev){
		ev.preventDefault();
		
		var url = $(this).attr("href");
		var title = $(this).attr("title");
		var target = $(this).attr("target");
		var mainHTML = "";
		mainHTML += "<h3>You Are Leaving Our Site</h3>";
		mainHTML += "<p>You are now leaving our site and being linked to a third party website. Any products and services accessed through this link are not provided, endorsed or guaranteed by Great Western Bank. Great Western Bank is not responsible for the site's content and/or privacy policies and does not guarantee that any file or program available for download and/or execution from or via this site is free of computer viruses or other conditions which could damage or interfere with data, hardware or software. Great Western Bank will not be liable for any loss or damage resulting from your use of any aspect of this site and all such use is solely at your risk.</p>";
		mainHTML += '<p><a href="'+url+'" title="'+title+'" target="'+target+'">Click here</a> to continue.</p>';
		mainHTML += '<p>To return to the Great Western Bank website, click close.</p>';

		newWindow(mainHTML);
	});
	

//Auto select in text boxes
	$(":input[type=text]").focus(function(){ this.select(); });

//Hovers for buttons and images
	$(".hover")
		.mouseover(function(){
			var cur = $(this).attr("src");
			path = cur.substring(0,cur.lastIndexOf('/')+1);
			file = cur.substring(cur.lastIndexOf('/')+1, cur.indexOf('.'));
			ext  = cur.substring(cur.indexOf('.')+1, cur.length);
			$(this).attr("src",path+file+"_over."+ext);
		})
		.mouseout(function(){
			var cur = $(this).attr("src");
			path = cur.substring(0,cur.lastIndexOf('/')+1);
			file = cur.substring(cur.lastIndexOf('/')+1, cur.indexOf('_over'));
			ext  = cur.substring(cur.indexOf('.')+1, cur.length);
			$(this).attr("src",path+file+"."+ext);
		});

//Products collapseables
	//Add details button, add click event, excuse the first, then toggle details and hide the <div> next to the header.
	$("#main .products :header")
		.before("<div class=\"details minus\">Hide Details</div>")
		.click(function(){
			if ($(this).prev(".details").hasClass("plus")) {
				name = $(this).children("span").text();
				$(this).prev(".details").text("Hide Details");
				pageTracker._trackPageview("/product/header/"+name+"/showing/");
			} else {
				name = $(this).children("span").text();
				$(this).prev(".details").text("Show Details");
				pageTracker._trackPageview("/product/header/"+name+"/hiding/");
			}
			$(this).prev(".details").toggleClass("plus").toggleClass("minus");
			$(this).next("div").slideToggle();
		})
		.prev(".details").click(function(){
			if ($(this).hasClass("plus")) {
				name = $(this).next("h3").children("span").text();
				$(this).text("Hide Details");
				pageTracker._trackPageview("/product/details/"+name+"/showing/");
			} else {
				name = $(this).next("h3").children("span").text();
				$(this).text("Show Details");
				pageTracker._trackPageview("/product/details/"+name+"/hiding/");
			}
			$(this).toggleClass("plus").toggleClass("minus");
			$(this).next(":header").next("div").slideToggle();
		}).end()
		.not(":first")
		.prev(".details").toggleClass("plus").toggleClass("minus").text("Show Details").end()
		.next("div").hide();

//Blue buttons on interior pages for Your Financial Life and Making Life Great
	$(".bluebutton")
		.attr("src","/_files/Images/bt_blue_plus.gif")
		.attr("title","Content is collapsed. Click to view.")
		.next(":header").andSelf()
		.click(function(){
			$(this).siblings(".information").slideToggle();
			if ($(this).hasClass("bluebutton")) {
				$el = $(this);
				thing = "bluebutton";
				name = $(this).next("h3").text();
			} else {
				$el = $(this).prev(".bluebutton");
				thing = "header";
				name = $(this).text();
			}
			if ($el.attr("src").indexOf('plus') != -1) {
				$el.attr("src","/_files/Images/bt_blue_minus_over.gif").attr("title","Content is expanded. Click to hide.");
				pageTracker._trackPageview("/sidebar/"+thing+"/"+name+"/showing/");
			} else {
				$el.attr("src","/_files/Images/bt_blue_plus_over.gif").attr("title","Content is collapsed. Click to view.");
				pageTracker._trackPageview("/sidebar/"+thing+"/"+name+"/hiding/");
			}
		}).siblings(".information").hide();

//Allows us to set a widget to stay open on page load
	if (location.hash !== "") {
		var hash = location.hash.substring(1);
		var $possible = $(".widget").find("."+hash);
		if ($possible.length === 0) {
			$possible = $(".widget").find("#"+hash);
		}
		$possible.find(".bluebutton").click();
	}
	
//FAQs can go anywhere. Let's have fun!
	$("#faqs dt").addClass("expands").addClass("closed")
		.click(function(){
			var name = $.trim($(this).text());
			var action = "";
			if ($(this).hasClass("closed")) { action = "showing"; } else { action = "hiding"; }
			 
			$(this).toggleClass("closed");
			$(this).next().slideToggle();
			pageTracker._trackPageview("/faq/"+name+"/"+action+"/");
		});
	$("#faqs dd").hide();
	
	$("<div />").text("Show All Answers").addClass("expand_all").addClass("closed")
		.click(function(){
			$(this).toggleClass("closed");
			$("#faqs dt").click();
		}).prependTo("#faqs");
		
//How about a blue table? I can't wait!
	$("table.blue")
		.find("tr:even").addClass("alt").end()
		.each(function(){
			var to = $(this).offset();
			var th = $(this).outerHeight();
			var tw = $(this).outerWidth();
			
			var ul = $(this).find("tr:first").children("th:first,td:first");
			var ur = $(this).find("tr:first").children("th:last,td:last");
			var ll = $(this).find("tr:last").children("th:first,td:first");
			var lr = $(this).find("tr:last").children("th:last,td:last");
			
			var upper = to.top - 1 + "px";
			var lower = to.top + th - 9 + "px";
			var left = to.left + "px";
			var right = to.left + tw - 11 + "px";
			
			$("<div class=\"upperleft\"></div>")
				.height(10).width(12)
				.css({ "position": "absolute", "top": upper, "left": left})
				.prependTo(ul);
			$("<div class=\"upperright\"></div>")
				.css({ "position": "absolute", "top": upper, "left": right})
				.prependTo(ur);
			$("<div class=\"lowerleft\"></div>")
				.css({ "position": "absolute", "top": lower, "left": left})
				.prependTo(ll);
			$("<div class=\"lowerright\"></div>")
				.css({ "position": "absolute", "top": lower, "left": right})
				.prependTo(lr);
		});

//Email Address Validation ('Enter your email again... and again...')
	$('form[name=FORM_454]').bind('submit', function() {
		if( $('#Form_5_16').val() != $('#Form_44_20').val() ) {
			alert('The following form field(s) were incomplete or incorrect: \n\nEmail Address:\nEmail Addresses don\'t match.');
			$(this).find('input[name=Submit]').attr('disabled', '');
			return false;
		}
	});
	
});