if (typeof $ === 'function') {
    /* Fix up the Google CSE search box so that it posts to our site instead of google.com */
    $('#site-search').prepend('<input type="hidden" name="cof" value="FORID:10" />').attr('action', document.location.protocol + '//www.utexas.edu/law/search/');

    /* Add some default text to the Search Box */
    $('#site-search-box').val('Search this site…').click(function () {
        $(this).val('').css('color', 'black');
    });

    $('#site-search-button').click(function () {
        if ($('#site-search-box').val() === 'Search this site…') {
            $('#site-search-box').val('');
        }
    });

    /* Create the "Info for..." dropdown functionality.
       Applying a click handler to a site-wide <div> prevents copy-and-pasting on iOS devices, hence the conditional */
    if (navigator.userAgent.match(/iPad|iPhone|iPod/i) === null) {
        $('#branding, #utlaw-content-wrapper').click(function () {
            $('#main-menu-audiences-list').hide();
            $('#main-menu-audiences').removeClass('opened');
        });
    } else {
        // iOS Safari Mobile gets this event handler instead
        document.addEventListener('touchstart', function () {
            $('#main-menu-audiences-list').hide();
            $('#main-menu-audiences').removeClass('opened');
        }, false);
    }
    $('#main-menu-audiences>a').click(function () {
        $('#main-menu-audiences-list').slideToggle('fast');
        $('#main-menu-audiences').toggleClass('opened');
        return false;
    });

    /* If the window is taller than the page content, set the footer to 'pin' to the bottom of the window */
    if ($('body').outerHeight(true) < $(window).height()) {
        wrapper = $('#utlaw-content-wrapper');
        wrapper.css('min-height', wrapper.height() + ($(document).height() - $('body').outerHeight()) + 'px');
    }
}

/* G O O G L E   A N A L Y T I C S */
if (document.domain.indexOf('wwwtest') < 0) {
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-12076726-1'],
              ['_setAllowHash', false],
              ['_setDomainName', '.utexas.edu'],
              ['_trackPageview'],
              ['ut._setAccount', 'UA-11887171-2'],
              ['ut._setAllowHash', false],
              ['ut._setDomainName', '.utexas.edu'],
              ['ut._trackPageview']
    );

    (function () {
        var ga   = document.createElement('script'),
            s    = document.getElementsByTagName('script')[0];

        ga.type  = 'text/javascript';
        ga.async = true;
        ga.src   = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

        s.parentNode.insertBefore(ga, s);
    }());
}
