jQuery(function ($) {
    /**
     * Get an object of properties sniffed from WordPress's body_class() function.
     *
     * Should be called on DOMContentLoaded
     *
     * @return Object
     */
    function getWP(body) {
        var i, m, n, WP = {}, c = body.className, a = c.split(/\s+/);
        WP.hasClass = {};
        while (n = a.pop()) {
            WP.hasClass[n] = 1;
        }
        WP.page = {
            id: (m = c.match(/(^|\s)page-id-(\d+)($|\s)/)) ? m[2] : null,
            template: (m = c.match(/(^|\s)page-template-(\S+)($|\s)/)) ? m[2] : null,
            parent: (m = c.match(/(^|\s)parent-pageid-(\S+)($|\s)/)) ? m[2] : null
        };
        WP.post = {
            id: (m = c.match(/(^|\s)postid-(\d+)($|\s)/)) ? m[2] : null
        };
        WP.subsite = (m = c.match(/(^|\s)subsite-(\S+)($|\s)/)) ? m[2] : null;
        WP.category = (m = c.match(/(^|\s)category-(\S+)($|\s)/)) ? m[2] : null;
        WP.isSingle = !!WP.hasClass.single;
        WP.isArchive = !!WP.hasClass.archive;
        WP.isHome = !!WP.hasClass.home;
        WP.isPage = !!WP.page.id;
        WP.isPost = !!WP.post.id;
        WP.loggedIn = !!WP.hasClass['logged-in'];
        return WP;
    }
    window.WP = getWP(document.body);

    // page-accordion functionality (on DOMReady)
    $('div.page-accordion').each(function () {
        var that = this;
        var visible = 0;
        var expand = $(this).hasClass('page-accordion-e');
        // add show button and get H3.id in one step
        var h3Id = $('h3.page-accordion-heading', this).prepend('<span class="page-accordion-show">show</span>&nbsp;')[0].id;
        $(this)[0].className = 'page-accordion-js page-accordion-hidden';
        $('div.page-accordion-content', this)
        	.append('<div class="page-accordion-content-bottom"><span>hide</span></div>')
        	.children().first().each(function () {
        		// wpautop writes invalid markup, that creates an empty P in IE's DOM.
        		// we strip this P because it creates unwanted vertical space
        		if (this.nodeName == 'P' && !this.hasChildNodes()) {
            		$(this).remove();
            	}
        	});
        function toggle() {
        	visible = !visible;
            $('span.page-accordion-show', that).html(visible ? 'hide' : 'show');
            $(that).toggleClass('page-accordion-hidden page-accordion-visible');
            // must trigger re-layout for IE7 or IE8 in compat mode
            // @link http://stackoverflow.com/questions/1702399/how-can-i-force-reflow-after-dhtml-change-in-ie7/1702485#1702485
            document.body.className += '';
        }
        $('span.page-accordion-show, div.page-accordion-content-bottom span', this).click(toggle);
        // allow expand by settings or if page loaded to the heading's id
        if (expand || location.hash == ('#' + h3Id)) {
        	// not elegant to flash closed then reopen, but it'll do for now
        	toggle();
        }
    });

    // decorate subsite home link, if on top, and if no menu title visible
    $('#child-menu ul.menu > li:first-child > a').each(function () {
        if ($(this.parentNode.parentNode.parentNode).prev('h2').length) {
            // has a menu title
            return;
        }
        if (WP.subsite !== null) {
            m = this.href.match(/^https?:\/\/[^\/]+\/([^\/]+)\/$/);
            if (m && m[1] === WP.subsite) {
                this.className += ' menu-subsite-home';
                this.parentNode.className += ' menu-subsite-home-li';
            }
        }
    });

    // for recursively hiding submenus
    function handleMenuUl(ul, show) {
        if (! show) {
            $(ul).addClass('hide-menu-ul');
        } else {
            $(ul).children().each(function (i) {
                var $li = $(this);
                var $childUl = $li.children('ul');
                if ($childUl.length) {
                    var showChildUl = 0;
                    var isCurrent = $li.hasClass('current-menu-item');
                    var containsCurrent = !!$li.find('li.current-menu-item').length;
                    if (isCurrent || containsCurrent) {
                        showChildUl = 1;
                    }
                    handleMenuUl($childUl[0], showChildUl);
                }
            });
        }
    }

    var $menu = $('#child-menu ul.menu');
    if ($menu.length) {
        // recursively walk menu ULs
        handleMenuUl($menu[0], 1);
    }

    // create WP.templateUrl
    $('#themeStyleLink').each(function () {
        var pieces = this.href.split('/');
        pieces.pop();
        WP.templateUrl = pieces.join('/');
    });

    if ($('p.editThis').get(0)) {
        $.ajax({
          url: WP.templateUrl + '/editThis.js',
          dataType: 'script',
          cache: true
        });
    }
    
    if (location.hash == '#great-job') {
        jQuery.getScript("http://onlinetest.education.ufl.edu/shared/js/great-job/go.js");
    }
})
