//
// Gennady Yurkin
//
// $Id: WhatsGrilling.js,v 1.3.2.2.2.10 2007/10/11 12:11:21 vvisich Exp $
//


//----------------------- Whats Grilling States -----------------------
// Base state class for server logic implementation

var WhatsGrillingState = Base.extend
({
	AddButtonText : "",
	RemoveEnabled : false,
	CancelEnabled : false,
	
	WSS_THIS : null,
	
	constructor : function(addButtonText, removeEnabled, cancelEnabled)
	{
		this.AddButtonText = addButtonText;
		this.RemoveEnabled = removeEnabled;
		this.CancelEnabled = cancelEnabled;
		WSS_THIS = this;
	},
	
	CancelClick : function()
	{
		iSelItem = -1;
		cookoutCalculator.ResetselectedItemIdFromWhatsGrillingList();
		
		FillWhatsGrillingList();
		return new AddState();
	},
	
	RemoveClick : function()
	{
		Anthem_InvokeControlMethod
		(
			"whatsGrilling", 
			"RemoveItem", 
			[ItemIdToIndex(iSelItem)], 
			function(result)
			{
				Reset();
			}
		);
		cookoutCalculator.ResetselectedItemIdFromWhatsGrillingList();
		return new AddState();
	}
});

var AddState = WhatsGrillingState.extend
({
	constructor : function()
	{
		this.base("Add to List", false, false);
	}, 
	
	FavoriteChangeMission : function()
	{
		return new StoreAndAddState();
	},
	
	AddItemClick : function(cutType, cut, thickness, doneness)
	{
		Anthem_InvokeControlMethod("whatsGrilling", "AddItem", [ItemIdToIndex(iSelItem), cutType, cut, thickness, doneness], function(result){});
		return new AddState();
	}
});

var StoreAndAddState = WhatsGrillingState.extend
({
	constructor : function()
	{
		this.base("Store & Add to List", false, false);
	}, 
	
	FavoriteChangeMission : function()
	{
		return new AddState();
	},
	
	AddItemClick : function(cutType, cut, thickness, doneness)
	{
		Anthem_InvokeControlMethod
		(
			"whatsGrilling", 
			"StoreAndAddItem", 
			[ItemIdToIndex(iSelItem), cutType, cut, thickness, doneness], 
			function(result)
			{
				if(result.value)
				{
					var itemFavorite = get_Favorite();
					itemFavorite.checked = false;
					cookoutCalculator.ItemFavoriteChekedChanged();
				}
			}
		);
		return WSS_THIS;
	}
});

var UpdateState = WhatsGrillingState.extend
({
	constructor : function()
	{
		this.base("Update", true, true);
	}, 
	
	FavoriteChangeMission : function()
	{
		return new StoreAndUpdateState();
	},
	
	AddItemClick : function(cutType, cut, thickness, doneness)
	{
		Anthem_InvokeControlMethod("whatsGrilling", "UpdateItem", [ItemIdToIndex(iSelItem), cutType, cut, thickness, doneness], function(result){});
		return new AddState();
	}
});

var StoreAndUpdateState = WhatsGrillingState.extend
({
	constructor : function()
	{
		this.base("Store & Update", true, true);
	}, 
	
	FavoriteChangeMission : function()
	{
		return new UpdateState();
		//return new AddState();
		
	},
	
	AddItemClick : function(cutType, cut, thickness, doneness)
	{
		Anthem_InvokeControlMethod
		(
			"whatsGrilling", 
			"StoreAndUpdateItem", 
			[ItemIdToIndex(iSelItem), cutType, cut, thickness, doneness], 
			function(result)
			{
				if(result.value)
				{
					var itemFavorite = get_Favorite();
					itemFavorite.checked = false;

					cookoutCalculator.ItemFavoriteChekedChanged();
				}
			}
		);
		
		return WSS_THIS;
	}
});

//----------------------  Whats Grilling Page Class  ------------------------

var WhatsGrilling = Base.extend
({
	GrillingState : null,
	WGTHIS        : null,
	
	constructor : function()
	{
		GrillingState = new AddState();
		WGTHIS        = this;
	},
	
	FavoriteChekedChanged : function(sender)
	{
		GrillingState = GrillingState.FavoriteChangeMission();
		//WGTHIS.UpdateControlVeiw();
		get_AddButton().value = GrillingState.AddButtonText;
		return false;
	},

	ItemSelected : function(id)
	{
		GrillingState = new UpdateState();
		WGTHIS.UpdateControlVeiw();
		SetSelectedItem(id);
		return true;
	},
	
	ClearState : function()
	{
		GrillingState = new AddState();
		WGTHIS.UpdateControlVeiw();
	},
	
	AddItemClick : function(cutType, cut, thickness, doneness)
	{
		GrillingState = GrillingState.AddItemClick(cutType, cut, thickness, doneness);
		WGTHIS.UpdateControlVeiw();
	},
	
	RemoveClick : function()
	{
		GrillingState = GrillingState.RemoveClick();
		WGTHIS.UpdateControlVeiw();
	},
	
	CancelClick : function()
	{
		GrillingState = GrillingState.CancelClick();
		WGTHIS.UpdateControlVeiw();
		Reset();
	},

	UpdateControlVeiw : function()
	{
		get_AddButton().value = GrillingState.AddButtonText;
		get_RemoveButton().disabled = (GrillingState.RemoveEnabled) ? "" : "disabled";
		get_CancelButton().disabled = (GrillingState.CancelEnabled) ? "" : "disabled";
	}

	
});

//----------------------- Whats Grilling Init ------------------------

var whatsGrilling = null;

function WhatsGrillingInit()
{
	whatsGrilling = new WhatsGrilling();
	whatsGrilling.UpdateControlVeiw();
}

function ItemIdToIndex(itemId)
{
	var index = 0;
	for(var property in Cookout.CloneItems)
	{
		if(Cookout.CloneItems[property].Id == itemId) return index;
		index++;
	}
	
	return -1;
}

function SetSelectedItem(id)
{
	for(var property in Cookout.CloneItems)
	{
		if(property == id)
		{
			get_qty().value  = Cookout.CloneItems[property].Qty;
			get_name().value = Cookout.CloneItems[property].Name;
			get_note().value = Cookout.CloneItems[property].Notes;
			get_Favorite().checked = "";
			InitStartValue(Cookout.CloneItems[property].CutType, Cookout.CloneItems[property].Cut.Id, Cookout.CloneItems[property].Thickness.Id, Cookout.CloneItems[property].Doneness.Id);
			return;
		}
	}
}
