Index: /CKEditor/branches/prototype/_source/plugins/image/dialogs/image.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/image/dialogs/image.js	(revision 2786)
+++ /CKEditor/branches/prototype/_source/plugins/image/dialogs/image.js	(revision 2787)
@@ -22,6 +22,98 @@
 imageDialog = function( editor, dialogType )
 {
+	var regexSize = /^\s*(\d+)px\s*$/i ;
 	var uploadAction = 'nowhere.php';
 	var imageDialog = ( dialogType == 'image' );
+
+	var addChange = function( name, input, object ){
+		var dialog = input.getDialog();
+		if ( input.isChanged() || ( dialog.editMode[ object ] == false && input.getValue() != '' ))
+		{
+			dialog.changedAttibutes[ object ][ name ] = input.getValue();
+			return true;
+		}
+		return false;
+	};
+
+	// Function called in onShow to load selected element.
+	var loadElements = function( editor, selection, ranges, element )
+	{
+		this.editObj[ 'image' ] = false;
+		this.editMode[ 'image' ] = false;		// Default:  Create a new Image
+		this.saveSelection();
+		var imgType = 'input';
+
+		//a=element.getChildren();for(i in a){alert(i+'--'+a[i])}
+	
+		if ( element.$.tagName.toLowerCase() == 'a' )
+		{
+			this.editMode[ 'link' ] = true;
+			var linkChildren = element.getChildren();
+			if ( linkChildren.count() == 1 ){		// 1 child.
+				var childTagName = linkChildren.getItem(0).$.tagName.toLowerCase();
+				if ( childTagName == 'img' || childTagName == 'input' )
+				{
+					this.editMode[ 'image' ] = true;
+					this.editObj[ 'image' ] = linkChildren.getItem(0);
+				}
+			}
+
+			this.editObj[ 'link' ] = element;
+
+			if ( imageDialog )
+			{
+				imgType = 'img';
+
+				// Fill out all fields.
+				if ( !readAttribute.apply( this, [ 'Link', '_cke_saved_href', 'txtUrl', element ] ) )
+					readAttribute.apply( this, [ 'Link', 'href', 'txtUrl', element ] )
+				readAttribute.apply( this, [ 'Link', 'target', 'cmbTarget', element ] );
+			}
+		}
+
+		if ( element.$.tagName.toLowerCase() == 'img' || element.$.tagName.toLowerCase() == 'input' || this.editObj[ 'image' ] )
+		{
+			this.editMode[ 'image' ] = true;
+			if ( !this.editObj[ 'image' ] )
+			{
+				this.editObj[ 'image' ] = element;
+			}
+		//	alert('tag: '+element.$.tagName.toLowerCase());
+			readAttribute.apply( this, [ 'info', 'alt', 'txtAlt' ] );
+			readAttribute.apply( this, [ 'info', 'width', 'txtWidth' ] );
+			readAttribute.apply( this, [ 'info', 'height', 'txtHeight' ] );
+			readAttribute.apply( this, [ 'info', 'border', 'txtBorder' ] );
+			readAttribute.apply( this, [ 'info', 'hspace', 'txtHSpace' ] );
+			readAttribute.apply( this, [ 'info', 'vspace', 'txtVSpace' ] );
+			readAttribute.apply( this, [ 'info', 'align', 'cmbAlign' ] );
+			readAttribute.apply( this, [ 'advanced', 'id', 'linkId' ] );
+			readAttribute.apply( this, [ 'advanced', 'dir', 'cmbLangDir' ] );
+			readAttribute.apply( this, [ 'advanced', 'lang', 'txtLangCode' ] );
+			readAttribute.apply( this, [ 'advanced', 'longdesc', 'txtGenLongDescr' ] );
+			readAttribute.apply( this, [ 'advanced', 'class', 'txtGenClass' ] );
+			readAttribute.apply( this, [ 'advanced', 'style', 'txtdlgGenStyle' ] );	
+			readAttribute.apply( this, [ 'advanced', 'title', 'txtGenTitle' ] );
+		}
+
+		// Fill out all fields.
+		if ( !readAttribute.apply( this, [ 'info', '_cke_saved_url', 'txtUrl' ] ) )
+			readAttribute.apply( this, [ 'info', 'src', 'txtUrl' ] )
+
+		return false;
+	};
+	
+	var readAttribute = function( page, attribute, input, object )
+	{
+		if( !object )
+			object = this.editObj[ 'image' ];
+
+		var attributeValue = object.getAttribute( attribute );
+		if ( attributeValue == null )
+			this.setValueOf( page, input, "" );
+		else
+			this.setValueOf( page, input, attributeValue );
+
+		return attributeValue;
+	}
 
 	return {
@@ -29,13 +121,166 @@
 		minWidth : 450,
 		minHeight : 400,
-		onShow : function( )
+		onOk : function( data )
 		{
-			//TODO: load content
+			var imgType = imageDialog?'img':'input';
+			var useLink = false;
+			var removeObj = false;
+
+			if ( this.editMode[ 'image' ] )
+			{
+				//a=this.editObj[ 'image' ].getParent();for(i in a){alert(i+'--'+a[i])}
+				var imgTagName = this.editObj[ 'image' ].$.tagName.toLowerCase();
+				
+				if ( imageDialog && imgTagName == 'input' )		// Image dialog and Input element
+				{
+					if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
+					{
+						// Replace INPUT-> IMG
+						removeObj = this.editObj[ 'image' ] ;
+						this.editObj[ 'image' ] = editor.document.createElement( 'img' );
+						removeObj.insertBeforeMe( this.editObj[ 'image' ] );
+						removeObj.getParent().remove( removeObj );
+						imgTagName = 'img';
+					}
+				}
+				else if ( !imageDialog && imgTagName == 'img' )		// ImageButton dialog and Image element
+				{
+					if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
+					{
+						// Replace IMG -> INPUT
+						removeObj = this.editObj[ 'image' ] ;
+						this.editObj[ 'image' ] = editor.document.createElement( 'input' );
+						this.editObj[ 'image' ].setAttribute ( 'type' ,'image' );
+						removeObj.insertBeforeMe( this.editObj[ 'image' ] );
+						removeObj.getParent().remove( removeObj );
+						imgTagName = 'input';
+					}
+				}
+			}
+			else	// Create a new image.
+			{
+				if ( imgType == 'img' )
+					this.editObj[ 'image' ] = editor.document.createElement( 'img' );
+				else
+				{
+					this.editObj[ 'image' ] = editor.document.createElement( 'input' );
+					this.editObj[ 'image' ].setAttribute ( 'type' ,'image' );
+				}
+			}
+
+			// Create a new link.
+			if ( this.editMode[ 'link' ] == false )
+				this.editObj[ 'link' ] = editor.document.createElement( 'a' );
+
+			// Set image attributes.
+			this.editObj[ 'image' ].setAttributes( this.changedAttibutes[ 'image' ] );
+
+			// Set link attributes.
+			this.changedAttibutes[ 'link' ][ 'href' ] =  'javascript:void(0)/*' + CKEDITOR.tools.getNextNumber() + '*/';
+			this.editObj[ 'link' ].setAttributes( this.changedAttibutes[ 'link' ] );
+			
+			if ( this.editObj[ 'link' ].getAttribute( '_cke_saved_href' ) )
+			{
+				useLink = true;
+				alert('jest link');
+			}
+
+			// Insert a new Image.
+			if ( this.editMode[ 'image' ] == false )
+			{
+				// It doesn't work with IE.
+				this.restoreSelection();
+				this.clearSavedSelection();
+				
+				if ( useLink )
+					//Insert a new Link.
+					if ( this.editMode[ 'link' ] == false )
+					{
+						this.editObj[ 'link' ].append( this.editObj[ 'image' ], false );
+						editor.insertElement( this.editObj[ 'link' ] );
+					}
+					else 	//Link already exists, image not.
+						this.editObj[ 'link' ].append( this.editObj[ 'image' ], false );
+				else
+					editor.insertElement( this.editObj[ 'image' ] );
+			}
+			else		// Image already exists.	
+			{
+				//Add a new link element.
+				if ( this.editMode[ 'link' ] == false && this.changedAttibutes[ 'link' ][ '_cke_saved_href' ] )
+				{
+					this.editObj[ 'image' ].insertBeforeMe( this.editObj[ 'link' ] );	
+					this.editObj[ 'image' ].appendTo( this.editObj[ 'link' ] );	
+				
+				}
+				//Remove Link, Image exists.
+				else if ( this.editMode[ 'link' ] == true && !this.changedAttibutes[ 'link' ][ '_cke_saved_href' ] )
+				{
+					//Remove link.
+					//ohoho. later...
+					/*
+						a=this.editObj[ 'image' ];
+						for(i in a)
+						{
+							alert(i+"---" + a[i] )
+						}
+					*/
+				}
+				else {};	//Image and Link already exists. Do nothing.
+			}
+	
+			return true;
 		},
-		onOk : function( )
+		onShow : function()
 		{
-			//TODO: save settings
-		},
-		
+			// Default: create a new element.
+			this.editObj = new Array();
+			
+			this.editMode = new Array();
+			this.editMode[ 'link' ] = false;
+			this.editMode[ 'image' ] = false;
+			
+			this.changedAttibutes = new Array();			
+			this.changedAttibutes[ 'link' ] = new Array();
+			this.changedAttibutes[ 'image' ] = new Array();
+			
+			// IE BUG: Selection must be in the editor for getSelection() to work.
+			this.restoreSelection();
+
+			var editor = this.getParentEditor(),
+				selection = editor.getSelection(),
+				ranges = selection.getRanges();
+
+			// Check selection. Fill in all the relevant fields if there's already one link selected.
+			if ( ranges.length == 1 )
+			{
+				element = false;
+				ranges[0].enlarge( CKEDITOR.ENLARGE_ELEMENT );
+				rangeRoot = ranges[0].getCommonAncestor( true );
+				
+				elementImg = rangeRoot.getAscendant( 'img', true );
+				elementA = rangeRoot.getAscendant( 'a', true );
+				elementInput = rangeRoot.getAscendant( 'input', true );
+				
+				if ( elementImg )
+					element = elementImg;
+				else
+					if ( elementA && elementA.getAttribute( 'href' ) )
+						element = elementA;
+					else
+						if ( elementInput && elementInput.getAttribute( 'type' ) 
+						&& elementInput.getAttribute( 'type' ) == 'image' )
+							element = elementInput;
+				
+				if ( element )
+				{
+					loadElements.apply( this, [ editor, selection, ranges, element ] );
+					selection.selectElement( element );
+					this.saveSelection();
+				}
+			}
+
+			this.getContentElement( 'info', 'txtUrl' ).focus();
+		},		
 		onLoad : function( )
 		{
@@ -72,5 +317,8 @@
 										type : 'text',
 										label : '',
-										validate: function( data ) {
+										validate: function()
+										{
+											addChange( 'src', this, 'image' );
+											addChange( '_cke_saved_url', this, 'image' );
 											return true;
 										}
@@ -83,4 +331,6 @@
 										onClick : function()
 										{
+											this.getDialog().setValueOf( "info", "txtUrl", "http://www.fckeditor.net/images/demo_screenshot.gif" );
+											
 										}
 									}
@@ -94,5 +344,7 @@
 						label : editor.lang.dlgImgAlt,
 						accessKey : 'A',
-						validate: function( data ) {
+						validate: function()
+						{
+							addChange( 'alt', this, 'image' );
 							return true;
 						}
@@ -126,5 +378,8 @@
 														{
 															var func = CKEDITOR.dialog.validate.integer( editor.lang.dlgImgTypeNumber );
-															return func.apply( this );
+															var isValid = func.apply( this );
+															if ( isValid )
+																addChange( 'width', this, 'image' );
+															return isValid;
 														}
 													},
@@ -137,5 +392,8 @@
 														{
 															var func = CKEDITOR.dialog.validate.integer( editor.lang.dlgImgTypeNumber );
-															return func.apply( this );
+															var isValid = func.apply( this );
+															if ( isValid )
+																addChange( 'height', this, 'image' );
+															return isValid;
 														}
 													}
@@ -162,8 +420,11 @@
 												labelLayout : 'horizontal',
 												label : editor.lang.dlgImgBorder,
-												validate: function( data )
+												validate: function()
 												{
 													var func = CKEDITOR.dialog.validate.integer( editor.lang.dlgImgTypeNumber );
-													return func.apply( this );
+													var isValid = func.apply( this );
+													if ( isValid )
+														addChange( 'border', this, 'image' );
+													return isValid;
 												}
 											},
