var rotate_images = new Array();
var rotate_sources = new Array();
var rotate_sources_index = new Array();

function findPosX( obj )
{
    var curleft = 0;
    if ( obj.offsetParent )
    {
        while ( 1 ) 
        {
            curleft += obj.offsetLeft;
            if ( ! obj.offsetParent )
                break;
            obj = obj.offsetParent;
        }
    }
    else if( obj.x )
        curleft += obj.x;
    return curleft;
}

function findPosY( obj )
{
    var curtop = 0;
    if ( obj.offsetParent )
    {
        while( 1 )
        {
            curtop += obj.offsetTop;
            if ( ! obj.offsetParent )
                break;
            obj = obj.offsetParent;
        }
    }
    else if ( obj.y )
        curtop += obj.y;
    return curtop;
}

function rotate_image_find( image_name, add_new )
{
    var i;
    var index = -1;
    for ( i = 0; i < rotate_images.length; i++ )
    {
        if ( rotate_images[i] == image_name )
        {
            index = i;
            break;
        }
    }
    if ( index == -1 && add_new == true )
    {
        index = rotate_images.length;
        rotate_images[ rotate_images.length ] = image_name;
        
        rotate_sources[index] = new Array();
        rotate_sources_index[index] = -1;
    }
    
    return index;
}

function rotate_image_add( image_name, src )
{
    var index = rotate_image_find( image_name, true );
    rotate_sources[index][rotate_sources[index].length] = src;
}

function rotate_image_start( image_name, timeout )
{
    var index = rotate_image_find( image_name, false );
    if ( index == -1 )
        return;
        
    timeout *= 1000;
    
    rotate_image( index, timeout );
}

function fade_image( value, index, timeout )
{
//    rotate_sources_index[index] = ( rotate_sources_index[index] + 1 ) % rotate_sources[index].length;
    var name = rotate_images[index];
    var img = document.getElementById( name );
    var float_img = document.getElementById( name + '_fade' );

    if ( value == 100 )
    {
        img.src = float_img.src;
        document.body.removeChild( float_img );
    }
    else
    {
        float_img.style.opacity = value / 50;
        float_img.style.filter = 'alpha(opacity=' + ( value * 5 ) + ')';
        setTimeout( "fade_image( " + ( value + 1 ) + ", " + index + ", " + timeout + " );", timeout );
//        alert( 'setTimeout( "fade_image( ' + ( value + 1 ) + ', ' + index + ', ' + timeout + ' );", ' + timeout + ');' );
    }
}

function rotate_image( index, timeout )
{
    rotate_sources_index[index] = ( rotate_sources_index[index] + 1 ) % rotate_sources[index].length;
    var name = rotate_images[index];
    var img = document.getElementById( name );

    var value = 1;
    var float_img = document.createElement("IMG");
    float_img.id = name + '_fade';
    float_img.style.position = "absolute";
    float_img.style.left = findPosX( img ) + "px";
    float_img.style.top = findPosY( img ) + "px";
    float_img.style.zIndex = 99;
    float_img.style.opacity = 0;
    float_img.style.filter = 'alpha(opacity=0)';
    float_img.src = rotate_sources[index][rotate_sources_index[index] ];
    document.body.appendChild(float_img);


    setTimeout( "fade_image( 1, " + index + ", " + ( timeout / 8 / 50 )  + " );", timeout / 8 / 50 );
    setTimeout( "rotate_image( " + index + ", " + timeout + " );", timeout );
}
