/*
Site:		NWDA: ERDF
File:		/assets/scripts/global.js
Version:	2009-07-16
Author: 	
----------------------------------------------- */


/*	Global Configuration
----------------------------------------------- */
var isIE6 = false;
var isSafari = false;


/*	Global Literal Object: Site-wide functions
----------------------------------------------- */
var Global = {

    /* Configuration */

    /* jQuery Object References */

    /* Utility: Browser Tests / Specific Fixes/Hooks */
    utilBrowserTests: function() {

        /* Detect if User Agent is IE6 using object detection, apply fix for background flicker bug */
        if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { } }

        /* Detect if User Agent is Safari, add class '.safari' to div#Container */
        if ($.browser.safari) { isSafari = true; $('div#Container').addClass('safari'); }

    },

    /* Utility: External Links */
    utilExternalLinks: function() {
        $('a.external').each(function() {
            var tThis = $(this);
            tThis.click(function() {
                return !window.open(tThis.attr('href'));
            });
        });
    },

    /* Initialise */
    init: function() {

        /* Class Context */
        var cc = this;

        /* Configuration */

        /* jQuery Object References to the Elements this speeds up the DOM */

        cc.utilBrowserTests();

    }

};


/*	Component: .c-projects-showcase
----------------------------------------------- */
var ComponentProjectsShowcase = {

    /* Configuration */
    cProjectsShowcaseItemsCount: null,
    cProjectsShowcaseItemsLength: null,

    /* jQuery Object References */
    jOProjectsShowcase: null,
    jOProjectsShowcaseHeader: null,
    jOProjectsShowcaseNavigation: null,

    eventsShowcase: function() {

        /* Class Context */
        var cc = this;

        $('a', cc.jOProjectsShowcaseNavigation).click(function() {

            var tThis = $(this); /* a */
            var tThisClassStr = tThis.attr('class').toString();
            var tThisClass = tThisClassStr.split("slideshow-image-");
            var tThisHref = 'li#slideshow-image-' + tThisClass[1];

            /* Current visable item and next */
            var tCurrentItem = $('li.active', cc.jOProjectsShowcase);
            var tNextItem = $(tThisHref, cc.jOProjectsShowcase);

            /*
            TODO Performance test, the following might be faster
            if ( ('#' + tCurrentItem.attr('id') ) == tThisHref) {
            */
            /* if tThis is already active then return false */
            if (tThis.hasClass('active')) {
                return false;
            } else {
                $('a.active', cc.jOProjectsShowcaseNavigation).removeClass('active');
                tThis.addClass('active');
            }

            /* Fade tCurrentItem item out and show tNextItem */
            if ($.browser.msie) {

                tCurrentItem.removeClass('active').hide();
                tNextItem.addClass('active').show();

            } else {

                tCurrentItem.removeClass('active').fadeTo('slow', 0.0, function() {
                    tNextItem.addClass('active').fadeTo('slow', 1.0);
                });

            }

            return false;

        });

    },

    buildShowcase: function() {

        /* Class Context */
        var cc = this;

        /* Setup array for Showcase Navigation */
        var aProjectsShowcaseNavigation = [];

        /* Set cc.cProjectsShowcaseItemCount because we are about to reuse it */
        cc.cProjectsShowcaseItemsCount = 0;

        /* Build an array for navigation by itterating each .c-b li apply classes */
        $('li', cc.jOProjectsShowcase).each(function(i) {

            cc.cProjectsShowcaseItemsCount++;

            var tThis = $(this); /* li */
            var tThisNext = tThis.children('span.s-i-i'); /* span.s-i-i */

            /* Add class to li */
            tThis.attr('id', 'slideshow-image-' + cc.cProjectsShowcaseItemsCount + '');

            /* Set opacity on span that we will use for transparent background */
            tThisNext.css('opacity', '0.8');
            aProjectsShowcaseNavigation[i] = '<a class="slideshow-image-' + cc.cProjectsShowcaseItemsCount + '" href="#slideshow-image-' + cc.cProjectsShowcaseItemsCount + '">' + cc.cProjectsShowcaseItemsCount + '</a><span class="s-h-o"> | </span>';

            /* If cc.cProjectsShowcaseItemsCount = cc.cProjectsShowcaseItemsLength then add events */
            if (cc.cProjectsShowcaseItemsCount == cc.cProjectsShowcaseItemsLength) {

                /* Define jOProjectsShowcaseNavigation and add aProjectsShowcaseNavigation array items */
                cc.jOProjectsShowcaseNavigation = $('<p class="c-projects-showcase-navigation">' + aProjectsShowcaseNavigation.join('') + '</p>');

                /* Append cc.jOProjectsShowcaseNavigation to cc.jOProjectsShowcaseHeader */
                cc.jOProjectsShowcaseNavigation.appendTo(cc.jOProjectsShowcaseHeader);

                cc.eventsShowcase();
            }

        });

        /* Hide all li's except first and add active class to first */
        if ($.browser.msie) {

            $('li', cc.jOProjectsShowcase).hide();
            $('li:first', cc.jOProjectsShowcase).addClass('active').show('slow');

        } else {

            $('li', cc.jOProjectsShowcase).css('opacity', '0.0');
            $('li:first', cc.jOProjectsShowcase).addClass('active').fadeTo('slow', 1.0).show();

        }

    },

    init: function() {

        /* Class Context */
        var cc = this;

        cc.jOProjectsShowcase = $('.c-projects-showcase');
        cc.cProjectsShowcaseItemsLength = $('li', cc.jOProjectsShowcase).length;

        /* Only run slideshow if more than 1 project */
        if (cc.cProjectsShowcaseItemsLength > 1) {
            cc.jOProjectsShowcaseHeader = $('.c-h', cc.jOProjectsShowcase);
            cc.buildShowcase();
        }

    }

};