@@ -173,8 +434,11 @@
 												labelLayout : 'horizontal',
 												label : editor.lang.dlgImgHSpace,
-												validate: function( data )
+												validate: function()
 												{
 													var func = CKEDITOR.dialog.validate.integer( editor.lang.dlgImgTypeNumber );
-													return func.apply( this );
+													var isValid = func.apply( this );
+													if ( isValid )
+														addChange( 'hspace', this, 'image' );
+													return isValid;
 												}
 											},
@@ -184,8 +448,11 @@
 												labelLayout : 'horizontal',
 												label : editor.lang.dlgImgVSpace,
-												validate: function() {
+												validate: function()
+												{
 													var func = CKEDITOR.dialog.validate.integer( editor.lang.dlgImgTypeNumber );
-													return func.apply( this );
-
+													var isValid = func.apply( this );
+													if ( isValid )
+														addChange( 'vspace', this, 'image' );
+													return isValid;
 												}
 											},
@@ -209,5 +476,10 @@
 													[ editor.lang.dlgImgAlignTextTop , 'textTop'],
 													[ editor.lang.dlgImgAlignTop , 'top']
-												]
+												],
+												validate: function()
+												{
+													addChange( 'align', this, 'image' );
+													return true;
+												}
 											}
 										]
@@ -244,5 +516,7 @@
 						label : editor.lang.dlgImgURL,
 						style : 'width: 100%',
