var FLASH_OBJ_ID		= "gwim";
var FLASH_OBJ_DIV_ID	= "flash_content";

var flash2cmIdMap = {
    "theBigPicture" : "/default-content/", 
    "commonQuestions" : "/faq/", 
    "calculatorWithChecklist" :"/calculator/", 
    "findTheRightIRA" : "/accounts/", 
    "quiz" : "/accounts/quiz/", 
    "learnTheBasics" : "/accounts/basics/", 
    "iraComparison" : "/accounts/ira-comparison/", 
    "iraChooseTheRightOne" : "/accounts/roth-vs-traditional-ira/", 
    "rolloverIRA" : "/accounts/rollover-ira/", 
    "rothIRA" : "/accounts/roth-ira/", 
    "traditionalIRA" : "/accounts/traditional-ira/", 
	 "transferIRA" : "/accounts/transfer-ira/", 
	 "consolidate" : "/consolidate/", 
    "featuredIRA" : "/featured/", 
	 "featuredHighYield" : "/featured/high-yield-ira-cd/", 
	 "featuredStartTodayIRA" : "/featured/starttodayira/", 
	 "featuredCreditCard" : "/featured/irarewards/", 
    "howToOpen" : "/open-ira/", 
    "eighteenMonthCD" : "/products/18-month-cd/", 
    "waysToInvest" : "/products/", 
    "highYieldCD" : "/products/high-yield-cd/", 
    "mutualFunds" : "/products/mutual-funds/", 
	"stocksBonds" : "/products/stocks-bonds-etfs/", 
    "moneyMarketSavings" : "/products/money-market-savings/", 
    "Roth2010" : "/2010roth/", 
	"time_frame" : "time_frame", 
	"retirement_savings" : "retirement_savings", 
	"your_annual_income" : "your_annual_income", 
	"monthly_savings" : "monthly_savings", 
	"investment_style" : "investment_style", 
	"your_results" : "your_results", 
	"get_there" : "get_there",
	"fdicrollover":"fdicrollover"
};

var lastDisclaimerId = null;
var LOADED = HASHED = false;
var _this = this;

function $(id){ return document.getElementById(id) }

function resize(h) {
	resizeFlashObj(FLASH_OBJ_ID, h);
	resizeDiv(FLASH_OBJ_DIV_ID, h);
}

function resizeFlashObj(id, h) {
	if ( navigator.appVersion.indexOf("MSIE 6")> -1 ) {
		var flashMov=getMovie(id);
		
		flashMov.height=h;
	}
	
	if ( navigator.userAgent.indexOf("Safari")> -1 ) {
		var flashMov=getMovie(id);
		
		flashMov.height=h;
	}
}

/* We only need to use this if user deeplinks to section; the second link after that site needs to reload to get off of the deep link path
	/featured/high-yield-ira-cd/ to the root. hide flash so we don't show Flash section change before page re draws */
function hideFlashDiv() {
	$("disclaimer_text").style.display = "none";
	$("page_footer").style.display = "none";
	$(FLASH_OBJ_DIV_ID).style.display = "none";
}

function resizeDiv(id, h) {
	$(id).style.height=(h - 1) + "px";
}

function getMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) { return window[movieName]; }
	
	return document[movieName];
}

function openWin(URL, width, height) {
	var focusFail=false;
	var features="width=" +width+",height=" +height+",scrollbars=yes,menubar=no,toolbar=no,resizable=yes,left=100,top=100,fullscreen=no";
	var newWin=window.open(URL,"translatorWindow",features);
	
	if ( navigator.appVersion.indexOf("NT") != -1 ) {
		if (navigator.appVersion.indexOf("NT 5") != -1) {
			focusFail = false;
		} else if (ie4 == true) {
			focusFail = true;
		}
	}
	
	if ( focusFail == false ) {
		if (window.focus) { newWin.focus(); }
	}
}

function getValueFromQS(id) {
	var s=location.search.substring(1);
	
	if ( s != "" ) {
		var kvs = ( s.indexOf("&") > -1 ) ? s.split("&") : [s];
		
		for( var i = 0; i<kvs.length; i++ ) {
			var tmp = kvs[i].split("=");
			
			if (tmp[0] == id) { return tmp[1]; }
		}
	}
	
	return false;
}

function setCookie(key, value) {
	cookies.set(key, value);
}

function getCookie(key) {
  var c = cookies.get(key);
  
  return ( c != undefined ) ? c : "";
}

function delCookie(key) {
  cookies.del(key);
}

