/* ==========================================
   INITIALISE ONCE DOM IS LOADED
   ========================================== */

jQuery(document).ready(function($){

	// MISC VARIABLE CONFIG
	$pmask = $('#page_mask');

	// MISC FUNCTION CALLS

	LinkTargets();
	//ToggleMenu();

	// STICKY SIDE MENU

            // LOCATE THE SIDE MENU
            $sideMenu = jQuery('.side_menu.sticky');

            // IF A SIDE MENU EXISTS...
            if ( $sideMenu.length > 0 ) {

                // CALCULATE AN ALLOWANCE FOR THE LOGO...
                $logoHeight = jQuery('#head_logo').height()*2;

                // RETRIEVE THE MENU HEIGHT...
                $menuHeight = $sideMenu.height();

                // RETRIEVE ITS CURRENT TOP POSITION...
                $menuTop = $sideMenu.offset().top;

                // AND COMPENSATE FOR THE LOGO
                $menuTop = $menuTop - $logoHeight;

                // ALSO RETRIEVE THE PAGE AND FOOTER HEIGHT
                $pageHeight = jQuery(document).height();
                $footHeight = jQuery('#page_foot').height()

            }

	// 'TO TOP' NAVIGATION

	/* Show/hide the button ------- */

	$btt = $('#btt');
	if(typeof $btt != 'undefined') {
		$btt.hide();
		$page = $('#page_head');
		if($page.length > 0) {
			$po = $page.offset().top;

			$(window).scroll(function(){
				$st = $(window).scrollTop();
				if( $st > $po ){
					$btt.fadeIn(200);
				} else {
					$btt.fadeOut(400);
				};
			});

			/* Scroll with smoothness ------- */

			$btt.find('a').click(function(e){
				$('html, body').animate({
					scrollTop: 0
				}, 500);
				e.preventDefault();
			});
		}
	}

});

/* ==========================================
   INITIALISE ONCE FULL PAGE IS LOADED
   ========================================== */

/* window.onload = function(){

	// CALL THE COOKIE FUNCTION
	//jQuery(document).RunCookiePrompter();

}; */

/* ==========================================
   INITIALISE WHEN WINDOW IS RESIZED
   ========================================== */

            // WHEN THE PAGE IS RESIZED, DOUBLE CHECK THE KEY VARIABLES...
            jQuery(window).resize( function ($) {
		$sideMenu = jQuery('.side_menu.sticky');
                if ( $sideMenu.length > 0 ) {
                    $logoHeight = jQuery('#head_logo').height()*2;
                    $menuHeight = $sideMenu.height();
                    $pageHeight = jQuery(document).height();
                    $footHeight = jQuery('#page_foot').height()
                }
            });

/* ==========================================
   INITIALISE WHEN WINDOW IS SCROLLED
   ========================================== */

           jQuery(window).scroll( function ($) {

		$sideMenu = jQuery('.side_menu.sticky');

                // IF A SIDE MENU EXISTS...
                if ( $sideMenu.length > 0 ) {

                    // RETRIEVE THE CURRENT 'VIEW' POSITION OF THE PAGE
                    $scrollTop = jQuery(window).scrollTop();

                    // IF THE MENU IS DISAPPEARING OFF THE TOP OF THE PAGE... 
                    if ( $menuTop < $scrollTop ) {

                        // CALCULATE THE OPTIMUM POSITION TO KEEP THE MENU VISIBLE
                        $menuPosition = $scrollTop - $menuTop;

                        // CALCULATE THE LIKELY BOTTOM POSITION OF THE MENU BASED ON THIS
                        $menuBottomEst = $menuPosition + $menuHeight;

                        // CALCULATE THE TOP POSITION OF THE FOOTER, IN RELATION TO THE MENU TOP
                        $footerTop = ( $pageHeight - $footHeight ) - ( $menuTop + $logoHeight*1.5 );

                        // IF THE NEW TOP POSITION PUSHES THE MENU DOWN TOO FAR...
                        if ( $footerTop < $menuBottomEst ) { 

                            // CALCULATE THE MAXIMUM TOP POSITION OF THE MENU...
                            $maxPosition = $footerTop - $menuHeight;

                            // AND ADJUST THE TOP VALUE
                            $menuPosition = $maxPosition;

                        }

                        // ADJUST THE TOP POSITION TO KEEP IT VISIBLE
                        $sideMenu.css( 'top', $menuPosition );

                    } else {

                        $sideMenu.css('top', '0');

                    }
                }
            });

/* ==========================================
   FUNCTIONS
   ========================================== */

	// ADJUST LINK TARGETS
	function LinkTargets() {
		jQuery('a.off-site').click(function() { window.open(this.href); return false; });
		jQuery('a.new-win').click(function() { window.open(this.href); return false; });
		jQuery('a.download').click(function() { window.open(this.href); return false; });
		jQuery('a.pdf').click(function() { window.open(this.href); return false; });

		jQuery('#mc-embedded-subscribe-form').submit(function() { this.target='_blank'; });
	};

	// TOGGLE MENU
	function ToggleMenu() {
		jQuery('.menu-tog').click(function(e){
			e.preventDefault();
			jQuery($pmask).toggleClass('active');
			jQuery('#head_menu').toggleClass('open');
			jQuery('#head_logo, #title_strip, #page, #carousel, #tier_1_nav_strip, #tier_2_nav_strip, #tool_strip, #page_foot').toggleClass('blur');
		});
	};

	// ADD COOKIE PROMPTER TO JQUERY (if required)
/*	jQuery.fn.RunCookiePrompter = function() {

		// CONFIGURE
		var cookieFlagExpiry = "365";
		var cookiePrompt = ['',
	'		<div class="center">',
	'			<p class="heading">Cookies on our website</p>',
	'			<p class="message">We use cookies to enhance your experience of our website. Full details can be found in our <a href="/" title="Read our cookie policy...">cookie policy</a>. By using this website, you agree to our cookies being stored on your device.</p>',
	'			<p class="dismiss"><a title="Dismiss this message bar...">I understand, please close this message</a></p>',
	'			<div class="clear"></div>',
	'		</div>'].join("\n");

		// CHECK FOR AN EXISTING COOKIE ACCEPTANCE FLAG...
		if ( !( jQuery.cookie("BRBC-cookie-flag") ) ) {

			// ...THEN ATTEMPT TO CREATE IT IF IT DOESN'T EXIST
			jQuery.cookie("BRBC-cookie-flag", "1", {expires: parseInt(cookieFlagExpiry), path: '/'} );

			// CHECK TO SEE IF COOKIES ARE ENABLED IN BROWSER (BY CHECKING FOR THE JUST-CREATED COOKIE)
			if ( jQuery.cookie("BRBC-cookie-flag") == "1" ) {

				// SUCCESS: FLAG DIDN'T EXIST PREVIOUSLY, AND COOKIES ARE ENABLED IN BROWSER, SO DISPLAY ONE-TIME COOKIE PROMPT
				jQuery('#cookie-prompt').append(cookiePrompt);
				jQuery('#cookie-prompt p.dismiss a').click(function() { jQuery('#cookie-prompt div.center').animate({"height": "15px"}, "slow"); return false; });

			}
		}
	} */