﻿
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);

(function ($) {
    if(!$.validator) { return; }
    $.validator.unobtrusive.parseDynamicContent = function (selector) {
        //use the normal unobstrusive.parse method
        $.validator.unobtrusive.parse(selector);

        //get the relevant form
        var form = $(selector).first().closest('form');

        //get the collections of unobstrusive validators, and jquery validators
        //and compare the two
        var unobtrusiveValidation = form.data('unobtrusiveValidation');
        var validator = form.validate();

        $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
            if (validator.settings.rules[elname] == undefined) {
                var args = {};
                $.extend(args, elrules);
                args.messages = unobtrusiveValidation.options.messages[elname];
                $('[name=' + elname + ']').rules("add", args);
            } else {
                $.each(elrules, function (rulename, data) {
                    if (validator.settings.rules[elname][rulename] == undefined) {
                        var args = {};
                        args[rulename] = data;
                        args.messages = unobtrusiveValidation.options.messages[elname][rulename];
                        $('[name=' + elname + ']').rules("add", args);
                    }
                });
            }
        });
    }
})($);

(function($) {
  $.fn.outerHtml = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
  }
})(jQuery);

(function ($) {
    $.fn.lightBoxEx = function (themeUrl) {
        $(this).lightBox({
            imageLoading: themeUrl + 'lightbox-ico-loading.gif', 	
            imageBtnPrev: themeUrl + 'lightbox-btn-prev.gif', 	
            imageBtnNext: themeUrl + 'lightbox-btn-next.gif', 	
            imageBtnClose: themeUrl + 'button-stang-active.png', 	
            imageBlank: themeUrl + 'lightbox-blank.gif'
        });
    }
})(jQuery);

(function ($) {
    $.fn.sortableEx = function (url, getdata) {
        if(!getdata) { return; }

        var fixHelper = function (e, ui) {
            ui.children().each(function () {
                $(this).width($(this).width());
            });
            return ui;
        };
        $(this).sortable({ helper: fixHelper,
            distance: 30,
            update: function (event, ui) {
                if (ui.item) {
                    ui.item.preloader('after');
                }
                var data = JSON.stringify(getdata());

                $.ajax({
                    url: url,
                    type: 'POST',
                    dataType: 'json',
                    data: data,
                    contentType: "application/json; charset=utf-8",
                    success: function (result, status, request) { 
                        if (ui.item) {
                            ui.item.clearDecoration();
                        }
                    },
                    error: function (request, status, error) {
                        if (ui.item) {
                            ui.item.clearDecoration();
                        }
                    }
                });
            }
        }).disableSelection();
    }
})(jQuery);

