
/* Script by: www.jtricks.com
 * Version: 20060303
 * Latest version:
 * www.jtricks.com/javascript/navigation/floating.html
 * Modified by Phillip Temple:
 * now uses an init function
 * possible to separate into js file
 * put in a header at top of page above which menu won't rise
 * enable multiple menus with arbiraty DIV id
 */

var fm_max_menus = 5;
var fm_ptr = 0;
var target_x = new Array(fm_max_menus);
var target_y = new Array(fm_max_menus);
var target_h = new Array(fm_max_menus);
var fm_id = new Array(fm_max_menus);
var fm_dom = new Array(fm_max_menus);

var has_inner = typeof(window.innerWidth) == 'number';
var has_element = document.documentElement && document.documentElement.clientWidth;

var fm_shift_x = new Array(fm_max_menus);
var fm_shift_y = new Array(fm_max_menus);
var fm_next_x = new Array(fm_max_menus);
var fm_next_y = new Array(fm_max_menus);

function move_menu(num)
{
    if (document.layers)
    {
        fm_dom[num].left = fm_next_x[num];
        fm_dom[num].top = fm_next_y[num];
    }
    else
    {
        fm_dom[num].style.left = fm_next_x[num] + 'px';
        fm_dom[num].style.top = fm_next_y[num] + 'px';
    }
}

function compute_shifts(num)
{
    fm_shift_x[num] = has_inner
        ? pageXOffset
        : has_element
          ? document.documentElement.scrollLeft
          : document.body.scrollLeft;
    if (target_x[num] < 0)
        fm_shift_x[num] += has_inner
            ? window.innerWidth
            : has_element
              ? document.documentElement.clientWidth
              : document.body.clientWidth;

    fm_shift_y[num] = has_inner
        ? pageYOffset
        : has_element
          ? document.documentElement.scrollTop
          : document.body.scrollTop;
    if (target_y[num] < 0)
        fm_shift_y[num] += has_inner
            ? window.innerHeight
            : has_element
              ? document.documentElement.clientHeight
              : document.body.clientHeight;

    if (fm_shift_y[num] < target_h[num]) fm_shift_y[num] = target_h[num];
}

function float_menu(num)
{
    var step_x, step_y;

    compute_shifts(num);

    step_x = (fm_shift_x[num] + target_x[num] - fm_next_x[num]) * .07;
    if (Math.abs(step_x) < .5)
        step_x = fm_shift_x[num] + target_x[num] - fm_next_x[num];

    step_y = (fm_shift_y[num] + target_y[num] - fm_next_y[num]) * .07;
    if (Math.abs(step_y) < .5)
        step_y = fm_shift_y[num] + target_y[num] - fm_next_y[num];

    if (Math.abs(step_x) > 0 ||
        Math.abs(step_y) > 0)
    {
        fm_next_x[num] += step_x;
        fm_next_y[num] += step_y;
        move_menu(num);
    }

    setTimeout(('float_menu('+num+')'), 20);
}

function init_floating_menu(div_id, x,y,h)
{
    target_x[fm_ptr] = x;
    target_y[fm_ptr] = y;
    target_h[fm_ptr] = h;
    fm_id[fm_ptr] = div_id;
    fm_dom[fm_ptr] =
        document.getElementById
        ? document.getElementById(div_id)
        : document.all
          ? document.all[div_id]
          : document.layers[div_id];

    compute_shifts(fm_ptr);
    if (document.layers)
    {
        // Netscape 4 cannot perform init move
        // when the page loads.
        fm_next_x[fm_ptr] = 0;
        fm_next_y[fm_ptr] = 0;
    }
    else
    {
        fm_next_x[fm_ptr] = fm_shift_x[fm_ptr] + target_x[fm_ptr];
        fm_next_y[fm_ptr] = fm_shift_y[fm_ptr] + target_y[fm_ptr];
        move_menu(fm_ptr);
    }
    float_menu(fm_ptr);
    fm_ptr++;
}

// Toggle an element style between hidden and 'block'
function toggleView(elementName) {
    current = document.getElementById(elementName).style.display;
    if (current == 'none' || current == '') document.getElementById(elementName).style.display = 'block';
    else document.getElementById(elementName).style.display = 'none';
}

function showView(elementName) {
    current = document.getElementById(elementName).style.display;
    if (current == 'none' || current == '') document.getElementById(elementName).style.display = 'block';
}

function hideView(elementName) {
    document.getElementById(elementName).style.display = 'none';
}
