function Bingo()
{
	this.oldNum = new Array();
	this.Numbers = 0;
}

Bingo.prototype.handleLoad = function(control, context, sender)
{
	this.sender = sender;
	this.Status = null;
	this.View = "now";
	
	for(var i = 0; i < 75; i++){
		var text = new TextBlock(this.sender.FindName("oldNum"), (i+1).toString());
		var data = (i + 1).toString();
		if(i < 9) data = "0" + data;
		text.Text.SetText(data);
		text.Text.SetColor("#FFFFFF");
		text.Text.SetFontSize(70);
		
		text.Position.Set(parseInt(i/10) * 110,(i-(parseInt(i/10)*10)) * 60);
	}
	this.sender.AddEventListener("KeyDown",Silverlight.createDelegate(this,this.onKeyDown));
	
	this.sender.FindName("timer").AddEventListener("Completed",Silverlight.createDelegate(this,this.timerCompleted));
	this.sender.FindName("wait5").AddEventListener("Completed",Silverlight.createDelegate(this,this.wait));
	
	this.sender.FindName("randamMusic").AddEventListener("MediaEnded",Silverlight.createDelegate(this,this.mediaRepeat));

}

Bingo.prototype.wait = function(sender, e)
{
	for(var i=0;i<7; i++){
		sender.FindName(1 + "_" + i).Stroke = "#00FF00";
		sender.FindName(2 + "_" + i).Stroke = "#00FF00";
	}
	this.Status = "Stop";
}

Bingo.prototype.mediaRepeat = function(sender, e)
{
	timeChange(this.sender.FindName("randamMusic"), 0);
	//this.sender.FindName("randamMusic").stop();
	this.sender.FindName("randamMusic").play();
}

Bingo.prototype.randam = function()
{
	this.sender.FindName("randamMusic").play();
	this.Status = "Start";
	this.sender.FindName("timer").begin();
	//this.sender.FindName("randamMusic").play();
	
}

Bingo.prototype.timerCompleted = function(sender, e)
{
    if(this.Status != "Stop" && this.Status != "Wait"){
    
    	for(var i=0;i<7; i++){
    		if(this.Status != "Stop"){
				sender.FindName(1 + "_" + i).Opacity = Math.floor(Math.random() * 2) + 0.1;
				sender.FindName(2 + "_" + i).Opacity = Math.floor(Math.random() * 2) + 0.1;
			}
		}
	
		
		sender.begin();
	}
}

Bingo.prototype.BingoNum = function()
{
	if(this.Numbers < 75){
		do{
			var flag = 0;
			var rNum = Math.floor(Math.random() * 75) + 1;
			for(i=0;i<this.Numbers;i++){
				if(rNum == this.oldNum[i]){
					flag = 1;
				}
			}
			//alert(flag);
			//alert();
		}while(flag > 0);
		this.oldNum[this.Numbers] = rNum;
		
		this.Numbers++;
		this.sender.FindName("Number").Text = this.Numbers.toString();
		
		return rNum;
	}else{
		return 0;
	}
}

Bingo.prototype.onKeyDown = function(sender, e)
{
	
	if(e.Key == 35){
		//FullSc(sender);
	}
	//if(e.Shift){alert();}
	if(e.Key == 3){
		if(this.View == "now"){
			if(this.Status == "Stop"){
				for(var i=0;i<7; i++){
					sender.FindName(1 + "_" + i).Stroke = "#FF0000";
					sender.FindName(2 + "_" + i).Stroke = "#FF0000";
				}
				this.Status = "Start";
				this.sender.FindName("timer").begin();
				this.sender.FindName("randamMusic").play();
			}else if(this.Status == null){
				this.randam();
			}else if(this.Status == "Start"){
				var rNum = this.BingoNum();
				
				this.sender.FindName("randamMusic").stop();
				this.sender.FindName("ex").Volume = 1;
				this.sender.FindName("ex").Source = "./media/ex" + Math.floor(Math.random() * 14) + ".mp3";
				this.sender.FindName("ex").play();
				
				
				this.Status = "Wait";
				if(rNum < 10){
					var num2 = NumberG(rNum).split(",");
					var num1 = NumberG(0).split(",");
				}else{
					var num1 = NumberG(rNum.toString().charAt(0)).split(",");
					var num2 = NumberG(rNum.toString().charAt(1)).split(",");
				}
				
				for(var i=0;i<7; i++){
					sender.FindName(1 + "_" + i).Opacity = 0.1;
					sender.FindName(2 + "_" + i).Opacity = 0.1;
				}
				for(i=0;i<num1.length;i++){
					sender.FindName(1 + "_" + num1[i]).Opacity = 1;
				}
				for(i=0;i<num2.length;i++){
					sender.FindName(2 + "_" + num2[i]).Opacity = 1;
				}
				
				this.sender.FindName("wait5").begin();
			}
		}
	}
	if(e.Key == 42){
		if(this.sender.FindName("randamMusic").Volume == 0){
			this.sender.FindName("randamMusic").Volume = 1;
			//this.sender.FindName("ex").Volume = 1;
		}else{
			this.sender.FindName("randamMusic").Volume = 0;
			//this.sender.FindName("ex").Volume = 0;
		}
	}
	if(e.Key == 9){
		if(this.sender.FindName("oldNum").Opacity != 1){
			this.sender.FindName("oldNum").Opacity = 1;
			//this.sender.FindName("oldNum").children.GetItem(0).Text = this.oldNum[this.Numbers - 1] + "," + this.oldNum[this.Numbers - 2];
			//this.sender.FindName("oldNum").children.GetItem(1).Text = this.oldNum[this.Numbers - 3] + "," + this.oldNum[this.Numbers - 4];
			for(var i =0; i < this.oldNum.length; i++){
				this.sender.FindName("text_" + this.oldNum[i]).Foreground = "#FF0000";
			}
			this.View = "old";
		}else{
			this.sender.FindName("oldNum").Opacity = 0;
			this.View = "now";
		}
	}
	//alert(e.Key);
	
	//alert(e.PlatformKeyCode);
}

function FullSc(sender)
{
	slplug = sender.getHost();
	
	sender.FindName("root").width = slplug.content.actualWidth;
	sender.FindName("root").height = slplug.content.actualHeight;

	slplug.content.fullscreen = !slplug.content.fullscreen;
}

function NumberG(num)
{
	if(num == 0)	return "0,1,2,3,4,5";
	if(num == 1)	return "1,2";
	if(num == 2)	return "0,1,3,4,6";
	if(num == 3)	return "0,1,2,3,6";
	if(num == 4)	return "1,2,5,6";
	if(num == 5)	return "0,2,3,5,6";
	if(num == 6)	return "0,2,3,4,5,6";
	if(num == 7)	return "0,1,2,5";
	if(num == 9)	return "0,1,2,3,5,6";
	
	return	"0,1,2,3,4,5,6";
}

function ShowNumber()
{
}

function timeChange(media,val)
{
	var timeSpan = media.position;
        timeSpan.seconds = val;
        media.position = timeSpan;
}
