if (!window.Simon)
	window.Simon = {};

Simon.Scene = function() 
{
}

Simon.Scene.prototype =
{
	handleLoad: function(plugIn, userContext, rootElement) 
	{
		this.plugIn = plugIn;
		this.rootElement = rootElement;
		this.sideBarState = 0;
		this.waitingForInput = false;
		
		this.red = rootElement.findname("red");
		this.blue = rootElement.findname("blue");
		this.green = rootElement.findname("green");
		this.yellow = rootElement.findname("yellow");
		this.start = rootElement.findname("start");
		this.showTimer = rootElement.findname("showTimer");
		this.playerTimer = rootElement.findname("playerTimer");
		this.nextLevelTimer = rootElement.findname("nextLevelTimer");
		this.collapseSideBar = rootElement.findname("collapseSideBar"); 
		this.expandSideBar = rootElement.findname("expandSideBar"); 
		this.txtCollapse = rootElement.findname("txtCollapse"); 
		this.volumeControl = rootElement.findname("imgVolume"); 
		this.volumeUp = rootElement.findname("volumeUp");
		this.volumeDown = rootElement.findname("volumeDown");
		
		this.start.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.startGame));
		this.txtCollapse.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleCollapse));
		
        this.red.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleKeyPress));		
        this.blue.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleKeyPress));		
        this.green.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleKeyPress));		
        this.yellow.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleKeyPress));		
        this.volumeControl.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVolume));		
		
		this.showTimer.addEventListener("Completed", Silverlight.createDelegate(this, this.handleShow));
		this.playerTimer.addEventListener("Completed", Silverlight.createDelegate(this, this.handleTimeout));
		this.nextLevelTimer.addEventListener("Completed", Silverlight.createDelegate(this, this.handleNextLevel));
		
		this.rootElement.addEventListener("KeyUp", Silverlight.createDelegate(this, this.handleKeyPress));
		
		this.disableInput();
		
	},
	
	
	handleMouseDown: function(sender, eventArgs) 
	{
		var mouseDownAnimation = sender.findName("mouseDown");
		mouseDownAnimation.stop();
		
		mouseDownAnimation["Storyboard.TargetName"] = sender.Name;
		
		var moveCL = sender.findName("moveCL");
		moveCL.from = sender["Canvas.Left"];
		moveCL.to = moveCL.from + 5;

		var moveCT = sender.findName("moveCT");
		moveCT.from = sender["Canvas.Top"];
		moveCT.to = moveCT.from + 5;
		
		mouseDownAnimation.begin(); 

	},
	
	startGame: function(sender, eventArgs) 
	{
	
	    if (this.sideBarState == 1)
	    {
	        this.handleCollapse(null, null);
	    }
	    
	    this.start.opacity = "0";
	
	    sender.findname("gameOver").opacity="0";
	    
	    this.score = 0;
	    this.level = 1;
	    this.showTimerDurationMS = 50;
	    this.sequences = new Array();
	    
	    this.setLevelSequence();
	    
	    this.showTimer.begin();
	    this.timerRunning = true;

	},
	
	setLevelSequence: function()
	{
		var sequence = "";
		
		if (this.level != 1)
		{
		    sequence = this.sequences[this.level - 2];
		}
    
        sequence += "|" + Math.ceil(Math.random() * 4);
		
		this.sequences.push(sequence);
		
		this.showNum = 0;
	    this.handleShow(null, null);
	},
	
	
	handleShow: function(sender, eventArgs)
	{
	    
    
	    this.rootElement.findname("txtLevel").text = '' + this.level;
	    this.rootElement.cursor = "Wait";
	    this.red.cursor = "Wait";
	    this.green.cursor = "Wait";
	    this.blue.cursor = "Wait";
	    this.yellow.cursor = "Wait";
	    
	    if (this.showNum < this.level)
	    {
	        var sequence = this.sequences[this.level -1];
	        var currentColourIdx = sequence.split("|")[this.showNum + 1];
	        var currentColour;
	        
	        switch (currentColourIdx)
	        {
	            case "1":
	                currentColour = "red";
	                break;
	                
	            case "2":
	                currentColour = "blue";
	                break;
	                
	            case "3":
	                currentColour = "green";
	                break;
	                
	            case "4":
	                currentColour = "yellow";
	                break
	                
	        }
	        
	        this.handleMouseDown (this.rootElement.findname(currentColour), null);
	        
            this.rootElement.findname(currentColour + "Beep").stop();
            this.rootElement.findname(currentColour + "Beep").play();
	        
	        this.showNum ++ ;
	        this.disableInput();
	        this.showTimer.begin();
	    }
	    else
	    {
	        
	        this.showTimer.stop();
	        this.rootElement.cursor = "Default";
       	    this.red.cursor = "Hand";
	        this.green.cursor = "Hand";
	        this.blue.cursor = "Hand";
	        this.yellow.cursor = "Hand";

	        this.playerTimer.begin();
	        this.enableInput();
	        this.sequenceCount = 0;
	    }
	
	},

	handleTimeout: function(sender, eventArgs) 
	{
	
        this.disableInput();	
	    this.playerTimer.stop();
	    this.showTimer.stop();
	    sender.findname("gameOver").opacity="1";
	    this.start.opacity = "1";

	},
	

	handleKeyPress: function(sender, keyEventArgs)
	{
	    
	    if (!this.waitingForInput)
	    {
	        return;
	    }
	    else
	    {
	        this.disableInput();
	    }
	
	    this.playerTimer.stop();
	    var currentColour = "";
	    var currentColourIdx = -1;
	    

	    
	    // Check for mouse click ...
	    if (sender.tostring() == "Rectangle")
	    {
	    	
	        currentColour = sender.name;
	        switch (currentColour)
	        {
	            case "red":
	                currentColourIdx = 1;
	                break;
	                
	            case "blue":
      	            currentColourIdx = 2;
	                break;

	            case "green":
      	            currentColourIdx = 3;
	                break;

	            case "yellow":
                    currentColourIdx = 4;
	                break;
	        }
	    
	    }
	    else
	    {
            switch (keyEventArgs.key)
            {
                case 75:    // 7 Red
                case 54:    // y
            	    currentColour = "red";
            	    currentColourIdx = 1;
                    break;
                    
                case 76:    // 8 Blue
                case 50:    // u
                    currentColour = "blue";
                    currentColourIdx = 2;
                    break;
                    
                case 72:    // 4 Green
                case 37:    // h
                    currentColour = "green";
                    currentColourIdx = 3;
                    break;
                    
                case 73:    // 5 Yellow
                case 39:    // j
                    currentColour = "yellow";
                    currentColourIdx = 4;
                    break;
            }
        }
        
        if (currentColourIdx != -1)
        {
            this.handleMouseDown (this.rootElement.findname(currentColour), null);
            this.rootElement.findname(currentColour + "Beep").stop();
            this.rootElement.findname(currentColour + "Beep").play();
        }
        else
        {
            this.handleTimeout(sender, null);
            return;
        }
        
        //Check for right input
        this.sequenceCount ++;
	    var sequence = this.sequences[this.level -1];
	    if (sequence.split("|")[this.sequenceCount] != currentColourIdx)
	    {
            this.handleTimeout(sender, null);
            return;
	    }
       
        if (this.sequenceCount == this.level)
        {
            this.nextLevelTimer.begin();
	    }
	    else
	    {
	        //Wait for next input
	        this.playerTimer.begin();
	        this.enableInput();
	    }
	},
	
	handleCollapse: function(sender, keyEventArgs)
	{
        if (this.sideBarState == 0)
        {
     	    this.collapseSideBar.stop();
		    this.expandSideBar.begin();
		    this.sideBarState = 1;
		    this.txtCollapse.text = "<";
        }
        else
        {
       	    this.collapseSideBar.begin();
		    this.expandSideBar.stop();
		    this.sideBarState = 0;
		    this.txtCollapse.text = ">";
        }
	},
		
	handleNextLevel: function(sender, keyEventArgs)
	{
	
	    // Start Next Level
	    
	    this.nextLevelTimer.stop();
        this.level ++;
        this.setLevelSequence();
        this.showTimerDurationMS -= 1;
        this.showTimer.Duration = "0:0:0." + this.showTimerDurationMS;
	    this.showTimer.begin();
	},
	
	disableInput: function()
	{
	    this.waitingForInput = false;
	    this.red.IsHitTestVisible = false;
		this.blue.IsHitTestVisible = false;
		this.green.IsHitTestVisible = false;
		this.yellow.IsHitTestVisible = false;
	
	},

	enableInput: function()
	{
	    this.waitingForInput = true;
	    this.red.IsHitTestVisible = true;
		this.blue.IsHitTestVisible = true;
		this.green.IsHitTestVisible = true;
		this.yellow.IsHitTestVisible = true;
	
	},
	
	handleVolume: function(sender, eventArgs)
	{
	
        var newVolume = 0;
	    if (sender.findname("blueBeep").volume == 0)
	    {
	        newVolume = 1;
	    }
	    
	    sender.findname("blueBeep").volume = newVolume;
        sender.findname("redBeep").volume = newVolume;
	    sender.findname("greenBeep").volume = newVolume;
        sender.findname("yellowBeep").volume = newVolume;

        if (newVolume == 0 )
        {
            this.volumeUp.begin();
            this.volumeDown.stop();
        }
        else
        {
            this.volumeDown.begin();
            this.volumeUp.stop();

        }
	}
	
}
