﻿/*================================================== JAVASCRIPT */
/*
CSS Document for MSL Group 
Build: Pete Robelou on behalf of SAS
*/

$('html').addClass('js');

//--------------------------------------------- GLOBAL VARIABLES
// BROWSER CHECK 
var ieCheck = false;

// BACKGROUND IMAGE
var bgImageTotal;
var randomNumber;
var imgPath;

// MAIN NAVIGATION
var navMainOpenSpeed = 200;
var navMainCloseSpeed = 200;

var navMainOver;
var navMainOverDelay;

var navMainTimer;
var navMainTimerDelay = 300;

var navMainItem;
var navMainDropdownColor;
var navMainLowerDropdownColor;

//--------------------------------------------- INITILIZE
$(document).ready(function () {
    // BROWSER CHECK
    if ($.browser.msie) {
        ieCheck = true;
    };

    // IE FIXES
    if (ieCheck == true) {
        // FIX FIRST LAST CHILD IE
        flChild();

        // FIX BACKGROUND IMAGE CACHING IE6
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    };

    // REGION SWITCHER
    $('#region_switch').change(function () {
        if ($(this).find('option:selected').text() != 'Regional websites') {
            var loc = $(this).find('option:selected').attr('value');
            window.open(loc);

        }
    });

    // RANDOM BACKGROUND IMAGE
    randomBg();

    // MAIN NAVIGATION
    mainNavigation();

    // BANNER CAROUSEL
    bannerCarousel();

    // INLINE CAROUSEL
    inlineCarousel();

    // EXPANDABLE CONTENT
    expandableContent();

    // EXTERNAL LINK
    externalLink();

    // FORMS         
    formInputs();

    // STYLED SELECT BOXES
    $('#nav_site_search select').sSelect();
    $('#filter_content select').sSelect();
    $('#contour select').sSelect();
});

//--------------------------------------------- FUNCTIONS
//---------- FIX FIRST LAST CHILD IE
function flChild() {
    $(function () {
        $('ul.list_category li:first-child').addClass('first-child');
        $('.content_banner p:last-child').addClass('last-child');
        $('.filter div:last-child').addClass('last-child');
        $('.map_caption ul li:first-child').addClass('first-child');
        $('.column_right .content_holder ul:last-child').addClass('last-child');
        $('.column_container_one_third div:last-child').addClass('last-child');
        $('.column_container_one_fourth div:last-child').addClass('last-child');
        $('.column_container_one_fifth div:last-child').addClass('last-child');
        $('.column_container_one_sixth div:last-child').addClass('last-child');
        $('.column_container_one_half div:last-child').addClass('last-child');        
    });
};

//---------- RANDOM BACKGROUND IMAGE
function randomBg() {
    // total background images
    bgImageTotal = 2;

    // generates random number
    randomNumber = Math.round(Math.random() * (bgImageTotal - 1)) + 1;

    // generates image path
    imgPath = ('/images/bg_bubbles_0' + randomNumber + '.gif');

    // adds background image css tag
    $('#site_container').css('background-image', ('url("' + imgPath + '")'));
};

