var popover_windowWidth = 0;
var popover_windowHeight = 0;
var popover_hl;
var popover_debug = 0;
var popover_debug_messages = true;
var popover_is_moving = false;
var popover_base_y = 0;
var popover_base_x = 0;
var popover_obj;
var popover_x1 = 0, popover_y1 = 0, popover_x2 = 0, popover_y2 = 0;
var popover_width = 0, popover_height = 0;
var popover_speed = 0;
var popover_inited = false;
var popover_scrollbars_width = 18;
var popover_cx = 0, popover_cy = 0;
function popover_init(el)
{
    if (!el)
        return false;
    popover_obj = el;
    var oldHtml = el.innerHTML;
    el.innerHTML = '<textarea id="SR_scrollbarsTestElement">XXX</textarea>';
    var st = document.getElementById('SR_scrollbarsTestElement');
    if (st)
    {
        st.wrap = 'off';
        popover_scrollbars_width = st.offsetHeight;
        st.wrap = 'soft';
        popover_scrollbars_width = popover_scrollbars_width - st.offsetHeight;
        if (popover_scrollbars_width <= 0)
            popover_scrollbars_width = 18;
    }
    el.innerHTML = oldHtml;
    delete st, oldHtml;
    popover_windowWidth = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
    popover_windowWidth -= popover_scrollbars_width;
    popover_windowHeight = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
    popover_windowHeight -= 4;//popover_scrollbars_width + 4;
    popover_width = parseInt(popover_obj.style.width);
    popover_height = parseInt(popover_obj.style.height);
    popover_inited = true;
    return true;
}

var popover_margin_top = 0,
    popover_margin_right = 0,
    popover_margin_bottom = 0,
    popover_margin_left = 0;
function popover_set_margins(top, right, bottom, left)
{
    popover_margin_top = top;
    popover_margin_right = right;
    popover_margin_bottom = bottom;
    popover_margin_left = left;
}

function popover_coord(val, f_asis)
{
    val = val.toString();
    if (!isNaN(parseInt(val)))
        return parseInt(val);
    var mtop = popover_margin_top,
        mright = popover_margin_right,
        mbottom = popover_margin_bottom,
        mleft = popover_margin_left,
        swidth = popover_scrollbars_width;
    if (!(!f_asis))
        mtop = mright = mbottom = mleft = swidth = 0;
    switch (val.toLowerCase())
    {
        case 'left': val = 0 + popover_margin_left; break;
        case 'center':
            val = mleft + parseInt((popover_windowWidth -
                mright - mleft - popover_width - swidth) / 2);
            break;
        case 'right':
            val = parseInt(popover_windowWidth - popover_width - mright);
            break;
        case 'top': val = mtop; break;
        case 'middle':
            val = mtop +
                parseInt((popover_windowHeight -
                    mbottom - mtop - popover_height) / 2);
                break;
        case 'bottom': val = popover_windowHeight - popover_height - mbottom; break;
        case 'width': val = popover_width; break;
        case 'height': val = popover_height; break;
        case 'windowheight': val = popover_windowHeight; break;
        case 'windowwidth': val = popover_windowWidth; break;
        case 'window-width': val = 0 - popover_width; break;
        case 'window-height': val = 0 - popover_height; break;
    }
    return val;
}

function popover_start_to(id, speed, f_jump, x2, y2, func, f_invert)
{
    var x1 = parseInt(popover_obj.style.left) - popover_base_x;
    var y1 = parseInt(popover_obj.style.top) - popover_base_y;
    popover_motion = true;
    popover_start(id, speed, f_jump, x1, y1, x2, y2, 1, func, f_invert);
}

popover_is_visible = false;
popover_motion = false;
function popover_start(id, speed, f_jump, x1, y1, x2, y2, f_coordsAsIs, func, f_invert)
{
    if (popover_obj &&
        popover_is_visible &&
        !popover_motion)
        popover_close();
    popover_motion = false;

    if (!popover_init(document.getElementById(id)))
    {
        if (popover_debug_messages)
            alert('PopOver error: object not found.');
        return;
    }
    popover_is_visible = true;
    popover_speed = speed;
    document.body.onscroll = popover_onscroll;
    window.onscroll = popover_onscroll;
    popover_base_y = popover_base_vertical();
    var elWidth = popover_width;
    var elHeight = popover_height;
    if (!x2 &&
        isNaN(x2))
        x2 = x1;
    if (!y2 &&
        isNaN(y2))
        y2 = y1;
    popover_x1 = x1 = popover_coord(x1, f_coordsAsIs);
    popover_y1 = y1 = popover_coord(y1, f_coordsAsIs);
    popover_x2 = x2 = popover_coord(x2, f_coordsAsIs);
    popover_y2 = y2 = popover_coord(y2, f_coordsAsIs);
    if (popover_debug)
        alert('Press any key to start PopOver from ' + x1 + ',' + y1 + ' to ' + x2 + ',' + y2 + ', speed ' + speed +
            "\nmargins: " + popover_margin_top + ',' + popover_margin_right + ',' + popover_margin_bottom + ',' + popover_margin_left);
    if (popover_hl)
        window.clearTimeout(popover_hl);
    popover_move(x1, y1, x2, y2, speed, f_jump, func, f_invert);
}

