/**
 * @author boro
 */
Team = Class.extend({
	construct: function(name, combinationsN){
		this.name = name;
		this.maxCombinations = combinationsN;
		this._combinations = new Array;
	},
	
	addCombination: function(n){
		if(this.isFull())return false;
		this._combinations.push(n);
		return true;
	},
	
	hasCombination: function(n){
		for(var i=0; i< this._combinations.length; i++){
			if(this._combinations[i] == n)return true;
		}
		return false;
	},
	
	isFull: function(){
		if(this._combinations.length >= this.maxCombinations) return true;
		return false;
	},
	
	getCombinations: function(){
		return this._combinations.join(", ");
	},
	
	resetArray: function(){
		this._combinations = null;
		this._combinations = new Array();
	}
});

Lottery = {
	TeamsPoll: { 
		first: new Team("30th", 250),
		second: new Team("29th", 188),
		third: new Team("28th", 142),
		fourth: new Team("27th", 107),
		fifth: new Team("26th", 81)
	},

	allCombinations: null,
	populateAllCombinations: function(){
		Lottery.allCombinations = null;
		Lottery.allCombinations = new Array();
		Lottery.TeamsPoll.first.resetArray();
		Lottery.TeamsPoll.second.resetArray();
		Lottery.TeamsPoll.third.resetArray();
		Lottery.TeamsPoll.fourth.resetArray();
		Lottery.TeamsPoll.fifth.resetArray();
		for(var i=0; i<1000; i++){
			Lottery.allCombinations.push(i+1);
		}
		return true;
	},
	
	dealCombinations: function(){
		if(!Lottery.allCombinations)return false;
		
		if(Lottery.allCombinations.length <= 0)return false;
		
		var stop = false;
		
		while (!stop) {
		
			var i = Math.floor(Math.random() * Lottery.allCombinations.length);
			
			//document.getElementById("panel").innerHTML = Lottery.allCombinations[i];
			
			if (!Lottery.TeamsPoll.fifth.isFull()) 
				Lottery.TeamsPoll.fifth.addCombination(Lottery.allCombinations[i]);
			else 
				if (!Lottery.TeamsPoll.fourth.isFull()) 
					Lottery.TeamsPoll.fourth.addCombination(Lottery.allCombinations[i]);
				else 
					if (!Lottery.TeamsPoll.third.isFull()) 
						Lottery.TeamsPoll.third.addCombination(Lottery.allCombinations[i]);
					else 
						if (!Lottery.TeamsPoll.second.isFull()) 
							Lottery.TeamsPoll.second.addCombination(Lottery.allCombinations[i]);
						else 
							if (!Lottery.TeamsPoll.first.isFull()) 
								Lottery.TeamsPoll.first.addCombination(Lottery.allCombinations[i]);
							else 
								stop = true;
			
			Lottery.allCombinations[i] = Lottery.allCombinations.pop();
		}
		
		return true;
		//Lottery.dealCombinations();
	},
	
	draft: function(){
		return Math.floor(Math.random() * 1000) + 1;
	},
	
	getWiningTeam: function(combination){
		if(Lottery.TeamsPoll.fifth.hasCombination(combination))
			return Lottery.TeamsPoll.fifth;
		else
		if(Lottery.TeamsPoll.fourth.hasCombination(combination))
			return Lottery.TeamsPoll.fourth;
		else
		if(Lottery.TeamsPoll.third.hasCombination(combination))
			return Lottery.TeamsPoll.third;
		else
		if(Lottery.TeamsPoll.second.hasCombination(combination))
			return Lottery.TeamsPoll.second;
		else
		if(Lottery.TeamsPoll.first.hasCombination(combination))
			return Lottery.TeamsPoll.first;
		else
			return false;
	}
	
}

GUI = {
	panel: null,
	firstPanel: null,
	secondPanel: null,
	thirdPanel: null,
	fourthPanel: null,
	fifthPanel: null,
	othersPanel: null,
	
	runDraft: function(){
		GUI.panel.innerHTML = "Calculating, please wait.";
		//POPULATE
		if (!Lottery.populateAllCombinations()) {
			alert("Failed to populate combinations array.");
			return false;
		}
		//DEAL
		if (!Lottery.dealCombinations()) {
			alert("Failed to deal combinations.");
			return false;
		}
		GUI.firstPanel.innerHTML = Lottery.TeamsPoll.first.getCombinations();
		GUI.secondPanel.innerHTML = Lottery.TeamsPoll.second.getCombinations();
		GUI.thirdPanel.innerHTML = Lottery.TeamsPoll.third.getCombinations();
		GUI.fourthPanel.innerHTML = Lottery.TeamsPoll.fourth.getCombinations();
		GUI.fifthPanel.innerHTML = Lottery.TeamsPoll.fifth.getCombinations();
		if(GUI.othersPanel)
			GUI.othersPanel.innerHTML = Lottery.allCombinations.join(", ");
			
		//DRAFT
		var draftCombination = Lottery.draft();
		if(GUI.fifthPanel.innerHTML.match(" "+draftCombination+","))
			GUI.fifthPanel.innerHTML = GUI.fifthPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");
		else 
		if(GUI.fourthPanel.innerHTML.match(" "+draftCombination+","))
			GUI.fourthPanel.innerHTML = GUI.fourthPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");
		else 
		if(GUI.thirdPanel.innerHTML.match(" "+draftCombination+","))
			GUI.thirdPanel.innerHTML = GUI.thirdPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");
		else 
		if(GUI.secondPanel.innerHTML.match(" "+draftCombination+","))
			GUI.secondPanel.innerHTML = GUI.secondPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");
		else 
		if(GUI.firstPanel.innerHTML.match(" "+draftCombination+","))
			GUI.firstPanel.innerHTML = GUI.firstPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");
		else 
		if(GUI.othersPanel.innerHTML.match(" "+draftCombination+","))
			GUI.othersPanel.innerHTML = GUI.othersPanel.innerHTML.replace(" "+draftCombination+",", "<span class=\"hit\"> "+draftCombination+",</span>");


		var winTeam = Lottery.getWiningTeam(draftCombination);
		if(winTeam) GUI.panel.innerHTML = "And Stamkos goes to: "+winTeam.name+" team. ("+draftCombination+")";
		else GUI.panel.innerHTML = "The Lottery was won by team not in one of last 5 places, so Stamkos goes to "+Lottery.TeamsPoll.first.name+" team.";									
		
	}
}
