
var Commands = function(list, treats){
	this.commandList = list;
	this.treats = treats;
}

Commands.prototype = {
	
	tricks: [],
	currentTrickIndex: null,
	
	init: function(){
		
		var links = Dom.getElementsByClassName('commandLink', 'span', 'commandList');
		
		for(var i=0; i < this.commandList.length; i++){
			var trick = new Commands.Trick(this.commandList[i], links[i], this.treats);
			this.tricks.push(trick);
			Event.addListener(links[i], 'click', this.commandSelected.createDelegate(this, [i]));
		}
		
		petProfile.treat.deliveredEvent.subscribe(this.treatGiven, this, true);
		
	},
	
	commandSelected: function(index){
		
		if(this.currentTrickIndex != null){
			this.tricks[this.currentTrickIndex].link.style.fontWeight = "normal";
			this.tricks[this.currentTrickIndex].link.className = "giveBoneLink";
		}
		
		this.currentTrickIndex = index;
		this.tricks[this.currentTrickIndex].makeTrick();
		this.tricks[this.currentTrickIndex].link.style.fontWeight = "bold";
		this.tricks[this.currentTrickIndex].link.className = "";
	},
	
	treatGiven: function(){
		if(this.currentTrickIndex !== null){
			this.tricks[this.currentTrickIndex].addTreat();
		}
	}
	
}

Commands.Trick = function(data, link, treat){
	this.data = data;
	this.link = link;
	this.treat = treat;
}

Commands.Trick.prototype = {
	
	receivedTreats:0,
	
	makeTrick: function(){
		
		if(this.receivedTreats >= this.data.treats){
			petProfile.mainPic.showMessage(this.data.message);
			petProfile.mainPic.change(this.data.big_size_file);
		}
		else{
			
			petProfile.treat.messagesEnabled = false;
			
			var remain = this.data.treats - this.receivedTreats;
			
			if(this.receivedTreats == 0){
				var message = "Please give me " + remain +" " + (remain!=1?this.treat[1]:this.treat[0])+" to follow your command!";
			}
			else{
				var message = remain +" more " + (remain!=1?this.treat[1]:this.treat[0])+"!"
			}	
			
			petProfile.mainPic.showMessage(message);
			
		}

	},
	
	addTreat: function(){
		this.receivedTreats++;
		this.makeTrick();
	}
	
}
