var $annualRates =  '#onlineAnnual,' +
					'#onlineAnnualPrevious,' +
					'#easyAnnual,' +
					'#easyAnnualPrevious,' +
					'#noticeAnnual,' +
					'#noticeAnnualPrevious,' +
					'#taxfreeAnnual,' +
					'#taxfreeAnnualPrevious,' +
					'#childrensAnnual,' +
					'#childrensAnnualPrevious';
					
var $monthlyRates = '#onlineMonth,' +
					'#onlineMonthPrevious,' +
					'#easyMonth,' +
					'#easyMonthPrevious,' +
					'#noticeMonth,' +
					'#noticeMonthPrevious,' +
					'#taxfreeMonth,' +
					'#taxfreeMonthPrevious,' +
					'#childrensMonth,' +
					'#childrensMonthPrevious';					
					
var accountViews =  '#view1,' + 
					'#view2,' + 
					'#view3,' + 
					'#view4,' + 
					'#view5';
					
var accountViewsPrevious =  '#view1Previous,' +
							'#view2Previous,' + 
							'#view3Previous,' + 
							'#view4Previous,' + 
							'#view5Previous';		
							
var getcurrentPreviousDropdownValue = '#currentPreviousSelector';
var getcurrentPreviousDropdownValueSelected = '#currentPreviousSelector :selected';
var getAccountTypesDropdownValue = '#accountTypeSelector';					
var getAccountTypesDropdownValueSelected = '#accountTypeSelector :selected';
var getRatesDropdownValue = '#annualMonthlyRatesSelector';
var getRatesDropdownValueSelected = '#annualMonthlyRatesSelector :selected';			
var accountClosed = 'interest-rates-closed.html';

// double tracking hit
var jqtransformReload = 1;


$(document).ready(function () {	
							
	$('#currentPreviousList')[0].reset();
	$('#accountTypesList')[0].reset();
	
	
	// current / previous rates show / hide
	$('#currentRatesView').show();
	$('#previousRatesView').hide();
	
							
	// page load show / hide						
	$($annualRates).show();
	$($monthlyRates).hide();
							
							
	//find all form with class jqtransform and apply the plugin						
	$("form.jqtransform").jqTransform();	
	
});

$(function(){			
	
	$('#currentPreviousList ul li a').click(function(){	
		
		if (($(getcurrentPreviousDropdownValue).val() == "current") && ($(getAccountTypesDropdownValueSelected).val() != "viewAll")) {	
			window.location.hash = "#current_" + $(getAccountTypesDropdownValueSelected).val();
		}	
		
		else if (($(getcurrentPreviousDropdownValue).val() == "previous") && ($(getAccountTypesDropdownValueSelected).val() != "viewAll")) {
			window.location.hash = "#previous_" + $(getAccountTypesDropdownValueSelected).val();
		}		
		
		else if (($(getcurrentPreviousDropdownValue).val() == "current") && ($(getAccountTypesDropdownValueSelected).val() == "viewAll")) {
			window.location.hash = "#current_showAll";
		}	
		
		else if (($(getcurrentPreviousDropdownValue).val() == "previous") && ($(getAccountTypesDropdownValueSelected).val() == "viewAll")) {	
			window.location.hash = "#previous_showAll";
		}
		
		else if ($(getcurrentPreviousDropdownValue).val() == "closed") {			
			document.location = accountClosed;
		}
		
		ie6Fix();
		
	}); 
	
	$('#accountTypesList ul li a.selected').click(function(){	
		//do nothing
	});
		   
	$('#accountTypesList ul li a').click(function(){
		
		if (($(getcurrentPreviousDropdownValueSelected).val() == "current") && ($(getAccountTypesDropdownValue).val() != "viewAll")) {
			window.location.hash = "#current_" + $(getAccountTypesDropdownValueSelected).val();
		}
		
		else if (($(getcurrentPreviousDropdownValueSelected).val() == "previous") && ($(getAccountTypesDropdownValue).val() != "viewAll")) {
			window.location.hash = "#previous_" + $(getAccountTypesDropdownValueSelected).val();
		}
		
		else if (($(getcurrentPreviousDropdownValueSelected).val() == "current") && ($(getAccountTypesDropdownValue).val() == "viewAll")) {				
			window.location.hash = "#current_showAll";
		}
		
		else if (($(getcurrentPreviousDropdownValueSelected).val() == "previous") && ($(getAccountTypesDropdownValue).val() == "viewAll")) {			
			window.location.hash = "#previous_showAll";
		}
		
		ie6Fix();
		
	});
	
	
	function ie6Fix() {
		
		// IE6 FIX
		if ($(getRatesDropdownValueSelected).val() == "annual") {
			$($monthlyRates).css('visibility','hidden');
			$($annualRates).css('visibility','visible');
		}
		else if ($(getRatesDropdownValueSelected).val() == "monthly") {
			$($annualRates).css('visibility','hidden');
			$($monthlyRates).css('visibility','visible');
		}	
		
	}
	
	
});