function restorePreChartDisclaimer() {
	if (lastDisclaimerId != null) {
		changeFlashDisclaimer(lastDisclaimerId);
	}
}

function changeFlashDisclaimer(id) {
	var flash2jsIdMap = {
		"theBigPicture" : "home_content",
		"learnTheBasics" : "accounts_basics",
		"quiz" : undefined,
		"commonQuestions" : "faq_default",
		"findTheRightIRA" : "accounts_default",
		"calculatorWithChecklist" : "calculator_content",
		"checklist" : "checklist_default",
		"checklistOffTrack" : "checklist_off_track",
		"iraComparison" : "accounts_ira_comparison",
		"iraChooseTheRightOne" : "accounts_roth_vs_traditional_ira",
		"rolloverIRA" : "accounts_rollover_ira",
		"transferIRA" : "accounts_transfer_ira",
		"consolidate" : "consolidate_default",
		"fdicrollover" : "fdicrollover_default",
		"rothIRA" : "accounts_roth_ira",
		"traditionalIRA" : "accounts_traditional_ira",
		"featuredIRA" : "featured_default",
		"featuredHighYield" : "featured_high_yield_cd_ira",
		"featuredStartTodayIRA" : "featured_start_today",
		"featuredCreditCard" : "featured_credit_card",
		"howToOpen" : "open_ira_default",
		"eighteenMonthCD" : "products_18_month_cd",
		"waysToInvest" : "products_default_MODEL",
		"waysToInvestNW" : "products_default_NW",
		"highYieldCD" : "products_high_yield_cd",
		"mutualFunds" : "products_mutual_funds",
		"stocksBonds" : "products_stocks_bonds",
		"moneyMarketSavings" : "products_money_market_savings_MODEL",
		"moneyMarketSavingsNW" : "products_money_market_savings_NW",
		"roth" : "charts_roth",
		"traditional" : "charts_traditional",
		"maximize" : "checklist_maximize",
		"Roth2010" : "roth2010_default"
	};
	
	if( id != "rothChart" && id != "traditionalChart" ) {
		lastDisclaimerId = id;
	}
	
	if( id == "waysToInvest" || id == "moneyMarketSavings" ) {
		if ( getCookie("state") == "ID" || getCookie("state") == "WA" ) {
			id += "NW";
		}
	}
	
	var key = flash2jsIdMap[id];
	
	try {
		var txt = (disclaimer_js[key] != undefined) ? disclaimer_js[key] : "";
		//console.log(flash2jsIdMap[id] + " :: " + key + " :: " + disclaimer_js[key]);
		
		$("disclaimer_text").innerHTML = txt;
	} catch (e) {}
}

//change out page disclaimer based on page key; for Flash page only
function changeFlashPageTitle(id) {
	var flash2PageTitle = {
		"theBigPicture": "The Big Picture on IRAs",
		"learnTheBasics": "Learn the IRA Basics from Bank of America",
		"quiz" : "Retirement Savings Quiz",
		"commonQuestions" : "IRA FAQs from Bank of America",
		"findTheRightIRA" : "Choose the Right IRA",
		"calculatorWithChecklist" : "IRA Retirement Calculator: Learn How to Pursue Your Retirement Goal with Bank of America",
		"iraComparison" : "IRA Comparison",
		"iraChooseTheRightOne" : "Which one is for you",
		"rolloverIRA" : "Rollover IRA: 401k Rollover to an IRA",
		"transferIRA" : "Transfer IRA",
		"consolidate" : "Consolidate",
		"rothIRA" : "Roth IRA: Open a Roth IRA Account",
		"traditionalIRA" : "Traditional IRA: Open a Traditional IRA Account",
		"featuredIRA" : "Featured IRA from Bank of America, N.A.",
		"featuredExample" : "Featured Example IRA from BofA, NA",
		"featuredHighYield" : "Featured IRA: High Yield CD IRA from Bank of America, N.A.",
		"featuredStartTodayIRA" : "Featured IRA: Start Today IRA from Bank of America, N.A.",
		"featuredCreditCard" : "Featured Credit Card: Credit Card from Bank of America, N.A.",
		"howToOpen" : "How to Open Your IRA",
		"eighteenMonthCD" : "18-Month CD IRA Investment Option from Bank of America",
		"waysToInvest" : "Learn Ways to Invest an IRA from Bank of America",
		"highYieldCD" : "High Yield CD IRA Investment Option from Bank of America, N.A.",
		"mutualFunds" : "Mutual Funds",
		"stocksBonds" : "Stocks, Bonds, and ETFs",
		"moneyMarketSavings" : "Money Market Savings IRA Investment Option from Bank of America",
		"Roth2010" : "Roth IRA 2010"

	};
	
	SWFAddress.setTitle( flash2PageTitle[id] );
}

