﻿(function ($) {
    $.fn.openModalWindow = function (callack) {
        var $url = $(this).attr("href");
        var $title = $(this).attr("title");
        var $dialog = $("<div />");
        var target = $(this).attr("target");
        $dialog.load($url, function () {
            $.validator.unobtrusive.parse($dialog);
            $dialog.dialog('option', 'position', 'center');
        }).dialog({
            autoOpen: false,
            modal: true,
            closeOnEscape: false,
            resizable: false,
            draggable: false,
            title: $title,
            position: "top",
            width: "auto",
            show: "fade",
            close: function () {
                $dialog.empty();
                $dialog.remove();
            }
        });
        $dialog.dialog("option", "buttons", {
            "Fortryd": function () {
                $(this).dialog("close");
                $(this).remove();
            },
            "OK": function () {
                var $target = $('#' + target);
                $target.validate();
                if (!$target.valid()) {
                    window.scroll(0, 0);
                    return false;
                }
                var dlg = $(this);

                var $progressindicator = $("<div />");
                $progressindicator.css("text-align", "center");
                $progressindicator.html('<br/><br/><img src="/frontend/images/progress-indicator.gif" alt="" />')
                    .dialog({ modal: true, title: "Behandler data", width: "auto", resizable: false, draggable: false });

                $.ajax({
                    url: $target.attr("action"),
                    type: 'POST',
                    data: $target.serialize(),
                    success: function (response, status, xhr) {
                        if (xhr.status == 206) {
                            $dialog.html(xhr.responseText);
                            $progressindicator.dialog('close');
                            $progressindicator.remove();
                            window.scroll(0, 0);
                        } else {
                            $progressindicator.dialog('close');
                            $progressindicator.remove();
                            dlg.dialog('close');
                            dlg.remove();
                            if (callack) {
                                callack($target, response);
                            }
                        }
                    },
                    error: function (xhr, status, error) {
                        if (status == "error" && xhr.status == 400) {
                            $dialog.html(xhr.responseText);
                            $progressindicator.dialog('close');
                            $progressindicator.remove();
                            window.scroll(0, 0);
                        } else {
                            $progressindicator.append('<br/>Der opstod en fejl ved behandling af dataen,<br/> kontakt venligt <a href="mailto:webmaster@kulturnatten.dk">webmaster@kulturnatten.dk</a>');
                        }
                    }
                });
            }
        });
        $dialog.dialog('open');
        return false;
    };

    $(function () {
        $(".btn_submit").click(function () {
            $(this).parent('form').submit();
            return false;
        });

        $("a[rel=dialog]").live('click', function (e) {
            $(this).openModalWindow(function (form, response) {
                window.location.reload()
            });
            return false;
        });

        $("a[rel=delete_dialog]").live('click', function (e) {
            e.preventDefault();
            var $btn = $(this);
            var $msg = $(this).attr("title");

            var $deleteDialog = $('<div/>');
            $deleteDialog.append($msg).dialog({
                resizable: false,
                draggable: false,
                height: "auto",
                width: "auto",
                title: $msg,
                modal: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                        if ($btn.attr("href") != "#") {
                            $.ajax({
                                url: $btn.attr('href'),
                                success: function (response) {
                                    $btn.parents(".listItem").remove();
                                }
                            });
                        } else {
                            $btn.parents(".listItem").remove();
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });
            return false;
        });

        $("a[rel=confirm_dialog]").click(function (e) {
            e.preventDefault();
            var $btn = $(this);
            var $msg = $(this).attr("title");

            var $deleteDialog = $('<div/>');
            $deleteDialog.append($msg).dialog({
                resizable: false,
                draggable: false,
                height: "auto",
                width: "auto",
                title: $msg,
                modal: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                        if ($btn.attr("href") != "#") {
                            $.ajax({
                                url: $btn.attr('href'),
                                success: function (response) {
                                    window.location.reload();
                                }
                            });
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });
            return false;
        });
    });
} (jQuery));

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
