﻿function handleMouseEnter(sender, eventArgs) {
    sender.children.getItem(0).fill.gradientStops.getItem(0).offset = 0;
	sender.children.getItem(0).fill.gradientStops.getItem(1).offset = 0.7;
}

function handleMouseLeave(sender, eventArgs) {
    sender.children.getItem(0).fill.gradientStops.getItem(0).offset = 0;
	sender.children.getItem(0).fill.gradientStops.getItem(1).offset = 1;
}

function handleMouseDown(sender, eventArgs) {
    sender.children.getItem(0).fill.gradientStops.getItem(0).offset = 0.7;
	sender.children.getItem(0).fill.gradientStops.getItem(1).offset = 0;
}

function handleMouseUp(sender, eventArgs) {
    sender.children.getItem(0).fill.gradientStops.getItem(0).offset = 0;
	sender.children.getItem(0).fill.gradientStops.getItem(1).offset = 1;
	DisableRPSButtons();
	
	var myAction = sender.name;
	var oppAction = GetRandomOpponentAction();
	var outcome = DetermineOutcome(myAction,oppAction);
	
    tbOutcome.opacity = 0;
	SetMediaSource(meHand,myAction + ".wmv");
	PlayMeHandAfterWaitTiming(oppAction + outcome + ".wmv");

	SetMediaSource(oppHand,oppAction + outcome + ".wmv",_currentCollectionIndex);
	
	//wait until the media ends before updating the stats
	setTimeout('UpdateStatistics("' + myAction + '","' + oppAction + '","' + outcome + '")',3000);
}

function meHandMediaEnded(sender, args)
{
    if( !ProcessGameWinner() )
    {
        //set the to property of the canvas.left of the outcome so that the text is centered
        var sb = sender.findName("storyBoardOutCome");
        switch(sender.findName('tbOutcome').text)
        {
            case 'You Win':
                sb.children.getItem(0).to = 35;
                break;
            case 'You Lose':
                sb.children.getItem(0).to = 30;
                break;
            case 'You Tie':
                sb.children.getItem(0).to = 38;
                break;
        }
        sb.begin();
        sender.source = "";
        //re-enable buttons
	    EnableRPSButtons();
    	
        //if scoreboard hidden, then open and close it to show the score
        if( sender.findName('lfo').getValue('Canvas.Left') < 0 )
        {
            LeftFlyOutMouseLeftButtonUp(sender,args);
            setTimeout('LeftFlyOutMouseLeftButtonUp(null,null)',1500);
        }
    }
}
function oppHandMediaEnded(sender, args){SetRandomWaitMedia();}
function HelpMouseEnter(sender, args){sender.findName("helpEnterStoryBoard").begin();}
function HelpMouseLeave(sender, args){sender.findName("helpLeaveStoryBoard").begin();}
function FactMouseEnter(sender, args){sender.findName("factEnterStoryBoard").begin();}
function FactMouseLeave(sender, args){sender.findName("factLeaveStoryBoard").begin();}
function Fact2MouseEnter(sender, args){sender.findName("fact2EnterStoryBoard").begin();}
function Fact2MouseLeave(sender, args){sender.findName("fact2LeaveStoryBoard").begin();}
function SettingsMouseEnter(sender, args){sender.findName("settingsEnterStoryBoard").begin();}
function SettingsMouseLeave(sender, args){sender.findName("settingsLeaveStoryBoard").begin();}
function SettingCollection1MouseEnter(sender,args){sender.findName("settingCollection1MouseEnterStoryBoard").begin();}
function SettingCollection1MouseLeave(sender,args){sender.findName("settingCollection1MouseLeaveStoryBoard").begin();}
function SettingCollection2MouseEnter(sender,args){sender.findName("settingCollection2MouseEnterStoryBoard").begin();}
function SettingCollection2MouseLeave(sender,args){sender.findName("settingCollection2MouseLeaveStoryBoard").begin();}