function sectionChange(id, fromBrowserBackButton, isNWRedirect) {
	if ( path.length > 1 && LOADED && !fromBrowserBackButton ) { hideFlashDiv(); }
	
	setTimeout( 'delayedSectionChange(\"' + id + '\",'+fromBrowserBackButton+', '+isNWRedirect+')', 10 );

	return true;
}

function delayedSectionChange(id, fromBrowserBackButton, isNWRedirect) {
	//Set logos ... function defined in home_utils.js ... setLogos(showBAC, showBAI)
	//change visible @ very top of page ... logo_bofa.jpg and logo_bai.jpg
	if ( id == "mutualFunds" || id == "stocksBonds" || id == "featuredStartTodayIRA" ) {
		setLogos(false, true);
	} else {
		if ( id == "eighteenMonthCD" || id == "18MonthCD" || id == "highYieldCD" || id == "moneyMarketSavings" || id == "featuredHighYield" || id=="fdicrollover" ) {
			setLogos(true,false);
		} else {
			setLogos(true,true);
		}
	}
	
	//set chat visibility. like setLogos showChat is defined in home_utils.js
	if (id == "waysToInvest" || id == "mutualFunds" || id == "stocksBonds" || id == "highYieldCD" || id == "moneyMarketSavings" || id == "eighteenMonthCD" || id == "featuredHighYield" || id == "featuredStartTodayIRA") {
		showChat(false);
	} else {
		showChat(true);
	}
		
	changeFlashPageTitle(id);
	changeFlashDisclaimer(id);
	
	//This must be a deep link
	if ( path.length > 1 ) {
		if ( !LOADED ) {
			LOADED = true;
			
			if ( isNWRedirect ) {//redirect to landing page
				window.location.href = prefix + domain + port + "#"+flash2cmIdMap[id];
			}
		} else {
			window.location.href = prefix + domain + port + "#"+flash2cmIdMap[id];
		}
	} else {
		if ( !fromBrowserBackButton && LOADED ) {
			SWFAddress.setValue(flash2cmIdMap[id]);
		} else {
			LOADED = true;
		}
	}
	
	_cmCreatePageviewTag(flash2cmIdMap[id]);
	
	scrollTo(0,0);
}

function throwPageView(id) {
	_cmCreatePageviewTag(flash2cmIdMap[id]);
}

/* CORE METRICS TAGGING FACADE */

function CoreMetricsAdapter() {
	addCoreMetricsAdapterMethod(this, "createCoreMetricsTag", function(method) {
		_this[method]();
	});
	
	addCoreMetricsAdapterMethod(this, "createCoreMetricsTag", function(id, method) {
  		_this[method](id);
	});
	
	addCoreMetricsAdapterMethod(this, "createCoreMetricsTag", function(id, actionType, method) {
		_this[method](id, actionType);
	});
	
	addCoreMetricsAdapterMethod(this, "createCoreMetricsTag", function(id, path, method) {
  		_this[method](id, path);
	});
}
 
var coreMetricsAdapter = new CoreMetricsAdapter();

function addCoreMetricsAdapterMethod(object, name, fn) {
	var old = object[name];
	
    object[name] = function() {
	    if (fn.length == arguments.length) {
			return fn.apply(this, arguments);
		} else if (typeof old == 'function') {
			return old.apply(this, arguments);
		}
    };
}

function createCoreMetricsTag() {
	switch ( arguments.length ) {
		case 1:
			coreMetricsAdapter.createCoreMetricsTag(arguments[0]);
			break;
		case 2:
			coreMetricsAdapter.createCoreMetricsTag(arguments[0], arguments[1]);
			break;
		case 3:
			coreMetricsAdapter.createCoreMetricsTag(arguments[0], arguments[1], arguments[2]);
			break;
	}
}

/* END CORE METRICS TAGGING FACADE */

function swapDisclaimer(el1, el2) {
    $(el1).style.display = 'none';
    $(el2).style.display = 'block';
}

var current_disclaimer = 'defaultDisclaimer';

function showDisclaimer(el) {
    // hide current disclaimer
    $(current_disclaimer).style.display = 'none';
    // show new one
    $(el).style.display = 'block';
    // remember current one
    current_disclaimer = el;
}
