
function loadDownloadPopupFromWYSIWYG(strType,mediaId,mediaTagId) {

	CreatePauseOverlay('true');

	var injectIn = strType;

	$('pause_waiting').setStyle('visibility', 'hidden');

	var popupHtml = '<div class="popup-header">Download invoegen</div><div class="popup-text"></div><div class="popup-footer"></div>';

	new Element('div').set('html',popupHtml).addClass('image-popup').injectAfter($('pause_overlay')).makeDraggable({handle:$$('.popup-header')});

	if(mediaId!="")
	{
		getDownloadPopupHtml('mediaTagId=' + mediaTagId + '&mediaId=' + mediaId,injectIn);
	}
	else 
	{
		getDownloadPopupHtml('',injectIn);
	}
}

function getDownloadInfo(mediaId) {
	
	new Request({url: '/ajax/downloadpopup/download.php', onSuccess: function (ResponseText,ResponseXML) {
		
		var downloads = ResponseXML.getElementsByTagName('download');

		if(downloads.length)
		{
			//alert(downloads.length);
			
			var url = ResponseXML.getElementsByTagName('url')[0].childNodes[0].nodeValue;
			var filename = ResponseXML.getElementsByTagName('filename')[0].childNodes[0].nodeValue;
			var mime = ResponseXML.getElementsByTagName('mime')[0].childNodes[0].nodeValue;
			var size = ResponseXML.getElementsByTagName('size')[0].childNodes[0].nodeValue;
			var thumb = ResponseXML.getElementsByTagName('thumb')[0].childNodes[0].nodeValue;
			
			var html = '';
			
			if(thumb && thumb!="")
			{
				html += '<a href="'+url+'" target="_blank"><img src="' + thumb + '" border="0" /></a><br />';
			}
			
			html += filename + '<br >';
			html += mime + '<br >';
			html += size + '<br >';
			
			$$('.image-info').set('html',html);

		}
		else
		{
			var errors = ResponseXML.getElementsByTagName('error');
			
			if(errors) {
				
			}
		}
		
	}}).send('mediaId=' + mediaId);
	
}

