// OOBA Splash page - only shown once to each user
$(function(){
// If user clicks on Login link, set seenOOBA2 cookie to 'yes' and show popup layer
// If it's already set to 'yes' - do nothing (proceed with the link)
	$('a[href="https://online.thechelsea.co.uk/public/authentication/pre_login.do"]').click(function() {
		if ($.cookie('seenOOBA2_CBS') != 'yes') 
		{
			$.cookie('seenOOBA2_CBS', 'yes', {expires: 365} );
			Cufon.replace('.oobaPopup h2');
			// Horisontal centering - move (540px / 2) pixels to the left from the center of the page
			$('.oobaPopup').css('margin-left', '-270px');
			// Vertical centering - add top margin but if value becomes negative set to 10px instead
			var oobaMarginTop = Math.max((($(window).height() - $('.oobaPopup').height())/2 - 50), 10);
			$('.oobaPopup').css('margin-top', oobaMarginTop);
			$('.oobaPopup').show('slow');
			$('.oobaPopupContainer').show();
			return false;
		}
	}); 
	
	$('a.continueToLogin').click(function() {
		$('.oobaPopup').hide(); $('.oobaPopupContainer').hide();
    });

	$('a.oobaClose').click(function() {
		 $('.oobaPopup').hide('slow'); $('.oobaPopupContainer').hide();
	});
});

// PAGES WITH TABS THAT REQUIRE IN PAGE LINKS, OR MORE SPECIFICALLY LINKS TO A NAMED ANCHOR EG. #LEGAL UTITLISE THIS FUNCTION SO AS NOT TO DISRUPT URL AND HISTORY
jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});
$(function(){
	
	$('#inPageScroll').live('click', function() {
		$('#scrolled').scrollTo();
	});
	
	$('a[href=#how_to_apply]').click(function() {	
		$('#centerColumnContent').scrollTo();
	});
});



// MANAGING SAVINGS PAGE, SHOW/HIDE FAQ ANSWERS
$(function(){
	$("a.faqQuestion").click(function(event){
	   event.preventDefault();
	   $(this).next("div.faqAnswer").slideToggle(500);
	 });
});	  



// HOME //
// CAN WE HELP... DROPDOWN REDIRECTS //
// RESET "CAN WE HELP YOU..." DROPDOWNS TO ORIGINAL VALUE
$(function(){
	$('#selectApply, #selectQuote, #selectRates, #selectFind').find('option:first').attr('selected', 'selected').parent('select');
});
// REDIRECT USER TO RELEVANT DROPDOWN LINK ON SELECTION OF CAN WE HELP DROPDOWN
$(function(){
	$("#rightColumn .helpContent SELECT").change(function(){
		location = $("OPTION:selected", this).val();
	});
}); 

// CONTENT //
// INTEREST RATES SHOW/HIDE CURRENT/PREVIOUS RATES
function showPreviousRates() {
	$('#previousRates').css('display','block');
	$('#currentRates').css('display','none');
};

function showCurrentRates() {
	$('#currentRates').css('display','block');
	$('#previousRates').css('display','none');
};


// ACCOUNT STATEMENT NOTES ROLLOVER UPDATES
$(function() {	
	var note1Text = "<p><strong><u>Note 1</u></strong><br />"
	+ "This is the 'cleared' balance of your account at the close of business on 5&nbsp;April&nbsp;2010.</p>";
	var note2Text = "<p><strong><u>Note 2</u></strong><br />"
	+ "This is the date of the most recent interest payment made for this account.</p>";
	var note3Text = "<p><strong><u>Note 3</u></strong><br />"
	+ "This is the total gross value of interest paid for this account (ie before tax).</p>";
	var note4Text = "<p><strong><u>Note 4</u></strong><br />"
	+ "This figure is the amount of tax we have deducted (if any) from the gross interest, and paid directly to HM Revenue and Customs.</p>";
	var note5Text = "<p><strong><u>Note 5</u></strong><br />"
	+ "This is the net value of interest actually paid to you, ie after tax has been deducted.</p>";
	var note6Text = "<p><strong><u>Note 6</u></strong><br />"
	+ "This shows the next expected interest payment date, and is determined by the terms of your account.</p>";
	
	$("#annualStatementNotes a")./*mouseout(function(){
	  $(".annualStatementDefinition").html("<p><strong>Rollover each of the notes below to show a more detailed explanation of each part of the Annual Statement:</strong></p>");
	}).*/mouseover(function(){
		if (($(this).attr("title")) == "note1"){
	  		$(".annualStatementDefinition").html(note1Text);
		}
		else if (($(this).attr("title")) == "note2"){
	  		$(".annualStatementDefinition").html(note2Text);
		}
		else if (($(this).attr("title")) == "note3"){
	  		$(".annualStatementDefinition").html(note3Text);
		}
		else if (($(this).attr("title")) == "note4"){
	  		$(".annualStatementDefinition").html(note4Text);
		}
		else if (($(this).attr("title")) == "note5"){
	  		$(".annualStatementDefinition").html(note5Text);
		}
		else if (($(this).attr("title")) == "note6"){
	  		$(".annualStatementDefinition").html(note6Text);
		}
	});
});

