﻿var _currentCollectionIndex = 0;
var _mediaPath = 'assets/';
var _mediaGeneric = ['Rock.wmv','Paper.wmv','Scissors.wmv'];
var _collections = ['newsroom','computer'];
var _mediaCollection =              ['RockLose.wmv',    'RockWin.wmv',  'RockTie.wmv',  'PaperLose.wmv',    'PaperWin.wmv', 'PaperTie.wmv', 'ScissorsLose.wmv', 'ScissorsWin.wmv',  'ScissorsTie.wmv'];
var _mediaCollectionWaitTiming =    [1400,              1700,           1000,           1700,               1500,           1600,           1600,               1300,               1700];
var _mediaWait = ['Wait1.wmv','Wait2.wmv'];
var _mediaAnalysis = ['LotaRock.wmv','LotaPaper.wmv','LotaScissors.wmv','ConsecutiveRock.wmv','ConsecutivePaper.wmv','ConsecutiveScissors.wmv','Pattern.wmv'];

//*********************************
//* Downloader functions
//*********************************
function InitMediaDownload()
{
    DownloadIntroMedia();
    DownloadStartupMedia();
}
function DownloadIntroMedia()
{
    var mediaSource = _mediaPath + _collections[_currentCollectionIndex] + '/' + 'Intro.wmv';
    DoDownloadMediaElement(mediaSource,"DownloadIntroMedia_ProgressChanged","DownloadIntroMedia_Completed");
}
function DownloadIntroMedia_ProgressChanged(sender,args)
{
    //update intro progress indicator
    var percentage = Math.floor(sender.downloadProgress * 100);
    progTxtIntro.text = percentage + "%";
    progRectIntro.width = percentage * 2;
}
function DownloadIntroMedia_Completed(sender,args)
{
    //set intro source
    if (sender.status == 200 || sender.status == 204)//success
        oppHand.setSource(sender);
    else
        alert(sender.StatusText);
        
    //hide progress indicator
    progIntro.opacity = 0;
}
function DownloadStartupMedia()
{
    //disable buttons until complete
    DownloadGenericMedia();
    DownloadCurrentCollection(true);
}
function DownloadGenericMedia()
{
    //kick off Generic files download
    for(var i=0;i<_mediaGeneric.length;i++)
    {
        var mediaSource = _mediaPath + _mediaGeneric[i];
        DoDownloadMediaElement(mediaSource,null,"DownloadStartupMedia_Completed");
    }
}
function DownloadCurrentCollection(isStartup)
{
    if( !isStartup )
    {
        //show progress indicator
        progMedia.opacity = 1;
        
        //disable buttons
        DisableRPSButtons();
        
        _countCollectionLoaded = 0;
    }
    //kick off collection download
    for(var i=0;i<_mediaCollection.length;i++)
    {
        var mediaSource = _mediaPath + _collections[_currentCollectionIndex] + '/' + _mediaCollection[i];
        if( isStartup )
            DoDownloadMediaElement(mediaSource,null,"DownloadStartupMedia_Completed");
        else
            DoDownloadMediaElement(mediaSource,null,"DownloadCurrentCollection_Completed");
    }
}
var _countLoaded = 0;
function DownloadStartupMedia_Completed(sender,args)
{
    var countToLoad = _mediaGeneric.length + _mediaCollection.length;
    if (sender.status == 200 || sender.status == 204)//success
    {
        _countLoaded++;
        //update progress indicator
        var percentage = Math.floor(_countLoaded/countToLoad * 100);
        progTxtMedia.text = percentage + "%";
        progRectMedia.width = percentage * 2;
        
        if( _countLoaded == countToLoad )//when complete
        {
            //hide progress indicator
            progMedia.opacity = 0;
            EnableRPSButtons();
            
            //kick off requests to load media in the background
            DownloadWaitMedia();
            DownloadAnalysisMedia();
        }
    }
    else
    {
        alert(sender.StatusText);
    }
}
var _countCollectionLoaded = 0;
function DownloadCurrentCollection_Completed(sender,args)
{
    var countToLoad = _mediaCollection.length;
    if (sender.status == 200 || sender.status == 204)//success
    {
        _countCollectionLoaded++;
        //update progress indicator
        var percentage = Math.floor(_countCollectionLoaded/countToLoad * 100);
        progTxtMedia.text = percentage + "%";
        progRectMedia.width = percentage * 2;
        
        if( _countCollectionLoaded == countToLoad )//when complete
        {
            //hide progress indicator
            progMedia.opacity = 0;
            
            //enable buttons
            EnableRPSButtons();
            
            //kick off requests to load media in the background
            DownloadWaitMedia();
            DownloadAnalysisMedia();
        }
    }
    else
    {
        alert(sender.StatusText);
    }
}
function DownloadWaitMedia()
{
    //kick off wait files download for current collection
    for(var i=0;i<_mediaWait.length;i++)
    {
        var mediaSource = _mediaPath + _collections[_currentCollectionIndex] + '/' + _mediaWait[i];
        DoDownloadMediaElement(mediaSource,null,null);
    }
}
function DownloadAnalysisMedia()
{
    //kick off analysis files download for current collection
    for(var i=0;i<_mediaAnalysis.length;i++)
    {
        var mediaSource = _mediaPath + _collections[_currentCollectionIndex] + '/' + _mediaAnalysis[i];
        DoDownloadMediaElement(mediaSource,null,null);
    }
}

//*********************************
//* Workflow functions
//*********************************
function SetRandomWaitMedia()
{
    SetMediaSource(oppHand, _mediaWait[getRandomInt(_mediaWait.length)], _currentCollectionIndex);
}
function SetMediaSource(srcElement,srcPath,intCollection)
{
    if( intCollection != null )
    	srcElement.source = _mediaPath + _collections[intCollection] + '/' + srcPath;
    else
    	srcElement.source = _mediaPath + srcPath;
    	
    //alert(srcElement.source);
}
function PlayMeHandAfterWaitTiming(oppAction)
{
    var waitTiming = _mediaCollectionWaitTiming[_mediaCollection.indexOf(oppAction)]
	setTimeout('meHand.play()',waitTiming);
}


//*********************************
//* Event Handlers
//*********************************
function muteButton_MouseLeftButtonUp(sender, args)
{
    if( oppHand.isMuted )
    {
        oppHand.isMuted = "false";
        sender.findName("muteIcon").opacity = 0;
    }
    else
    {
        oppHand.isMuted = "true";
        sender.findName("muteIcon").opacity = 1;
    }
}

//*********************************
//* Supporting functions
//*********************************
function DoDownloadMediaElement(mediaSource,downloadProgressChanged,completed)
{
    var downloader = pageHost.createObject("downloader");
    if( downloadProgressChanged )
        downloader.downloadProgressChanged = "javascript:" + downloadProgressChanged;
    if( completed )
        downloader.completed = "javascript:" + completed;

    downloader.open("GET", mediaSource, true);
    downloader.send();
}