/**
 * Create a dialog that contains the file manager.
 * 
 * The `.mediagalleryUiFilemanagerdialog()` widget creates a dialog that contains the new implemented file manager.
 * The widget opens on click at the given element the dialog with the file manager.
 * 
 * You can pass two options. The first is the dialog option that defines the design and handling of the dialog.
 * You can use all options the `.epUiDialog()` offers. The second option is called filemanager und defines the content of it.
 * Here you can use all options the `.mediagalleryUiFilemanager()` offers.
 * 
 * ### Examples
 * Apply .mediagalleryUiFilemanagerdialog() widget to an element.
 * 
 * JavaScript:
 * 
 *     de_epages("#filemanager").mediagalleryUiFilemanagerdialog({																	
 *     dialog:    {
 *     draggable:     true,
 *     modal:         false,
 *     buttons: {
 *     Close:{
 *     click: function(){ console.debug("do something")}
 *     }
 *     }
 *     },
 *     filemanager: {
 *     selectable:    "image/",
 *     currentFolder: "BackgroundImages"
 *     }
 *     });
 * 
 * HTML:
 * 
 *     <button id="filemanager">Select file</button>
 * 
 * 
 * @class jQuery.ui.mediagalleryUiFilemanagerdialog
 * @extends jQuery.widget
 * 
 * @uses jQuery.ui.widget
 * @uses de_epages
 * @uses ep.ui.dialog
 * @uses de_epages.mediagallery.ui.filemanager
 * @since 6.15.0
 */

/**
 * @cfg {Object} [dialog] Here you can define all options ep.ui.dialog can have.
 */

/**
 * @cfg {Object} [filemanager] Here you can define all options de_epages.mediagallery.ui.filemanager can have.
 */

/**
 * See `jQuery.ui.mediagalleryUiFilemanagerdialog` for details.
 * 
 * @param {Object} [options] A map of additional options pass to the method.
 * @param {Object} [dialog] Here you can define all options ep.ui.dialog can have.
 * @param {Object} [filemanager] Here you can define all options de_epages.mediagallery.ui.filemanager can have.
 * 
 * @method mediagalleryUiFilemanagerdialog
 * @member jQuery
 * 
 * @since 6.15.0
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			de_epages.mediagallery.ui.filemanagerdialog
 *
 * @revision		$Revision: 1.9 $
 */

/*jslint nomen:true*/
/*global define*/

define("de_epages/mediagallery/ui/filemanagerdialog", [
	"jquery/ui/widget",
	"de_epages",
	"$dict!de_epages/mediagallery/dictionary",
	"ep/ui/dialog",
	"de_epages/mediagallery/ui/filemanager"
], function ($, de_epages, mediagalleryDict) {
	"use strict";
    /*
     *
     * @dictionary		de_epages.mediagallery.dictionary
     *
     * @translation		{FileRepository}
     *
     */

    var tFileRepository = mediagalleryDict.translate("FileRepository");

    $.widget("ui.mediagalleryUiFilemanagerdialog", {

        options: {
            dialog: {
                title: tFileRepository,
                width: 920,
                height: 563,
                draggable: false,
                modal: true,
                autoOpen: false
            },
            filemanager: {
                accept: "*/*",
                selectable: "",
                currentFolder: "MediaGallery"
            }
        },

        _create: function () {
            var self = this,
                o = self.options;

            self.dialog = $('<div class="fileManagerDialog"></div>').appendTo("body").uiDialog(o.dialog);

            self.element.on("click.mediagalleryUiFilemanagerdialog", function () {
                self.open();
            });

        },

        /**
         * This method opens the dialog.
         * 
         * @method open
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        open: function () {
            if (!this.filemanager) {
                this._createFilemanager();
            }
            $(this.dialog).uiDialog("open");
        },

        /**
         * This method closes the dialog.
         * 
         * @method close
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        close: function () {
            $(this.dialog).uiDialog("close");
        },

        /**
         * This method destroys the widget.
         * 
         * @method destroy
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        destroy: function () {
            if (this.dialog) {
                this.dialog.remove();
                this.element.off("click.mediagalleryUiFilemanagerdialog");
            }

            this._superApply(arguments);
        },

        _createFilemanager: function () {
            var self = this,
                o = this.options;

            self.filemanagerContainer = $('<div class="fileManager"></div>').appendTo(self.dialog);
            self.filemanager = self.filemanagerContainer.mediagalleryUiFilemanager(o.filemanager);

            // trigger event if filemanager is ready
            self.filemanager.on("filemanagerReady", function () {
                self.element.trigger("filemanagerReady");
            });
        },

        /**
         * This method destroys the file manager in the dialog.
         * 
         * @method resetFilemanager
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        resetFilemanager: function () {
            this.filemanager.mediagalleryUiFilemanager("destroy");
            this.filemanager = undefined;
        },

        /**
         * This method returns the value of the current gallery usage.
         * 
         * @method getCurrentUsage
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        getCurrentUsage: function () {
            return this.filemanager.mediagalleryUiFilemanager("getCurrentUsage");
        },

        /**
         * This method returns an array containing all selected files which are selectable.
         * 
         * @method getSelectedElements
         * @member jQuery.ui.mediagalleryUiFilemanagerdialog
         * 
         * @since 6.15.0
         */
        getSelectedElements: function () {
            return this.filemanager.mediagalleryUiFilemanager("getSelectedElements");
        }
    });

    return de_epages;
});