/**
  Dependencies: Jquery
  See: quotes.js for quotes
  Global: btb
**/

(function ($) {

    // Global
    btb = {

        base: '..',

        init: function () {

            var homepage;

            if($('body#home').length) {
                homepage = true;
                this.base = '.';
            }

            // A touch of class
            this._pageStyles();

            // Quote block
            var $quote = $('<div id="hp_quote"><div class="quote"></div><div class="author"></div></div>');

            // Apply the page template (but not for the homepage)
            if(!homepage) {
                // Prevent flash of unformatted text
                $('body').hide();
                this._template($quote);
            }
            else {
                $('#header').append($quote);
                this._headerBackground();
            }
            
            // Assign random quote
            this._randomQuote($quote);

        },

        _pageStyles: function() {
            //$('p:first').addClass('first');
            $('h2').not(':first').before('<hr />');
        },

        _headerBackground: function() {
            // Cookie is used to set the header image for the user session
            var rand = get_cookie('style-header-bg');
            if (!rand) {
                rand = Math.floor(Math.random() * 5 ) + 1;
                document.cookie = "style-header-bg=" + rand + ";";
            }
            var imgPath = this.base + '/theme/img/montage_' + rand + '.jpg';
            $('#header')
            .css('background-image', 'url('+ imgPath +')');
        },

        _template: function ($quote) {
            $.get('../theme/tmpl/template.html', function(template) {
                $('a:first').remove(); // Home link for non JS users
                $('body').wrapInner('<div />');
                $('body').append(template);
                // Shuffle the content into place
                $('#page_content').append($('body div:first'));
                $('#header').append($quote); // Place quote

                btb._headerBackground();

                $('body').show();

                // Place content in sidebars (if required)
                btb._sidebars();

                // Indicate the active path
                var cur = $(location).attr('href');
                $('ul.toplevel a').not(':first').filter( function (item) {
                    return (cur.search($(this).attr('href')) > -1)
                }).addClass('current').wrap('<strong />');

            });
        },

        _randomQuote: function($quote) {
            $.getJSON(this.base + '/theme/data/quotes.js', function(data) {
                var rand = Math.floor(Math.random()*data.quotes.length);
                $('.quote', $quote).text(data.quotes[rand].quote);
                $('.author', $quote).text(data.quotes[rand].author);
            });
        },

        _sidebars: function() {

            var path = $(location).attr('href');

            // About page
            (path.search('/about/') > -1)
            && $.get(this.base + '/theme/inc/sidebar_about.html', function(data) {
                $(data).appendTo('#page_left');
            });

            // Funders page
            (path.search('/funders/') > -1)
            && $.get(this.base + '/theme/inc/sidebar_funders.html', function(data) {
                $(data).appendTo('#page_left');
            });

            // Contact page
            (path.search('/contact/') > -1)
            && $.get(this.base + '/theme/inc/sidebar_contact.html', function(data) {
                $(data).appendTo('#page_left');
            });
            
            // Links page
            (path.search('/links/') > -1)
            && $.get(this.base + '/theme/inc/sidebar_links.html', function(data) {
                $(data).appendTo('#page_left');
            });

            // Publications section move sub nav into sidebar
            (path.search('/publications/') > -1)
                 && $('#page_left')
                    .empty()
                    .append('<h6 class="accessibility">in publications:</h6>')
                    .append($('#nav_publications'))
                    .wrapInner('<div />');

            // Resources section move sub nav into sidebar
            (path.search('/resources/') > -1)
                && $('#page_left')
                    .empty()
                    .append('<h6 class="accessibility">in resources:</h6>')
                    .append($('#nav_resources'))
                    .wrapInner('<div />');

        }

    };


    function get_cookie(cookie_name)
    {
        var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

        if (results) {
            return (unescape(results[2]));
        }
        else {
            return null;
        }
    }


})(jQuery);


jQuery(document).ready(function(){
    btb.init();
});


// Google tracking code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5894921-1']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


