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

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls {

	public class OrderPrintForm : Control {

		#region Event Overrides

		protected override void Render( HtmlTextWriter writer ) 
		{

			CSContext cSContext = CSContext.Current;
			NameValueCollection queryString = cSContext.Context.Request.QueryString;
			string applicationKey = cSContext.ApplicationKey;
			int postID = cSContext.PostID;
			int height = Convert.ToInt32( queryString["Height"] );
			int width = Convert.ToInt32( queryString["Width"] );

			String returnUrl = MakeAbsoluteUrl( GalleryUrls.Instance().ViewGallery(applicationKey) );
			String imageUrl = MakeAbsoluteUrl( GalleryUrls.Instance().PictureUrl(applicationKey, postID) );
			String thumbUrl = MakeAbsoluteUrl( GalleryUrls.Instance().ThumbnailURL(applicationKey, postID ) );
		
			writer.WriteLine( @"<form id='sflyc4p' name='sflyc4p' action='http://www.shutterfly.com/c4p/UpdateCart.jsp' method='post' >" );
			AddInput( writer, "addim", "1" );
			AddInput( writer, "protocol", "SFP,100" );
			AddInput( writer, "pid", "C4PP" );
			AddInput( writer, "psid", "GALL" );
			AddInput( writer, "referid", "gallery" );
			AddInput( writer, "returl", returnUrl );
			AddInput( writer, "imraw-1", imageUrl );
			AddInput( writer, "imrawheight-1", height.ToString() );
			AddInput( writer, "imrawwidth-1", width.ToString() );
			AddInput( writer, "imthumb-1", thumbUrl );
			AddInput( writer, "imbkprnta-1", "" );
			writer.WriteLine( @"<input type='submit' />" );
			writer.WriteLine( @"</form>" );
			writer.WriteLine( @"<script language='javascript' type='text/javascript' >" );
			writer.WriteLine( @"<!--" );
			writer.WriteLine( @"document.forms['sflyc4p'].submit();" );
			writer.WriteLine( @"// -->" );
			writer.WriteLine( @"</script>" );


		}


		#endregion

		#region Private Methods
		
		private void AddInput( HtmlTextWriter writer, String name, String value ) 
		{
			writer.AddAttribute( HtmlTextWriterAttribute.Type, "hidden" );
			writer.AddAttribute( HtmlTextWriterAttribute.Name, name );
			writer.AddAttribute( HtmlTextWriterAttribute.Value, value, true );
			writer.RenderBeginTag( HtmlTextWriterTag.Input );
			writer.RenderEndTag();
		}

		private String MakeAbsoluteUrl( String relativeUrl ) {
			HttpContext context = HttpContext.Current;
			if ( context == null ) {
				return relativeUrl;
			}

			Uri currentUrl = context.Request.Url;
			UriBuilder uri = new UriBuilder( currentUrl.Scheme, currentUrl.Host, currentUrl.Port, relativeUrl );
			return uri.ToString();
		}

		#endregion

	}
}