// LAUNCH SAVINGS PRODUCT COMPARISON APPLICATION
$(function(){	
	
	// if javascript is turned off, make sure the Savings Account Tool doesn't show
	$(".rhsSavingsComparisonBanner").css('display','block');

	// LAUNCH
	$('#launchApplication').click(function() {
		window.open('/savings/comparison/index.html?int_cmp=lhs_compare_nov11', '', 'location=no,width=1024,height=768,resizable=yes,scrollbars=yes,toolbar=no,status=no');
	});
	
});

// APPLY LINKS - OPEN NEW WINDOW
$(function() {
		   
	/*$('.loginLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/authentication/pre_login.do',null,openAppFeatures
		);
	});*/
		   
	$('.ebondAppLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/savings/application/load_ebond.do',null,openAppFeatures
		);
	});
	
	$('.esaverRewardAppLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/savings/application/load_deferred_product.do',null,openAppFeatures
		);
	});
	
	$('.eISAppLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/savings/application/load_eisa.do',null,openAppFeatures
		);
	});

	
	$('.savingsDemoLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 450; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=870,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'http://www.thechelsea.co.uk/savings/demo/index.html?int_cmp=onlineacc_demo',null,openAppFeatures
		);
	});	
	
	
	$('.OobaDemoLaunch').click(function() {
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 350; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=870,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'http://www.thechelsea.co.uk/security/demo/index.html',null,openAppFeatures
		);
	});	
	
	
	
	
});	


// LAUNCH MORTGAGE APPLICATION

function mortgageAppLaunch() {
	var middlePosition = screen.width/2 - 503; 
	var hightValue = screen.height - 110; 
	openAppFeatures = 'top=25,left=' + 
	middlePosition + 
	',height=' + 
	hightValue +
	',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
	window.open(															
		'https://online.thechelsea.co.uk/public/mortgages/new_direct_mortgage.do',null,openAppFeatures
	);
	
};


