$(document).ready(function(){
	// Flash banner insertion
	$('#banner').flash(null, {version: 7}, function(htmlOptions){
		var banner = $(this);
		// Set options
		htmlOptions.src     = banner.attr('rel');
		htmlOptions.width   = 900;
		htmlOptions.height  = 203;
		htmlOptions.quality = 'high';
		htmlOptions.wmode   = 'transparent';
		// Embed it!
		banner.prepend($.fn.flash.transform(htmlOptions));
	});
	// Exit confirmations
	$('.sticky').each(function(){
		var box = $(this);
		this.stickypos = box.offset().top;
		box.css({
			position: 'absolute',
			top:       this.stickypos
		});
	});
	$(window).scroll(function(){
		var scrolled = $(this).scrollTop();
		$('.sticky').each(function(){
			$(this).css('top', (scrolled > this.stickypos) ? scrolled : this.stickypos);
		});
	});
	// Menu flyout effects
	$('#menu li.first').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	// Image swapper
	$('.hoverswap').hover(function(){
		// Switch to hover source
		this.src = this.hover_src;
	}, function(){
		// Restore original source
		this.src = this.cache_src;
	})
	// Preloading
	.each(function(){
		// Find the extension and name
		var ext = this.src.slice(-4);
		var img = this.src.slice(0, -4);
		// Cache the current source
		this.cache_src = this.src;
		this.hover_src = (img.slice(-3) != '_on') ? img +'_on'+ ext : this.cache_src;
		// Preloading!
		$('<img>').css({
			display:  'block',
			position: 'absolute',
			top:      '-4000px'
		}).attr('src', this.hover_src);
	});
	// ------------------------------------------------------------------------
	// Modal "popup" windows
	$('body').append(
		'<div id="ajax_popup">'+
			'<div id="ajax_header"><span class="ajax_close">X</span>BANKWEST Document Viewer</div>'+
			'<div id="ajax_content"></div>'+
		'</div>'+
		'<div id="ajax_shade" class="ajax_close"></div>'
	);
	// Modal overlay
	$('#ajax_shade').height($(window).height()).css('opacity', 0.8);
	// Handle IE6. Other browsers properly handle position:fixed.
	if ( ! window.XMLHttpRequest){
		// Make the overlay absolutely positioned
		$('#ajax_popup, #ajax_shade').css('position', 'absolute');
		// Scroll the overlay with the window
		$(window).scroll(function(){
			$('#ajax_popup:visible').css('top', $(window).scrollTop() + 100 +'px');
		});
	}
	// ------------------------------------------------------------------------
	// Trigger bindings
	Bankwest.rebind();
});
// Bankwest class
var Bankwest = {
	// Document bindings
	rebind: function(scope){
		var scope = scope || document.body;
		// Table coloring for alt rows
		$('.colortable tr:nth-child(even)', scope).addClass('alt');
		// --------------------------------------------------------------------
		// Popup activators
		$('a.popup', scope).click(Bankwest.popup);
		// ------------------------------------------------------------------------
		// Special style for #id links
		$('a[href~="#"]', scope).each(function(){
			if ($(this).parent().is('li')) return;
			$(this).css({
				paddingLeft:    '3px',
				textDecoration: 'none',
				fontSize:       '9px',
				fontWeight:     'bold'
			});
		});
		// --------------------------------------------------------------------
		// Modal close binds
		$('.ajax_close', scope).click(function(){
			// Hide the popup and overlay
			$('#ajax_popup, #ajax_shade', scope).hide();
			// Hide the content
			$('#ajax_content', scope).css('visibility', 'hidden').html('');
			// Show the <object>s
			$('#body object').css('visibility', 'visible');
			return false;
		});
		// --------------------------------------------------------------------
		// Page printing
		$('.print_page', scope).click(function(){
			window.print();
			return false;
		});
		// --------------------------------------------------------------------
		// Exit confirmations
		$('.confirm_exit', scope).click(function(){
			// If the user does not accept this confirmation, nothing will happen
			if ( ! confirm(
				'You are now leaving the BANKWEST web site.\r\n'+
				'Views or opinions on these web sites do not necessarily represent those of BANKWEST.'
			)) return false;
		});
	},
	// Modal popups
	popup: function(){
		// Hide <object>s, to prevent issues with overlap and improve performance
		$('#body object').css('visibility', 'hidden')
		// Reset overlay to the window height
		$('#ajax_shade').css('height', $(window).height()).show();
		// Reset the popup location for IE6
		if ( ! window.XMLHttpRequest) $('#ajax_popup').css('top', $(window).scrollTop() + 100 +'px');
		// Unhide the popup
		$('#ajax_popup').show();
		// Load the new content
		$('#ajax_content').load(this.href + '?ajax=1', function(){
			// Rebind, for new content
			Bankwest.rebind(this);
			// Show the content
			$(this).css('visibility', 'visible');
		});
		// For links...
		return false;
	}
};