'use strict';

overlord_service.factory('mdService', ['$http', '$compile', '$timeout', function ($http, $compile, $timeout) {
    var growl = {};
    growl.default_options = {
        offset: {
            from: "top",
            amount: 20
        },
        align: "right",
        width: 250,
        duration:500,
        delay: 4000,
        stackup_spacing: 10
    };

    growl.show = function (element, scope, options) {
        var options = jQuery.extend({}, growl.default_options, options);
        if (options.top_offset) {
            options.offset = {
                from: "top",
                amount: options.top_offset
            };
        }
        var offsetAmount = options.offset.amount;
        jQuery(".bootstrap-growl").each(function (index) {
            var offset = parseInt($(this).css(options.offset.from)) + $(this).outerHeight() + options.stackup_spacing;
            if (isNaN(offset)) {
                offset = 0;
            }
            return offsetAmount = Math.max(offsetAmount, offset);
        });
        element.css("position", "fixed");
        element.css("margin", 0);
        element.css("z-index", "9999");
        element.css("display", "none");
        element.css(options.offset.from, offsetAmount + "px");
        if (options.width !== "auto") {
            element.css("width", options.width + "px");
        }
        switch (options.align) {
            case "center":
                element.css("left", "50%");
                element.css("margin-left", "-" + (element.outerWidth() / 2) + "px");
                break;
            case "left":
                element.css("left", "20px");
                break;
            default:
                element.css("right", "20px");
        }
        element.fadeIn(options.duration);
        if (options.delay > 0) {
            $timeout(function () {
                element.fadeOut(options.duration, function() {
                    scope.$eval(options.dismiss_function);
                });
            }, options.delay);
        }
        return element;
    };
    return growl;
}])
;
