using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using AspNetForums.Components;

namespace AspNetForums
{
	/// <summary>
	/// EmoticonGallery 的摘要说明。
	/// </summary>
	public class EmoticonsGallery : Page
	{
		protected Image Image1;
		protected Table EmoticonsTable;
		private string fileName, toolTip, celltext, temp; 
		private string appPath = Globals.GetSiteUrls().Home;

		private void Page_Load(object sender, EventArgs e)
		{
			if (Request.QueryString["Category"] == null)
				return;

			StreamReader streamReader = new StreamReader(MapPath("~/Support/Script/Emoticon.js"));
			this.RegisterClientScriptBlock("EmoticonScript", streamReader.ReadToEnd());
			streamReader.Close();

			int ColumnCount = 10;
			int index = 0;

			string category = Request.QueryString["Category"];
			string webPath = "Emoticons/" + category + "/";
			string path = Server.MapPath(webPath);
			TableRow row = new TableRow();

			if (File.Exists(path + "\\emoticons.xml"))
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(path + "\\emoticons.xml");
				XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/emoticons/emoticon");
				foreach (XmlNode xmlNode in xmlNodeList)
				{
					TableCell tableCell = new TableCell();
					tableCell.BorderWidth = 1;

					temp = Server.MapPath(webPath + xmlNode.Attributes["FileName"].Value);

					if (File.Exists(temp))
					{
						fileName = xmlNode.Attributes["FileName"].Value;
						toolTip = xmlNode.Attributes["ToolTip"].Value;
						string fullWebFileName = appPath + webPath + fileName;
						celltext = "<img border=0 alt='" + toolTip + "' src='" + fullWebFileName + "' onclick=\"returnImage('" + fullWebFileName + "','" + toolTip + "');\">";

						tableCell.Text = celltext;
						row.Cells.Add(tableCell);
						index++;
						if (index%ColumnCount == 0)
						{
							EmoticonsTable.Rows.Add(row);
							row = new TableRow();
						}
					}
				}
			}
			else
			{
				DirectoryInfo dir = new DirectoryInfo(path);
				foreach (FileInfo file in dir.GetFiles())
				{
					string ext = Path.GetExtension(file.FullName).ToLower();
					if (ext == ".jpg" || ext == ".gif" || ext == ".png" || ext == ".bmp")
					{
						TableCell tableCell = new TableCell();
						tableCell.BorderWidth = 1;

						celltext = "<img onclick=\"returnImage('" + appPath + webPath + file.Name + "','" + file.Name + "');\" src='" + appPath + webPath + file.Name + "' alt='" + file.Name + "'>";

						tableCell.Text = celltext;
						row.Cells.Add(tableCell);
						index++;
						if (index%ColumnCount == 0)
						{
							EmoticonsTable.Rows.Add(row);
							row = new TableRow();
						}
					}

				}
			}
			Response.Expires = -1;
		}

		#region Web 窗体设计器生成的代码

		protected override
			void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private
			void InitializeComponent()
		{
			this.Load +=
				new
					EventHandler(this.Page_Load);

		}

		#endregion
	}
}