var annualMonthlySelected;

function annualRatesChanger(ratesType) {
	
	annualMonthlySelected = ratesType;
	
	if(ratesType == "monthly") {
		
		$($annualRates).hide();	
		$($monthlyRates).show();
		
		$('.greyButton').css('background','url("/img/buttons/button-red-left.png") no-repeat scroll 0 0 transparent');
		$('.greyButton a').css('background','url("/img/buttons/button-red-middle.png") repeat-x scroll left top transparent');
		$('span.greyButton').css('background','url("/img/buttons/button-red-right.png") no-repeat scroll 0 0 transparent');
		$('.greyButton a').css('color','#ffffff');		
		
		$('.redButton').css('background','url("/img/buttons/button-grey-left.png") no-repeat scroll 0 0 transparent');
		$('.redButton a').css('background','url("/img/buttons/button-grey-middle.png") repeat-x scroll left top transparent');
		$('span.redButton').css('background','url("/img/buttons/button-grey-right.png") no-repeat scroll 0 0 transparent');
		$('.redButton a').css('color','#292929');		
		
		// IE6 FIX
		$($monthlyRates).css('visibility','visible');
	}
	else if(ratesType == "annual") {
		
		$($monthlyRates).hide();	
		$($annualRates).show();	
		
		$('.redButton').css('background','url("/img/buttons/button-red-left.png") no-repeat scroll 0 0 transparent');
		$('.redButton a').css('background','url("/img/buttons/button-red-middle.png") repeat-x scroll left top transparent');
		$('span.redButton').css('background','url("/img/buttons/button-red-right.png") no-repeat scroll 0 0 transparent');
		$('.redButton a').css('color','#ffffff');		
		
		$('.greyButton').css('background','url("/img/buttons/button-grey-left.png") no-repeat scroll 0 0 transparent');
		$('.greyButton a').css('background','url("/img/buttons/button-grey-middle.png") repeat-x scroll left top transparent');
		$('span.greyButton').css('background','url("/img/buttons/button-grey-right.png") no-repeat scroll 0 0 transparent');
		$('.greyButton a').css('color','#292929');
		
		// IE6 FIX
		$($annualRates).css('visibility','visible');
	}
	
}



