
(function($){
$.fn.lightbox = function(){
    var state = false;

    $(document).keyup(function(e) { if (e.keyCode == 27 && $('#lightbox').length > 0) hide(); });
    $('#lightbox-close').click(function(e) { if ($('#lightbox').length > 0) hide(); });

    this.each(function(i) {
        $($(this).attr('href')).hide();
        $(this).click(show);
    });

    return this;

    function get_window_size() {
        return {
            width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
            height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight),
            y: window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop)
        }; 
    }

    function show(e) {
        if (state) return false;
        state = true;

        var window_size = get_window_size();

        var content_div = $($(this).attr('href'));
        var width = $(document).width() * .65;
        var height = (content_div.height()) + 60;

        var newLeft = (window_size.width - width) / 2;
        var newTop = (window_size.height - height) / 2;
        if ($('#lightbox').css('position') == 'absolute') {
            newTop += window_size.y;
            var offset = $('.page-title:first').offset().top;
            if (newTop < offset)
                newTop = offset;
        }

        var curLeft = window_size.width/2;
        var curTop = window_size.height/2;

        var div = document.createElement('div');
        div.setAttribute('id', 'overlay');
        $('#directory-content').append(div);

        $('#lightbox').hide().css({ top: curTop, left: curLeft, width: '1px', height: '1px' });
        $('#lightbox-content').html('');
        $('#lightbox').animate({ opacity: "show", width: width, height: height, top: newTop, left: newLeft }, 250, null, function() { $('#lightbox-content').html(content_div.html()); $('#results-wrapper').addClass('hidden-for-print'); state = false; });

        return false;
    }


    function hide() {
        if (state) return false;
        state = true;

        var window_size = get_window_size();
        var newTop = window_size.height/2;
        var newLeft = window_size.width/2;

        $('#overlay').detach();
        $('#lightbox').unbind('click');
        $('#lightbox-content').html('');
        $('#lightbox').animate({ opacity: "hide", width: '1px', height: '1px', top: newTop, left: newLeft }, 250, null, function() { $('#results-wrapper').removeClass('hidden-for-print'); state = false; });
        return false;
    }
}
})(jQuery);



$(document).ready(function() {
    $('a[rel=lightbox]').lightbox();

    $('#querytype-person').click(function(){ $('#school-department').hide(); $('#school-person').show(); });
    $('#querytype-department').click(function(){ $('#school-person').hide(); $('#school-department').show(); });

    $('#toggle-advanced-search').click(function(){ $('#search-box-basic').hide(); $('#search-box-advanced').show(); return false; });
    $('#toggle-basic-search').click(function(){ $('#search-box-advanced').hide(); $('#search-box-basic').show(); return false; });

    $('#toggle-advanced-querytype-person').click(function(){ $('#advanced-querytype-department').hide(); $('#advanced-querytype-person').show(); return false; });
    $('#toggle-advanced-querytype-department').click(function(){ $('#advanced-querytype-person').hide(); $('#advanced-querytype-department').show(); return false; });

    $('#q').focus();
});