function popover_move(x1, y1, x2, y2, speed, f_jump, func, f_invert)
{
    if (!func)
    {
        var k, b;
        if (Math.abs(x2 - x1) < Math.abs(y2 - y1))
        {
            f_invert = true;
            if (x1 == x2)
            {
                func = x1;
            }
            else
            {
                k = (y2 - y1) / (x2 - x1);
                b = y1 - k * x1;
                func = '(py - (' + b + ')) / (' + k + ')';
            }
        }
        else
        {
            if (y1 == y2)
            {
                func = y1;
            }
            else
            {
                f_invert = false;
                k = (y2 - y1) / (x2 - x1);
                b = y1 - k * x1;
                func = 'px * (' + k + ') + (' + b + ')';
            }
        }
    }
    if (!f_invert)
        popover_move_straight(x1, y1, x2, y2, speed, func, 'popover_move_end(' + f_jump + ')');
    else
        popover_move_invert(x1, y1, x2, y2, speed, func, 'popover_move_end(' + f_jump + ')');
}

function popover_move_end(f_jump)
{
    popover_obj.style.top = popover_base_y + popover_y2;
    popover_obj.style.left = popover_x2;
    if (f_jump)
        popover_harmonic_jumps(popover_speed, popover_x2, popover_y2);
    else
        popover_move_stop();
}

function popover_harmonic_jumps(speed, x, y, f_rcall, timePassed)
{
    if (!popover_obj)
        return;
    popover_move_stop();
    return;
    var step = speed > 0 ? -1 : 1;
    var cx, cy, px, py;
    cx = px = x;
    cy = py = y;
    var smoothx = 2, csmoothx = 0,
        smoothy = 2, csmoothy = 0;
    var delay = 1;
    var w = 1.5;
    var str = 'speed=' + speed + ', w = ' + w + "\n\n";
    for (x = 0; x < 10; x ++)
    {
        //sp = 20 * (Math.cos(w * x) + Math.sin(w * x)) * Math.exp(-1 * w * x);
        //sp = 20 * Math.sin(x) * Math.exp(w * x);
        //sp = w * Ma
        sp = speed - w * Math.pow(x, 2) / 2;
        str += x + ' : ' + sp + "\n";
    }
    alert(str);
    return;
}

function popover_move_straight(x1, y1, x2, y2, speed, func, endExec, f_rcall)
{
    if (!popover_obj)
        return;
    if (!f_rcall)
    {
        popover_obj.style.top = y1;
        popover_obj.style.left = x1;
        popover_obj.style.display = 'block';
    }
    if (!endExec)
        endExec = '';
    popover_is_moving = true;
    var step = x1 < x2 ? 1 : -1;
    var cx, cy, px, py;
    cx = px = x1;
    cy = py = y1;
    var smoothx = speed * 1, csmoothx = 0,
        smoothy = speed * 1, csmoothy = 0;
    var delay = 5;
    while(x1 != x2)
    {
        px += step;
        eval ('py = Math.round(' + func + ')');
        csmoothx = Math.abs(px - cx);
        csmoothy = Math.abs(py - cy);
        if (csmoothx < smoothx &&
            csmoothy < smoothy)
            continue;
        cx = px;
        cy = py;
        if (x1 < x2 &&
            cx > x2 ||
            x1 > x2 &&
            cx < x2 ||
            cx == x2)
            break;
        popover_cy = popover_obj.style.top = popover_base_y + cy;
        popover_cx = popover_obj.style.left = cx;
        popover_hl = window.setTimeout('popover_move_straight(' +
            cx + ', ' + cy + ', ' + x2 + ', ' + y2 +
            ', ' + speed + ', \'' + func + '\', \'' + endExec.replace(/'/g, '\\\'') + '\', 1)', delay);
        return;
    }
    if (endExec.length)
        eval(endExec);
}

function popover_move_invert(x1, y1, x2, y2, speed, func, endExec, f_rcall)
{
    if (!popover_obj)
        return;
    if (!f_rcall)
    {
        popover_obj.style.top = y1;
        popover_obj.style.left = x1;
        popover_obj.style.display = 'block';
    }
    if (!endExec)
        endExec = '';
    popover_is_moving = true;
    var step = y1 < y2 ? 1 : -1;
    var cx, cy, px, py;
    cx = px = x1;
    cy = py = y1;
    var smoothx = speed * 1, csmoothx = 0,
        smoothy = speed * 1, csmoothy = 0;
    var delay = 5;
    while(y1 != y2)
    {
        py += step;
        eval ('px = Math.round(' + func + ')');
        csmoothx = Math.abs(px - cx);
        csmoothy = Math.abs(py - cy);
        if (csmoothx < smoothx &&
            csmoothy < smoothy)
            continue;
        cx = px;
        cy = py;
        if (y1 < y2 &&
            cy > y2 ||
            y1 > y2 &&
            cy < y2 ||
            cy == y2)
            break;
        popover_cy = popover_obj.style.top = popover_base_y + cy;
        popover_cx = popover_obj.style.left = cx;
        popover_hl = window.setTimeout('popover_move_invert('+
            cx + ', ' + cy + ', ' + x2 + ', ' + y2 +
            ', ' + speed + ', \'' + func + '\', \'' + endExec.replace(/'/g, '\\\'') + '\', 1)', delay);
        return;
    }
    if (endExec.length)
        eval(endExec);
}

