/**
 * Create a box that displays the current utilisation of the MediaGallery.
 * 
 * The `.mediagalleryUiMemoryusage()` method creates a box that displays the current utilisation of the MediaGallery.It shows how much memory is used / still free.
 * 
 * ### Examples
 * Apply .mediagalleryUiMemoryusage() widget to an element.
 * 
 * JavaScript:
 * 
 *     de_epages("#memoryusage").mediagalleryUiMemoryusage({
 *     currentUsage: 48000
 *     });
 * 
 * HTML:
 * 
 *     <div id="memoryusage"></div>
 * 
 * 
 * @class jQuery.ui.mediagalleryUiMemoryusage
 * @extends jQuery.widget
 * 
 * @uses jQuery.dict
 * @uses jQuery.tmpl
 * @uses jQuery.ui.widget
 * @uses de_epages
 * @since 6.15.0
 */

/**
 * @cfg {Number} [currentUsage] A number containing the current usage in bytes.
 */

/**
 * @cfg {Number} [maxSize] A number containing the maximal usage in bytes.
 */

/**
 * See `jQuery.ui.mediagalleryUiMemoryusage` for details.
 * 
 * @param {Object} [options] A map of additional options pass to the method.
 * @param {Number} [currentUsage] A number containing the current usage in bytes.
 * @param {Number} [maxSize] A number containing the maximal usage in bytes.
 * 
 * @method mediagalleryUiMemoryusage
 * @member jQuery
 * 
 * @since 6.15.0
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			de_epages/mediagallery/ui/memoryusage
 */

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

define("de_epages/mediagallery/ui/memoryusage", [
	"jquery/ui/widget",
	"de_epages",
	"$dict!de_epages/mediagallery/dictionary",
	"$tmpl!de_epages/mediagallery/ui/memoryusage"
], function ($, de_epages, mediagalleryDict, tmplMemoryusage) {
	"use strict";
    /*
     *
     * @dictionary			de_epages.mediagallery.dictionary
     *
     * @translation		{MemoryUsage}
     *									{FreeMemory}
     *									{UsedMemory}
     *									{TotalMemory}
     *
     */

    // translations
    var translation = {
        tMemoryUsage: mediagalleryDict.translate("MemoryUsage"),
        tFreeMemory: mediagalleryDict.translate("FreeMemory"),
        tUsedMemory: mediagalleryDict.translate("UsedMemory"),
        tTotalMemory: mediagalleryDict.translate("TotalMemory")
    },
        unit = "MB";

    $.widget("ui.mediagalleryUiMemoryusage", {

        options: {
            maxSize: 100000,
            currentUsage: 0
        },

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

            $.extend(o, translation);

            this.element.addClass("ep-uiMemoryUsage");
            this.template = tmplMemoryusage(o).appendTo(this.element);

            $.extend(this, $.tmplItem(this.template).elements);

            this.updateUsage(o.currentUsage);
        },

        /**
         * This method updates the memory utilisation to the given value.
         * 
         * @method updateUsage
         * @member jQuery.ui.mediagalleryUiMemoryusage
         * 
         * @since 6.15.0
         */
        updateUsage: function (currentUsage) {
            var o = this.options,
                percentage = Math.ceil(100 * currentUsage / o.maxSize),
                prevCssClass = this.cssClass;

            if (percentage > 100) {
                percentage = 100;
            }

            if (percentage > 90) {
                this.cssClass = "PercentageCritical";
            } else if (percentage > 60) {
                this.cssClass = "PercentageMiddle";
            } else {
                this.cssClass = "PercentageNormal";
            }

            this.statusBar.removeClass(prevCssClass).addClass(this.cssClass);

            this.statusBar.css({
                width: percentage + "px"
            });

            this.percentage.html(percentage + "%");
            this.freeMemory.html(Math.ceil((o.maxSize - currentUsage) / 1024) + " " + unit);
        },

        /**
         * This method removes all classes and childs which were added to the element.
         * 
         * @method destroy
         * @member jQuery.ui.mediagalleryUiMemoryusage
         * 
         * @since 6.15.0
         */
        destroy: function () {
            this._superApply(arguments);

            if (this.element) {
                this.element.removeClass("ep-uiMemoryUsage");
                this.container.remove();
            }
        }
    });

    return de_epages;

});