(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 sound = function (insertSnippet, snippetWrap, createContext, override)
	{
		var temp, i, j
		, defaultArt = "http://file.astropenguin.net/blog/layout-images/disc_s.png"
		, handler = function ()
		{
			// Input fields
			var input_url = Dand.wrap('input', null, "v_snippet_input_single", null, new IKey("type", "text"))
			, input_albumArt = Dand.wrap('input', null, "v_snippet_input_single", null, new IKey("type", "text"))
			, input_lrc = Dand.wrap('input', null, "v_snippet_input_single", null, new IKey("type", "text"))
			;
			
			if (this._stage)
			{
				input_url.value = this._url;
				input_albumArt.value = this._albumArt;
				input_lrc.value = this._lrc;
			}
			
			new MessageBox(
				(this._stage ? "Edit" : "Insert") + " sound snippet"
				, Dand.wrape([
						Dand.wrapc("v_instruction flsf", "Link to sound:")
						, input_url
						
						, Dand.wrapc("v_instruction flsf", "Link to album art(optional):")
						, input_albumArt
						
						, Dand.wrapc("v_instruction flsf", "Link to lrc(optional):")
						, input_lrc
					]
				)
			, "OK", "Cancel", visualizer.bind({url:input_url, albumArt:input_albumArt, lrc:input_lrc, stage: this._stage})).show();
		}
	
		, visualizer = function (submitted)
		{
			
			var src, albumArt , lrc
			, stage = this.stage;
			
			if (override)
			{
				src = override.url;
				albumArt = override.albumArt;
				lrc = override.lrc;
			}
			else
			{
				src = this.url.value;
				albumArt = this.albumArt.value;
				lrc = this.lrc.value;
			}
			
			if (submitted && src)
			{
				
				if (!stage)
				{
					// Visualize component
					temp =  Dand.wrapc('v_box'
					, [
						Dand.wrape(null, new IKey
							(
								"style", "height: 75px; background-size: auto 75px; background-repeat: no-repeat;"
								+ "background-image: url(" + (albumArt ? albumArt : defaultArt) + ")"
							)
						)
						, Dand.wrapc('v_description', "Sound snippet")
					  ]
					, [
						new DataKey("value", "")
						, new DataKey("url", src)
						, new DataKey("albumArt", albumArt)
						, new DataKey("lrc", lrc)
					  ]
					);
					insertSnippet(j = snippetWrap("Sound", temp), Boolean(override));
				}
				else
				{
					// Edit properties
					stage.firstChild.style.backgroundImage = "url(" + (albumArt ? albumArt : defaultArt) + ")" ;
					
					IDOMElement(stage).setAttribute
					(
						[
							new DataKey("url", src)
							, new DataKey("albumArt", albumArt)
							, new DataKey("lrc", lrc)
						]
					);
					
				}
				
				i = {_url: src, _albumArt: albumArt, _lrc: lrc, _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)
	{
		// [sound url=""  albumArt="" lrc="" ][/sound]");
		var element = IDOMElement(stage)
		, props = ["url", "albumArt", "lrc"];
		
		return "[sound" + compileProp( element, props ) + "][/sound]";
	};


	__static_method( sound, "compile", compile );

	ns[ NS_EXPORT ]( EX_CLASS, "Sound", sound );
})();