function SettingCollection1MouseLeftButtonUp(sender,args)
{
    _currentCollectionIndex = 0;
    InitializeSettings();
    DownloadCurrentCollection();
}
function SettingCollection2MouseLeftButtonUp(sender,args)
{
    _currentCollectionIndex = 1;
    InitializeSettings();
    DownloadCurrentCollection();
}
function LeftFlyOutMouseLeftButtonUp(sender, args)
{
    if( pageHost.findName('lfo').getValue('Canvas.Left') < 0 )
    {
        pageHost.findName("lfoOutStoryboard").begin();
        pageHost.findName("lfoGlyphTop").Indices = "163";
        pageHost.findName("lfoGlyphBottom").Indices = "163";
    }
    else
    {
        pageHost.findName("lfoInStoryboard").begin();
        pageHost.findName("lfoGlyphTop").Indices = "164";
        pageHost.findName("lfoGlyphBottom").Indices = "164";
    }
}
function RightFlyOutMouseLeftButtonUp(sender, args)
{
    if( pageHost.findName('rfo').getValue('Canvas.Left') > 0 )
    {
        pageHost.findName("rfoOutStoryboard").begin();
        pageHost.findName("rfoGlyphTop").Indices = "164";
        pageHost.findName("rfoGlyphBottom").Indices = "164";
    }
    else
    {
        pageHost.findName("rfoInStoryboard").begin();
        pageHost.findName("rfoGlyphTop").Indices = "163";
        pageHost.findName("rfoGlyphBottom").Indices = "163";
    }
}
function ScoreboardMouseEnter(sender, args)
{
    sender.findName("scoreboardEnterStoryBoard").begin();
    sender.findName("lfoRectangleGeometry").rect = "3,-500,1000,1000";
} 
function ScoreboardMouseLeave(sender, args)
{
    sender.findName("scoreboardLeaveStoryBoard").begin();
    setTimeout('pageHost.findName("lfoRectangleGeometry").rect = "3,0,105,130"',300);
}
function ResetMouseLeftButtonUp(sender, args)
{
    if( sender == null || window.confirm('Are you sure you want to reset the game?\n\nYou will lose any unposted data.'))
    {
        choiceRock = choicePaper = choiceScissors = oppChoiceRock = oppChoicePaper = oppChoiceScissors = recordWin = recordLose = recordTie = 0;
        choiceHistory = [];
        DisplayCurrentRecord();
    }
}
function PostScoresLeftMouseButtonUp(sender, args)
{
    if( window.confirm('Are you sure you want to post your scores?\n\nThis will reset your game scores.'))
    {
        alert('Not currently Implemented');
        //PageMethods.PublishResultsToSharePoint(recordWin,recordLose);
        ResetMouseLeftButtonUp(null,null);
    }
}
function AnalyzeLeftMouseButtonUp(sender, args)
{
    if( choiceHistory.length < 6 )
    {
        switch(getRandomInt(5))
        {
            case 0:
                alert( 'You should try playing a few mores games before I can analyze anything for you.' );
                break;
            case 1:
                alert( 'Analyzing your game play without playing at least a handful of games seems rather odd... don\'t you think?' );
                break;
            case 2:
                alert( 'How about you give me a few more games to work with here.\n\nOtherwise, analysis is kind of worthless.' );
                break;
            case 3:
                alert( 'What the heck am I supposed to analyze? Try playing a few games first.' );
                break;
            case 4:
                alert( 'What, you think I can read your mind... at least give me a couple more games to figure you out.' );
                break;
        }
        return;
    }
    else
    {
    	PageMethods.AnalyzeGamePlay(choiceHistory,OnAnalyzeGamePlaySucceeded);
    }
        
}
function OnAnalyzeGamePlaySucceeded(result)
{
    alert(result);
    if(result.length > 0 )
    {
        oppHand.source = _mediaPath + _collections[_currentCollectionIndex] + "/" + result + ".wmv";
    }
}