var get_books;
$(function() {
    var schoolOptLast = $('#school').val();
    $('#school')[$.browser.webkit ? 'change' : 'click'](function() {
        if($(this).val() != schoolOptLast) {
            schoolOptLast = $(this).val();
            var d = $('#depts').empty();
            
            if(schoolOptLast != '-') {
                $.each(depts[schoolOptLast]['depts'], function() {
                    var opt = $('<label><input type="checkbox" name="department[]"></label>');
                    opt.append(this[1] + ' (' + this[0] + ')').children('input').val(this[0]);
                    d.append(opt);
                });
            } else {
                var deptsx = [];
                $.each(depts, function(k, v) {
                    $.each(v['depts'], function() {
                        deptsx.push(this);
                    });
                });
                deptsx.sort(function(a, b) { if(a[0] == b[0]) return 0; return a[0] > b[0]; });
                $.each(deptsx, function() {
                    var opt = $('<label><input type="checkbox" name="department[]"></label>');
                    opt.append(this[1] + ' (' + this[0] + ')').children('input').val(this[0]);
                    d.append(opt);
                });
            }
        }
    });
    
    var creditSchoolOptLast = $('#credit-school').val();
    $('#credit-school')[$.browser.webkit ? 'change' : 'click'](function() {
        if($(this).val() != creditSchoolOptLast) {
            creditSchoolOptLast = $(this).val();
            var d = $('#credit-types').empty();
            
            if(creditSchoolOptLast != '-') {
                $.each(attributes[creditSchoolOptLast], function() {
                    var opt = $('<label><input type="checkbox" name="attributes[]"></label>');
                    opt.append(this+'').children('input').val(this);
                    d.append(opt);
                });
            } else {
                $.each(attributes, function(k, v) {
                    $.each(v, function() {
                        var opt = $('<label><input type="checkbox" name="attributes[]"></label>');
                        opt.append(this+'').children('input').val(this);
                        d.append(opt);
                    });
                });
            }
        }
    });
    
    $('.remove-instructor').hide();
    
    if((ins = $('.remove-instructor')).length > 1) {
        ins.slice(1, ins.length).show();
        $('.add-instructor').slice(0, ins.length - 1).hide();
    }
    
    $('.add-instructor').live('click', function() {
        $('.instructor').eq(-1).after(ni = $('.instructor').eq(0).clone(true)).find('.add-instructor').hide();
        ni.find('a').show();
        ni.find('input').val('');
        return false;
    });
    
    $('.remove-instructor').live('click', function() {
        $(this).parent().remove();
        if(!$('.add-instructor:visible').length)
            $('.add-instructor').eq(-1).show();
        return false;
    });
    
    $('.remove-building').hide();
    
    if((bldgs = $('.remove-building')).length > 1) {
        bldgs.slice(1, bldgs.length).show();
        $('.add-building').slice(0, bldgs.length - 1).hide();
    }
    
    $('.add-building').live('click', function() {
        $('.building').eq(-1).after(ni = $('.building').eq(0).clone(true)).find('.add-building').hide();
        ni.find('a').show();
        ni.find('input').val('');
        return false;
    });
    
    $('.remove-building').live('click', function() {
        $(this).parent().remove();
        if(!$('.add-building:visible').length)
            $('.add-building').eq(-1).show();
        return false;
    });
    
    $('.show-secs').live('click', function() {
        var btn = $(this);
        var coursediv = $(this).parents('div.course');
        var course = coursediv.attr('id').split('-').slice(1);
        
        var existing_sections = [];
        coursediv.find('td.section_id').each(function() {
            existing_sections.push($(this).html())
        });

        $(this).addClass('load');

        var url = '/course/' + year + '/' + semester + '/' + course[0] + '/' + course[1];
        $.post(url, {'exclude': existing_sections}, function(data) {
            btn.removeClass('load');
            btn.hide();
            coursediv.find('table').eq(0).append(data);
        }, 'html');
        
        return false;
    });
    
    $('.show-disc').live('click', function() {
        var btn = $(this);
        var coursediv = $(this).parents('div.course');
        var course = coursediv.attr('id').split('-').slice(1);
        
        var existing_sections = [];
        coursediv.find('td.section_id').each(function() {
            existing_sections.push($(this).html())
        });

        $(this).addClass('load');

        var url = '/course/' + year + '/' + semester + '/' + course[0] + '/' + course[1];
        $.post(url, {'labs': '1'}, function(data) {
            btn.removeClass('load');
            btn.hide();
            
            var ldg_table = coursediv.find('table').clone().insertAfter(coursediv.find('table').eq(0));
            ldg_table.find('tr:gt(0)').remove();
            ldg_table.append(data);
        }, 'html');
        
        return false;
    });
    
    var cmts = {};
    var cmt_manager = function() {
        var comments = $(this).parents('div.course').find('.comments');
        if(comments.css('height') == '0px') {
            cmts[comments.parents('div.course').attr('id')] = {'cmts': $(this).html()};
            $(this).html('<span>&uarr;</span> Hide Comments');
            comments.animate({height: comments[0].contentWindow.document.body.scrollHeight}, 500, function() {
                window.scrollTo(0, parseInt(comments.offset().top));
                
                cmts[comments.parents('div.course').attr('id')]['sz'] = comments[0].contentWindow.$('#cc').outerHeight() + 40;
                cmts[comments.parents('div.course').attr('id')]['int'] = setInterval(function() {
                    var disqus_height = comments[0].contentWindow.$('#cc').outerHeight() + 40;
                    if(cmts[comments.parents('div.course').attr('id')]['sz'] != disqus_height) {
                        cmts[comments.parents('div.course').attr('id')]['sz'] = disqus_height;
                        comments.animate({height: disqus_height}, 100);
                    }
                }, 150);
            });
        } else {
            $(this).html(cmts[$(this).parents('div.course').attr('id')]['cmts']);
            clearInterval(cmts[$(this).parents('div.course').attr('id')]['int']);
            comments.animate({height: '0'}, 500);
        }
    }
    $('.show-cmts').live('click', function() {
        var btn = $(this);
        var comments = $(this).parents('div.course').find('.comments');
        
        var course = btn.attr('id').split('-').slice(1);
        if(!comments[0].src.length) {
            btn.addClass('load');
            comments.load(function() {
                btn.removeClass('load');
                cmt_manager.call(btn);
            })[0].src = '/course_comments/' + course[0] + '/' + course[1];
        } else {
            cmt_manager.call(btn);
        }
        return false;
    });
    
    $('a[href="#hide"]').live('click', function() {
        var a = $(this);
        $('.search-info').slideUp(200, function() {
            $('.search-info').prev('input').animate({'margin-bottom': '20px'}, 100);
        });
        $.cookie('hide_hints', true, {expires: 365, path: '/'});
        return false;
    });
    
    $('a#hs-search').click(function() {
        if($('.section').eq(0).css('display') == 'none') {
            $('.section').slideDown();
            $(this).html('&uarr; Hide Search Form');
        } else {
            var sections = $('.section');
            sections.slice(0, sections.length - 1).slideUp();
            $(this).html('&darr; Show Search Form');
        }
        
        return false;
    });
    
    get_books = function() {
        var args = Array.prototype.slice.call(arguments);
        $('#books-' + args.join('-')).load('course/' + args.join('/'));
    }
});