CKEDITOR.plugins.add( 'cmslinkpage',
{
	init: function( editor )
	{
    CKEDITOR.dialog.add( 'cmsLinkPageDialog', function ( editor )
		{
			return {
				title : 'Wstaw/Edytuj odnośnik do strony',
				minWidth : 400,
				minHeight : 200,
        resizable: CKEDITOR.DIALOG_RESIZE_NONE,
				contents :
				[
					{
						id : 'tab1',
						label : 'Adres url:',
						elements :
						[
							{
								type : 'text',
								id : 'url',
								label : 'Adres url:',
								validate : CKEDITOR.dialog.validate.notEmpty( "Adres url nie może być pusty." ),
                commit: function( element )
                {
                  var href = this.getValue();
                  if ( href )
                  {
                    element.setAttribute("href", href);
                    if ( !element.getText() )
                    {
                      element.setText( this.getValue() );
                    }
                  }
                }
							},
							{
								type : 'text',
								id : 'text',
								label : 'Tekst odnośnika:',
                setup: function( element ) {
                  this.setValue( element.getText() );
                },
                commit: function(element) {
                  var currentValue = this.getValue();
                  if(currentValue !== "" && currentValue !== null) {
                    element.setText(currentValue);
                  }
                }
							},
              {
                type : 'button',
                hidden : true,
                id : 'id0',
                label : editor.lang.common.browseServer,
                filebrowser :
                {
                  action : 'Browse',
                  url: '/admin/pages/pages_url_browser/',
                  onSelect : function( url )
                  {
                    var dialog = this.getDialog();
                    dialog.getContentElement( 'tab1', 'url' ).setValue( url );
                    return false;
                  }
                }
              }
						]
					}
				],
        onShow: function()
        {
          var selection = editor.getSelection();
          var selector = selection.getStartElement();
          var element;

          if ( selector ) {
            element = selector.getAscendant( 'a', true );
          }

          if ( !element || element.getName() != 'a' )
          {
            element = editor.document.createElement( 'a' );
            if( selection ) {
              element.setText(selection.getSelectedText());
            }
            this.insertMode = true;
          }
          else {
            this.insertMode = false;
          }

          this.element = element;


          this.setupContent(this.element);
        },
				onOk : function()
				{
          var dialog = this;
          var anchorElement = this.element;

          this.commitContent(this.element);

          if(this.insertMode) {
            editor.insertElement(this.element);
          }
				}
			};
		} );

		editor.addCommand( 'cmsLinkPageDialog',new CKEDITOR.dialogCommand( 'cmsLinkPageDialog' ) );

		editor.ui.addButton( 'cmslinkpage',
		{
			label: 'Wstaw/Edytuj odnośnik do strony',
			command: 'cmsLinkPageDialog',
			icon: this.path + 'images/icon.png'
		} );
	}
} );