﻿function checkScrollbar(clear) {
    var content = $("div.content");
    var outer = $("#scrollframe");
    var inner = $("#innerScrollframe");

    var scrollbarWidth = 10;
    var scrollbarHeight = parseInt(outer.height(), 10) + 5;
    var scrollerHeight = 29;
    var scrollerWidth = 10;
    var diff = parseInt(inner.height(), 10) - parseInt(outer.height(), 10);

    if (clear) {      
        $("#dragger,#dragger_container").remove();
    }
    
    if (diff > 0 && outer.height() > 0) {
        $("#dragger,#dragger_container").remove();

        outer.parent().append($('<div></div>')
                      .attr("id", "dragger_container")
                      .css("width", scrollbarWidth)
                      .css("height", scrollbarHeight)
                      .append($('<div></div>').attr("id","dragger")
                                .css("height", scrollerHeight)
                                .css("width", scrollerWidth)
                                .css("top","0px")));
        inner.css("top", "0");
        inner.css("position", "absolute");
        inner.css("padding-right", "15px");

        diff = parseInt(inner.height(), 10) - parseInt(outer.height(), 10);

        $("#dragger").draggable({
            axis: 'y',
            containment: 'parent',
            drag: function () {
                var cPos = parseInt($("#dragger").css("top"), 10);
                var quot = diff / parseInt(outer.height(), 10);
                var nPos = parseInt(cPos * quot * 1.03 + 0.5, 10) - parseInt($("p:first").css("padding-bottom"));
                if (nPos < 0) nPos = 0;
                inner.css("top", "-" + nPos + "px");
            }
        });
    } else {
        $("#dragger,#dragger_container").remove();
        inner.css("top", "0");
        inner.css("padding-right", "0");    
    }
}
