var lastUploadImageUrl ='';

CKEDITOR.on( 'instanceReady', function( ev )
{
        
    // Ends self closing tags the HTML4 way, like <br>.
    ev.editor.dataProcessor.writer.selfClosingEnd = '>';
    ev.editor.dataProcessor.writer.setRules( 'p',
    {
        // Indicates that this tag causes indentation on line breaks inside of it.
        indent : true,
 
        // Inserts a line break before the <p> opening tag.
        breakBeforeOpen : true,
 
        // Inserts a line break after the <p> opening tag.
        breakAfterOpen : true,
 
        // Inserts a line break before the </p> closing tag.
        breakBeforeClose : true,
 
        // Inserts a line break after the </p> closing tag.
        breakAfterClose : true
    });

});


// When opening a dialog, its "definition" is created for it, for
// each editor instance. The "dialogDefinition" event is then
// fired. We should use this event to make customizations to the
// definition of existing dialogs.
CKEDITOR.on( 'dialogDefinition', function( ev )
{
    // Take the dialog name and its definition from the event
    // data.
    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;

    // Check if the definition is from the dialog we're
    // interested on (the "Link" dialog).
    if ( dialogName == 'link' )
    {
        // Get a reference to the "Link Info" tab.
        var infoTab = dialogDefinition.getContents( 'info' );

        // Add a text field to the "info" tab.
        //			infoTab.add( {
        //					type : 'text',
        //					label : 'My Custom Field',
        //					id : 'customField',
        //					'default' : 'Sample!',
        //					validate : function()
        //					{
        //						if ( /\d/.test( this.getValue() ) )
        //							return 'My Custom Field must not contain digits';
        //					}
        //				});

        // Remove the "Link Type" combo and the "Browser
        // Server" button from the "info" tab.
        infoTab.remove( 'linkType' );
        infoTab.remove( 'browse' );

        // Set the default value for the URL field.
        //			var urlField = infoTab.get( 'url' );
        //			urlField['default'] = 'www.example.com';

        // Remove the "Target" tab from the "Link" dialog.
        dialogDefinition.removeContents( 'target' );

        // Add a new tab to the "Link" dialog.
        //			dialogDefinition.addContents({
        //				id : 'customTab',
        //				label : 'My Tab',
        //				accessKey : 'M',
        //				elements : [
        //					{
        //						id : 'myField1',
        //						type : 'text',
        //						label : 'My Text Field'
        //					},
        //					{
        //						id : 'myField2',
        //						type : 'text',
        //						label : 'Another Text Field'
        //					}
        //				]
        //			});

        // Rewrite the 'onFocus' handler to always focus 'url' field.
        dialogDefinition.onFocus = function()
        {
            var urlField = this.getContentElement( 'info', 'url' );
            urlField.select();
        };
    }
                
    if ( dialogName == 'image' )
    {
        var infoTab = dialogDefinition.getContents( 'info' );
        var linkTab = dialogDefinition.getContents( 'Link' );
    
        //        Set the default value for the URL field.
        
        if(lastUploadImageUrl){       
            var urlField = infoTab.get( 'txtUrl' );
            var linkField = linkTab.get( 'txtUrl' );
            //            alert(lastUploadImageUrl)
            urlField['default'] = lastUploadImageUrl;
        //            linkField['default'] = lastUploadImageUrl;
        }
        var alignField = infoTab.get( 'cmbAlign' );
        alignField['default'] = 'left';
        
        infoTab.remove( 'txtBorder' );
        infoTab.remove( 'txtHSpace' );
        infoTab.remove( 'txtVSpace' );
        linkTab.remove( 'cmbTarget' );
        dialogDefinition.removeContents( 'advanced' );
        dialogDefinition.removeContents( 'target' );
        

    
        if ( dialogName == 'table' )
        {

            dialogDefinition.removeContents( 'advanced' );
        

        }
    }
});

$(document).ready(function() {
	
    $("a.post_prefix_image_big").fancybox({
        'transitionIn'	:	'elastic',
        'transitionOut'	:	'elastic',
        'speedIn'		:	600, 
        'overlayShow'	:	true,
        'hideOnContentClick' :true,
        'titlePosition': 'inside'
                
    });
        
    $(".collapse").collapse({
        show: function(){
            this.animate({
                opacity: 'toggle', 
                height: 'toggle'
            }, 300);
        },
        hide : function() {
                    
            this.animate({
                opacity: 'toggle', 
                height: 'toggle'
            }, 300);
        }
    });
	
});

function ExecuteCommand( commandName )
{
    // Get the editor instance that we want to interact with.
    var oEditor = CKEDITOR.instances.body;

    // Check the active editing mode.
    if ( oEditor.mode == 'wysiwyg' )
    {
        // Execute the command.
        // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#execCommand
        oEditor.execCommand( commandName );
    }
    else
        alert( 'You must be in WYSIWYG mode!' );
}

function insertUploadedImage(url){
    lastUploadImageUrl = url;
    ExecuteCommand('image');
    return false;
}
