﻿if (!window.windows_red) window.pc_flip = {};

pc_flip.Base = function() {
    this.control = null;
    this.rootCanvas = null;
    this.root = null;
    this.frontComputer = 1;
    this.autoRotate = 7000;
    
    this.learnUrl1 = "http://www.theultimatesteal.com/red";
    this.learnUrl2 = "products.aspx";
    this.learnUrl3 = "windows.aspx";
}

pc_flip.Base.prototype = {
    dispose: function() { this.root = null; },
    initialize: function(control, userContext, rootElement)
    {
        this.control = control;
        this.data = null;
        this.root = this.control.content;
        this.rootCanvas = this.root.findName("rootCanvas");

        this.registerButton(this.rootCanvas.findName("right_turn_cube"), this.rOverRight, this.rOutRight, this.clickRight);
        this.registerButton(this.rootCanvas.findName("left_turn_cube"), this.rOverLeft, this.rOutLeft, this.clickLeft);
        this.registerButton(this.rootCanvas.findName("left_action_cube"), this.rOverLeftAction, this.rOutLeftAction, this.clickRight);
        this.registerButton(this.rootCanvas.findName("right_action_cube"), this.rOverRightAction, this.rOutRightAction, this.clickLeft);

        this.registerButton(this.rootCanvas.findName("learn_button"), this.rOverLearn, this.rOutLearn, this.clickLearn);
        this.registerButton(this.rootCanvas.findName("buy_button"), this.rOverBuy, this.rOutBuy, this.clickBuy);

        this.rootCanvas.findName("zIndexTimerLeft").addEventListener("Completed", Silverlight.createDelegate(this, this.runZindexLeft));
        this.rootCanvas.findName("zIndexTimerRight").addEventListener("Completed", Silverlight.createDelegate(this, this.runZindexRight));
        this.rootCanvas.findName("loadRun").addEventListener("Completed", Silverlight.createDelegate(this, this.autoRotateReset));

        this.rootCanvas.findName("buy_button_fade_out").addEventListener("Completed", Silverlight.createDelegate(this, this.buyButtonFadeOut));
        this.rootCanvas.findName("buy_button_fade_in").addEventListener("Completed", Silverlight.createDelegate(this, this.buyButtonFadeIn));
        this.rootCanvas.findName("buy_button").opacity = 0;
    },

    buyButtonFadeIn: function(sender, args)
    {
        this.rootCanvas.findName("buy_button").isHitTestVisible = true;
    },

    buyButtonFadeOut: function(sender, args)
    {
        this.rootCanvas.findName("buy_button").isHitTestVisible = false;
    },

    registerButton: function(button, overAction, outAction, clickAction)
    {
        if (overAction != null)
            button.addEventListener("MouseEnter", Silverlight.createDelegate(this, overAction));
        if (outAction != null)
            button.addEventListener("MouseLeave", Silverlight.createDelegate(this, outAction));
        if (clickAction != null)
            button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, clickAction));
    },

    autoRotateReset: function(sender, eventArgs)
    {
        this.autoInterval = setInterval(Silverlight.createDelegate(this, this.clickRight), this.autoRotate);
    },

    rOverLearn: function(sender, eventArgs) { this.rootCanvas.findName("learn_over").begin(); },
    rOutLearn: function(sender, eventArgs) { this.rootCanvas.findName("learn_out").begin(); },
    clickLearn: function(sender, eventArgs) { window.location = (this["learnUrl" + this.frontComputer]); },

    rOverBuy: function(sender, eventArgs) { this.rootCanvas.findName("buy_over").begin(); },
    rOutBuy: function(sender, eventArgs) { this.rootCanvas.findName("buy_out").begin(); },
    clickBuy: function(sender, eventArgs)
    {
        if (this.frontComputer == 3)
        {
            openModal('modEtailers');
        }
        else
        {
            window.location = ('http://switch.atdmt.com/action/AOJoinRedHomeBuy');
        }
    },

    rOverRight: function(sender, eventArgs) { this.rootCanvas.findName("right_over").begin(); },
    rOutRight: function(sender, eventArgs) { this.rootCanvas.findName("right_out").begin(); },
    rOverLeft: function(sender, eventArgs) { this.rootCanvas.findName("left_over").begin(); },
    rOutLeft: function(sender, eventArgs) { this.rootCanvas.findName("left_out").begin(); },

    rOverRightAction: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("c2_fade_out").begin();
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("c3_fade_out").begin();
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("c1_fade_out").begin();
        }
    },
    rOutRightAction: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("c2_fade_in").begin();
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("c3_fade_in").begin();
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("c1_fade_in").begin();
        }
    },
    rOverLeftAction: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("c3_fade_out").begin();
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("c1_fade_out").begin();
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("c2_fade_out").begin();
        }
    },
    rOutLeftAction: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("c3_fade_in").begin();
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("c1_fade_in").begin();
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("c2_fade_in").begin();
        }
    },

    clickLeft: function(sender, eventArgs)
    {
        this.rootCanvas.findName("learn_more_text").Text = 'Learn more';
        this.rootCanvas.findName("learn_more_text")["Canvas.Left"] = 8.239;

        if (this.frontComputer == 1)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("c1_left_1to3").begin();
            this.rootCanvas.findName("c2_left_2to1").begin();
            this.rootCanvas.findName("c3_left_3to2").begin();
            this.rootCanvas.findName("buy_button_fade_in").begin();
            this.rootCanvas.findName("zIndexTimerLeft").begin();
            this.frontComputer = 2;
        }
        else if (this.frontComputer == 2)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("c1_left_3to2").begin();
            this.rootCanvas.findName("c2_left_1to3").begin();
            this.rootCanvas.findName("c3_left_2to1").begin();
            this.rootCanvas.findName("buy_button_fade_in").begin();
            this.rootCanvas.findName("zIndexTimerLeft").begin();
            this.frontComputer = 3;
        }
        else if (this.frontComputer == 3)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("learn_more_text").Text = 'Buy now';
            this.rootCanvas.findName("learn_more_text")["Canvas.Left"] = 19;
            this.rootCanvas.findName("c1_left_2to1").begin();
            this.rootCanvas.findName("c2_left_3to2").begin();
            this.rootCanvas.findName("c3_left_1to3").begin();
            this.rootCanvas.findName("buy_button_fade_out").begin();
            this.rootCanvas.findName("zIndexTimerLeft").begin();
            this.frontComputer = 1;
        }
        this.autoRotateReset();
    },
    clickRight: function(sender, eventArgs)
    {
        this.rootCanvas.findName("learn_more_text").Text = 'Learn more';
        this.rootCanvas.findName("learn_more_text")["Canvas.Left"] = 8.239;

        if (this.frontComputer == 1)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("c1_right_1to2").begin();
            this.rootCanvas.findName("c2_right_2to3").begin();
            this.rootCanvas.findName("c3_right_3to1").begin();
            this.rootCanvas.findName("buy_button_fade_in").begin();
            this.rootCanvas.findName("zIndexTimerRight").begin();
            this.frontComputer = 3;
        }
        else if (this.frontComputer == 3)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("c1_right_2to3").begin();
            this.rootCanvas.findName("c2_right_3to1").begin();
            this.rootCanvas.findName("c3_right_1to2").begin();
            this.rootCanvas.findName("buy_button_fade_in").begin();
            this.rootCanvas.findName("zIndexTimerRight").begin();
            this.frontComputer = 2;
        }
        else if (this.frontComputer == 2)
        {
            clearInterval(this.autoInterval);
            this.rootCanvas.findName("learn_more_text").Text = 'Buy now';
            this.rootCanvas.findName("learn_more_text")["Canvas.Left"] = 19;
            this.rootCanvas.findName("c1_right_3to1").begin();
            this.rootCanvas.findName("c2_right_1to2").begin();
            this.rootCanvas.findName("c3_right_2to3").begin();
            this.rootCanvas.findName("buy_button_fade_out").begin();
            this.rootCanvas.findName("zIndexTimerRight").begin();
            this.frontComputer = 1;
        }
        this.autoRotateReset();
    },
    runZindexLeft: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 10;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 5;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 7;
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 7;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 10;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 5;
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 5;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 7;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 10;
        }
    },
    runZindexRight: function(sender, eventArgs)
    {
        if (this.frontComputer == 1)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 10;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 7;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 5;
        }
        else if (this.frontComputer == 2)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 5;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 10;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 7;
        }
        else if (this.frontComputer == 3)
        {
            this.rootCanvas.findName("computer1")["Canvas.ZIndex"] = 7;
            this.rootCanvas.findName("computer2")["Canvas.ZIndex"] = 5;
            this.rootCanvas.findName("computer3")["Canvas.ZIndex"] = 10;
        }
    }


}
