function Item(parent, text, value, url)
{
	this.Text   = text;
	this.Value  = value;
	this.Parent = parent;
	this.Url    = url;
}

function DataSource()
{
	this.Items = new Array;
	
	this.Add = function(parent, text, value, url)
	{
		this.Items[this.Items.length] = new Item(parent, text, value, url);
	}
	
	this.GetByValue = function(value)
	{
		for(var index = 0; index < this.Items.length; index++)
		{
			if ( this.Items[index].Value == value)
				return this.Items[index];
		}
		return null;
	}
	
	this.ItemsEx = function(parent)
	{
		var subDataSource = new DataSource();
		for(var index = 0; index < this.Items.length; index++)
		{
			if ( this.Items[index].Parent == parent || this.Items[index].Parent == -1)
				subDataSource.Add(this.Items[index].Parent, this.Items[index].Text, this.Items[index].Value, this.Items[index].Url);
		}
				
		return subDataSource;
	}
}

function HolderGamma(gamma, lineaObject)
{
	this.Holder			= gamma;
	this.Selected		= null;
	this.HolderLinea	= lineaObject;
	
	this.ClearSelection = function()
	{
		if ( this.Selected != null )
			this.Selected.className = "";
	}
	
	this.SetSelection = function(e, id)
	{
		document.getElementById("h_L").style.visibility = "visible";
		document.getElementById("h_P").style.visibility = "hidden";
		this.ClearSelection();
		e.className = "slc";
		this.Selected = e;
		if ( id != null )
			this.HolderLinea.Fill(id);
	}
	
	this.SetSelectionById = function(id)
	{
		this.SetSelection( this.GetAnchor(g.GetByValue(id)), id );
	}
	
	this.Clear = function()
	{
		this.Holder.innerHTML = "";
	}
	
	this.AnchorId = function(item)
	{
		return "g_" + item.Value;
	}
	
	this.Anchor = function(item, selected)
	{
		anchor = "<td><a id=\"" + this.AnchorId(item) + "\" ";
		if ( selected )
			anchor += " class=\"slc\" ";
		anchor += "onclick=\"hGamma.SetSelection(this," + item.Value  + ");\" ";
		anchor += "href=\"#\">" + item.Text +"</a></td>";
		return anchor;
	}
	
	this.Fill = function(settore)
	{
		this.Selected = null;
		this.Clear();
		
		var accumulate;
		accumulate = "<table style='table-layout:fixed' border='0' cellspacing='0' padding='0'>";
		var gammaDataSource = g.ItemsEx(settore);
		for(var index = 0; index < gammaDataSource.Items.length; index++)
		{
			if ( index % 3 == 0 )
				accumulate += "<tr>";
			if ( index == 0 )
			{
				//accumulate += this.Anchor(gammaDataSource.Items[index], true);	
				accumulate += this.Anchor(gammaDataSource.Items[index]);
				this.HolderLinea.Fill(gammaDataSource.Items[index].Value);
			}
			else
				accumulate += this.Anchor(gammaDataSource.Items[index]);
		}
		
		while ( index % 3 )
		{
			accumulate += "<td><a style='background-image:none;'/></td>";
			++index;
		}
		accumulate += "</tr></table>"
		this.Holder.innerHTML = accumulate;	
		/*
		if ( gammaDataSource.Items.length > 0 )
		{
			this.Selected = this.GetAnchor(gammaDataSource.Items[0]);
		}
		*/
	}
	
	this.GetAnchor = function(item)
	{
		return document.getElementById( this.AnchorId(item) );
	}
}


function HolderLinea(holder, prodottoObject)
{
	this.Holder			= holder;
	this.Selected		= null;
	this.HolderProdotto	= prodottoObject;
	
	this.ClearSelection = function()
	{
		if ( this.Selected != null )
			this.Selected.className = "";
	}
	
	this.SetSelection = function(e, id)
	{
		document.getElementById("h_P").style.visibility = "visible";
		this.ClearSelection();
		e.className = "slc";
		this.Selected = e;
		if ( id != null )
			this.HolderProdotto.Fill(id);
	}
	
	this.SetSelectionById = function(id)
	{
		this.SetSelection( this.GetAnchor(l.GetByValue(id)), id );
	}
	
	this.Clear = function()
	{
		this.Holder.innerHTML = "";
	}
	
	this.AnchorId = function(item)
	{
		return "l_" + item.Value;
	}
	
	this.Anchor = function(item, selected)
	{
		anchor = "<td><a id=\"" + this.AnchorId(item) + "\" ";
		if ( selected )
			anchor += " class=\"slc\" ";
		anchor += "onclick=\"hLinea.SetSelection(this," + item.Value  + ");\" ";
		anchor += "href=\"#\">" + item.Text +"</a></td>";
		return anchor;
	}
	
	this.Fill = function(gamma)
	{
		this.Selected = null;
		this.Clear();
		this.HolderProdotto.Clear();
		
		var accumulate;
		accumulate = "<table style='table-layout:fixed' border='0' cellspacing='0' padding='0'>";
		var lineaDataSource = l.ItemsEx(gamma);
		for(var index = 0; index < lineaDataSource.Items.length; index++)
		{
			if ( index % 3 == 0 )
				accumulate += "<tr>";
			if ( index == 0 )
			{
				//accumulate += this.Anchor(lineaDataSource.Items[index], true);
				accumulate += this.Anchor(lineaDataSource.Items[index]);	
				this.HolderProdotto.Fill(lineaDataSource.Items[index].Value);
			}
			else
				accumulate += this.Anchor(lineaDataSource.Items[index]);
		}
		
		while ( index % 3 )
		{
			accumulate += "<td><a style='background-image:none;'/></td>";
			++index;
		}
		accumulate += "</tr></table>"	
		this.Holder.innerHTML = accumulate;	
		/*
		if ( lineaDataSource.Items.length > 0 )
		{
			this.Selected = this.GetAnchor(lineaDataSource.Items[0]);
		}
		*/
	}
	
	this.GetAnchor = function(item)
	{
		return document.getElementById( this.AnchorId(item) );
	}
}


function HolderProdotto(holder)
{
	this.Holder			= holder;
	
	this.Clear = function()
	{
		this.Holder.innerHTML = "";
	}
	
	this.AnchorId = function(item)
	{
		return "p_" + item.Value;
	}
	
	this.Anchor = function(item, selected)
	{
		anchor = "<td><a id=\"" + this.AnchorId(item) + "\" ";
		if ( selected )
			anchor += " class=\"slc\" ";
		anchor += "href=\"" + item.Url + "\">" + item.Text +"</a></td>";
		return anchor;
	}
	
	this.SetSelectionById = function(id)
	{
		this.GetAnchor(p.GetByValue(id)).className = "slc";
	}
	
	this.Fill = function(linea)
	{
		this.Clear();
		
		var accumulate;
		accumulate = "<table style='table-layout:fixed' border='0' cellspacing='0' padding='0'>";
		var prodottoDataSource = p.ItemsEx(linea);
		for(var index = 0; index < prodottoDataSource.Items.length; index++)
		{
			if ( index % 3 == 0 )
				accumulate += "<tr>";
			accumulate += this.Anchor(prodottoDataSource.Items[index]);
		}
		
		while ( index % 3 )
		{
			accumulate += "<td><a style='background-image:none;'/></td>";
			++index;
		}
		accumulate += "</tr></table>"
		
		this.Holder.innerHTML = accumulate;	
	}
	
	this.GetAnchor = function(item)
	{
		return document.getElementById( this.AnchorId(item) );
	}
}