/*
Site:		NNDC
File:		/Scripts/global.js
Version:	2009-08-12
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */

var isIE6 = false;
var isSafari = false;
var historyNeedIframe = jQuery.browser.msie && (jQuery.browser.version < 8 || document.documentMode < 8);
/*	Global Literal Object: Site-wide functions
----------------------------------------------- */


//  avoid widows on section links anchors
jQuery('a.sectionLink').each(
function () {
    this.innerHTML = this.innerHTML.replace(/\s([^\s<]{0,10})\s*$/, '&nbsp;$1');
}
 );

var Global = {

    /* Configuration */
    /* jQuery Object References to the Elements this speeds up the DOM */

    /* 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 jOThis = $(this);
            jOThis.click(function () {
                return !window.open(jOThis.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-featured
----------------------------------------------- */
var CFeatured = {

    /* Configuration */
    cCFeaturedSIB: null,
    cCFeaturedItemAnchorSII: null,

    /* jQuery Object References to the Elements this speeds up the DOM */
    jOECFeatured: null,
    jOECFeaturedUL: null,

    buildCFeatured: function () {

        /* Class Context */
        var cc = this;

        /* Prepend span background for opactiy */
        cc.cCFeaturedSIB.css('opacity', 0.9).prependTo(cc.jOECFeatured);

        $('li', cc.jOECFeaturedUL).each(function (index) {

            /* Adds 1 to index that would start at 0 */
            index++;

            var jOThis = $(this);
            var jOThisAnchor = $('a', jOThis);
            var jOThisP = $('p', jOThis);
            jOThisAnchor.append(cc.cCFeaturedItemAnchorSII);

            /* Insert read more */
            //jOThisP.append("<em>Read more</em>");

            if (jOThis.hasClass("active")) {
                jOThisP.show();
                jOThis.addClass('active');

                var moveBGPX = (index * 900 - 900);
                cc.jOECFeatured.css('background-position', '-' + moveBGPX + 'px');
            }

            /* Change page to jOThisAnchor.href */
            jOThisP.click(function () {
                return false;
            });

            jOThisAnchor.click(function (event) {
                var newHash = jOThisAnchor.attr('href');
                window.location.hash = newHash;
                if (historyNeedIframe) {
                    var ihistory = $("#jQuery_history")[0];
                    var iframe = ihistory.contentWindow.document;
                    iframe.open();
                    iframe.close();
                    iframe.location.hash = newHash;
                }

                /* Move background based on the current clicked index */
                var moveBGPX = (index * 900 - 900);
                cc.jOECFeatured.animate({ backgroundPosition: -moveBGPX }, 800);

                /* Update background colour for cc.cCFeaturedSIB based on h3 backgroud colour */
                var bgColor = jOThisAnchor.css('background-color');
                cc.cCFeaturedSIB.css('background-color', bgColor);

                /* Hide current visable p */
                $('p:visible', cc.jOECFeaturedUL).hide();
                $('li.active', cc.jOECFeaturedUL).removeClass('active');
                jOThisP.show();
                jOThis.addClass('active');

                return false;
            });

        });

    },

    /* Initialise */
    init: function () {

        /* Class Context */
        var cc = this;

        /* Configuration */
        cc.cCFeaturedSIB = $('<span class="s-i-bg"></span>');
        cc.cCFeaturedItemAnchorSII = '<span class="s-i-i"></span>';

        /* jQuery Object References to the Elements this speeds up the DOM */
        cc.jOECFeatured = $('.c-featured');
        cc.jOECFeaturedUL = $('ul', cc.jOECFeatured);

        cc.buildCFeatured();

    }

};

/*	DOM Ready events
----------------------------------------------- */
$(function () {

    /* Initialise Global */
    Global.init();

    /* If .c-featured initialise functions */
    if ($('.c-featured').length) {
        CFeatured.init();
    }

    /* Fire local page initialize methods */
    if (typeof initPage == "function") { initPage(); }

    /* Fire Global.utilExternalLinks() last so any external links added via DOM during local page initialization will be activated */
    // Global.utilExternalLinks();

});

if (historyNeedIframe) {
    $("body").prepend('<iframe id="jQuery_history" style="display: none;"' +
				    ' src="javascript:false;"></iframe>');
}

var lastHash = '';
function pollHash() {
    if (historyNeedIframe) {
        // On IE, check for location.hash of iframe
        var ihistory = jQuery("#jQuery_history")[0];
        var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
        var current_hash = iframe.location.hash.replace(/\?.*$/, '');
        if (lastHash != current_hash) {
            window.location.hash = current_hash;
            lastHash = current_hash;
            loadContentFromHash(lastHash);
        }
    } else {

        if (lastHash !== location.hash) {
            lastHash = location.hash;
            loadContentFromHash(lastHash);
        }

    }
}
function moveSlider(index) {
    var moveBGPX = (index * 900 - 900);
    $('.c-featured').animate({ backgroundPosition: -moveBGPX }, 800);
    $('p:visible', $('.c-featured')).hide();
    $('li', $('.c-featured')).find('span').hide();
    $('li.active', $('.c-featured')).removeClass('active');
    $('.i' + index, $('.c-featured')).find('p').show();
    $('.i' + index, $('.c-featured')).addClass('active');
    $('.i' + index, $('.c-featured')).find('span').show();
    var jOThisAnchor = $('a', $('.i' + index));
    var bgColor = jOThisAnchor.css('background-color');
    $('.s-i-bg').css('background-color', bgColor);



}
var initialLoad = true;
function loadContentFromHash(lastHash) {


    if (lastHash) {
        if (lastHash == '#/about-nndc/our-vision') {
            moveSlider(1);
        }
        if (lastHash == '#/key-locations/corby') {
            moveSlider(2);
        }
        if (lastHash == '#/key-locations/kettering') {
            moveSlider(3);
        }
        if (lastHash == '#/key-locations/rushden') {
            moveSlider(4);
        }
        if (lastHash == '#/key-locations/wellingborough') {
            moveSlider(5);
        }

        var url = lastHash.replace("#", "")
        url = url.replace("http", ""); //important to stop content injection
        url = url.replace("www", ""); //important to stop content injection

        $.get(url, function (data) {

            try {

                var mainContent = $('#mainContent', data);
                if (mainContent) {
                    $('#mainContent').html(mainContent.html());
                }


            } catch (ex) { }
        });
    } else {
        if (window.location.pathname == '/' && initialLoad == false) {
            moveSlider(1);
            $.get('/', function (data) {

                try {

                    var mainContent = $('#mainContent', data);
                    if (mainContent) {
                        $('#mainContent').html(mainContent.html());
                        initFlash();
                    }
                } catch (ex) { }
            });
        }


    }

    if (initialLoad == true) {
        initialLoad = false;
    }
}

loadContentFromHash(window.location.hash);
setInterval(pollHash, 100);


