var tweetajax   =	true;
var target		=	'';
CKEDITOR.dialog.add( 'embedpicture', function( editor ) {
	var src	=	'';
	var uploadform	=	function (){
		return	'<form name="embedpicture" id="embedpicture" action="" accept-charset="utf-8"  method="post" enctype="multipart/form-data" class="embedform">'+
				'<label for="FileInput">Upload Picture</label>'+
				'<input type="hidden" name="type" id="type" value="articleimage">'+
				'<input type="file" name="FileInput" id="FileInput" value="">'+
				'<input type="hidden" name="imageId" value="">'+
				'<input type="hidden" name="awsPath" value="_STORIES_IMAGES_DIR">'+
				'<input type="hidden" name="imgDiv" value="">'+
				'<div class="embedpictureloader">Please wait while uploading...</div>'
				'</form>';
	};

	var embedfrmsubmit 	= 	function(){
		$('#embedpicture').ajaxForm({
			async: false,
			clearForm: true,
			forceSync: true,
			dataType: 'json',
			headers: { 'X-CSRF-TOKEN': parent.$("meta[name=csrf-token]").attr("content") },
			beforeSerialize:function($Form, options){},
			beforeSubmit: function(formData, jqForm, options){
				$('.tmessage').hide();
				$('.embedpictureloader').show();
			},
			success: function(responseText, statusText, xhr, $form){
				var responseText 	= 	jQuery.parseJSON(xhr.responseText);
				if (responseText.status){
					src	=	responseText.s3path;
				}else{
					$('.tmessage').html(responseText.message);
					$('.tmessage').show();
				}
			},
			complete: function(){
				$('.embedpictureloader').hide();
			}
		});
	};
    return {
        title: 'Embed Picture',
        minWidth: 350,
        minHeight: 150,
        onOk: function() {
        	window.parent.CKupdate();
        	return true;
        },
        contents: 	[{	id: 'info',
        				elements: 	[{	type: 'html',
        								html: uploadform(),
        								validate: function(element){
        									$('.tmessage').hide();
        		                    		if ($('form #FileInput').val()=='')
        		                    		{
        		                    			$('.tmessage').html('Please upload a picture.');
        		                    			$('.tmessage').show();
        		                    			return false;
        		                    		}else{
        		                            	$('#embedpicture').attr('action',editor.config.customValues.imageupload);
        		                            	embedfrmsubmit();
        		                            	$('#embedpicture').trigger('submit');
        		                    		}
        								},
				        				setup: function(widget) {},
				                        commit: function(widget) {
				                        	if (src!=''){
				                        		data	=	'<div class="embedimgblock"><img src="'+src+'"></div>'
					                        	widget.setData( 'pictureblock', data);
				                        	}
				                        }
        							},
        		                    {
        		                        id: 'tweets',
        		                        type: 'html',
        		                    	html : '<div class="tmessage"></div>'
        		                    }]
        			}],
        buttons: [  	CKEDITOR.dialog.okButton,
                    	CKEDITOR.dialog.cancelButton],
    };
});
