var currentStep = new Array( 0, 0, 0 );
var blockWidth  = new Array( 0, 0, 0 );
var switchingObjects = new Array();
var timeoutID;

var projects = new Array(
        Array( "cassi.jpg", "cassibg.jpg", "#5a9bed", "http://www.cassi.dn.ua/" ),
        Array( "zero.gif", "at-bg.jpg", "#d4d0c8", "http://www.autotrading.dn.ua/" ),
        Array( "aura.png", "aura_bg.jpg", "#000178", "http://www.aura.donetsk.ua/" ),
//        Array( "avtohome.png", "avtohome_bg.gif", "black", "http://www.avtohome.com.ua/" ),
        Array( "kova.png", "kova_bg.jpg", "#FCF2E4", "" ),
        Array( "dki.jpg", "dki_bg.gif", "#a89773", "/clients/doncreditinvest/site/" ),
        Array( "dp.gif", "dp_bg.jpg", "#fff9dc", "/clients/donbass-palace/site/" ),
        Array( "razvitie.jpg", "razvitie_bg.gif", "#d0e6ad", "/clients/razvitie/site/" ),
        Array( "silur.jpg", "silur_bg.jpg", "#92b4e4", "/clients/silur/site/" ),
        Array( "isd.gif", "isd_bg.gif", "#ffffff", "/clients/isd/site/" ),
        Array( "dmz.gif", "dmz_bg.jpg", "#1e2a66", "/clients/dmz/site/" ),
        Array( "donsalt.gif", "donsalt_bg.jpg", "#f2f2f2", "/clients/donbass-salt/site/" ),
        Array( "bkz.jpg", "bkz_bg.gif", "#ffffff", "/clients/bkz/site/" ),
//        Array( "tochmash.gif", "tochmash_bg.jpg", "#c1c9cc", "/clients/tochmash/site/" ),
        Array( "metallurg.jpg", "metallurg_bg.jpg", "#000000", "/clients/metallurg/site/" ),
        Array( "prague.gif", "prague_bg.jpg", "#d6d4c8", "/clients/prague/site/" )
    );

var loadedImages = new Array();
var currProject = 0;
var currState = 0;
var movingDirection = 0;
var imgSrc = "img/";
var totalIterations = 0;
var minProject = 3;

function showparams( obj )
{
    s = '';
    k = 0;
    for( i in obj )
        {
        s = s + i +'=' + eval( 'obj.' + i )+'\n';
        k++;
        if( k>10 )
            {
            alert( s );
            s = '';
            k = 0;
            } 
        }
}

function initEverything()
{
    var obj, s, l;
    for( i=1; i<=3; i++ )
        {
        loadedImages[i] = new Array();
        loadedImages[i][0] = new Image();
        loadedImages[i][1] = new Image();
        loadedImages[i][0].src = imgSrc + projects[i][0];
        loadedImages[i][1].src = imgSrc + projects[i][1];
        }
    currProject = 3;
    currState = 0;
    for( i=1; i<=3; i++ )
        {
        s = '';
        s += '<table id=currban'+i+'bg width=100% cellpadding=0 cellspacing=0 border=0';
        s += ' style="background-image: url(/img/'+projects[i-1][1]+'); background-position: center;';
        s += ' background-color: '+projects[i-1][2]+';">';
        s += '<tr><td align=center><a href=/ onclick="return panelClick('+i+');">';
        s += '<img id=currban'+i+'logo src=/img/'+projects[i-1][0]+' width=224 height=124 border=0></a></td></tr>';
        s += '</table>';

        l = '';
        l += '<table id=swban'+i+'bg width=100% cellpadding=0 cellspacing=0 border=0';
        l += ' style="background-image: url(/img/'+projects[i][1]+'); background-position: center;">';
        l += '<tr><td align=center><img id=swban'+i+'logo src=/img/'+projects[i][0]+' width=224 height=124 border=0></td></tr>';
        l += '</table>';

        obj = findObject( 'currban'+i );
        if( obj )
            obj.innerHTML = s;

        obj = findObject( 'switchban'+i );
        if( obj )
            obj.innerHTML = l;

        setupInteractiveness( i, "currban"+i, i );
        }
    obj = findObject( 'mem_image' );
    if( obj )
        obj.style.backgroundImage = 'url(/img/today/'+todays[selectedToday].src+')';
}

function loadThem( direction )
{
    if( currState )
        return false;
    if( (direction > 0 && currProject < projects.length ) ||
        (direction < 0 && currProject > minProject ) )
        {
        if( !loadedImages[ currProject ] && (direction > 0) )
            loadedImages[ currProject ] = new Array();
        if( (direction > 0) && !loadedImages[ currProject ][ 0 ] )
            {
            loadedImages[ currProject ][ 0 ] = new Image();
            loadedImages[ currProject ][ 0 ].src = imgSrc + projects[ currProject ][0];
            }
        if( (direction > 0) && !loadedImages[ currProject ][ 1 ] )
            {
            loadedImages[ currProject ][ 1 ] = new Image();
            loadedImages[ currProject ][ 1 ].src = imgSrc + projects[ currProject ][1];
            }
        clearTimeout( timeoutID );

        currState++;

        movingDirection = direction;
        if( movingDirection > 0 )
            showElem( 'clforward' );
        else
            showElem( 'clback' );

        timeoutID = setTimeout( 'waitForThem();', 500 );
        }
}

function waitForThem()
{
    clearTimeout( timeoutID );
    if( (movingDirection < 0) || (loadedImages[ currProject ][ 0 ].complete && loadedImages[ currProject ][ 1 ].complete) )
        {
        if( movingDirection > 0 )
            hideElem( 'clforward' );
        else
            hideElem( 'clback' );
//        currState--;
        switchpanels();
        }
    else
        timeoutID = setTimeout( 'waitForThem();', 100 );
}

