(function ()
{
	var ns = __namespace( "Astro.Blog.AstroEdit.Visualizer.Snippet" );

	/** @type {System.utils.IKey} */
	var IKey                              = __import( "System.utils.IKey" );
	/** @type {System.utils.DataKey} */
	var DataKey                           = __import( "System.utils.DataKey" );
	/** @type {Dandelion.IDOMElement} */
	var IDOMElement                       = __import( "Dandelion.IDOMElement" );
	/** @type {Dandelion} */
	var Dand                              = __import( "Dandelion" );
	/** @type {Components.MessageBox} */
	var MessageBox                        = __import( "Components.MessageBox" );

	var compileProp = ns[ NS_INVOKE ]( "compileProp" );
	var escapeStr = ns[ NS_INVOKE ]( "escapeStr" );
	var unescapeStr = ns[ NS_INVOKE ]( "unescapeStr" );

	var spoiler = function(insertSnippet, snippetWrap, createContext, override)
	{
		var temp, i, j
		
		, handler = function ()
		{
			// Input fields
			var v_snippetInput = Dand.wrap("textarea", null, "v_snippet_input")
			, input_title = Dand.wrap("input", null, "v_snippet_input_single", null, new IKey("type", "text"))
			, input_expanded = Dand.wrapna("input", new IKey("type", "checkbox"))
			
			if (this._stage)
			{
				v_snippetInput.value = this._content;
				input_title.value = this._title;
				input_expanded.checked = (this._expanded == "on");
			}
			
			// Popup MessageBox
			new MessageBox("Insert spoiler content"
				, Dand.wrape([
						Dand.wrapc("v_instruction flsf", "Title")
						, input_title
						
						, Dand.wrapc("v_instruction flsf", "Content")
						, v_snippetInput
						
						, Dand.wrape( Dand.wrapne( "label", [ input_expanded, "Expanded" ] ) )
					]
				)
			, "OK", "Cancel", visualizer.bind({title: input_title, content:v_snippetInput, expanded: input_expanded, stage: this._stage})).show();
		}
		
		, visualizer = function( submitted, override )
		{
			var content, title, expanded
			, stage = this.stage;
			
			if( override )
			{
				content = unescapeStr( override.value );
				title = unescapeStr( override.title );
				expanded = override.expanded ? "on" : "";
			}
			else
			{
				content = this.content.value;
				title = this.title.value;
				expanded = this.expanded.checked ? "on" : "";
			}
			
			if (submitted && content)
			{
				// Shared Clause
				i = Dand.textNode(title || "Spoiler");
				if (!stage)
				{
					if (!content) return;
					
					// Visualize component
					temp =  Dand.wrapc("v_box",
						[
							// caption
							Dand.wrapc("v_caption", i)
							, Dand.textNode(content)
						],
						[
							new DataKey("value", content)
							, new DataKey("title", title)
							, new DataKey("expanded", expanded)
						]
					);
					insertSnippet(j = snippetWrap("Spoiler", temp), Boolean(override));
				}
				else
				{
					IDOMElement(stage).setAttribute
					([
						new DataKey("value", content)
						, new DataKey("title", title)
						, new DataKey("expanded", expanded)
					]);
					
					temp = stage.firstChild;
					temp.removeChild(temp.firstChild);
					temp.appendChild(i);
					
					stage.removeChild(stage.lastChild);
					stage.appendChild(Dand.textNode(content));
					
					// set temp back to stage
					temp = stage;
				}
				
				i = {
					_title: title
					, _content: content
					, _expanded: expanded || "off"
					, _stage: temp
				};
				
				// Set context menu
				createContext(i, j, handler);
			}
		}
		;
		
		if (override)
		{
			visualizer(true, override);
			override = false;
		}
		else
		{
			return handler;
		}
		return true;
	};
	
	var compile = function ( stage )
	{
		var element = IDOMElement( stage )
		, props = ["title", "expanded"];
		
		return "[spoiler"
			+ compileProp( element, props )
			+  "]"
			+ escapeStr( element.getDAttribute( "value" ) )
			+ "[/spoiler]";
	};

	__static_method( spoiler, "compile", compile );

	ns[ NS_EXPORT ]( EX_CLASS, "Spoiler", spoiler );
})();