// JScript source code

var Frame;
var allDivs;
var Div = new Array();
var totalDivs = 0;
var margin_bot = 100;
var rand_Step = 2;               // One step ( in pixels )
var ChangeDivsAfter = 7;     // ( in seconds )

var rand_step_counter = 0;
var div_counter = 0;
var rand_steps;
var mod;
var rand_delay = 0
var rand_timer;



function on_load()
{
    allDivs = document.getElementById("AllSoonPreviews");
    Frame = document.getElementById("PreviewFrame");
    allDivs.style.top = 0+"px";
    ChangeDivsAfter *= 1000;
    //totalDivs = Math.round( parseInt(allDivs.offsetHeight) / distance );

    var i = 0;
    for (var childItem in allDivs.childNodes) 
    {
        if (allDivs.childNodes[childItem].nodeType == 1)
        {
            Div[i] = allDivs.childNodes[childItem]; //alert(Div[i]);
            Div[i+"H"] = Div[i].offsetHeight;
            //alert(Div[i+"H"]);
            i++;
        }
    }
    totalDivs = Div.length;
    Frame.style.height = Div[div_counter+"H"]+"px";
    
   if(totalDivs>1)
    {
        rand_timer = setTimeout("moveUP()", ChangeDivsAfter);
    }

}

function countStepsUP()
{
    rand_steps = Math.ceil( (Div[div_counter+"H"]+margin_bot) / rand_Step ) - 1;
    mod = Div[div_counter+"H"]+margin_bot- (rand_Step * rand_steps); //alert(mod);
    Frame.style.height = Div[div_counter+1+"H"]+"px";
}
function countStepsDOWN()
{
    rand_steps = Math.ceil( (Div[div_counter-1+"H"]+margin_bot) / rand_Step );
    mod = Div[div_counter-1+"H"]+margin_bot- (rand_Step * rand_steps);
    Frame.style.height = Div[div_counter-1+"H"]+"px";
}

//function

function moveUP()
{
    if ( !rand_step_counter ) { countStepsUP(); }
    var y = allDivs.style.top;
    y = parseInt( y.replace(/px/, "") );
    
    if (rand_step_counter == rand_steps-1)
        {allDivs.style.top = (y - (rand_Step+mod))+'px';}
    else     {allDivs.style.top = (y - rand_Step)+'px';}

    rand_step_counter++;
    
    if (rand_step_counter == rand_steps)
    {
        rand_stopTimer();
        rand_step_counter = 0;
        div_counter++;
        if ( div_counter == totalDivs-1 ) { rand_stopTimer(); rand_timer = setTimeout("moveDOWN()", ChangeDivsAfter); }
        else { rand_timer = setTimeout("moveUP()", ChangeDivsAfter); }
        //alert(allDivs.style.top);
    }
    else{
            rand_stopTimer();
            rand_timer = setTimeout("moveUP()", rand_delay);
        }
        
}
function moveDOWN()
{
    if ( !rand_step_counter ) { countStepsDOWN(); }
    var y = allDivs.style.top;
    y = parseInt( y.replace(/px/, "") );
    
    if (rand_step_counter == rand_steps-1)
        {allDivs.style.top = (y + (rand_Step+mod))+'px';}
    else     {allDivs.style.top = (y + rand_Step)+'px';}
    
    rand_step_counter++;
    
    if (rand_step_counter == rand_steps)
    {
        rand_stopTimer();
        rand_step_counter = 0;
        div_counter--;
        if ( div_counter == 0 ) { rand_stopTimer(); rand_timer = setTimeout("moveUP()", ChangeDivsAfter); }
        else{ rand_timer = setTimeout("moveDOWN()", ChangeDivsAfter); }
    }
    else{
            rand_stopTimer();
            rand_timer = setTimeout("moveDOWN()", rand_delay);
        }
        
}

function rand_stopTimer() { if(rand_timer) clearTimeout(rand_timer); rand_timer = null; }    