// tabs history functionality
$(function(){
		   
	// Get the change in # value and apply the tabs-selected class to the corresponding tab
	$(window).bind( 'hashchange', function(){											   
										   
		var interestRateHashValue = location.hash;	
		
		if (interestRateHashValue == "" || interestRateHashValue == "#") {	
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
		
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 0);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('All Account Types');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(0) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			$(accountViewsPrevious).hide();
			$(accountViews).show();
			$('#previousRatesView').hide();
			$('#currentRatesView').show();
		}
		
		// CURRENT PANELS UPDATE	 
		else if (interestRateHashValue == "#current_view1") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 1);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Online accounts');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(1) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			currentPanelUpdate();			
			$("#view1").show();
		}
		else if (interestRateHashValue == "#current_view2") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 2);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Easy access');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(2) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			currentPanelUpdate();			
			$("#view2").show();
		}
		else if (interestRateHashValue == "#current_view3") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 3);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Notice accounts and Bonds');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(3) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			currentPanelUpdate();			
			$("#view3").show();
		}
		else if (interestRateHashValue == "#current_view4") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 4);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Tax free');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(4) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			currentPanelUpdate();			
			$("#view4").show();
		}
		else if (interestRateHashValue == "#current_view5") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 5);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Children\'s accounts');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(5) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			currentPanelUpdate();			
			$("#view5").show();
		}
		
		// CURRENT PANEL SHOW ALL
		else if (interestRateHashValue == "#current_showAll") {
			// CURRENT/PREVIOUS SELECT UPDATE
			currentAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 0);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('All Account Types');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(0) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			$(accountViewsPrevious).hide();
			$(accountViews).show();
			$('#previousRatesView').hide();
			$('#currentRatesView').show();
		}	
		
		// PREVIOUS RATES UPDATE		
		else if (interestRateHashValue == "#previous_view1") {
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 1);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Online accounts');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(1) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			previousPanelUpdate();			
			$("#view1Previous").show();
		}		  
		else if (interestRateHashValue == "#previous_view2") {	
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
		
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 2);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Easy access');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(2) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			previousPanelUpdate();			
			$("#view2Previous").show();
		}
		else if (interestRateHashValue == "#previous_view3") {
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 3);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Notice accounts and Bonds');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(3) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			previousPanelUpdate();			
			$("#view3Previous").show();
		}
		else if (interestRateHashValue == "#previous_view4") {
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 4);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Tax free');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(4) a').addClass('selected');
		
			// RELEVANT CONTENT SHOW/HIDE
			previousPanelUpdate();			
			$("#view4Previous").show();
		}
		else if (interestRateHashValue == "#previous_view5") {
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 5);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('Children\'s accounts');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(5) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			previousPanelUpdate();			
			$("#view5Previous").show();
		}
		
		// PREVIOUS PANEL SHOW ALL	
		else if (interestRateHashValue == "#previous_showAll") {
			// CURRENT/PREVIOUS SELECT UPDATE
			previousAccountsDropdownSelector();
			
			// ACCOUNT TYPE SELECT UPDATE
			// if user navigates direct to a section set selected dropdown item
			$("#accountTypeSelector").attr('selectedIndex', 0);
			
			// UPDATE SELECTED DROPDOWN ITEM STYLE TO SHOW SELECTED NAV ITEM
			$('#accountTypesList .jqTransformSelectWrapper div span').html('All Account Types');
			$('#accountTypesList .jqTransformSelectWrapper ul li a').removeClass('selected');			
			$('#accountTypesList .jqTransformSelectWrapper ul li:eq(0) a').addClass('selected');
			
			// RELEVANT CONTENT SHOW/HIDE
			$(accountViews).hide();
			$(accountViewsPrevious).show();
			$('#currentRatesView').hide();
			$('#previousRatesView').show();
		}	
		
		annualRatesChanger(annualMonthlySelected);
		
		
		if ($('#currentPreviousList .jqTransformSelectWrapper div span').html() == "Current Rates" && $('#currentPreviousSelector :selected').val() == "previous") {		
			$("#currentPreviousSelector").attr('selectedIndex', 0);
		}		
		else if ($('#currentPreviousList .jqTransformSelectWrapper div span').html() == "Previous Rates" && $('#currentPreviousSelector :selected').val() == "current") {
			//$('#currentPreviousSelector :selected').val('current');
			$("#currentPreviousSelector").attr('selectedIndex', 1);
		}
		
		
		// because the restyle of the dropdown elements causes a reload of the page, this counter check prevents a double hit on the stats
		if (jqtransformReload == 1) {		  
			jqtransformReload++;		  
		}
		else {
			// send hit to omniture
			interestRatePageTracking(interestRateHashValue);
		}
		
	})
	
	// when another tab is clicked, or the page is loaded, fire the update
	$(window).trigger( 'hashchange' );
	//alert(location.hash);  
	
	
	// CURRENT RATES DROPDOWN UPDATE
	function currentAccountsDropdownSelector() {		
		//$('#currentPreviousSelector :selected').val('current');
		$('#currentPreviousList .jqTransformSelectWrapper div span').html('Current Rates');
		$('#currentPreviousList .jqTransformSelectWrapper ul li a').removeClass('selected');			
		$('#currentPreviousList .jqTransformSelectWrapper ul li:eq(0) a').addClass('selected');			
	}
	// PREVIOUS RATES DROPDOWN UPDATE	
	function previousAccountsDropdownSelector() {
		//$('#currentPreviousSelector :selected').val('previous');
		$('#currentPreviousList .jqTransformSelectWrapper div span').html('Previous Rates');
		$('#currentPreviousList .jqTransformSelectWrapper ul li a').removeClass('selected');			
		$('#currentPreviousList .jqTransformSelectWrapper ul li:eq(1) a').addClass('selected');			
	}
		
	// CURRENT ACCOUNT TYPES DROPDOWN UPDATE
	function currentPanelUpdate() {
		$(accountViews).hide();	
		$(accountViewsPrevious).hide();	
		$('#previousRatesView').hide();
		$('#currentRatesView').show();
	}
	// PREVIOUS ACCOUNT TYPES DROPDOWN UPDATE
	function previousPanelUpdate() {
		$(accountViews).hide();	
		$(accountViewsPrevious).hide();	
		$('#currentRatesView').hide();	
		$('#previousRatesView').show();
	}
	
	
	
	
	
	
	
});




//tracking update where the hash value is bound to a prop(props are custom omniture variables) and sent to omniture
function interestRatePageTracking(interestRateHashReader) {
  // BEFORE SENDING THE HASH VALUE TO OMNITURE, RUN CHECK TO SEE WHAT THE HASH VALUE IS
  // THIS IS A CLEANUP TO ENSURE THAT IF URL HAS THE SOLE VALUE OF "#" THEN CHANGE THE VALUE SENT TO OMNITURE TO BLANK SO AS NOT TO CREATE A NEW ENTRY FOR "#"
  if (interestRateHashReader == "#" || interestRateHashReader == "#current_showAll") {
	  interestRateHashReader = "";
	  var s=s_gi('ybschelsea');
	  s.linkTrackVars='prop1';
	  s.linkTrackEvents='event1';
	  s.prop1 = s.pageName + interestRateHashReader;
	  s.tl(this,'o','Hashed Link');	
  }
  else {
	  var s=s_gi('ybschelsea');
	  s.linkTrackVars='prop1';
	  s.linkTrackEvents='event1';
	  s.prop1 = s.pageName + interestRateHashReader;
	  s.tl(this,'o','Hashed Link');	  
  }
};