/*	Component: .c-listing-news-accordion
----------------------------------------------- */
var ComponentListingNewsAccordion = {

    /* Configuration */

    /* jQuery Object References */
    jOcListingNewsAccordion: null,
    jOcListingNewsAccordionFirstItem: null,

    eventsAccordion: function() {

        /* Class Context */
        var cc = this;

        /* Click event for each li */
        $('h4', cc.jOcListingNewsAccordion).click(function() {
            var tThis = $(this); /* h4 */
            var tThisNext = tThis.next(); /* ul */

            /* If tThisNext is already visable do nothing */
            if (tThisNext.is(':visible')) {
                return false;
            }

            /* Remove current active item */
            $('.s-i-arrow', cc.jOcListingNewsAccordion).removeClass('s-i-arrow-down').addClass('s-i-arrow-right');
            $('li ul', cc.jOcListingNewsAccordion).slideUp();

            /* Set new item (li) to active state */
            tThisNext.slideDown();
            $('.s-i-arrow', tThis).removeClass('s-i-arrow-right').addClass('s-i-arrow-down');

        });

    },

    buildAccordion: function() {

        /* Class Context */
        var cc = this;

        /* Hide li ul's */
        $('li ul', cc.jOcListingNewsAccordion).hide();

        /* Set arrows to right (off state) */
        $('.s-i-arrow', cc.jOcListingNewsAccordion).removeClass('s-i-arrow-down').addClass('s-i-arrow-right');

        /* Hide li ul's */
        $('li ul', cc.jOcListingNewsAccordion).hide();

        /* Set first item (li) to active state */
        $('.s-i-arrow', cc.jOcListingNewsAccordionFirstItem).removeClass('s-i-arrow-right').addClass('s-i-arrow-down');
        $('ul', cc.jOcListingNewsAccordionFirstItem).show();

        cc.eventsAccordion();

    },

    init: function() {

        /* Class Context */
        var cc = this;

        cc.jOcListingNewsAccordion = $('.c-listing-news-accordion');
        cc.jOcListingNewsAccordionFirstItem = $('li:first', cc.jOcListingNewsAccordion);

        cc.buildAccordion();

    }

};


