//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.IO;
using CommunityServer.Controls;
using CommunityServer.Components;

namespace CommunityServer.ControlPanel.Controls
{
	/// <summary>
	/// Summary description for PanelTabControl.
	/// </summary>
	public class PanelTabControl : TabControl
	{
		public PanelTabControl()
		{
		}

		protected override string GetText(CommunityServer.Controls.Tab t)
		{
			if(t.HasText)
				return t.Text;
			else
				return CommunityServer.ControlPanel.Components.ResourceManager.GetString(t.ResourceName); 
		}

		public override string SelectedTab
		{
			get
			{
                if(Globals.IsNullorEmpty(base.SelectedTab))
				    base.SelectedTab =  Context.Request.QueryString["tab"];

                if(Globals.IsNullorEmpty(base.SelectedTab))
                {
                    foreach(CommunityServer.Controls.Tab t in this.GetTabs().Tabs)
                    {
                        if(t.IsRoot)
                        {
                            base.SelectedTab = t.Name;
                            break;
                        }
                    }
                }

                return base.SelectedTab;
                
			}
			set
			{
				base.SelectedTab = value;
			}
		}

		protected override void BuildBreadCrumb(System.Text.StringBuilder sb, CommunityServer.Controls.TabCollection tc)
		{
			
		}

		public override string FileLocation
		{
			get
			{
				string path = Context.Server.MapPath(Context.Request.Path);
				DirectoryInfo di = (new FileInfo(path)).Directory;
				path = Path.Combine(di.FullName,this.FileName);

				while(!File.Exists(path))
				{
					di = di.Parent;
					path = Path.Combine(di.FullName,this.FileName);
				}
				return path;

			}
			set
			{
				base.FileLocation = value;
			}
		}

		public string FileName
		{
			get 
			{
				Object state = ViewState["FileName"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "tabs.config";
			}
			set 
			{
				ViewState["FileName"] = value;
			}
		}


	}
}
