using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ctrls_navigation : System.Web.UI.UserControl
{
	private int _cid = -1;
	public int Cid
	{
		get{return _cid;}
		set{_cid = value;}
	}
	
	private bool _ShowChildren = false;
	public bool ShowChildren
	{
		get{return _ShowChildren;}
		set{_ShowChildren = value;}
	}
	
	
	protected void Page_Load(object sender, EventArgs e)
	{
		if( _cid != -1 )
		{
			Ycbw.Data.OleDbClient r = new Ycbw.Data.OleDbClient();
			
			r.Fill("select *,(select name from [b_article_categories] where id = c.root) as parentname from [b_article_categories] c where id = "+ _cid, "data_class");
			
			if( Int32.Parse(r.Data.Tables[0].Rows[0]["depth"].ToString()) == 0 )
				r.BindList("Select * from [b_article_categories] where parent = "+ _cid +" order by lft asc", "data_categories", data_categories);
			else
				r.BindList("Select * from [b_article_categories] where parent = (select parent from [b_article_categories] where id = "+ _cid +") and depth > 0 order by lft asc", "data_categories", data_categories);

			if(r.Data.Tables["data_categories"].Rows.Count > 0)
			{
				panel_navigation.Visible = true;
				
				category.Text = r.Tables["data_class"].Rows[0]["parentname"].ToString();
			}
			else
				panel_navigation.Visible = false;
			
			r.Dispose();
		}
		
	}
	
	protected void data_childs_load(object sender, EventArgs e)
	{
		DataList dl = (DataList)sender;
		
		if( !ShowChildren )
			dl.Visible = false;
		else
		{
			string cid = dl.DataKeyField;
			
			Ycbw.Data.OleDbClient r = new Ycbw.Data.OleDbClient();
	
			r.BindList("Select * from [b_article_categories] where parent = "+ cid +" order by lft asc", "dl", dl);
	
			if(r.Data.Tables[0].Rows.Count > 0)
				dl.Visible = true;
			else
				dl.Visible = false;
			
			r.Dispose();
		}
	}
	
	public string GetLink(object link, object folder, object id)
	{
		if( link.ToString() != "" )
			return link.ToString();
		else
			return folder.ToString() + "-" + id.ToString() + ".aspx";
	}
}
