using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

namespace VoServices.Footprint.Web.docs
{
	/// <summary>
	/// Summary description for _default.
	/// </summary>
	public partial class doc : BasePageTemplated
	{

		protected string[] _documents;
		protected string _docfile;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			_docfile = Request.QueryString["p"];
			if (!_docfile.EndsWith(".htm")) throw new System.Exception("Don't hack!");
			
			Includer.Src = _docfile;
			Includer.HrefBase = "doc.aspx?p=";
			Includer.PathBase = "files/";

			_docfile = Server.MapPath("files/" + _docfile);

			// list of documents in this folder
			string[] files = Directory.GetFiles(Path.GetDirectoryName(_docfile), "*.htm");
			string[] dirs = Directory.GetDirectories(Path.GetDirectoryName(_docfile));

            int dircount = dirs.Length;
            for (int i = 0; i < dircount; i++)
                if (dirs[i].EndsWith("CVS"))
                {
                    dirs[i] = dirs[dircount - 1];
                    dircount--;
                }

			_documents = new string[files.Length + dircount];
			Array.Copy(files, 0, _documents, 0, files.Length);
			for (int i = 0; i < dircount; i ++)
				_documents[files.Length + i] = dirs[i] + "/00_index.htm";
			Array.Sort(_documents);
		}

		protected string GetHtmlTitle(string html)
		{
			//locating the two <title>...</title> tags
			Regex title = new Regex("<title[^>]*>(.*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
			Match m = title.Match(html);
			return m.Groups[1].Value;
		}

		protected string LoadHtmlFile(string filename)
		{
			StreamReader infile = new StreamReader(filename);
			string res = infile.ReadToEnd();
			infile.Close();
			return res;
		}

		protected string GetUrlFromFilename(string filename)
		{
			string root = Server.MapPath("files");
			string tmp = filename.Substring(root.Length + 1);
			return tmp.Replace("\\", "/");
		}

		protected string GetParentNodeUrl(string filename)
		{
			int i = 0;
			string tmp = GetUrlFromFilename(filename);

			tmp = tmp.Substring(0, tmp.LastIndexOf("/"));
			if ((i = tmp.LastIndexOf("/")) <= 0)
				return "00_index.htm";
			else
			{
				tmp = tmp.Substring(0, i);
				tmp += "00_index.htm";
				return tmp;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
