var M_START = 2, M_CHECK_INTERVAL = 500;
var m_menuInterval, m_scrolling = false, m_lastY = 0, m_counter = 0, m_start_scrolling_after=316;

jQuery(function() {
    m_menuInterval = window.setInterval(scrollCheck, M_CHECK_INTERVAL);
});

/* Taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow; modified */
function getScrollY() {
    var scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;

}

function scrollCheck() {
    var s_y;

    if (m_scrolling) {
        m_counter = 1;
    }
    s_y = getScrollY();
    if (s_y != m_lastY) {
        m_scrolling = true;
        m_counter = 0;
    } else {
        m_scrolling = false;
    }

    if (m_counter > 0) {
        if (++m_counter == M_START) {
            m_counter = 0;
            try_animate_right_menu();
        }
    }
    m_lastY = s_y;
}

function try_animate_right_menu() {
    var m_y, diff;

    m_start_scrolling_after = parseInt(jQuery("#wrapper").position().top);
    m_y = parseInt(jQuery("#sidebar").css("top"));
    diff = m_lastY - m_start_scrolling_after;
    //console.log("Diff is " + diff);
    /* if (diff > 0) {
        //console.log("Should move menu to " + diff);
        jQuery("#sidebar").css({top: diff + "px"});
    } else {
        jquery("#sidebar").css({top: "0px"});
    } */
    animate_right_menu(diff);
    //console.log("Menu is at " + m_y + ", scroll at " + m_lastY);
}

function animate_right_menu(diff) {
    var m_size, c_size, v_y, m_position;

    m_size = parseInt(jQuery("#sidebar").css("height"))
    c_size = parseInt(jQuery("#wrapper").css("height"));
    //((diff > 0) ? diff : 0)
    if (diff < 0) {
        diff = 0;
    } else if (m_size + diff > c_size) {
        diff = c_size - m_size;
    }
    m_position = parseInt(jQuery("#sidebar").css("top"));
    v_y = getVisibleSizeY();
    if (m_position > diff) {
        if (m_position + m_start_scrolling_after > m_lastY + v_y) {
            // tweak the position
            jQuery("#sidebar").css({top: (m_lastY + v_y - m_start_scrolling_after)});
        }
    } else {
        if (m_position + m_start_scrolling_after + m_size <= m_lastY) {
            // tweak the position
            jQuery("#sidebar").css({top: (m_lastY - m_size - m_start_scrolling_after)});
        }
    }
    //jQuery("#sidebar").css({top: ((diff > 0) ? diff : 0) + "px"})
    jQuery("#sidebar").animate({top: diff}, 2000, 'easeOutBack');
}

/* Taken from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow */
function getVisibleSizeY() {
    var myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}