function popover_base_vertical()
{
    var y = window.pageYOffset ? window.pageYOffset : document.body.scrollTop;
    if (!y)
        y = 0;
    y = parseInt(y);
    return y;
}

var popover_move_stop_action;
function popover_move_stop()
{
    if (popover_hl)
        window.clearTimeout(popover_hl);
    popover_hl = '';
    popover_is_moving = false;
    if (popover_move_stop_action)
    {
        var act = popover_move_stop_action;
        popover_move_stop_action = '';
        eval(act);
        delete act;
    }
}

function popover_close_only()
{
    popover_close();
}

function popover_close(el, toExec)
{
    if (!popover_obj)
    {
        if (popover_debug)
            alert('PopOver error: object not found.');
        return;
    }
    if (el)
    {
        //el.onclick = popover_close_only;
        if (toExec)
        {
            popover_move_stop_action = 'popover_close_only()';
            eval(toExec);
            return;
        }
    }
    popover_move_stop();
    popover_obj.style.display = 'none';
    popover_is_visible = false;
}

function popover_onscroll()
{
    if (!popover_obj)
        return;
    var ny = popover_base_vertical();
    var diff = ny - popover_base_y;
    popover_base_y = ny;
    if (!popover_is_moving)
        popover_obj.style.top = parseInt(popover_obj.style.top) + diff;
}

var popover_drag_obj;
var popover_drag_x;
var popover_drag_y;
var popover_doc_init = false;
var popover_in_stop = false;
var popover_doc_onmousemove = '';
var popover_doc_onmouseup = '';
var popover_drag_target = '';
function popover_drag_start(event, el, id)
{
    if (popover_in_stop ||
        !(popover_drag_obj = document.getElementById(id)))
        return false;

    popover_move_stop_action = '';
    popover_move_stop();

    popover_doc_init = false;
    popover_drag_target = el;

    el.style.cursor = 'move';

    popover_drag_obj.onmousemove = popover_drag;
    popover_drag_obj.onmouseup = popover_drag_stop;
    
    popover_drag_x = event.screenX;
    popover_drag_y = event.screenY;
}

function popover_drag(ev)
{
    if (!ev)
        ev = event;
    
    if (!popover_drag_obj)
        return;
    if (!popover_doc_init)
    {
        popover_doc_onmousemove = document.onmousemove;
        popover_doc_onmouseup = document.onmouseup;
        document.onmousemove = popover_drag;
        document.onmouseup = popover_drag_stop;
        popover_doc_init = true;
    }

    var cx = ev.screenX, cy = ev.screenY;
    if (cx == popover_drag_x &&
        cy == popover_drag_y)
        return;

    popover_drag_target.style.cursor = 'move';

    popover_drag_obj.style.left = parseInt(popover_drag_obj.style.left) + parseInt(cx) - parseInt(popover_drag_x);
    popover_drag_x = cx;
    popover_drag_obj.style.top = parseInt(popover_drag_obj.style.top) + parseInt(cy) - parseInt(popover_drag_y);
    popover_drag_y = cy;
}

function popover_drag_stop()
{
    if (!popover_drag_obj)
        return;
    popover_in_stop = true;
    document.onmousemove = popover_doc_onmousemove;
    document.onmouseup = popover_doc_onmouseup;
    popover_doc_init = false;
    popover_drag_obj.onmousemove = '';
    popover_drag_obj.onmouseup = '';
    popover_drag_target.style.cursor = 'default';
    popover_drag_obj = '';
    popover_drag_target = '';
    popover_in_stop = false;
}

function popover_check_cookies(par)
{
    if (par < 0)
        return true;
    var clist = document.cookie.split(';');
    var viewedCnt, newClist = '', k;
    if (clist.length)
        for (k in clist)
            if (viewedCnt = clist[k].match(/^SRpopoverCnt=([0-9]+)/))
            {
                viewedCnt = parseInt(viewedCnt[1]);
                break;
            }
    if (!viewedCnt)
        viewedCnt = 0;
    if (viewedCnt >= par)
        return false;
    viewedCnt ++;
    var d = new Date();
    d.setTime(d.getTime() + 3600 * 24 * 180 * 1000);
    document.cookie = 'SRpopoverCnt=' + viewedCnt + '; expires=' + d.toGMTString();
    delete d;
    return true;
}