function getDownloadPopupHtml (strSendPost,injectIn) {

	/**
	 * Download Name
	 */
	var downloadName = '';
	var dom;
	var parentNode;

	new Request({url: '/ajax/downloadpopup/getDownloadPopup.php', onSuccess: function (ResponseText,ResponseXML) {

		var popUpTextElm = $(window.parent.document).getElement('.popup-text');

		popUpTextElm.set('html',ResponseText);

		var mediaTagId = $('mediaTagId').get('value');

		if(is_object(injectIn))
		{		
			// Is een object zal wel TinyMCE zijn dan..
			
			//alert(injectIn.selection.getContent());

			var node = injectIn.selection.getNode();
			dom = injectIn.dom;
			
			var downloadSrc = '';
			
			parentNode = injectIn.dom.getParent(injectIn.selection.getNode(), 'A');
			
			if(parentNode) {
			
				//alert(parentNode.nodeName);
				
				//alert(dom.getAttrib(parentNode, 'href'));
				//alert(strSendPost);
				downloadSrc = dom.getAttrib(parentNode, 'href');
			}
			else
			{
				//alert(parentNode);
			}
			
			/**
			 * Huidige selectie als naam gebruiken
			 */
			downloadName = injectIn.selection.getContent();

			if(downloadName) {
				//$('downloadAlt').set('value',downloadName);
			}

			/**
			 * Src van de gelecteerde download
			 */
			 			
			var DownloadAltText = dom.getAttrib(node, 'title');
			
			if(DownloadAltText != "")
			{
				//$('downloadAlt').set('value',DownloadAltText);
			}

			var currentDownloadUrl = $('downloadUrl').get('value');

			if(downloadSrc != "" && currentDownloadUrl=="")
			{
				$('downloadUrl').set('value',downloadSrc);
			}
		}

		cancelButton();

		$('mediaItem').addEvent('click',function (e) {

			var mediaId = $('mediaItem').get('value');
			var mediaTagId = $('mediaTagId').get('value');
		
			//alert(mediaId);
			//alert($('mediaItem').get('url'));
			
			var mediaUrl = $$('#mediaItem option[selected]').get('url');

			$$('#downloadUrl').set('value',mediaUrl);
			//getDownloadPopupHtml('mediaTagId=' + mediaTagId + '&mediaId=' + mediaId,injectIn);
			
			var mediaThumbUrl = $$('#mediaItem option[selected]').get('thumburl');
			
			var mediaInfoDiv = $$('.image-info');
			
			var mediaId = $$('#mediaItem option[selected]').get('value');
			
			getDownloadInfo(mediaId);
			
		}); 

		$('mediaTagId').addEvent('change',function (e) {

			var mediaTagId = $('mediaTagId').get('value');

			getDownloadPopupHtml('mediaTagId=' + mediaTagId,injectIn);
		});
		
		if(is_object(injectIn))
		{
			if(injectIn.selection && injectIn.selection.getNode().nodeName=="A")
			{
				var selectedDownloadNode = injectIn.selection.getNode();
				var selectedDownloadHtml = injectIn.selection.getContent();
				
				//alert(selectedDownloadHtml);

				var elm = injectIn.dom.getParent(selectedDownloadNode, "A");
				
				//alert(injectIn.selection);
				
				//injectIn.dom.remove(elm);
			}
		}

		$('cancelDownload').addEvent('click',function(e) {
				
			if(is_object(injectIn))
			{					
				injectIn.selection.setContent(selectedDownloadHtml);
			}
		});

		$('saveDownload').addEvent('click', function(e) {

			//alert('save');
			
			//alert(injectIn.selection.getContent());
			
			//var downloadAlt = $('downloadAlt').get('value');
			//var downloadUrl = $('downloadUrl').get('value');
				
			var downloadUrl = '';
			
			var intMediaId = $('mediaItem').get('value');


			if(!intMediaId || intMediaId == '')
			{
				intMediaId = 'undefined';
			}
			
			downloadUrl = $$('#downloadUrl').get('value');
			
			/**
			 * Kijken of download getracked moeten worden, zo ja dan de URL aanpassen.
			 */
			if($('trackDownload').get('checked'))
			{		
				var selectedOption = $$('#mediaItem option[selected]');
								
				if(selectedOption)
				{		
					var cryptorId = selectedOption.get('cryptor');
					
					downloadUrl = 'http://' + document.domain + '/media/count/' + cryptorId;
				}
			}
			
			var html = downloadUrl;
			
			//alert(html);
			
			if(elm == null) {
							
				if(is_object(injectIn))
				{
					//injectIn.execCommand('mceInsertContent', false, html, {skip_undo : 1});
					
					//injectIn.restoreSelection();
					
					//alert(tinyMCEPopup);
					
					//alert(injectIn.activeEditor.selection.getContent({format : 'text'})); 
					
					//alert('elm ' + html);
					
					//var bm = injectIn.selection.getBookmark();
					/* do some stuff */
					//alert(injectIn.activeEditor.selection.getContent({format : 'text'}));
					
					//alert(html);
					
					//alert(alert(injectIn.selection));
					
					//alert(selectedDownloadHtml);
					
					if(!downloadName) {
						downloadName = html;
					}
					
					//alert(html);
					
					if(Browser.Engine.trident4 || Browser.Engine.trident) {
						injectIn.execCommand('mceInsertContent', false,  '<a href="' + html + '">' + downloadName + '</a>', {skip_undo : 1});
					}
					else
					{
						injectIn.execCommand('Createlink', false, html, {skip_undo : 1});
					}
					
					//injectIn.selection.moveToBookmark(bm);
					//injectIn.execCommand(...);
				}
			}
			else
			{
				//alert('wel element dus aanpassen' + html);
				//dom.setAttrib(parentNode,'href',html);
				
				dom.setAttribs(parentNode, {
					href : html,
					title : ''
				});
			}

			

			removePopup();
		});

	}}).send(strSendPost);
}

