//
// Gennady Yurkin
//
// $Id: WhatsGrillingList.js,v 1.4.2.9.2.13 2007/09/25 13:17:27 akovalev Exp $
//

//--------------------------------- Table Events --------------------------------------

function fnMouseClick(item, id, linkFunctionScript)
{
	var table = get_itemsGrid();//document.getElementById('whatsGrilling_whatsGrillingList_itemsGrid');
	
	try
	{
		if(iSelItem != -1)
		{
			table.rows[iSelItem].style.backgroundColor='';
		}
	}
	catch(e){}

	item.style.backgroundColor='#FF9933';
	iSelItem = id;	
	try
	{ 
		//eval(linkFunctionScript);
	}
	catch(e){}
	
	cookoutCalculator.WhatsGrillingItemSelected(id);
	FillWhatsGrillingList();
	return false;
}

function fnMouseOver(item, id)
{
	if (id == iSelItem)
	{
		item.style.backgroundColor='#FF9933';
	}
	else
	{
		item.style.backgroundColor='#ffe5cc';
	}
}

function fnMouseOut(item, id)
{
	if (id == iSelItem)
	{
		item.style.backgroundColor='#FF9933';
	}
	else
	{
		item.style.backgroundColor='';
	}
}


//------------------------------ Table Row Builder -----------------------------------

var localItemsList = null;

function AddRows(itemsList)
{
	localItemsList = itemsList;
	var table = get_itemsGrid();
	var tbody = table.getElementsByTagName("TBODY")[0];
	var index = 0;

	for(var property in itemsList)
	{
		var tr = RowBuilder(itemsList[property], itemsList[property].Id);
		if(itemsList[property].Id == iSelItem) tr.style.backgroundColor='#FF9933';
		tbody.appendChild(tr);
	}
}

function GetValidName(cookoutItem)
{
	var itemName = (cookoutItem.Name == '') ? cookoutItem.Cut.Desc : cookoutItem.Name;
	var namePrefix = (cookoutItem.DublicateNumber != 0) ? "#" + cookoutItem.DublicateNumber : "";
	
	return namePrefix + " " + itemName;
}

function ClearList()
{
	var table = get_itemsGrid();
	var tbody = table.getElementsByTagName("TBODY")[0];
	
	while(table.rows.length > 1) table.deleteRow(1);
	
}

function AddRowsTimeout(timeout)
{
	setTimeout("AddRows(Cookout.CloneItems);", timeout);
}

function RowBuilder(cookoutItem, index)
{
	var tr = document.createElement('tr');
	tr.setAttribute("class", "wglHeader");
	var id = index;//cookoutItem.Id;

	tr.onclick     = function(){ fnMouseClick(this, id, "OnClick('whatsGrilling_whatsGrillingList', " + id + " );"); };
	tr.onmouseover = function(){ fnMouseOver(this, id); };
	tr.onmouseout  = function(){ fnMouseOut(this, id); };

	tr.style.cursor = "hand";
	
	tr.appendChild(CreateCell('lR tQty',  cookoutItem.Qty));
	tr.appendChild(CreateCell('lR tCom',  cookoutItem.Cut.Name));
	tr.appendChild(CreateCell('lR tThic', cookoutItem.Thickness.Name));
	tr.appendChild(CreateCell('lR tDonn', cookoutItem.Doneness.Name));
	tr.appendChild(CreateCell('lR tND',   GetValidName(cookoutItem), 'tNDLimit'));
	tr.appendChild(CreateCell('lR tPN',   cookoutItem.Notes, 'tNPLimit'));

	return tr;
}

function CreateCell(className, text, textClass)
{
	var td  = document.createElement('td');
	var div = document.createElement('div');
	
	td.className = className;
	div.className = textClass;
	
	div.appendChild(document.createTextNode(text));
	td.appendChild(div);
	
	return td;
}

function FillWhatsGrillingList()
{
	if(!IsTimelineSelected)
	{
		ClearList();
		AddRows(Cookout.CloneItems);
	}
}

var iSelItem = -1;

function fnSetSelectedIndex(index)
{
	iSelItem = index;
}

function get_itemsGrid()
{
	return document.getElementById('ItemsGrid');
}
