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

using System;
using System.Collections;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for GallerySlideShow.
	/// </summary>
	public class Slideshow : GalleryThemedControl
	{
		private Gallery gallery;
		private ArrayList pictures;
		private CSContext cSContext;

		#region Child Controls

		private IText galleryName;
		private IText categoryName;

		private HtmlImage slideShowImage;
		private HtmlImage playButton;
		private HtmlImage stopButton;
		private HtmlImage prevButton;
		private HtmlImage nextButton;

		private HtmlGenericControl pictureTitle;
		private HtmlGenericControl pictureDesc;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			galleryName = FindText( "GalleryName" );
			categoryName = FindText( "CategoryName" );

			slideShowImage = (HtmlImage)FindControl( "SlideShowImage" );
			playButton = (HtmlImage)FindControl( "PlayButton" );
			stopButton = (HtmlImage)FindControl( "StopButton" );
			prevButton = (HtmlImage)FindControl( "PrevButton" );
			nextButton = (HtmlImage)FindControl( "NextButton" );

			pictureTitle = (HtmlGenericControl)FindControl( "PictureTitle" );
			pictureDesc = (HtmlGenericControl)FindControl( "PictureDesc" );

			InitializeChildControls();
		}


		private void InitializeChildControls()
		{
			slideShowImage.Src = Globals.GetSkinPath() + "/images/gallery_slideshow.gif";
			slideShowImage.Alt = ResourceManager.GetString("Gallery_SlideShow_Loading");

			playButton.Src = Globals.GetSkinPath() + "/images/slideshow_play_off.gif";
			stopButton.Src = Globals.GetSkinPath() + "/images/slideshow_stop_off.gif";
			prevButton.Src = Globals.GetSkinPath() + "/images/slideshow_prev_off.gif";
			nextButton.Src = Globals.GetSkinPath() + "/images/slideshow_next_off.gif";

			playButton.Attributes.Add("onmouseover", playButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_play_on.gif'");
			stopButton.Attributes.Add("onmouseover", stopButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_stop_on.gif'");
			prevButton.Attributes.Add("onmouseover", prevButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_prev_on.gif'");
			nextButton.Attributes.Add("onmouseover", nextButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_next_on.gif'");

			playButton.Attributes.Add("onmouseout", playButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_play_off.gif'");
			stopButton.Attributes.Add("onmouseout", stopButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_stop_off.gif'");
			prevButton.Attributes.Add("onmouseout", prevButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_prev_off.gif'");
			nextButton.Attributes.Add("onmouseout", nextButton.ClientID + ".src='" + Globals.GetSkinPath() + "/images/slideshow_next_off.gif'");

			playButton.Style.Add("cursor", "hand");
			stopButton.Style.Add("cursor", "hand");
			prevButton.Style.Add("cursor", "hand");
			nextButton.Style.Add("cursor", "hand");

			playButton.Attributes.Add("onclick", "PlaySlideShow();");
			stopButton.Attributes.Add("onclick", "StopSlideShow();");
			prevButton.Attributes.Add("onclick", "SwitchPrevImage();");
			nextButton.Attributes.Add("onclick", "SwitchNextImage();");

			playButton.Alt = ResourceManager.GetString("Gallery_SlideShow_Play");
			stopButton.Alt = ResourceManager.GetString("Gallery_SlideShow_Stop");
			prevButton.Alt = ResourceManager.GetString("Gallery_SlideShow_Prev");
			nextButton.Alt = ResourceManager.GetString("Gallery_SlideShow_Next");
		}

		#endregion

		#region Event Overrides

		/// <summary>
		/// Create any child controls necessary for this control.
		/// </summary>
		protected override void CreateChildControls()
		{
			base.CreateChildControls();
			StringBuilder js = new StringBuilder();			
			cSContext = CSContext.Current;

			// Retrieve the gallery
			gallery = Galleries.GetGallery(cSContext.ApplicationKey);

			// Retrieve all the pictures for the given album ID.
			GalleryThreadQuery query = new GalleryThreadQuery();
			query.SectionID = CurrentGallery.SectionID;
			query.CategoryID = cSContext.CategoryID;
			query.PageSize = int.MaxValue;
			query.ApplicationPostType = GalleryPostType.Image;

			pictures = GalleryPosts.GetPictures(query).Threads;

			// Check if there are no pictures
			if(pictures.Count == 0)
			{
				Literal noRecords = (Literal)FindControl( "NoRecords" );
				noRecords.Text = ResourceManager.GetString( "Gallery_NoRecordsFound" );
				noRecords.Visible = true;
				slideShowImage.Visible = false;
			}

			// Start building the javascript.
			js.Append("<script language=\"javascript\" type=\"text/javascript\">" + Environment.NewLine);
			js.Append("var slideShowDelay = " + (gallery.SlideshowDelay * 1000) + ";" + Environment.NewLine);
			js.Append("var maxPictureCount = " + pictures.Count + ";" + Environment.NewLine);
			js.Append("var currentPictureIndex = -1;" + Environment.NewLine);
			js.Append("var continueSlideShow = true;" + Environment.NewLine);
			js.Append("var asImages = new Array(" + pictures.Count + ");" + Environment.NewLine);
			js.Append("var asTitles = new Array(" + pictures.Count + ");" + Environment.NewLine);
			js.Append("var asDescs = new Array(" + pictures.Count + ");" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);
			
			// Loop through the pictures building the client side array.
			int i = 0;
			foreach(GalleryPost picture in pictures)
			{
				using(GalleryImage galleryImage = new GalleryImage(GalleryImageType.Slideshow, picture))
				{
					galleryImage.PostID = picture.PostID;
					galleryImage.ProcessPicture();
					js.Append("asImages[" + i + "] = '" + galleryImage.ImageUrl + "';" + Environment.NewLine);
					js.Append("asTitles[" + i + "] = '" + picture.Subject.Replace("'", "\\'").Replace("\n", "<br/>").Replace("\r", "") + "';" + Environment.NewLine);
					js.Append("asDescs[" + i + "] = '" + picture.RenderedBody(PostTarget.Web).Replace("'", "\\'").Replace("\n", "<br/>").Replace("\r", "") + "';" + Environment.NewLine);
					i++;
				}
			}

			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("setTimeout('PlaySlideShow()', 1000);");
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: StopSlideShow()
			js.Append("function StopSlideShow()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    document.getElementById('" + playButton.ClientID + "').src = '" + Globals.GetSkinPath() + "/images/slideshow_play_off.gif';" + Environment.NewLine);
			js.Append("    document.getElementById('" + stopButton.ClientID + "').src = '" + Globals.GetSkinPath() + "/images/slideshow_stop_on.gif';" + Environment.NewLine);
			js.Append("    continueSlideShow = false;" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: PlaySlideShow()
			js.Append("function PlaySlideShow()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    document.getElementById('" + playButton.ClientID + "').src = '" + Globals.GetSkinPath() + "/images/slideshow_play_on.gif';" + Environment.NewLine);
			js.Append("    document.getElementById('" + stopButton.ClientID + "').src = '" + Globals.GetSkinPath() + "/images/slideshow_stop_off.gif';" + Environment.NewLine);
			js.Append("    continueSlideShow = true;" + Environment.NewLine);
			js.Append("    setTimeout('ChangeSlide()', slideShowDelay);" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: ChangeSlide()
			js.Append("function ChangeSlide()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    if (!continueSlideShow) return;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    NextImage();" + Environment.NewLine);
			js.Append("    setTimeout('ChangeSlide()', slideShowDelay);" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: NextImage()
			js.Append("function NextImage()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    if (maxPictureCount == 0) return;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    currentPictureIndex++;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    if (currentPictureIndex >= maxPictureCount) currentPictureIndex = 0;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    document.getElementById('" + slideShowImage.ClientID + "').src = asImages[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + slideShowImage.ClientID + "').alt = asTitles[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + pictureTitle.ClientID + "').innerHTML = asTitles[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + pictureDesc.ClientID + "').innerHTML = asDescs[currentPictureIndex];" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("}" + Environment.NewLine);

			// Function: PrevImage()
			js.Append("function PrevImage()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    if (maxPictureCount == 0) return;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    currentPictureIndex--;" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    if (currentPictureIndex < 0) currentPictureIndex = (maxPictureCount - 1);" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append("    document.getElementById('" + slideShowImage.ClientID + "').src = asImages[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + slideShowImage.ClientID + "').alt = asTitles[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + pictureTitle.ClientID + "').innerHTML = asTitles[currentPictureIndex];" + Environment.NewLine);
			js.Append("    document.getElementById('" + pictureDesc.ClientID + "').innerHTML = asDescs[currentPictureIndex];" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: SwitchNextImage()
			js.Append("function SwitchNextImage()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    StopSlideShow();" + Environment.NewLine);
			js.Append("    NextImage();" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			// Function: SwitchPrevImage()
			js.Append("function SwitchPrevImage()" + Environment.NewLine);
			js.Append("{" + Environment.NewLine);
			js.Append("    StopSlideShow();" + Environment.NewLine);
			js.Append("    PrevImage();" + Environment.NewLine);
			js.Append("}" + Environment.NewLine);
			js.Append(Environment.NewLine);
			js.Append(Environment.NewLine);

			js.Append("</script>");

			this.Page.RegisterClientScriptBlock("ImageSwappingLogic", js.ToString());
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if(!Page.IsPostBack)
				DataBind();
		}


		public override void DataBind()
		{
			base.DataBind();

			if(CurrentGallery != null)
				galleryName.Text = string.Format( ResourceManager.GetString( "Gallery_SlideShow_CurrentGallery" ), CurrentGallery.Name );

			if(cSContext.CategoryID != -1)
			{
				PostCategory currentCategory = PostCategories.GetCategory(cSContext.CategoryID, CurrentGallery.SectionID, CurrentUser.IsGalleryAdministrator);
				categoryName.Text = string.Format( ResourceManager.GetString( "Gallery_SlideShow_CurrentCategory" ), currentCategory.Name );
			}
		}


		#endregion

	}
}
