﻿var feedback = new function($) {

    var options = {
        container: "#newsfeed_container",
        loadService: "/Feedback/Home",
        saveService: "/Feedback/Save"
    };

    var container = null;
    var content = null;

    function init(o) {
        if (o == undefined || o == null) o = {};
        options = $.extend(options, o);

        container = $(options.container);
        container.find(".x-feedbackopen").click(function(e) {
            open($(this));
        });
    };

    function bindContentEvents() {
        if (!content) return;

        content.find(".close-popup").bind("click.feedback", function() { close() });

        content.find("form").bind("submit.feedback", function(e) {
            e.preventDefault();
            var f = $(this);

            $.post(options.saveService, f.serialize(),
					    function(data, textStatus, o) {
					        var contentType = o.getResponseHeader("Content-Type");

					        // if response type is html expect validation error
					        if (contentType.toLowerCase().indexOf("text/html") > -1) {
					            content.unbind(".feedback");
					            content.html(data);

					            //rebind events
					            bindContentEvents();
					            Viva.Notify.show(Viva.Notify.InformationType.Error, "Povej svoje menenje", "Pri shranjevanju je prišlo do napake.");
					        } else {
					            var item = $.parseJSON(data);
					            close();
					            Viva.Notify.show(Viva.Notify.InformationType.Info, item.title, item.description);
					        }


					    },
					    function(xhr, textStatus, ex) {
					        Viva.Notify.show(Viva.Notify.InformationType.Error, "Povej svoje menenje", "Pri pošiljanju zahtevka je prišlo do napake");
					    }
				    );

            return false;

        });
    };

    function open(o) {
        closeInternal();

        content = $("<div class=\"popup-window\"><div class=\"loading\">&nbsp;</div></div>");
        $("body").append(content);

        content.position({ my: "center center", at: "center center", of: o, collision: "none" });

        $.get(options.loadService, null,
            function(data) {
                content.hide();
                content.html(data);
                bindContentEvents();
                content.fadeIn("normal");
                content.position({ my: "center center", at: "center center", of: o, collision: "none" });
            }
        );
    };

    this.open = function() { open() };
    this.init = function() { init() };

    function close() {
        if (!content) return;
        content.fadeOut("normal", function() { closeInternal(); });
    };

    function closeInternal() {
        if (!content) return;
        content.remove();
        content = null;
    };

    $(window).keyup(function(e) {
        if (e.which != 27) return;
        close();
    });
} (jQuery);