var Glopart = {};

Glopart.SingleMessage = function(to) {
    Glopart.BuildMessage("/messages/singlemessage", {to: to})
};
Glopart.BuildMessage = function(url, data) {
    jQuery.ajax({
        url: url,
        data: data,
        dataType: "html",
        success: function(form) {
            jQuery.facebox(form);
            jQuery("#sendMessageForm").validationEngine();
            jQuery("#singleMsgSendBtn").click(function() {
                if (jQuery("#sendMessageForm").validationEngine({returnIsValid:true,scroll:false})) {
                    var msg = {};
                    var arr = jQuery("#sendMessageForm").serializeArray();
                    for (var i = 0; i < arr.length; i ++) {
                        msg[arr[i].name] = arr[i].value;
                    }
                    DwrCommon.sendSingleMessage(msg);
                    jQuery.facebox.close();
                }
            });
            jQuery("#singleMsgCancelBtn").click(function() {
                jQuery.facebox.close();
            });
        }
    });
};

jQuery(document).ready(function() {

    jQuery(document).ajaxStart(function() {
        jQuery.LoadingWindow.show();
    });
    jQuery(document).ajaxStop(function() {
        jQuery.LoadingWindow.hide();
    });

    dwr.engine.setPreHook(function() {
        jQuery.LoadingWindow.show();
    });
    dwr.engine.setPostHook(function() {
        jQuery.LoadingWindow.hide();
    });

    jQuery.ajaxSetup({
        error: function() {
            jQuery(this).empty().html("Произошел сбой, перегрузите страницу");
        }
    });

    processBubbleHints();

});

jQuery.LoadingWindow = function($) {
    this.wnd = null;
    $.extend(this, {
        show: function(text) {
            if ($.LoadingWindow.wnd == null) {
                $.LoadingWindow.wnd = $("<div id=\"loading-window\"><span></span></div>");
                $("body").append($.LoadingWindow.wnd);
            }
            if (text === undefined || text == "") {
                text = "Загрузка...";
            }
            $.LoadingWindow.wnd.children().html(text);
            $.LoadingWindow.wnd.css({
                opacity: 0,
                visibility: "visible",
                left: ($(window).width() - $.LoadingWindow.wnd.width()) / 2,
                top: ($(window).height() - $.LoadingWindow.wnd.height()) / 2
            });
            $.LoadingWindow.wnd.animate({
                opacity: 0.6
            }, 200);
        },

        hide: function() {
            $.LoadingWindow.wnd.animate({
                opacity: 0
            }, 300, function() {
                $(this).css("visibility", "hidden");
            });
        }
    });
    return this;
}.call({}, jQuery);


(function($) {

    jQuery.fn.bubbleHint = function(text) {

        return $(this).CreateBubblePopup({
            innerHtml: text,
            position: "top",
            align: "center",
            themeName: "all-black",
            themePath: Glopart.BUBBLE_THEME
        });

    };

    jQuery.fn.buildTabControl = function(config) {

        var def = $.extend({
            ajaxable: true,
            fragment: "tabcontent"
        }, config);

        $(this).each(function(idx, el) {
            var j = $(el);
            var selected = j.children("li.selected");
            var a = selected.children();

            var arrow = $('<div class="ui-tabs-arrow"><div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line1"><!-- --></div></div>');

            arrow.css({
                left: (a.outerWidth() - 15) / 2,
                top: a.innerHeight()
            });

            selected.append(arrow);

            if (def.ajaxable) {
                j.find("a").each(function(i, link) {
                    link = $(link);
                    var href = link.attr("href");
                    link.data("text", link.html());
                    link.bind('click', function(e) {
                        e.stopPropagation();
                        $("#" + def.fragment).refreshFragment(href);

                        var a = j.children("li.selected").removeClass("selected").children("a");
                        a.empty().html(a.data("text"));

                        jThis = $(this);
                        jThis.parent().addClass("selected").append(arrow);
                        arrow.css({
                            left: (jThis.outerWidth() - 15) / 2,
                            top: jThis.innerHeight()
                        });
                        jThis.empty().html(jThis.data("text"));

                        return false;
                    });
                    link.attr("href", "#");
                });
            }

        });
        return true;
    };

})(jQuery);

function gotoHref(href) {
    window.location = href;
}

function processBubbleHints() {
    jQuery(".bubble-hint").each(function(idx, el) {
        var title = jQuery(el).attr("title");
        if (title != null && title != "") {
            jQuery(el).bubbleHint(title);
        }
    });
}
