$(document).ready(function(){

    var docked = null;
    var init = 320;           // How many pixels from the top is the sidebar be positioned upon page load
    var dockedTop = 150;      // Pixels from top of window to dock sidebar
    var dockThreshold = 150;  // How many pixels above viewport should document scroll before docking
    var undockThreshold = 170;// How many pixels above viewport should document scroll be before undocking


    $(document).scroll(function(){

        if ($('.sidebar').length == 0) return true;

        var window_top = $(document).scrollTop();
        var menu_top = $('.sidebar').offset().top;
        
        if (!docked && (window_top >= dockThreshold)) {
            docked = true;
            $('.sidebar').css('top',dockedTop+'px');
            $('.sidebar').addClass('docked');
        } else if (docked && window_top <= undockThreshold) {
            docked = false;
            $('.sidebar').css('top',init+'px');
            $('.sidebar').removeClass('docked');
        }

    });



    $('.ql-handle').click(function(){
        var parent = $(this).parents('.sidebar');
        if ($(this).hasClass('open')) {
            parent.animate({'margin-left':'0px'});
            $(this).removeClass('open');
        } else {
            parent.animate({'margin-left':'-230px'});
            $(this).addClass('open');
        }
        return false;
    });



    var height1 = $('.sidebar .quicklinks').height()+11;
    $('.sidebar .quicklinks .ql-mid').height(height1);

    var height = $('.sidebar .promo').height()-59;
    $('.sidebar .promo .ql-mid').height(height);

});
