$(document).ready(function() {					   



/**
 * Careers video popup: any link with class="video" 
 */
	$('a.video').click(function(){
		var href = $(this).attr('href');
		window.open(href, 'secure','width=440,height=350,scrollbars=no,location=no,dependent=yes,resizable=no,top=100,left=100');return false; 
	});			

/**
 * Security lock popup in Login box 
 */
	$('#login a#security').click(function(){
		var href = $(this).attr('href');
		window.open(href, 'secure','width=565,height=450,scrollbars=yes,location=no,dependent=yes,resizable=no,top=25,left=25');return false; 
	});			
	$('#Userid').focus();

/**
  * Put the end quote marks at the end in the Careers banner 
  */
	$('.black-holder p span').after('<img src="/careers/global/images/cite-end.gif" class="cite-end" />');

/*******************************************************************************
=== TABBED INTERFACE ===========================================================
	* The first tab withing <ul id="tabs"> will be selected by default. 
*******************************************************************************/
	$('#tabs').tabs();

	$('#search-tabs > ul').tabs();

/***************************
	Retirement Tabs
***************************/
$('#tabs-ret').tabs();

/**
 * Displays navigation given tab name
 * @param tabType - the tab name for the associated nav
 * @param animate - to control whether the nav animates or not */

	function setTertiaryNavigation(tabType, animate) {
		
		if(tabType == "" || tabType == null) {
			tabType = "in";
		}
		
		$('#left-nav li ul li ul').css('display','none');
		$('#tabs-ret').tabs('select', '#'+tabType); 
		
		// Make active tertiary nav parents normal:
		$('.tertiary-in a').css('font-weight','normal');
		$('.tertiary-near a').css('font-weight','normal');
		$('.tertiary-far a').css('font-weight','normal');
		
		// Removed tertiary-selected class
		$('.tertiary').removeClass('tertiary-selected');
		
		// Make active tertiary nav parent bold:
		$('.tertiary-'+tabType+' a').css('font-weight','bold');
		
		// Add tertiary-selected class to item user selects 
		$('.tertiary-'+tabType).addClass('tertiary-selected');
		
		if(animate && $.cookie('retTab') != tabType) {
			$('.tertiary-'+tabType+' ul').slideDown('fast');
		} else {
			$('.tertiary-'+tabType+' ul').css('display', 'block');
		}
		
		$.cookie('retTab', tabType, { expires: 365, path: '/'});
	}

// Set the tab cookie
	setTertiaryNavigation($.cookie('retTab'), false);

// When tab is clicked, show it's tertiary left nav:
	$('a[href$="in"]').click(function(){
		setTertiaryNavigation("in", true);
	});
	
	$('a[href$="near"]').click(function(){
		setTertiaryNavigation("near", true);
	});
	
	$('a[href$="far"]').click(function(){
		setTertiaryNavigation("far", true);
	});

// When secondary left nav is clicked, show its tab and tertiary left nav:
	$('#left-nav .tertiary-in').click(function(){
		$.cookie('retTab', 'in', { expires: 365, path: '/'});
	});
	$('#left-nav .tertiary-near').click(function(){
		$.cookie('retTab', 'near', { expires: 365, path: '/'});
	});
	$('#left-nav .tertiary-far').click(function(){
		$.cookie('retTab', 'far', { expires: 365, path: '/'});
	});	

/************************************
	Risk assessment quiz
************************************/
$('#quiz-trigger').click(function(){
	
	if ($('#quiz-trigger').hasClass('retake-quiz-trigger')) {
		$('.instructions').show();
		$('#risk-default-content').show();
		$('#quiz-trigger').removeClass('retake-quiz-trigger');
		$('#quiz-trigger').addClass('close-quiz-trigger');
		trackOmnitureCalculator("risk_assessment_retake");
		
	} else {
		
		if(!$('#quiz-trigger').hasClass('close-quiz-trigger')) {
			trackOmnitureCalculator("risk_assessment");
		}
		$('#quiz-trigger').toggleClass('close-quiz-trigger'); 
	}
	$('#risk-form-results').hide();
	$('#risk-form-container').slideToggle(500);
	return false;
});

$("#risk-ass").submit(function() {
	if (
			$('input[name=retQ1]:checked').val() == null || 
			$('input[name=retQ2]:checked').val() == null || 
			$('input[name=retQ3]:checked').val() == null || 
			$('input[name=retQ4]:checked').val() == null || 
			$('input[name=retQ5]:checked').val() == null
		)
	{
		alert('Please fill out all the fields.')
		return false;
	}
	function getTotal(){
		return Number($('input[name=retQ1]:checked').val()) + Number($('input[name=retQ2]:checked').val()) + Number($('input[name=retQ3]:checked').val()) + Number($('input[name=retQ4]:checked').val()) + Number($('input[name=retQ5]:checked').val());
		}
		$('#risk-form-results div').removeClass('selected');
		if (getTotal() < 8){
			$('#conservative').addClass('selected')
		} else if (getTotal() > 7 && getTotal() < 13){
			$('#moderately-conservative').addClass('selected')
		} else if (getTotal() > 12 && getTotal() < 18){
			$('#moderate').addClass('selected')
		} else if (getTotal() > 17 && getTotal() < 23){
			$('#moderately-aggressive').addClass('selected')
		} else if (getTotal() > 22){
			$('#aggressive').addClass('selected')
		} 
		$('#risk-default-content').hide();
		$('#risk-form-container').hide();
		$('#risk-form-results').fadeIn('slow');
		$.scrollTo('#risk-quiz');
		$('#quiz-trigger').removeClass('close-quiz-trigger'); 

		$('#quiz-trigger').addClass('retake-quiz-trigger'); 
		
		trackOmnitureCalculator("risk_assessment_submit");
		return false;
});

/************************************
	Asset Allocation Calculator
************************************/
// Because we're delaying when this thing is hidden, we shrink the iframe at first.
$('#asset-allocation iframe').css('height','0px');
// S&P is slow, so we need to use this settimeout function so the calc can load before we hide it.
setTimeout( 
	function(){ 
		// After the delay, we hide the div, and make the iframe the necessary size.
		$('#asset-allocation').hide();
		$('#asset-allocation iframe').css('height','1250px');
		$('#asset-allocation-trigger').fadeIn('fast');
	}, 5000); 
// Now the page is ready for Jquery to toggle it.
$('#asset-allocation-trigger').click(function(){
	if(!$('#asset-allocation-trigger').hasClass('close-asset-allocation-trigger')) {
		trackOmnitureCalculator("asset_allocation");
	}
	$('#asset-allocation').slideToggle(500);
	$('#asset-allocation-trigger').toggleClass('close-asset-allocation-trigger');
	return false;
});

/************************************
	Projected Expenses Calculator
************************************/
$('#projected-expenses iframe').css('height','0px');
setTimeout( 
	function(){ 
		$('#projected-expenses').hide();
		$('#projected-expenses iframe').css('height','1250px');
		$('#projected-expenses-trigger').fadeIn('fast');
	}, 5000); 
	$('#projected-expenses-trigger').click(function(){
	if(!$('#projected-expenses-trigger').hasClass('close-projected-expenses-trigger')) {
		trackOmnitureCalculator("future_expenses");
	}
	$('#projected-expenses').slideToggle(500);
	$('#projected-expenses-trigger').toggleClass('close-projected-expenses-trigger');
	return false;
});

/*******************************************************************************
=== ACCORDION ==================================================================
	* Shows the next tag 
*******************************************************************************/
$(".accordion a").click(function() {
	$(this).next().toggle();
}).next().hide();


$("ul.exp li a.trigger").click(function() {
	$(this).toggleClass("open");
	$(this).next().toggle();
	return false;
})

/*******************************************************************************
=== LIGHT BOX ============================================================
	* The lightbox popup, uses the jquery.lightbox-0.5.pack.js library
*******************************************************************************/



	$(function() {
			
						 
	   $('a.lightbox').lightBox({
		overlayBgColor: '#666',
		overlayOpacity: 0.6,
		imageBlank: '/global/images/buttons/lightbox-blank.gif',
		imageLoading: '/global/images/buttons/lightbox-blank.gif',
		imageBtnClose: '/global/images/buttons/close-window.gif',
		imageBtnPrev: '/global/images/buttons/lightbox-btn-prev.gif',
		imageBtnNext: '/global/images/buttons/lightbox-btn-next.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'of'
	   });
	});
	
//track the lightbox click
$('a.lightbox').click(function() {
		trackOmnitureClickThrough(this.href);									
		return false;
})

/*******************************************************************************
=== INCREASE/DECREASE TEXT======================================================
	* INCREASE/DECREASE TEXT 
*******************************************************************************/
// For graceful degredation, this script reveals these tools that the CSS hides by default.
	//$('#tools').css('display', 'block');
	
// Read the cookie. Depending on what it is, apply the inline style to the body tag, and the selected class to the link that indicates the size.
	var size = $.cookie('fontSize');
	if (size == 'small') {
	$('body').css('font-size','');
		$('#text-size a').removeClass('selected');
		$('#text-size #small').addClass('selected');
	};
	if (size == 'medium') {
	$('body').css('font-size','140%');
		$('#text-size a').removeClass('selected');
		$('#text-size #medium').addClass('selected');
	};
	if (size == 'large') {
	$('body').css('font-size','160%');
		$('#text-size a').removeClass('selected');
		$('#text-size #large').addClass('selected');
	};
	
// To increase and decrease the text-size, adding inline styles to the body tag.
	$('#text-size a').click(function(event) {
									 
// This nullifies the font-size value by default.
	$('body').css('font-size', '');
	$.cookie('fontSize', 'small', { expires: 365, path: '/', domain: 'ameriprise.com'});
	
// This is the "smaller" font-size link.
    if (this.id == 'medium') {
      $('body').css('font-size', '140%');
	  $.cookie('fontSize', 'medium', { expires: 365, path: '/', domain: 'ameriprise.com'});
    }
	
// This is the "larger" font-size link.
    else if (this.id == 'large') {
      $('body').css('font-size', '160%');
	  $.cookie('fontSize', 'large', { expires: 365, path: '/', domain: 'ameriprise.com'});
    }
	
// Add the selected class to the link, if the user has selected it.
    $('#text-size a').removeClass('selected');
    $(this).addClass('selected');
	event.stopPropagation();
	});  

/*******************************************************************************
=== PRINT THIS PAGE ============================================================
	* Initiates a regular browser print job when user clicks.
*******************************************************************************/
  $('#print-page a').click(function() {
    window.print(); return false;
  });

/*******************************************************************************
=== SHARE THIS PAGE AND EMAIL THIS PAGE ============================================================
	* Opens the Share This dialouge
*******************************************************************************/
  $('#share-this-link').click(function(){
		$('#tools div').removeClass("open");
		$('#share-this-box').toggleClass("open");
		
		
			var s=s_gi('amppublic,ampglobal'); 
			s.linkTrackVars='eVar21,events,prop17'; 
			s.linkTrackEvents='event17'; 
			s.eVar21='st_sharethis'; 
			s.events='event17'; 
			s.prop17 = s_om.pageName;
			s.tl(this,'o','st_sharethis');
			
			
		return false;
  });		

  $('#email-this-link').click(function(){
		$('#tools div').removeClass("open");
		$('#email-this-box').toggleClass("open");
		
		
		 var s=s_gi('amppublic,ampglobal'); 
		 s.linkTrackVars='eVar21,events,prop17'; 
		 s.linkTrackEvents='event17'; 
		 s.eVar21='amp_email_this_page_click'; 
		 s.events='event17'; 
  	 s.prop17 = s_om.pageName;
		 s.tl(this,'o','amp_email_this_page_click');
		
		return false;
  });		

  // Close the tools boxex if link with this class is clicked
  $('.tools-close').click(function(){
		$('#tools div').removeClass("open");
		return false;
  });	
  
  // Trigger bgiframe so these boxes show over forms and flash
	
	
	
	if($('#share-this-box').bgiframe != null) {
		$('#share-this-box').bgiframe();
	}
	
	if($('#email-this-box').bgiframe != null) {
		$('#email-this-box').bgiframe();
	}
	
  
  

/*******************************************************************************
=== PDF LINKS ==================================================================
	* Makes any link to a .PDF open in a smaller cromeless window, 
	  and displays the PDF icon image after the link.
*******************************************************************************/
	//$('a[href$=".pdf"]').click(function(){ 
	//	var href = $(this).attr('href'); 
	//	window.open(href, 'popup', 'location=no,menubar=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,left=10,top=10,width=750,height=550'); return false; 
	//}); 
	//AS OF RIGHT NOW PDFS ARENT GETTING TRACKED IN OMNITURE BECAUSE OF THE WINDOW POPUP
	//SO WE ARE JUST ATTACHING THE TARGET=BLANK ATTRIBUTE INSTEAD OF FANCY PANTS
	//CHROMELESS POPUP
	//@author Bao
	
	$('a[href$=".pdf"]').attr("target","blank");
	$('a[href$=".pdf"]').after(' <img src="/global/images/pdf-ico.gif" class="pdf-icon" title="This is a PDF document" />');

/*******************************************************************************
=== HOMEPAGE BILLBOARD =========================================================
	* Makes the homepage billboard animate.
*******************************************************************************/
	var divToShow = $('dl dd a.current').attr('rel');
	
	if(divToShow != null) {
		$(divToShow).show();
	}
	
	$('#billboard dl dd a').mouseover(function(){
		var link = $(this);
		$('dl dd a.current').removeClass('current');
		link.addClass('current');
		$('#billboard .board').hide();
		rel = link.attr('rel');
		$(rel).show();
		return false;
	});


});// Jquery onload handler ends 



