var shownTruck;
var tapeTimer = false;

function tapeMove( id, delta )
{
    if ( !document.getElementById )
        return;
    var tape = document.getElementById( id );
    var el = document.getElementById( id + '-inner' );
    var eli = document.getElementById( id + '-inner-inner' );
    if ( !el || !eli )
        return;
    var newPosition = el.offsetLeft + delta;
    if ( newPosition > 0 )
    {
        newPosition = 0;
    }
    if ( -newPosition > eli.offsetWidth - tape.offsetWidth )
    {
        newPosition = tape.offsetWidth - eli.offsetWidth;
    }
    el.style.left = newPosition + 'px';
}

function startMove( id, delta, tout )
{
    tapeMove( id, delta );
    tapeTimer = setTimeout( 'startMove( "' + id + '", ' + delta + ', ' + tout + ' )', tout );
}

function stopMove()
{
    if ( tapeTimer )
    {
        clearTimeout( tapeTimer );
        tapeTimer=false;
    }
}

function showTruck( id )
{
    var newTruck = document.getElementById( 'truck_' + id );
    if ( !newTruck )
    {
        return;
    }
    var oldTruck = document.getElementById( 'truck_' + shownTruck );
    if ( oldTruck )
    {
        oldTruck.style.display = 'none';
    }
    newTruck.style.display = '';
    shownTruck = id;
}

// -- Areal spolocnosti --

function showZoom()
{
    var el = document.getElementById( 'zoom' );
    if ( !el )
    {
        return;
    }
    el.style.display = '';
}

function hideZoom()
{
    var el = document.getElementById( 'zoom' );
    if ( !el )
    {
        return;
    }
    el.style.display = 'none';
}

function moveZoom( e )
{
    if ( !e )
    {
        e = window.event;
    }
    var target = ( e.target )? e.target: e.srcElement;

    if ( target.tagName != 'IMG' )
    {
        return;
    }
    
    var x = ( e.offsetX )? e.offsetX: e.layerX; // e.x;
    var y = ( e.offsetY )? e.offsetY: e.layerY; // e.y;

    if ( !x )
    {
        x = 0;
    }
    if ( x < 0 )
    {
        x = 0;
    }
    if ( x >= target.width )
    {
        x = target.width - 1;
    }
    if ( !y )
    {
        y = 0;
    }
    if ( y < 0 )
    {
        y = 0;
    }
    if ( y >= target.height )
    {
        y = target.height - 1;
    }

    var bgX = Math.round( 1961 * x / target.width ) - 400/2;
    if ( bgX < 0 )
    {
        bgX = 0;
    }
    else if ( bgX > 1961 - 400 )
    {
        bgX = 1961 - 400;
    }

    var bgY = Math.round( 814 * y / target.height ) - 150/2;
    if ( bgY < 0 )
    {
        bgY = 0;
    }
    else if ( bgY > 814 - 150 )
    {
        bgY = 814 - 150;
    }

    var el = document.getElementById( 'zoom' );
    if ( !el )
    {
        return;
    }

    el.style.backgroundPosition = '-' + bgX + 'px -' + bgY + 'px';
}
