
var aAds = new Array();
aAds.push( {img:"images/vban_jamster.jpg", link:"http://clkde.tradedoubler.com/click?p=60843&a=1420419&g=16912278"} );
aAds.push( {img:"images/vban_news.jpg", link:"http://www.foxreality.com/news.php"} );
var nAdIndex = 0;

function FF_Initialize()
{
    // enable click listeners on playlist/search tabs
    Event.observe( $( 'PlaylistTab' ), 'click', activatePlaylistTab );
    Event.observe( $( 'SearchTab' ), 'click', activateSearchTab );

    // show playlist tab initially
    activatePlaylistTab();

    // register channel-style elements
    pvs.actionAddChannelStyleElement( document.body );

	$( 'SendToAFriend' ).hide();

	setInterval( rotateSkyscraperAd, 30000 );
}

function activatePlaylistTab()
{
    // update styles on tabs
	Element.addClassName( $( 'PlaylistTab' ), "Active" );
    Element.removeClassName( $( 'PlaylistTab' ), "Inactive" );
    Element.addClassName( $( 'SearchTab' ), "Inactive" );
    Element.removeClassName( $( 'SearchTab' ), "Active" );

    // hide search div
    Element.hide( $( 'ff_Search' ) );

    // show playlist div
    Element.show( $( 'ff_Controls' ) );
}

function activateSearchTab()
{
    // update styles on tabs
    Element.addClassName( $( 'PlaylistTab' ), "Inactive" );
    Element.removeClassName( $( 'PlaylistTab' ), "Active" );
	Element.addClassName( $( 'SearchTab' ), "Active" );
    Element.removeClassName( $( 'SearchTab' ), "Inactive" );

    // update "Active" styles on tabs
    //Element.removeClassName( $( 'PlaylistTab' ), "Active" );
    //Element.addClassName( $( 'SearchTab' ), "Active" );

    // hide playlist div
    Element.hide( $( 'ff_Controls' ) );

    // show search div
    Element.show( $( 'ff_Search' ) );
}

function submitSearch()
{
    var sQuery = $F( "txtQuery" );
    var sClass = $RF( "frmSearch", "radioType" );

    pvs.actionSearch( sQuery, sClass );
}

function PVS_eventNowPlaying()
{
    $( 'SendToAFriend' ).show();
}

function PVS_eventStoppedPlaying()
{
    $( 'SendToAFriend' ).hide();
}

// ---------------------------------
// -------- Send To A Friend -------
// ---------------------------------

var oCurrentVideo = null;

function onShareVideoPopup()
{
    oCurrentVideo = pvs.getCurrentVideo();
    if( oCurrentVideo )
    {
        pvs.actionHideVideoPlayer();
        var sVideoTitle = oCurrentVideo.getPrimaryDetail();
        Element.update( $( "EmailPopup_Choice" ), sVideoTitle );
    }
    else
        ShareVideoPopup_Close();
}

function onClickedShareVideoSubmit()
{
    var sFromName = $F( "txtEmailYourName" );
    var sFromEmail = $F( "txtEmailYourEmail" );
    var sToEmail = $F( "txtEmailFriendEmail" );

    pvs.actionSendToAFriend( oCurrentVideo, sFromName, sFromEmail, sToEmail, onShareVideoSuccess, onShareVideoFailed );  
}

function onShareVideoSuccess()
{
    ShareVideoPopup_Close();
}

function onShareVideoFailed()
{
	Element.update( $( "EmailPopup_Error" ), "Please make sure you entered valid email addresses, and filled in all fields." );
}

function ShareVideoPopup_Close()
{
    closeAllLightBoxes();
    pvs.actionRestoreVideoPlayer();
}

function rotateSkyscraperAd()
{
	if( nAdIndex < 0 || nAdIndex >= aAds.length )
		nAdIndex = 0;
	if( aAds[nAdIndex] )
	{
		Element.update( $( "ff_AdWrapper" ), '<a href="' + aAds[nAdIndex].link + '"><img src="' + aAds[nAdIndex].img + '" /></a>' );
		nAdIndex++;
	}
}

// ---------------------------------
// ----------- Utilities -----------
// ---------------------------------

function $RF( el, radioGroup )
{
    if( $( el ).type == 'radio' )
    {
        var el = $( el ).form;
        var radioGroup = $( el ).name;
    }
    else if( $( el ).tagName.toLowerCase() != 'form' )
    {
        return false;
    }
    return $F( $( el ).getInputs( 'radio', radioGroup ).find(
        function(re) {return re.checked;}
    ));
}