-						validate: function( data ) {
+						validate: function()
+						{
+							addChange( '_cke_saved_href', this, 'link' );
 							return true;
 						}
@@ -252,5 +526,10 @@
 						id : 'browse',
 						style : 'float:right',
-						label : editor.lang.browseServer
+						label : editor.lang.browseServer,
+						onClick : function()
+						{
+							this.getDialog().setValueOf( "Link", "txtUrl", "http://www.fckeditor.net/" );
+							
+						}
 					},
 					{
@@ -265,5 +544,10 @@
 							[ editor.lang.linkTargetSelf , '_self'],
 							[ editor.lang.linkTargetParent , '_parent']
-						]
+						],
+						validate: function()
+						{
+							addChange( 'target', this, 'link' );
+							return true;
+						}
 					}
 				]
@@ -302,5 +586,10 @@
 								type : 'text',
 								id : 'linkId',
-								label : editor.lang.dlgGenId
+								label : editor.lang.dlgGenId,
+								validate : function()
+								{
+									addChange( 'id', this, 'image' );
+									return true;
+								}
 							},
 							{
@@ -314,10 +603,20 @@
 									[ editor.lang.dlgGenLangDirLtr, 'ltr' ],
 									[ editor.lang.dlgGenLangDirRtl, 'rtl' ]
-								]
+								],
+								validate : function()
+								{
+									addChange( 'dir', this, 'image' );
+									return true;
+								}
 							},
 							{
 								type : 'text',
 								id : 'txtLangCode',
-								label : editor.lang.dlgGenLangCode
+								label : editor.lang.dlgGenLangCode,
+								validate : function()
+								{
+									addChange( 'lang', this, 'image' );
+									return true;
+								}
 							}
 						]
@@ -326,5 +625,10 @@
 						type : 'text',
 						id : 'txtGenLongDescr',
-						label : editor.lang.dlgGenLongDescr
+						label : editor.lang.dlgGenLongDescr,
+						validate : function()
+						{
+							addChange( 'longdesc', this, 'image' );
+							return true;
+						}
 					},
 					{
@@ -336,10 +640,20 @@
 								type : 'text',
 								id : 'txtGenClass',
-								label : editor.lang.dlgGenClass
+								label : editor.lang.dlgGenClass,
+								validate : function()
+								{
+									addChange( 'class', this, 'image' );
+									return true;
+								}
 							},
 							{
 								type : 'text',
 								id : 'txtGenTitle',
-								label : editor.lang.dlgGenTitle
+								label : editor.lang.dlgGenTitle,
+								validate : function()
+								{
+									addChange( 'title', this, 'image' );
+									return true;
+								}
 							},
 						]
@@ -348,5 +662,10 @@
 						type : 'text',
 						id : 'txtdlgGenStyle',
-						label : editor.lang.dlgGenStyle
+						label : editor.lang.dlgGenStyle,
+						validate : function()
+						{				
+							addChange( 'style', this, 'image' );
+							return true;
+						}
 					}
 				]
