/// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
/// ' Microsoft Content Management Server - Sample Code 
/// '
/// ' This sample code is provided "AS IS" with no warranties, and confers no rights. 
/// ' You assume all risk for your use.  2001 Microsoft Corporation. All rights reserved.
/// '
/// ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	
	using System;
	using System.Web;
	using System.ComponentModel;
	using System.Web.UI.HtmlControls;
	using System.Web.UI.WebControls;
	//Add reference for localization
	using System.EnterpriseServices;
	using System.Globalization;
	using System.Resources;
	using System.Threading;
	//Add reference for navigation tree control
	using Microsoft.Web.UI.WebControls;
	//Add reference to the CMS API
	using Microsoft.ContentManagement.Publishing;
	using Microsoft.ContentManagement.Web.Caching;
	//Add reference to the MCMS control library
	using McmsWebControlLibrary;

	namespace WoodgroveNet.UserControls
	{
	/// <summary>
	///		Summary description for LatestPress.
	/// </summary>
	public abstract class LatestPress : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Literal LTLatestPressReleases;
		protected System.Web.UI.WebControls.Table tablePressRelease;

		//Resource Manager for localization
		protected ResourceManager LocalizationResourceManager;

		private void Page_Load(object sender, System.EventArgs e)
		{
			//This function implements the ValidationCallback mechanism
			CmsHttpCachePolicy.Current.AddValidationCallbackAllCmsContent();

			//Call function to generate a list of press releases
			GetLatestPressReleases();	
			
			LTLatestPressReleases.Text = LocalizationResourceManager.GetString("LatestPress");
		}

		#region Private member variables
		private string pressChannel;
		private string hidePageName;
		private int numLinks;
		#endregion Private member variables

		#region Properties
		/// <summary>
		///		The channel used for this control.
		/// </summary>
		/// 		
		[System.ComponentModel.Description("The channel containing the Press Releases."),
		Category("Press Releases"),]
		public string PressChannel
		{
			get 
			{ 
				return pressChannel; 
			}
			set 
			{ 
				pressChannel = value; 
			}
		}	
		//Allow user to change the value of property - in design view
		bool ShouldSerializePressChannel(){return true;}

		/// <summary>
		///		The number of links to show.
		/// </summary>
		/// 		
		[System.ComponentModel.Description("The number of Press Release links to show."),
		Category("Press Releases"),]
		public int NumberOfLinks
		{
			get { return numLinks; }
			set { numLinks = value; }
		}	
		//Allow user to change the value of property - in design view
		bool ShouldSerializeNumberOfLinks(){return true;}

		/// <summary>
		///		Option to hide a certain page (by name).
		/// </summary>
		/// 		
		[System.ComponentModel.Description("Optional hidden page name."),
		Category("Press Releases"),]
		public string HiddenPageName
		{
			get { return hidePageName; }
			set { hidePageName = value; }
		}	
		//Allow user to change the value of property - in design view
		bool ShouldSerializeHiddenPageName(){return true;}
		#endregion Properties

		#region Public Functions
		
		//public System.Web.UI.WebControls.Table GetLatestPressReleases()
		public void GetLatestPressReleases()
		{	
			//Instantiate CMS AutosessionHTMLContext
			CmsHttpContext McmsContext = CmsHttpContext.Current;
		
			//Create Channel object for press release channel
			Channel pPressChannel = null;
			if(PressChannel != "")
			{
				try
				{
					pPressChannel = (Channel)McmsContext.Searches.GetByPath(PressChannel);
				}
				catch(Exception)
				{
					return; //Early return
				}
			}

			//Check for null channel object
			if(pPressChannel == null)
			{
				return;  //Early return
			}
			//Create anchor object for the links
			HtmlAnchor navLink;
					
			//Keep track of the number of postings displayed
			int intCounter = 0;

			//Sort pages by date
			PostingCollection pPages = pPressChannel.Postings; 
			pPages.SortByStartDate();

			// Add a table to show each press posting object in the press Channel.
			foreach(Posting pPosting in pPages)
			{
				//Use 3 as the default number of links if no value is supplied in the HTML
				if(NumberOfLinks.Equals(null))
				{
					NumberOfLinks = 3;
				}
				
					//User option to hide a certain posting name and restrict results
					if(pPosting.Name != HiddenPageName && NumberOfLinks > intCounter)
					{
						System.Web.UI.WebControls.TableRow	row;
						System.Web.UI.WebControls.TableCell	cell;
						row = new TableRow();			
						//Prepare Channel anchor link
						navLink = new HtmlAnchor();
			
						//Keep track of the number of results
						intCounter =  intCounter + 1;

						//A link to the page
						navLink.InnerHtml = "<span class=homeprtitlesummary>" + Server.HtmlEncode(pPosting.DisplayName) + "</span>";
						navLink.HRef = pPosting.Url;
				
						// Add link to the table
						cell = new System.Web.UI.WebControls.TableCell();
						cell.Wrap = false;
						cell.Controls.Add(navLink);
						row.Cells.Add(cell);
					
						//Add row to the table
						tablePressRelease.Rows.Add(row);
					
				}
			}	//Close for Press Postings
		} //Close GetLatestPressReleases Function

		#endregion Public Functions			

		#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);
			LocalizationResourceManager = (ResourceManager) Application["WoodgroveNetResourceManager"];
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