function switchpanels()
{
    for( i=0; i<3; i++ )
        {
        var gotaway = findObject( "currban"+(i+1) );
        var switcher = findObject( "switchban"+(i+1) );
        var cont = findObject( "cont"+(i+1) );
        if( !switchingObjects[i] )
            switchingObjects[i] = new Array( gotaway, switcher );
        if( cont.style && cont.style.pixelWidth )
            blockWidth[i] = cont.style.pixelWidth;
        else
            {
            if( cont.offsetWidth )
                blockWidth[i] = cont.offsetWidth;
            }
        if( movingDirection > 0 )
            currentStep[i] = blockWidth[i];
        else
            currentStep[i] = 0;
        }

    preparePanels();

    return false;
}

function preparePanels()
{
    for( i=0; i<3; i++ )
        {
        panelName = "currban" + (i+1);
        projectNum = currProject-3+i;
        if( movingDirection <=0 )
            preparePanel( projectNum, panelName );

        panelName = "swban" + (i+1);
        projectNum = currProject-3+i+movingDirection;
        //if( movingDirection <= 0 )
        preparePanel( projectNum, panelName );
        }
    clearTimeout( timeoutID );
    timeoutID = setTimeout( 'shiftPanels()', 500 );
}

function shiftPanels()
{
    for( i=0; i<3; i++ )
        {
        panelName = "currban" + (i+1);
        position = 0;
        shiftPanel( panelName, position );
        panelName = "switchban" + (i+1);
        position = movingDirection*blockWidth[i];
        shiftPanel( panelName, position );
        }
    clearTimeout( timeoutID );
    totalIterations = 0;
//    alert( "shifted!" );
    timeoutID = setTimeout( 'showThem()', 100 );
}

function showThem()
{
    for( i=0; i<3; i++ )
        {
        panelName = "switchban" + (i+1);
        showElem( panelName );
        }
    clearTimeout( timeoutID );
    timeoutID = setTimeout( 'switchThem()', 1 );
}

function finalizePanels()
{
    for( i=0; i<3; i++ )
        {
        panelName = "currban" + (i+1);
        preparePanel( currProject-3+i, panelName );
        setupInteractiveness( currProject-3+i, panelName, i+1 );
        shiftPanel( panelName, 0 );
        }
    clearTimeout( timeoutID );
    timeoutID = setTimeout( 'removeSwitchers()', 500 );
}

function setupInteractiveness( projectNum, panelName, idx )
{
    var panelLogo = findObject( panelName+"logo" );
    var panelDiv = findObject( "currban"+idx );
    if( panelLogo && panelLogo.style && panelLogo.style.cursor )
        panelLogo.style.cursor = "pointer";
    if( panelDiv && panelDiv.style && panelDiv.style.cursor )
        panelDiv.style.cursor = "pointer";
}

function panelClick( shift )
{
    if( projects[currProject-4+shift][3] != '' )
        location = projects[currProject-4+shift][3];
    return false;
}


function removeSwitchers()
{
    for( i=0; i<3; i++ )
        {
        panelName = "switchban" + (i+1);
        shiftPanel( panelName, blockWidth[i] );
        hideElem( panelName );
        }
    clearTimeout( timeoutID );
    currState--;
}

function preparePanel( projectNum, panelName )
{
    var panelObj = findObject( panelName );
    var panelTbl = findObject( panelName+"bg" );
    var panelImg = findObject( panelName+"logo" );

    if( panelTbl )
        {
        panelTbl.style.backgroundImage = 'url(' + imgSrc + 
            projects[ projectNum ][1] + ')';
        panelTbl.style.backgroundColor = projects[ projectNum][2];
        }
    if( panelImg )
        {
        panelImg.src = imgSrc + projects[ projectNum ][0];
        }
}

function shiftPanel( panelName, position )
{
    var panelObj = findObject( panelName );
    if( panelObj && panelObj.style )
        panelObj.style.left = position;
}

function switchThem()
{
    for( i=0; i<3; i++ )
        {
        var source = switchingObjects[i][0];
        var switcher = switchingObjects[i][1];
        var coeff = 1;
        if( (currentStep[i]-movingDirection*40 > blockWidth[i]) ||
            (currentStep[i]-movingDirection*40 < 0) )
            coeff = 2;
        currentStep[i] += -movingDirection*20*coeff;
        if( currentStep[i] < 0 )
            currentStep[i] = 0;
        if( currentStep[i] > blockWidth[i] )
            currentStep[i] = blockWidth[i];
        if( switcher.style )
            {
            if( movingDirection > 0 )
                switcher.style.left = currentStep[i];
            else
                switcher.style.left = currentStep[i] - blockWidth[i];
            }
        if( source.style )
            {
            if( movingDirection > 0 )
                source.style.left = currentStep[i] - blockWidth[i];
            else
                source.style.left = currentStep[i];
            }
        }

    clearTimeout( timeoutID );
    if( ( (movingDirection > 0) && 
            (currentStep[0] || currentStep[1] || currentStep[2]) ) ||
        ( (movingDirection < 0) && 
            ( (currentStep[0] < blockWidth[0]) || 
              (currentStep[1] < blockWidth[1]) || 
              (currentStep[2] < blockWidth[2]) ) ) )
        {
        timeoutID = setTimeout( 'switchThem()', 1 );
        totalIterations ++;
        }
    else
        {
        currProject += movingDirection;
        movingDirection = 0;
        if( currProject >= projects.length )
            hideElem( 'btnforward' );
        else
            showElem( 'btnforward' );
        if( currProject <= minProject )
            hideElem( 'btnback' );
        else
            showElem( 'btnback' );
        finalizePanels();
        }
}

