var current_offset=0;
var threshold_lines=20;
var pressed = false;

function nothing() {
    return;
}

function align_layout() {
    align_subnav();
    align_sidebar();
}

function align_sidebar() {
    if ($('sidebar').offsetHeight < 446) {
        $('sidebar').style.height="446px";        
    }
    
    if ($('right').offsetHeight < $('content_main').offsetHeight) {
        $('right').style.height = $('content_main').offsetHeight+"px";
        $('sidebar').style.height = $('content_main').offsetHeight-$('content_print').offsetHeight+"px";
        $('content_print').style.marginTop = $('content_main').offsetHeight - $('sidebar').offsetHeight - $('content_print').offsetHeight+"px";
    }
}

function align_subnav() {
    var content_height = $('content').offsetHeight;
    var subnav_height = $('subnav').offsetHeight;
    var i = 0;
    try {
        var measurement = $('subnav_content').select("a.measurement")[0];    
    } catch (e) {
        var measurement = $('subnav_selected');    
    }
    
    if (content_height > subnav_height) {
        $('subnav').style.height = content_height+"px";
        $('subnav_content').style.height = content_height-78+"px";
        i = 78;
        while ($('subnav_content').offsetHeight % $('subnav_selected').offsetHeight) {
            i = i +1;
            $('subnav_content').style.height = content_height-i+"px";
        }                   
    }
            
    if ($('page_length').value > $('subnav_content').offsetHeight/measurement.offsetHeight) {
        $('move_up').style.visibility = "visible";
        $('move_down').style.visibility = "visible";
    }
            
    threshold_lines = ($('subnav_content').offsetHeight/measurement.offsetHeight)/2;
    var current_pos = $('subnav_selected').offsetTop;       
    var threshold = threshold_lines * measurement.offsetHeight;
    
    if (current_pos > threshold) {
        var diff = current_pos - threshold;        
        var new_pos = 0;
        while (new_pos < diff)
            new_pos = new_pos + $('subnav_selected').offsetHeight;
        //$('subnav_content').scrollTop = diff + $('subnav_selected').offsetHeight/2;
        $('subnav_content').scrollTop = new_pos;
    }
}

function move_up(pe) {
    if (!pressed) {
        pe.stop();
        return;
    }
        
    var step = $('subnav_selected').offsetHeight;
    
    if ($('subnav_content').scrollTop - step > 0)    
        $('subnav_content').scrollTop = $('subnav_content').scrollTop - step;
    else
        $('subnav_content').scrollTop = 0;        
}

function move_down(pe) {
    if (!pressed) {
        pe.stop();
        return;
    }
    
    var step = $('subnav_selected').offsetHeight;
    
    $('subnav_content').scrollTop = $('subnav_content').scrollTop + step;    
}

function up_subnav() {
    pressed = true;
    new PeriodicalExecuter(move_up, 0.1);    
}

function down_subnav() {
    pressed = true;
    new PeriodicalExecuter(move_down, 0.1);        
}

function stop_subnav() {
    pressed = false;    
}

window.onload = align_layout;
