﻿function AbsLeft(elem)
{
    return elem.offsetLeft + ((elem.offsetParent != null) ? AbsLeft(elem.offsetParent) : 0);
}

function AbsTop(elem)
{
    return elem.offsetTop + ((elem.offsetParent != null) ? AbsTop(elem.offsetParent) : 0);
}

function VMLPos(base, left, top, width, height)
{
    var w = "", h = "";
    var left = ";left:" + (left<0?(base.left+base.width-2-left):(base.left+left)) + "px";
    var top = ";top:" + (top<0?(base.top+base.height-2-top):(base.top+top)) + "px";
    if (width != null)
        w = ";width:" + (width<=0?(base.width-width):width) + "px";
    if (height != null)
        h = ";height:" + (height<=0?(base.height-height):height) +"px;";
    return "position:absolute" + left + top + w + h + ";";
}

// use width to align text to the right
function VMLText(base, left, top, text, width)
{
    return "<v:rect strokecolor='none' fillcolor='none' style='" + 
        VMLPos(base, left, top, width != null ? width : Math.min(150, Graph.offsetWidth - left), 1) + 
        (width != null ? ";text-align:right" : "") + 
        "' filled = 'f' stroked = 'f'><v:fill></v:fill><v:stroke></v:stroke><v:textbox>" + text + "</v:textbox></v:rect>";
}

function RedrawGraph()
{
    disablePendingGraphRedraw = true;
    var s = [];
    var maxpace = Math.floor(getMaxPace() * FullPaceUnitsFactor + 1);
    var maxrest = Math.min(Math.floor((getMaxRestTime() + 15) / 60) + 1, 10);
    var g = { left: AbsLeft(Graph)+1, top: AbsTop(Graph)+1, width: Graph.offsetWidth-2, height: Graph.offsetHeight-2 };
    var c = { left: g.left+25, top: g.top+15, width: g.width-25-25-5-5, height: g.height-15-5-20 };
    //s.push("<v:rect fillcolor='gray' strokecolor='none' style='" + VMLPos(c,0,0,0,0) + "'></v:rect>");
    // horizontal distance x-axis
    //if (IsSwim)
    {
        var global = IsSwim ? getGlobalLaps() : (getGlobalDistance() / 1000);
        if (getGlobalSegments() == 1)
            s.push(VMLText(g, c.width / 2 + (IsSwim ? 7 : -15), c.height+15, IsSwim ? "Laps" : "Kilometers"));
        else
        {
            var step = 1;
            if (IsRun && global < 3)
                step = (global <= 1) ? 0.2 : 0.5;
            if (global > 8) step = 2;
            if (global > 10) step = Math.round(global / 5);
            if (step > 10) step = 20;
            var scaleend = global;
            if ((global % step) > 0 && (global % step) <= step / 2) 
                scaleend -= step;
            for (var i = step; i < scaleend; i += step)
            {
                // ticks on x distance axis
                s.push("<v:rect style='" + VMLPos(c, i * c.width / global, c.height-2, 1, 3) + "'></v:rect>"); 
                // distance captions
                s.push(VMLText(g, 
                    i * c.width / global + (IsSwim ? (17 - i.toString().length * 5) : 7), 
                    c.height+15, 
                    IsSwim ? i : i.toFixed(1)));
            }
        }
        s.push(VMLText(g, c.width - 7, c.height+15, IsSwim ? global : global.toFixed(1), 50));
    }
    //s.push(VMLText(g, g.width / 2 - 25, g.height - 27, "Laps"));
    // vertical pace left y-axis
    var step = Math.floor(maxpace / 10.0 + 1); // 1 to 10 is 1, 11 to 20 is 2, etc.
    maxpace = Math.floor((maxpace + step + 1) / step) * step; // 7 is 7, 11 is 12, 25 is 27
    if (maxpace > 9)
        { c.left += 7; c.width -= 7; }
    for (var i = 0; i <= maxpace; i += step)
    {
        var numwidth = 18 * maxpace.toString().length;
        // ticks on y pace (left) axis
        s.push("<v:rect style='" + VMLPos(c, 0, c.height - i * c.height / maxpace, 3, 1) + "'></v:rect>"); 
        // left pace captions
        s.push(VMLText(g, 0, c.height - i * c.height / maxpace + 3, i, numwidth));
    }
    s.push(VMLText(g, c.left - g.left, 0, "Pace (" + (IsSwim ? PaceUnits : "km/h") + ")"));
    // vertical rest right y-axis
    for (var i = 0; i <= maxrest; i++)
    {
        // ticks on y rest (right) axis
        s.push("<v:rect style='" + VMLPos(c, c.width - 1, c.height - i * c.height / maxrest, 3, 1) + "'></v:rect>"); 
        // right rest captions
        if (i < 10)
            s.push(VMLText(g, c.width + (c.left - g.left) - 3, c.height - i * c.height / maxrest + 3, i));
    }
    s.push(VMLText(g, c.left + c.width - g.left - 90, 0, "Rest (min)"));
    s.push("<v:rect style='" + VMLPos(c,1,0,1,0) + "'></v:rect>"); // y pace (left) axis
    s.push("<v:rect style='" + VMLPos(c,0,-1,0,1) + "'></v:rect>"); // x distance axis
    s.push("<v:rect style='" + VMLPos(c,-2,0,1,0) + "'></v:rect>"); // y rest (right) axis
    var ticks = 0;
    var global = IsSwim ? getGlobalLaps() : (getGlobalDistance() / 100);
    for (var i = 0; i < exerSegs(); i++)
    {
        var seg = exerRow(i);
        var segwidth = c.width / global;
        var segvalue = IsSwim ? seg.getLaps() : (seg.getDist() / 100);
        // segment's rest bar
        if (seg.getRest() > 0)
        {
            var r = "<v:rect style='" + 
                VMLPos(c, 
                Math.max(0, (ticks + segvalue) * segwidth - 10), // left
                c.height - (seg.getRest() * c.height / (maxrest * 60)), // top
                8, // width
                seg.getRest() * c.height / (maxrest * 60) // height
                ) + "' fillcolor='#4444e0' strokecolor='#4444e0'></v:rect>";
            s.push(r);
        }
        // segment's running line
        r = "<v:rect style='" + 
            VMLPos(c, 
            Math.max(0, ticks * segwidth + 3), // left
            c.height - seg.getPace() * FullPaceUnitsFactor * c.height / maxpace, // top
            Math.max(1, segvalue * segwidth - 6), // width
            1) + "' fillcolor='red' strokecolor='red'></v:rect>";
        s.push(r);
        ticks += segvalue;
    }
    Graph.innerHTML = "";
    window.setTimeout(function(){Graph.innerHTML = s.join("");},250);
    disablePendingGraphRedraw = false;
}