//---------- MAIN NAVIGATION
function mainNavigation() {
    // mouse over nav item
    $('#nav_main li').mouseenter(function () {
        // adjusts rollover delay
        if ($('#nav_main_dropdown_container').is(':visible')) {
            navMainOverDelay = 0;
        } else {
            navMainOverDelay = 300;
        };

        if ($(this).hasClass('dropdown')) {
            // clears rollover delay
            clearTimeout(navMainOver);

            // clears timeout delay
            clearTimeout(navMainTimer);

            // gets current nav item
            navMainItem = $(this);

            // delays activating dropdown
            navMainOver = setTimeout(function () {   
                // animates close the dropdown container
                $('#nav_main_dropdown_lower_container').slideUp(navMainCloseSpeed);

                // sets background color for dropdown and adds/removes class for dropdown
                if ($(navMainItem).hasClass('nav_main_02')) {
                    navMainDropdownColor = '#0086b4';
                    navMainLowerDropdownColor = '#1596c2';
                    $('#nav_main_dropdown_container').removeClass();
                    $('#nav_main_dropdown_container').addClass('nav_main_02');
                } else if ($(navMainItem).hasClass('nav_main_03')) {
                    navMainDropdownColor = '#006d9a';
                    navMainLowerDropdownColor = '#1a7ca4';
                    $('#nav_main_dropdown_container').removeClass();
                    $('#nav_main_dropdown_container').addClass('nav_main_03');
                } else if ($(navMainItem).hasClass('nav_main_04')) {
                    navMainDropdownColor = '#5f2662';
                    navMainLowerDropdownColor = '#6f3c72';
                    $('#nav_main_dropdown_container').removeClass();
                    $('#nav_main_dropdown_container').addClass('nav_main_04');
                } else if ($(navMainItem).hasClass('nav_main_07')) {
                    navMainDropdownColor = '#114764';
                    navMainLowerDropdownColor = '#285f7c';
                    $('#nav_main_dropdown_container').removeClass();
                    $('#nav_main_dropdown_container').addClass('nav_main_07');
                };

                // gets content from dropdown
                var dropdownContent = $('.nav_dropdown', navMainItem).html();

                // adds dropdown content to dropdown container
                $('#nav_main_dropdown_container').html(dropdownContent);

                // removes active class then adds it to current nav item
                $('#nav_main li.dropdown').removeClass('active');
                $(navMainItem).addClass('active');

                // adds background color and animates open the dropdown container
                $('#nav_main_dropdown_container').css('background-color', navMainDropdownColor);
                $('#nav_main_dropdown_lower_container').css('background-color', navMainLowerDropdownColor);
                $('#nav_main_dropdown_container').slideDown(navMainOpenSpeed);

            }, navMainOverDelay);

        } else {
            closeMainNavigation();
        };
    });

    // mouse out nav item
    $('#nav_main li.dropdown').mouseleave(function () {
        // delays calling close navigation function
        navMainTimer = setTimeout('closeMainNavigation()', navMainTimerDelay);

        // clears rollover delay
        clearTimeout(navMainOver);
    });

    // mouse over dropdown container
    $('#nav_main_dropdown_container').mouseenter(function () {
        // clears timeout delay
        clearTimeout(navMainTimer);

        // mouse over dropdown nav item
        $('ul li', this).mouseenter(function () {
            // gets content from dropdown
            var dropdownLowerContent = $('.nav_dropdown_lower', this).html();

            // hides dropdown content
            if (!$(this).hasClass('active')) {                
                $('#nav_main_dropdown_lower_container ul').hide();
            }            

            // gets height of dropdown
            var dropdownLowerHeight = $('.nav_dropdown_lower', this).height();

            // removes active class then adds it to current dropdown nav item
            $('#nav_main_dropdown_container ul li').removeClass('active');
            $(this).addClass('active');

            // checks if dropdown lower nav is visble
            if (!$('#nav_main_dropdown_lower_container').is(':visible')) {
                $('#nav_main_dropdown_lower_container').css('height', '0px').css('display', 'block');
            };

            // animates open/height the dropdown lower container
            $('#nav_main_dropdown_lower_container').stop().animate({ height: dropdownLowerHeight }, navMainOpenSpeed, function () {
                // adds dropdown content to dropdown container
                $('#nav_main_dropdown_lower_container').html(dropdownLowerContent);

                // shows dropdown content
                $('#nav_main_dropdown_lower_container').show();
            });

        });
    });

    // mouse out dropdown container
    $('#nav_main_dropdown_container').mouseleave(function () {
        // delays calling close navigation function
        navMainTimer = setTimeout('closeMainNavigation()', navMainTimerDelay);
    });

    // mouse over dropdown lower container
    $('#nav_main_dropdown_lower_container').mouseenter(function () {
        // clears timeout delay
        clearTimeout(navMainTimer);
    });

    // mouse out dropdown lower container
    $('#nav_main_dropdown_lower_container').mouseleave(function () {
        // delays calling close navigation function
        navMainTimer = setTimeout('closeMainNavigation()', navMainTimerDelay);
    });
};

function closeMainNavigation() {
    $('#nav_main_dropdown_container').slideUp(navMainCloseSpeed);
    $('#nav_main_dropdown_lower_container').slideUp(navMainCloseSpeed);
    $('#nav_main li.dropdown').removeClass('active');
};