/*	Component: .c-resources
----------------------------------------------- */
var ComponentResourcesAccordion = {

    /* Configuration */

    /* jQuery Object References */
    jOcResourcesAccordion: null,
    jOcResourcesAccordionFirstItem: null,
    jOcResourcesAccordionFirstItemTableHead: null,

    eventsAccordion: function() {

        /* Class Context */
        var cc = this;

        /* Click event for each li */
        $('h2', cc.jOcResourcesAccordion).click(function() {
            var tThis = $(this); /* h2 */
            var tThisParent = tThis.parent(); /* div */
            var tThisNext = tThis.next(); /* table */

            /* If tThisNext is already visable do nothing */
            if (tThisNext.is(':visible')) {
                return false;
            }

            /* Remove current active item */
            $('.c-b-active', cc.jOcResourcesAccordion).removeClass('c-b-active');

            /* Hide current visable item and show next, if IE6 disable slide animation anmation */
            if (isIE6) {
                $('.c-b div', cc.jOcResourcesAccordion).hide();
                tThisNext.show();
            } else {
                $('.c-b div', cc.jOcResourcesAccordion).slideUp();
                tThisNext.slideDown();
            }

            /* Add active class to tThisParent */
            tThisParent.addClass('c-b-active');

        });

    },

    buildAccordion: function() {

        /* Class Context */
        var cc = this;

        /* Hide li tables */
        $('.c-b div', cc.jOcResourcesAccordion).hide();

        /* If browser is IE hide thead rather than place offscreen */
        if ($.browser.msie) {
            $('.c-b thead', cc.jOcResourcesAccordion).hide();
        } else {
            $('.c-b thead', cc.jOcResourcesAccordion).addClass('s-h-o');
        }

        /* Set first item (div) to active state */
        cc.jOcResourcesAccordionFirstItem.addClass('c-b-active');
        $('div', cc.jOcResourcesAccordionFirstItem).show();

        /* Create dummy table head based on that of first item's table */
        $('<table>' + cc.jOcResourcesAccordionFirstItemTableHead.html() + '</table>').insertBefore(cc.jOcResourcesAccordionFirstItem);

        cc.eventsAccordion();

    },

    init: function() {

        /* Class Context */
        var cc = this;

        cc.jOcResourcesAccordion = $('.c-resources');
        cc.jOcResourcesAccordionFirstItem = $('.c-b:first', cc.jOcResourcesAccordion);
        cc.jOcResourcesAccordionFirstItemTableHead = $('table thead', cc.jOcResourcesAccordionFirstItem);

        cc.buildAccordion();

    }

};


/*	Component: .c-quicklinks
----------------------------------------------- */
var ComponentQuicklinks = {

    /* Configuration */
    cQuicklinksInactiveText: null,

    /* jQuery Object References */
    jOQuicklinks: null,

    builQuicklinks: function(obj) {

        /* Class Context */
        var cc = this;

        var $OQuicklinksUL = $('ul', cc.jOQuicklinks);

        var $OQuicklinksActive = $('li.active', $OQuicklinksUL);

        /* Add width to filter content */
        $(cc.jOQuicklinks).width($(cc.jOQuicklinks).width());

        /* If Filter has an active LI display that text else display cc.cFilterInactiveText */
        if ($OQuicklinksActive.length) {
            $(cc.jOQuicklinks).prepend('<p>' + $OQuicklinksActive.text() + '</p>');
        } else {
            $(cc.jOQuicklinks).prepend('<p>' + cc.cQuicklinksInactiveText + '</p>');
        }

        /* Filter hover */
        $(cc.jOQuicklinks).hover(function() {
            $OQuicklinksUL.show();
        }, function() {
            $OQuicklinksUL.hide();
        });

        $OQuicklinksUL.slideUp('slow');

    },

    init: function() {

        /* Class Context */
        var cc = this;

        cc.jOQuicklinks = $('.c-quicklinks .c-b');
        cc.cQuicklinksInactiveText = 'Please select';

        cc.builQuicklinks();

    }

};


/*	DOM Ready events
----------------------------------------------- */
$(function() {

    /* Initialise Global */
    Global.init();

    /* If .c-projects-showcase component exisits initialise functions */
    if ($('.c-projects-showcase').length) {
        ComponentProjectsShowcase.init();
    }

    /* If .c-listing-news-accordion component exisits initialise functions */
    if ($('.c-listing-news-accordion').length) {
        ComponentListingNewsAccordion.init();
    }

    /* If .c-resources component exisits initialise functions */
    if ($('.c-resources').length) {
        ComponentResourcesAccordion.init();
    }

    /* If .c-quicklinks component exisits initialise functions */
    if ($('.c-quicklinks').length) {
        ComponentQuicklinks.init();
    }

    /* Fire Global.utilExternalLinks() last so any external links added via DOM during local page initialization will be activated */
    Global.utilExternalLinks();

});