﻿if (!window.LottoLogic)
    window.LottoLogic = {};

LottoLogic.Scene = function() {
}

LottoLogic.Scene.prototype =
{
    Picks: {
        ball1: {},
        ball2: {},
        ball3: {},
        ball4: {},
        ball5: {},
        ball1Text: {},
        ball2Text: {},
        ball3Text: {},
        ball4Text: {},
        ball5Text: {},
        buttonText: {},
        hasClicked: false
    },

    Indicators: {
        Label: {}
    },

    handleLoad: function(plugIn, userContext, rootElement) {
        this.plugIn = plugIn;

        this.root = rootElement;
        this.button = rootElement.findName('pickThreeButton');
        this.buttonFour = rootElement.findName('pickFourButton');
        this.buttonFive = rootElement.findName('pickFiveButton');

        //Set the ball values and text fields
        this.Picks.ball1Text = rootElement.findName('ball1Text');
        this.Picks.ball2Text = rootElement.findName('ball2Text');
        this.Picks.ball3Text = rootElement.findName('ball3Text');
        this.Picks.ball4Text = rootElement.findName('ball4Text');
        this.Picks.ball5Text = rootElement.findName('ball5Text');
        this.Picks.ball1 = rootElement.findName('ball1');
        this.Picks.ball2 = rootElement.findName('ball2');
        this.Picks.ball3 = rootElement.findName('ball3');
        this.Picks.ball4 = rootElement.findName('ball4');
        this.Picks.ball5 = rootElement.findName('ball5');
        this.Picks.buttonText = rootElement.findName('textPickThree');
        this.Indicators.Label = rootElement.findName('textStatus');

        //Event Handlers...
        this.button.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
        this.button.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));

        this.button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
        this.button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));

        this.buttonFour.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnterFour));
        this.buttonFour.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeaveFour));
        this.buttonFour.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUpFour));

        this.buttonFive.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnterFive));
        this.buttonFive.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeaveFive));
        this.buttonFive.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUpFive));
        this.ResetBoxes();
    },

    // Sample event handlers
    handleMouseEnter: function(sender, eventArgs) {
        // The following code shows how to find an element by name and call a method on it.
        var mouseEnterAnimation = sender.findName("mouseEnter");
        mouseEnterAnimation.stop();
        mouseEnterAnimation.begin();
    },

    // Sample event handlers
    handleMouseEnterFour: function(sender, eventArgs) {
        // The following code shows how to find an element by name and call a method on it.
        var mouseEnterAnimation = sender.findName("mouseEnterFour");
        mouseEnterAnimation.stop();
        mouseEnterAnimation.begin();
    },

    handleMouseLeaveFour: function(sender, eventArgs) {
        var mouseLeaveAnimation = sender.findName("mouseLeaveFour");
        mouseLeaveAnimation.begin();
    },

    // Sample event handlers
    handleMouseEnterFive: function(sender, eventArgs) {
        // The following code shows how to find an element by name and call a method on it.
        var mouseEnterAnimation = sender.findName("mouseEnterFive");
        mouseEnterAnimation.begin();
    },

    handleMouseLeaveFive: function(sender, eventArgs) {
        var mouseLeaveAnimation = sender.findName("mouseLeaveFive");
        mouseLeaveAnimation.begin();
    },

    handleMouseDown: function(sender, eventArgs) {
        var mouseDownAnimation = sender.findName("mouseDown");
        mouseDownAnimation.begin();
    },

    handleMouseUp: function(sender, eventArgs) {
        var mouseUpAnimation = sender.findName("mouseUp");
        mouseUpAnimation.begin();

        if (this.Picks.hasClicked == false) {
            sender.findName('SpinnerStart').stop();
            sender.findName('SpinnerStart').begin();
            this.Indicators.Label.Text = "Your random numbers are.....";
            this.Indicators.Label.Foreground = "Blue";
            this.Picks.ball1Text.Text = document.getElementById("_box1").value;
            this.Picks.ball2Text.Text = document.getElementById("_box2").value;
            this.Picks.ball3Text.Text = document.getElementById("_box3").value;
            this.Picks.hasClicked = true;
            this.HideButtons();
            sender.findName('DisplayValuesThree').stop();
            sender.findName('DisplayValuesThree').begin();
        }
        else {
            sender.findName('DisplayValuesThree').stop();
            sender.findName('SpinnerStart').stop();
            this.ResetBoxes();
        }
    },

    handleMouseUpFour: function(sender, eventArgs) {
        if (this.Picks.hasClicked == false) {
            sender.findName('SpinnerStart').stop();
            sender.findName('SpinnerStart').begin();

            this.Indicators.Label.Text = "Your random numbers are.....";
            this.Indicators.Label.Foreground = "Blue";
            this.Picks.ball1Text.Text = document.getElementById("_box41").value;
            this.Picks.ball2Text.Text = document.getElementById("_box42").value;
            this.Picks.ball3Text.Text = document.getElementById("_box43").value;
            this.Picks.ball4Text.Text = document.getElementById("_box44").value;
            this.Picks.hasClicked = true;

            this.HideButtons();

            sender.findName('DisplayValuesFour').stop();
            sender.findName('DisplayValuesFour').begin();
        }
        else {
            sender.findName('SpinnerStart').stop();
            sender.findName('DisplayValuesFour').stop();
            this.ResetBoxes();
        }
    },

    handleMouseUpFive: function(sender, eventArgs) {
        if (this.Picks.hasClicked == false) {
            sender.findName('SpinnerStart').stop();
            sender.findName('SpinnerStart').begin();
            this.Indicators.Label.Text = "Your random numbers are.....";
            this.Indicators.Label.Foreground = "Blue";
            this.Picks.ball1Text.Text = document.getElementById("_box51").value;
            this.Picks.ball2Text.Text = document.getElementById("_box52").value;
            this.Picks.ball3Text.Text = document.getElementById("_box53").value;
            this.Picks.ball4Text.Text = document.getElementById("_box54").value;
            this.Picks.ball5Text.Text = document.getElementById("_box55").value;
            this.Picks.hasClicked = true;

            this.HideButtons();
            sender.findName('DisplayValuesFive').stop();
            sender.findName('DisplayValuesFive').begin();
        }
        else {
            sender.findName('SpinnerStart').stop();
            sender.findName('DisplayValuesFive').stop();
            this.ResetBoxes();
        }
    },

    handleMouseLeave: function(sender, eventArgs) {
        var mouseLeaveAnimation = sender.findName("mouseLeave");
        mouseLeaveAnimation.begin();
    },

    ResetBoxes: function() {
        //Handle reset values
        this.root.findName('DisplayValuesThree').stop();
        this.root.findName('DisplayValuesFour').stop();
        this.root.findName('DisplayValuesFive').stop();
        this.Picks.ball1.Opacity = 0;
        this.Picks.ball2.Opacity = 0;
        this.Picks.ball3.Opacity = 0;
        this.Picks.ball4.Opacity = 0;
        this.Picks.ball5.Opacity = 0;
        this.Picks.ball1Text.Text = "";
        this.Picks.ball2Text.Text = "";
        this.Picks.ball3Text.Text = "";
        this.Picks.ball4Text.Text = "";
        this.Picks.ball5Text.Text = "";
        this.Indicators.Label.Text = "Click one of the 'Pick #' button's above to start game.";
        this.Indicators.Label.Foreground = "#FF506C7B";
        this.Picks.hasClicked = false;

        this.button.Opacity = 1;
        this.Picks.buttonText.Text = "Pick 3";
        this.Picks.buttonText["Canvas.Left"] = 35;
        this.buttonFour.Opacity = 1;
        this.buttonFive.Opacity = 1;

        CallStatus(3);
        CallStatus(4);
        CallStatus(5);
    },

    HideButtons: function() {
        //Hide buttons on play
        this.buttonFour.Opacity = 0;
        this.buttonFive.Opacity = 0;
        this.button.Opacity = 1;
        this.Picks.buttonText.Text = "Start Over";
        this.Picks.buttonText["Canvas.Left"] = 25;
    }
}