//---------- BANNER CAROUSEL
function bannerCarousel() {
    // adds a caption container to the carousel
    $('#carousel_banner').append('<div id="caption_container" class="content_banner"><div class="content_holder"></div></div>');    

    // activates the carousel
    $('#carousel_banner').cycle({
        fx: 'scrollHorz',
        easing: 'easeInOutQuad',
        speed: '1800',
        timeout: 4000,
        slideExpr: '.slide img',
        manualTrump: false,
        next: '#carousel_banner_nav_next',
        prev: '#carousel_banner_nav_prev',
        before: onBefore,
        onPagerEvent: pagerClick,
        onPrevNextEvent: pagerClick,
        pager: '#carousel_banner_nav',
        pagerAnchorBuilder: function (idx, slide) {
            // return selector string for existing anchor 
            return '#carousel_banner_nav li:eq(' + idx + ') a';
        }
    });
};

// on click of navigation
function pagerClick() {
    // pause the carousel
    $('#carousel_banner').cycle('pause');

    // reset timeout
    clearTimeout();

    // resume carousel after 15 seconds
    setTimeout(function () {
        $('#carousel_banner').cycle('resume');
    }, 15000);
};

// before transition takes place
function onBefore() {   
    // ------------------------------------------ animate caption
    // gets content of the next caption
    var captionContent = $(this).parent().find('.content_banner .content_holder').html();

    // gets height of the next caption
    var captionHeight = $(this).parent().find('.content_banner').height();

    // hides old caption content
    $('#carousel_banner #caption_container .content_holder').hide();

    // animates the caption container
    $('#carousel_banner #caption_container').stop().animate({ height: captionHeight }, 1200, function () {

        // appends new caption content
        $('#carousel_banner #caption_container .content_holder').html(captionContent);

        // shows new caption content
        $('#carousel_banner #caption_container .content_holder').show();
    });
};

//---------- INLINE CAROUSEL
function inlineCarousel() {
    $('.carousel_inline_container').each(function (i) {

        // adds unique id to each carousel container
        $(this).attr('id', 'carousel_inline_' + (i));

        // gets unique id
        var carouselId = $(this).attr('id');

        // checks if carousel should display 1, 3 or 5 items
        if ($(this).hasClass('carousel_inline_wide')) {
            var carouselItems = 5;
        } else if ($(this).hasClass('solo')) {
            carouselItems = 1;
        }
        else {
            carouselItems = 3;
        };

        // activates carousel by unique id
        $('#' + carouselId + ' .carousel_inline').jCarouselLite({
            btnNext: '#' + carouselId + ' .carousel_nav_next',
            btnPrev: '#' + carouselId + ' .carousel_nav_prev',
            visible: carouselItems
        });
    });        
};

//---------- EXPANDABLE CONTENT
function expandableContent() {
    $('.content_expander .link_expander').click(function () {
        $(this).parent().find('.content_expandable').slideToggle(function () {
            if ($(this).parent().find('.link_expander span').text() == 'Show more +') {
                $(this).parent().find('.link_expander span').text('Show less -');
            } else {
                $(this).parent().find('.link_expander span').text('Show more +');
            };
        });
        return false;
    });
};

//---------- EXTERNAL LINK
function externalLink() {
    $('.external').click(function () {
        var link = $(this).attr('href');
        window.open(link);
        return false;
    });
};

//---------- FORMS
function formInputs() {
    $('input[type="text"]').focus(function () {
        if ($(this).val() == $(this)[0].defaultValue) {            
            $(this).val('');
        }
        if ($(this).val() != $(this)[0].defaultValue) {
            $(this).select();
        }
    });
    $('input[type="text"]').blur(function () {        
        if ($.trim($(this).val()) == '') {
            $(this).val($(this)[0].defaultValue ? $(this)[0].defaultValue : '');
        }
    });
    $('input[type="submit"]').click(function () {
        var searchValue = $(this).parent().find('input[type="text"]');

        if ($(searchValue).val() == $(searchValue)[0].defaultValue) {
            $(searchValue).val('');
        };
    });
};
