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

using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Extends the pager control to be able to page by PostID's when using a pagesize of 1
	/// </summary>
	public class SlideshowLink : HyperLink
	{
		public override string ClientID
		{
			get
			{
				return this.ID;
			}
		}
		/// <summary>
		/// Gets or sets the name of the resource to dispaly.
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Gets or sets the name of the resource to display." ),
		DefaultValue( "" ),
		]
		public virtual String ResourceName 
		{
			get 
			{
				Object state = ViewState["ResourceName"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["ResourceName"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the name of the resource file to pull from
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Gets or sets the name of the resource file." ),
		DefaultValue( null ),
		]
		public virtual String ResourceFile 
		{
			get 
			{
				Object state = ViewState["ResourceFile"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["ResourceFile"] = value;
			}
		}

		/// <summary>
		/// Gets or sets weather to render href as a popup
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Opens the slideshow in a new window" ),
		DefaultValue( true ),
		]
		public virtual bool UsePopup 
		{
			get 
			{
				Object state = ViewState["UsePopup"];
				if ( state != null ) 
				{
					return (bool)state;
				}
				return true;
			}
			set 
			{
				ViewState["UsePopup"] = value;
			}
		}

		/// <summary>
		/// Gets or sets weather to links to the Pro Flashed based Slideshow
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Links to the Pro Flashed based Slideshow" ),
		DefaultValue( true ),
		]
		public virtual bool UsePro 
		{
			get 
			{
				Object state = ViewState["UsePro"];
				if ( state != null ) 
				{
					return (bool)state;
				}
				return true;
			}
			set 
			{
				ViewState["UsePro"] = value;
			}
		}

		/// <summary>
		/// The width of the popup window
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "The width of the popup window" ),
		DefaultValue( 800 ),
		]
		public virtual int PopupWidth 
		{
			get 
			{
				Object state = ViewState["PopupWidth"];
				if ( state != null ) 
				{
					return (int)state;
				}
				return 800;
			}
			set 
			{
				ViewState["PopupWidth"] = value;
			}
		}

		/// <summary>
		/// The height of the popup window
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "The height of the popup window" ),
		DefaultValue( 550 ),
		]
		public virtual int PopupHeight 
		{
			get 
			{
				Object state = ViewState["PopupHeight"];
				if ( state != null ) 
				{
					return (int)state;
				}
				return 550;
			}
			set 
			{
				ViewState["PopupHeight"] = value;
			}
		}
		protected override void OnPreRender(EventArgs e)
		{            
			base.OnPreRender( e );
			CSContext cSContext = CSContext.Current;
	    
			if (!Globals.IsNullorEmpty( ResourceName ))
				this.Text = ResourceManager.GetString(ResourceName, ResourceFile);

			string slideShowUrl;
			if(Globals.IsNullorEmpty(cSContext.ApplicationKey))
			{
				slideShowUrl = GalleryUrls.Instance().ViewSlideshowPro();
			}
			else
			{
				if(UsePro)
					slideShowUrl =  GalleryUrls.Instance().ViewSlideshowPro(cSContext.ApplicationKey, cSContext.CategoryID);
				else
					slideShowUrl =  GalleryUrls.Instance().ViewSlideshow(cSContext.ApplicationKey, cSContext.CategoryID);
			}
			if (UsePopup)
			{
				this.NavigateUrl = "javascript:void(0);";
				this.Attributes.Add("onclick", string.Format("javascript:window.open('{0}', '', 'width={1},height={2},status=0,titlebar=0,toolbar=0,scrollbars=0');", slideShowUrl, this.PopupWidth, this.PopupHeight));

			}
			else
			{
				this.NavigateUrl = slideShowUrl;
			}
		}
	}
}
