
// ---------------------------
// ------ Configuration ------
// ---------------------------

var aVoteButtons = new Array();
aVoteButtons.push( "vote_one" );
aVoteButtons.push( "vote_two" );
aVoteButtons.push( "vote_three" );
aVoteButtons.push( "vote_four" );
aVoteButtons.push( "vote_five" );
aVoteButtons.push( "vote_six" );

var aSegments = new Array();
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"New York"} );
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Christopher and Adrianne"} );
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Evel Dick"} );
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Sanjaya"} );
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Len Goodman"} );
aSegments.push( {id:1573729, instance:1573729, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Ant"} );

var oWelcomeSegment =         {id:1573728, instance:1573728, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Intro"};
var oThanksForVotingSegment = {id:1573735, instance:1573735, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Thanks for Voting"};
var oAlreadyVotedSegment =    {id:1573736, instance:1573736, playlist:90004, channel:"FoxVoting", type:"segment", description:"", title:"Already Voted"};

var sVoteID = "Really2007";
var oCurrentSegment = null;
var oLightBox = null;

function Vote_Initialize()
{
    // hide vote button
    // Element.hide( $( "vote_Panel" ) );

    // register click listeners 
     for( var x = 0; x < aVoteButtons.length; x++ ) 
     { 
         Event.observe( $( aVoteButtons[x] ), 'click', Vote_PlaySegment.bind( this, x ) ); 
     }

    // register channel-style elements
    pvs.actionAddChannelStyleElement( document.body );
}

function PVS_eventOnAppReady()
{
    //Vote_PlayWelcome();
}

function PVS_eventNowPlaying()
{
	gtv.setDefaultFLVChannel( "FoxVoting" );
}

// ---------------------------
// -------- Playback ---------
// ---------------------------

function Vote_PlayWelcome()
{
    Vote_Play( oWelcomeSegment );
}

function Vote_PlaySegment( nIndex )
{
    var oSeg = aSegments[nIndex];
    if( oSeg )
    {
        Element.update( $( "vote_CurrentVoteName" ), "<span>" + oSeg.title + "</span>");
        // Element.show( $( "vote_Panel" ) );
        oCurrentSegment = oSeg;
        Vote_Play( oCurrentSegment );
    }
}

function Vote_ThanksForVoting()
{
    Vote_Play( oThanksForVotingSegment );
}

function Vote_PlayAlreadyVoted()
{
    Vote_Play( oAlreadyVotedSegment );
}

function Vote_Play( oSeg )
{
    if( oSeg )
    {
        oSeg.instance = oSeg.id;
        oSeg.route = null;
        gtv.setPlayThrough( false );
        gtv.playContent( oSeg );
    }
}

// ---------------------------
// --------- Voting ----------
// ---------------------------

function alreadyVoted()
{
	return 	( pvs.readCookie( "Vote" ) == "1" );
}

function VotePopup_Submit()
{
    var sVote = pvs.readCookie( "Vote" );
    if( sVote != "1" )
    {
        if( oCurrentSegment != null )
        {
            var sCode = $F( "txtCode" );
            if( sCode != null )
            {
                if( sCode.length > 0 )
                {
                    var sVote = oCurrentSegment.title;
                    var sURL = "/pvs/lib/imageverify/AlphaNumericVerifyAndVote.php";
                    var sParams = "?code=" + escape( sCode ) + "&voteid=" + escape( sVoteID ) + "&vote=" + escape( sVote );

                    var fnSuccess = onVoteSuccess;
                    var fnRejectedImage = onVoteRejectedImageVerification;
                    var fnRejectedVoted = onVoteRejectedAlreadyVoted;
                    var fnFailedTechnical = onVoteTechnicalProblem;

                    new Ajax.Request( sURL, {
                        parameters: sParams,
                        method: 'get',
                        onException: function()
                        {
                            fnFailedTechnical();
                        },
                        onSuccess: function( response )
                        {
                            if( response.responseText.indexOf( "Success" ) != -1 )
                                fnSuccess();
                            else
                            {
                                if( response.responseText.indexOf( "Image verification failed" ) != -1 )
                                    fnRejectedImage();
                                else if( response.responseText.indexOf( "Vote rejected" ) != -1 )
                                    fnRejectedVoted();
                                else
                                    fnFailedTechnical();
                            }
                        },
                        onFailure: function()
                        {
                            fnFailedTechnical();
                        }
                    });
                }
                else
                {
                    if( $( "VotePopup_Error" ) )
                        Element.update( $( "VotePopup_Error" ), "Please type in the letters you see in the image before voting" );
                }
            }
        }
        else
        {
            alert( "You need to watch a segment before you can vote!" );
        }
    }
    else
    {
        onVoteRejectedAlreadyVoted();
    }
}

function onVoteSuccess()
{
    VotePopup_Close();
    Vote_ThanksForVoting();
    pvs.setCookie( "Vote", "1", 999, "/" );
	// Element.update( $( "vote_Panel" ), '<a href="#" onClick="Vote_PlayAlreadyVoted(); return false;"><img src="./images/vote_forme.jpg" /></a>' );
}

function onVoteRejectedImageVerification()
{
    if( $( "VotePopup_Error" ) )
        Element.update( $( "VotePopup_Error" ), "You entered the wrong image verification code!<br />(please make sure cookies are enabled)" );
}

function onVoteRejectedAlreadyVoted()
{
    VotePopup_Close();
    Vote_Play( oAlreadyVotedSegment );
}

function onVoteTechnicalProblem()
{
    if( $( "VotePopup_Error" ) )
        Element.update( $( "VotePopup_Error" ), "We are sorry, but for technical reasons please try voting again later." );
}

function VotePopup_Close() 
 { 
     closeAllLightBoxes(); 
     pvs.actionRestoreVideoPlayer();
 }

function clearVote()
{
    pvs.setCookie( "Vote", "0", 999, "/" );
}