jQuery(document).ready(function()
{ 
	// sIFR load and anchor point fixing
	sIFR.initialize();
	
	setTimeout(function()
	{
		var getAnchor = jQuery(location).attr('hash');
		if (getAnchor != '')
		{
			var anchorPosition = jQuery(getAnchor).offset();
			var windowPosition = jQuery(window).scrollTop();
			if (windowPosition != anchorPosition) { jQuery.scrollTo(anchorPosition.top); }
		}
	}, 800);
	
	
	// Modal Windows
	var removeModalWindow = '';
	var newModalWindow = '';
	
	function closeModalWindow()
	{
		window.clearTimeout(newModalWindow);
		jQuery('#modal-window').stop().fadeOut(500);
		jQuery('#overlay').stop().fadeOut(500);
		removeModalWindow = setTimeout(function() { jQuery('#modal-window').remove(); jQuery('#overlay').remove(); }, 550);
	}	
	
	function pullInSIFR()
	{
		var isInternetExplorer = jQuery.browser.msie;
		if (!isInternetExplorer)
		{
			sIFR.replace(lloydstsbvag, { selector: '#log-in-information h1', css: '.sIFR-root { margin-bottom: 0px; padding-bottom: 0px; color: #333399; font-weight: bold; }', wmode: 'transparent'} );
			sIFR.replace(lloydstsbvag, { selector: '#log-in-information h3.sifr', css: [ '.sIFR-root { margin-bottom: 0px; padding-bottom: 0px; color: #00864f; font-weight: normal; }', 'a { color: #00864f; text-decoration: none; }', 'a:hover { color: #5abe18; text-decoration: underline; }'], wmode: 'transparent'} );
		}
	}
	
	function changeModalWindowHeight()
	{
		var currentModalWindowHeight = jQuery('#modal-window .content').height();
		var newModalWindowHeight = jQuery('#modal-window .content #RadEditorPlaceHolderControl1').height();
		if (currentModalWindowHeight != newModalWindowHeight) { jQuery('#modal-window .content').animate({ height: newModalWindowHeight}, 800); }		
	}
	
	
	
	jQuery('#header #internet-banking-login #button-log-on a').click(function()
	{
		window.clearTimeout(removeModalWindow);
		window.clearTimeout(newModalWindow);
		
		jQuery('body').prepend('<div id="modal-window">\n<div class="top"><p class="close"><a href="#"><span>Close</span></a></p></div>\n<div class="content"></div>\n</div>\n<div id="overlay"><!-- --></div>');
		
		var bodyHeight = jQuery('#wrapper').height();
		bodyHeight = bodyHeight+20;
		jQuery('#overlay').css('height', bodyHeight).fadeIn(800);
		
		var linkToLoad = jQuery(this).attr('href')+ ' #RadEditorPlaceHolderControl1';
		jQuery('#modal-window .content').load(linkToLoad, function() { jQuery('#modal-window').fadeIn(800, function() { pullInSIFR(); }); }).css('overflow', 'hidden');
		
		// Close the window
		jQuery('#modal-window .top a').click(function() { closeModalWindow(); return false; });
		jQuery('#overlay').click(function() { closeModalWindow(); });

		return false;
	});

	// Change the content of the window
	jQuery('.pop-up-footer a').live('click', function()
	{
		window.clearTimeout(removeModalWindow);
		
		var linkToLoad = jQuery(this).attr('href')+ ' #RadEditorPlaceHolderControl1';
		var modalWindowHeight = jQuery('#modal-window .content').height();

		jQuery('#modal-window .content #RadEditorPlaceHolderControl1').fadeOut(300, function() { jQuery('#modal-window .content').css('height', modalWindowHeight); jQuery('#modal-window .content #RadEditorPlaceHolderControl1').remove(); });
			
		newModalWindow = setTimeout(function()
		{
			jQuery('#modal-window .content').load(linkToLoad, function () { jQuery('#modal-window .content #RadEditorPlaceHolderControl1').fadeIn(300, function () { pullInSIFR(); setTimeout(function() { changeModalWindowHeight(); }, 300); }); });
			// Second timed check to functions, just in case there's been a muck-up
			setTimeout(function() {	pullInSIFR(); changeModalWindowHeight(); }, 1500);
		}, 350);
		return false;
	});
	
	
	
});