/*
** Framechannel rss slide player for Philips TV
** hitory:
**    07/22/2009 lec - expanded to support framechannel's crossfade and caching.
** 
*/ 
TRANSITION_STEP = 10;   // steps to achieve transition
TRANSITION_TIME = 1000; // time alloted for transition
TRANSITION_DELTA = 20; // timer value
OPACITY_STEP = 10;

function crossFade(from, to) 
{
    var profile = this.profile;
    if( profile.feedRefresh == true ) 
    {
        /*
        ** Force the transition,
        ** we expired in between issuing the request to get feed and 
        ** the callback function, also force the timer to give it 5 more seconds
        ** to complete
        */

        profile.opacity = 0;
        profile.frame_pix[to].dur += 5000;
        
        
    }
    if( OPACITY_STEP >= 100 ) 
    {
        /*
        ** We are using an abrupt transition
        ** invert opacity
        */
        profile.opacity = 0;
    }
    
    if( from != -1 )
    {
        /*
        ** If this is not the first time
        */
        set_opacity(profile.frame_pix[from].id, profile.opacity );
    }
    set_opacity(profile.frame_pix[to].id, 100 - profile.opacity );
    profile.opacity -= OPACITY_STEP;
    
    if( profile.opacity <= 0 ) 
    {
        /*
        ** We reached the end of fade sequence, replace the holdover slide
        */
        replace_holdover( profile );
        
        update_reference(profile,from,to);
        

        profile.opacity = 100;
        prep_slide_transition(profile,from,to);
        
        /*
        ** Philips TVs are showing a bug where all the slides have their opacity turned up high
        ** Lets just run the the stack and make sure that all but "to"s is turned off
        */

        for( var i = 0; i < profile.frame_pix.length; i++ ) 
        {
            if( i != to ) 
            {
                
                set_opacity(profile.frame_pix[i].id, 0 );
            }
        }
        
            
    } else {
        /*
        ** Not done with the fade, just reschedule us for next fade step
        */
        var callStr = profile.frame_pix[to].transition+"(" + from + "," + to +")" ;
        debug(callStr+","+TRANSITION_DELTA );
        profile.slide_timer_id 
            = setTimeout(profile.frame_pix[to].transition+"(" + from + "," + to +")", TRANSITION_DELTA );
    }
    
}

function set_opacity(img_name, opacity_value )
{
    var profile = this.profile;
    if( profile.browser == "MSIE" ) 
    {
        document.getElementById(img_name).style.filter="alpha(opacity=" + opacity_value + ")";
    } else {
        document.getElementById(img_name).style.opacity = opacity_value / 100;
    }
}

function replace_holdover( profile )
{
    if( profile.hold_over != null )
    {
        /*
        ** the slide from the previous frame was kept at this level in the array
        ** replace it, and remove the reference
        */
        delete (profile.frame_pix[profile.last_pix]);
        profile.frame_pix[profile.last_pix] = profile.hold_over;
        delete(profile.hold_over);
        profile.hold_over = null;  // make sure we will not do it again 
    }    
    
}

function update_reference(profile,from, to)
{
    profile.last_pix = profile.index;
    /*
    ** now add reference to the "to" slide for
    ** next time around, adjust the opacity back to 100
    ** adjust the index and reschedule us to run at the end of 
    ** the slide duration 
    */
    profile.index = to + 1;
    if( profile.index >= profile.frame_pix.length )
    {
        profile.index = 0;
    }
}

function prep_slide_transition(profile,from, to)
{
    var duration = profile.frame_pix[to].dur;
    /*
    ** Yes I know it overkill to carry the transition on the slide at this time
    ** it is inplace for when we have a lot more transitions and they are passed in 
    ** the XML file.
    */
    profile.slide_timer_id 
        = setTimeout(profile.frame_pix[to].transition+"("+ to + "," + profile.index + ")", duration );
}



function change_focus( from, to )
{
    var profile = this.profile;
    if( from != -1 ) 
    {
        translate(profile.frame_pix[from].id, OFFSCREEN );
    }
    replace_holdover(profile);
    update_reference(profile,from,to);
    prep_slide_transition(profile,from, to);
    translate(profile.frame_pix[to].id, ONSCREEN );
}

function translate(img_name, x_location )
{
    var profile = this.profile;
    document.getElementById(img_name).style.left = x_location;
}


    

function fitImage( index)
{
    var profile = this.profile;
    var frame_W = parseInt(profile.width);
    var frame_H = parseInt(profile.height);
    var image_W = parseInt(profile.frame_pix[index].wdt);
    var image_H = parseInt(profile.frame_pix[index].hgt);
    var dx = image_W - frame_W;
    var dy = image_H - frame_H;
    var ty;
    var tx;
    var f_ar = frame_H/frame_W;
    var i_ar = image_H/image_W; /* image factor */
    
    /*
    ** Ok now we determine the type of rectangle if the image ratio > frame ratio
    ** then map verical and calculate horizontal
    */
    do 
    {
        
        if( i_ar == f_ar )
        {
            ty = frame_H; // map both
            tx = frame_W;
            break;
            
        } 
        if( i_ar < f_ar )
        {
            ty = frame_W * i_ar; // map width
            tx = frame_W;
            break;
        } 
        if ( i_ar <= 1)
        {
            tx = frame_H / i_ar;
            ty = frame_H; // map height
            break;
        }
        
        if ( i_ar < 1/f_ar ) 
        {
            tx = frame_H / i_ar;
            ty = frame_H;    // map height
            break;
        }
    
        ty = frame_H; // map width
        tx = frame_H/i_ar;
        break;
        
    }while(0)
    
    profile.adjusted_W = (tx).toString(10);
    profile.adjusted_H = (ty).toString(10);
}



    
   
    
    
    
            
    
            
            

        