// MORTGAGES - HOW TO APPLY - QUERYSTRING CHECK TO DISPLAY RELEVANT APPLY METHODS
$(function() {
	
	currentPageURL = window.location.pathname;
	
	if (currentPageURL == "/mortgages/how-to-apply.html") {
		
		var applyMethod = retrieveQuerystringValue("apply");
		$('#howToApplyIntroCopy').empty();
		
		var howToApplyIntroCopy;
		//alert(applyMethod);
		
		switch (applyMethod) {
			case "online":			
				$('#applyOnlineContainer').show();
				$('#applyBranchContainer').hide();
				$('#applyTelephoneContainer').hide();
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>online</strong>.</p>';
				break;
			case "branch":
				$('#applyOnlineContainer').hide();
				$('#applyBranchContainer').show();
				$('#applyTelephoneContainer').hide();
				// span container to width of 2 cols
				$('#applyBranchContainer').css('width','760px');
				$('#applyBranchContainer').css('margin-right','0');
				$('#applyBranchContainer .applyBranchTitle').css('width','758px');
				$('#applyBranchContainer .applyBranchContent').css('width','742px');
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>in branch</strong>.</p>';
				break;
			case "telephone":
				$('#applyOnlineContainer').hide();				
				$('#applyBranchContainer').hide();
				$('#applyTelephoneContainer').show();
				// span container to width of 2 cols
				$('#applyTelephoneContainer').css('width','760px');
				$('#applyTelephoneContainer .applyTelephoneTitle').css('width','758px');
				$('#applyTelephoneContainer .applyTelephoneContent').css('width','742px');	
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>over the phone</strong>.</p>';
				break;
			case "onlinebranch":
				$('#applyOnlineContainer').show();
				$('#applyBranchContainer').show();
				$('#applyTelephoneContainer').hide();
				// span container to width of 2 cols
				$('#applyBranchContainer').css('width','760px');
				$('#applyBranchContainer').css('margin-right','0');
				$('#applyBranchContainer .applyBranchTitle').css('width','758px');
				$('#applyBranchContainer .applyBranchContent').css('width','742px');
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>online</strong>, or <strong>in branch</strong>.</p>';
				break;
			case "onlinetelephone":
				$('#applyOnlineContainer').show();
				$('#applyBranchContainer').hide();
				$('#applyTelephoneContainer').show();
				// span container to width of 2 cols
				$('#applyTelephoneContainer').css('width','760px');
				$('#applyTelephoneContainer .applyTelephoneTitle').css('width','758px');
				$('#applyTelephoneContainer .applyTelephoneContent').css('width','742px');				
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>online</strong>, or <strong>over the phone</strong>.</p>';
				break;
			case "branchtelephone":
				$('#applyOnlineContainer').hide();
				$('#applyBranchContainer').show();
				$('#applyTelephoneContainer').show();
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>in branch</strong>, or <strong>over the phone</strong>.</p>';
				break;
			case "onlinebranchtelephone":
				$('#applyOnlineContainer').show();
				$('#applyBranchContainer').show();
				$('#applyTelephoneContainer').show();
				// do nothing
				howToApplyIntroCopy = '<p>You can apply for this Chelsea mortgage product <strong>online</strong>, <strong>in branch</strong>, or <strong>over the phone</strong>.</p>';
				break;
			default:
				$('#applyOnlineContainer').show();

				$('#applyBranchContainer').show();
				$('#applyTelephoneContainer').show();
				howToApplyIntroCopy = '<p>You can apply for a Chelsea mortgage product <strong>online</strong>, <strong>in branch</strong>, or <strong>over the phone</strong>. Please note that not all mortgage products allow you to apply online. Details of our products and how you can apply for them are shown in our <a href="mortgage-finder.html" title="Mortgage Finder">Mortgage&nbsp;Finder</a>, and on our <strong>mortgage product pages</strong>.</p>';
			
		}
		
		$(howToApplyIntroCopy).appendTo('#howToApplyIntroCopy');
		
	}
	else {
		// do nothing	
	}	
		   
});
function retrieveQuerystringValue(productAvailability) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == productAvailability) {
			return pair[1];
		}
	}
} 

// MORTGAGE SECTION - USER HAS CLICKED APPLY NOW LINK ON - HIT RECORDED
$(function(){
	
	$('.productPageLink').live('click', function() {
			
		var productPage = window.location.pathname;
		productPage = productPage.substring(productPage.lastIndexOf('/') + 1);
		productPage = productPage + "_apply_product"
				
		// BEFORE SENDING THE HASH VALUE TO OMNITURE, RUN CHECK TO SEE WHAT THE HASH VALUE IS
		var s=s_gi('ybschelsea');
		s.linkTrackVars='prop10';
		s.linkTrackEvents='event1';
		s.prop10 = productPage;
		//console.log(s.prop10);	
		//alert(s.prop10);
		s.tl(this,'o','Hashed Link');
		
		applyPageRedirectDelay = 'window.location = "' + $(this).attr("href") + '"';
		//setTimeout(applyPageRedirectDelay, 100);
		window.location = $(this).attr("href");
		
	});
	
	$('.kfiLaunch').click(function() {		
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/kfi/kfi_initial_page.do',null,openAppFeatures
		);
	});
	
	$('.offsetDemonstratorLaunch').click(function() {		
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/mortgages/offset_demo/your_mortgage.do',null,openAppFeatures
		);
	});
	
	$('.fullBorrowingCalcLaunch').click(function() {		
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/mortgages/quick_enquiry.do',null,openAppFeatures
		);
	});
	
	$('.fullCostCalcLaunch').click(function() {		
		var middlePosition = screen.width/2 - 503; 
		var hightValue = screen.height - 110; 
		openAppFeatures = 'top=25,left=' + 
		middlePosition + 
		',height=' + 
		hightValue +
		',width=1000,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes'; 
		window.open(															
			'https://online.thechelsea.co.uk/public/mortgages/quick_calc.do',null,openAppFeatures
		);
	});
	
});
			
