
var resourceController = {UnknownError:"Okänt fel", RequiredField:"The field is required."};
var templateController = { Error: "<div class='field-error'>{0}</div>", Preloader: "<div class='loader'></div>", SmallPreloader: "<span class='loader'></span>" };

(function ($) {
    $.fn.clearDecoration = function (selector) {
        var element = this;
        if (selector != null && selector.length > 0){
            element = element.find(selector);
        }
        if (element == null || element.length == 0){
            return this;
        }

        element.each(function (){
            if (this.decoration == null){
                return;
            }
            
            this.decoration.remove();
            this.decoration = null;
        });

        return this;
    };
    $.fn.error = function (error, action, selector, templateName) {
        var element = this.clearDecoration(selector);
        if (selector != null && selector.length > 0){
            element = element.find(selector);
        }
        if (element == null || element.length == 0){
            return this;
        }

        var template = null;

        templateName = templateName || "Error";

        if (templateName in templateController){
            template = templateController[templateName];
        }
        else{
            template = templateController["Error"];
        }

        if (template == null){
            return this;
        }

        var message = "UnknownError";

        if (error != null){
            message = error.toString();
        }
        if (message in resourceController){
            message = resourceController[message];
        }
        
        template = template.replace(/\{0\}/g, message);
        
        action = action || "after";
        
        var decoration = $(template);

        element.each(function (){
            this.decoration = decoration.clone();
            if (action in element){
                $(this)[action](this.decoration);
            }
            else{
                $(this).after(this.decoration);
            }
        });

        var thisElement = this;

        setTimeout(function () {thisElement.clearDecoration(selector);}, 5000);

        return this;
    };
    $.fn.preloader = function (action, selector, templateName) {
        var element = this.clearDecoration(selector);
        if (selector != null && selector.length > 0){
            element = element.find(selector);
        }
        if (element == null || element.length == 0){
            return this;
        }
        
        var template = null;

        templateName = templateName || "Preloader";

        if (templateName in templateController){
            template = templateController[templateName];
        }
        else{
            template = templateController["Preloader"];
        }

        if (template == null){
            return this;
        }
        
        var decoration = $(template);

        element.each(function (){
            this.decoration = decoration.clone();
            if (action in element){
                $(this)[action](this.decoration);
            }
            else{
                $(this).after(this.decoration);
            }
        });
        
        return this;
    };
    $.fn.heightToWindow = function () {
        var current = window.heightToWindowElements;
        if (current == null) {
            current = new Array();

            window.heightToWindowElements = current;
            window.heightToWindowFunction = function(element) {
                $(element).css({ maxHeight: $(window).height() - $(element).offset().top - 5 });
            };

            $(window).resize(function () {
                setTimeout(function () {
                    var count = current.length;
                    for (var index = 0; index < count; index++) {
                        window.heightToWindowFunction(current[index]);
                    }
                }, 0);
            });
        }

        var count = this.length;
        for (var index = 0; index < count; index++) {
            var item = this[index];
            current.push(item);
            window.heightToWindowFunction(item);
        }

        return this;
    };
})(jQuery);