/**
 * Popup a window that plays a 352 x 240 flash video
 */
function popupVid352by240(vid) {
	var popup = window.open('/global/media/video-players/videopopup-352-240.asp?video='+vid,'video','location=no,menubar=no,toolbar=no,status=no,resizable=no,scrollbars=no,width=352,height=260');
}


/*******************************************************************************
=== TOP NAVIGATION =============================================================
	* Suckfish method to show the parent tab's sub navigation.
*******************************************************************************/
/*
 * sfDelay - Suckerfish CSS menu with JS delay.
 *
 * Copyright (c) 2008 Peter Ryan (peter-ryan.co.uk)
 * This function is free for anyone to use, modify and redistribute.
 *
 * $Id: functions.js,v 1.1.1.1.4.3 2009/08/18 18:54:15 bqle Exp $
 */

/* http://simonwillison.net/2004/May/26/addLoadEvent/ */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
};


sfHover = function() {
	var timeout = 400;
	var cssClass = "sfhover";

	var queue = [];
	var reCSS = new RegExp("\\b" + cssClass + "\\b");
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
  
		// mouseover and mouseout handlers for regular mouse based interface.
		sfEls[i].onmouseover = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onmouseout = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// focus and blur handlers for keyboard based navigation.
		sfEls[i].onfocus = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onblur = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// click event handler needed for tablet type interfaces (e.g. Apple iPhone).
		sfEls[i].onclick = function(e) {
			if (this.className.search(reCSS) == -1) {
				// CSS not set, so clear all sibling (and decendants) menus, and then set CSS on this menu...
				var elems = this.parentNode.getElementsByTagName("li");
				for (var i=0; i<elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className += " " + cssClass;
			} else {
				// CSS already set, so clear all decendant menus and then this menu...
				var elems = this.getElementsByTagName("li");
				for (var i=0; i<elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className = this.className.replace(reCSS, "");
			}
			if (e && e.stopPropagation)
				e.stopPropagation();
			else
				window.event.cancelBubble = true;
		}
	}

	queueFlush = function () {
		while (queue.length) {
			clearTimeout(queue[0][0]);
			queueTimeout();
		}
	}

	queueTimeout = function() {
		if (queue.length) {
			var el = queue.shift()[1];
			el.className = el.className.replace(reCSS, "");
		}
	}
}
addLoadEvent(sfHover);


/////////////////////////////////////////////////////////////////////////

//    Script for cookie setting/getting                                //

/////////////////////////////////////////////////////////////////////////
var intdays = 366;
var now = new Date();
now.setTime(now.getTime() + intdays * 24 * 60 * 60 * 1000);
var cookie_path = "/"; //"/" as root
var cookie_domain = document.domain; //"www.ameriprise.com"

function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((path) ? "; path=" + path : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/**
 * Check to see if the value is blank, 
 * If not, redirect to the url, otherwise, display the error message
 * This is the fix for the onlicks being called for the dropdown forms for jobs/schools etc..
 * @author Bao
 * @date 2009.04.01
 * 
 * @param url - the url this will redirect to, if value isnt blank
 * @param value - the value to test to see if its blank or not
 * @param errorMessage - the error message to display if value is blank
 */
function validateDropdown(url, value, errorMessage) {
	if(value == "") {
		alert(errorMessage);
	} else {
		location = url;
	}
}