dojo.provide("UI.Widgets.Video.Tour");

dojo.addOnLoad(function() {
	var embedded       = false,
		dlgVideo       = new UI.Dialog.Generic(dojo.byId("dlg-videos")),
		swfVideo       = dojo.byId("flv-video"),
		swfVideoTmpl   = swfVideo.cloneNode(true)
		swfVideoParent = swfVideo.parentNode;
	
		
	dlgVideo.useModal()
		.addOpenTriggers(dojo.byId("btn-videos"))
		.addEventListener("beforeopen", function() {
			if ( embedded ) {
				// Once the video has been embedded, just reinsert the already generated node
				if ( !document.all ) {
					swfVideoParent.appendChild(swfVideo);
					return;
				}
				
				// Unless this IE, in which case we have to do the whole thing again because it will freeze
				swfVideoParent.removeChild(swfVideo);
			}
			
			swfVideo = swfVideoTmpl.cloneNode(true);
			swfVideoParent.appendChild(swfVideo);
			
			swfobject.embedSWF("/content/media/videos/player_v2.swf", "flv-video", "900", "507", "9.0.0", "/content/media/videos/express-install.swf", {
					url:    "/content/media/videos/tutorial_v1.flv",
					width:  "900",
					height: "507"
				}, {
					scale:  "noscale",
					salign: "lt",
					wmode:  "transparent"
				}, {}
			);
			
			embedded = true;
		})
		.addEventListener("close", function() {
			// IE will continue to play the video, so remove the node to force it to stop.  Ya, that's awesome
			dojo.query("#flv-video").orphan();
		});
	
	dojo.connect(dojo.byId("dialog-system-modal"), "click", function(event) {
		dlgVideo.close();
	});
});
