if (!LZ) {
    var LZ = {};
};
LZ.User = {
    Profile: {},
    Info: {},
    Func: {}
};
LZ.User.MSG_SUCCESS = {
    style: "success",
    text: "<span class='bold'>Форма успешно сохранена</span>",
    close: {
        link: "LZ.User.Profile.display(2)",
        text: "Посмотреть реквизиты"
    },
    allHide: "true"
};
LZ.User.MSG_LOAD = {
    style: "load",
    text: "Загрузка...",
    allHide: "true"
};
LZ.User.MSG_404 = {
    style: "error",
    text: "Страница не найдена",
    close: {
        link: "location.href=/profile/",
        text: "На главную профиля"
    },
    allHide: "true"
};
LZ.User.MSG_EDIT_PROFILE = "<div id='submit'><span>Ожидание...</span><a href='#edit'  onclick='LZ.User.Profile.display(0);return false;'>Редактировать</a></div>";
LZ.User.MSG_SAVE_PROFILE = "<div id='submit'><span>Ожидание...</span><a href='#post'  onclick='LZ.User.Profile.post();return false;'>Сохранить</a></div>";
LZ.User.MSG_ERROR_AUTH = '<span>Пользователь должен быть авторизован !!!</span>';
LZ.User.SEARCH_BLOCK = '<div class="searchBlock mb5"><div class="head"><div><div class="f-bold" id="profileTitle"></div></div></div><div class="border p5"><table id="profileContent"></table></div></div>';
LZ.User.HASH = "";
LZ.User.noCache = new Date();
LZ.User.Func.loginBlock = function(id, form, menu){
    $(id).addClass("hide");
    $.getJSON("/l_doc2.nsf/checkUser.act?_=" + LZ.User.noCache.getTime(), function(data){
        if (data.check == true) {
            $(id).load(menu, function(){
                $("#t_name").html(data.info.name);
                $("#t_units").html("Количество юнитов: <b>" + data.info.units + "</b>");
                $("#t_bonus").html("Бонусные юниты: <b>" + data.info.bonus + "</b>");
            });
            $(id).removeClass("hide");
        }
        else {
            $(id).load(form, function(){
                $(id).removeClass("hide");
            });
        }
    });
};
LZ.User.ScrollingTo = function(hash){
    var $target = $(hash);
    $target = $target.length && $target || $("#" + this.hash.slice(1));
    if ($target.length) {
        var targetOffset = $target.offset().top;
        $('html,body').scrollTop(targetOffset);
        return false;
    }
};
LZ.User.Func.logout = function(){
    $.get("/l_doc2.nsf/logoutE3?redirectTo=" + location.href, function(data){location.reload();});
};
LZ.User.Func.loadForm = function(source){
    $(LZ.User.Profile.div).load(source);
};
LZ.User.Func.submit = function(id, div, msg){
    LZ.User.Profile.alert({
        style: "load",
        text: "Отправка",
        prepend: div
    });
    var action = $(id).attr("action");
    var inputs = $(id + " INPUT");
    $("#redirectTo").val();
    var postData = {};
    $.each(inputs, function(i, item){
        if (item.type == "checkbox" && !item.checked) {
            return;
        }
        else 
            if (item.type == "radio" && !item.checked) {
                return;
            }
        postData[item.name] = item.value;
    });
    $.post(action, postData, function(data){
        if (data.indexOf("#PERMIT#") == -1) {
            LZ.User.Profile.alert({
                style: "error",
                text: data,
                prepend: div
            });
        }
        else {
            if (msg && msg.indexOf("#DATA#") != -1) {
                div[0].innerHTML = data;
            }
            else 
                if (msg) 
                    LZ.User.Profile.alert({
                        style: "success",
                        text: msg,
                        close: {
                            link: "location.reload()",
                            text: "Закрыть"
                        }
                    });
                else 
                    location.reload();
        }
    });
};
LZ.User.Profile.get = function(id){
    if (!LZ.User.Profile.div) 
        LZ.User.Profile.div = $(id);
    LZ.User.Profile.alert(LZ.User.MSG_LOAD);
    $.ajax({
        type: "GET",
        url: "/domreg2.nsf/PersonInfoE3?openagent&" + LZ.User.noCache.getTime(),
        dataType: "json",
        success: function(data){
            LZ.User.Profile.requestData = data;
            if (LZ.User.Profile.requestData.error) 
                LZ.User.Profile.alert({
                    style: "error",
                    text: LZ.User.MSG_ERROR_AUTH
                });
            else 
                LZ.User.Profile.display(2);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            LZ.User.Profile.alert({
                style: "error",
                text: textStatus
            });
        }
    });
};
LZ.User.Profile.display = function(mode){
    data = LZ.User.Profile.requestData;
    $(LZ.User.Profile.div).html(LZ.User.SEARCH_BLOCK);
    var profileContent = $E("#profileContent");
    var profileTitle = $E("#profileTitle");
    profileTitle.innerHTML = "Личная информация";
    var f = "<div id='action' name='" + data.form.name + "' act='" + data.form.action + "'></div>";
    $(profileContent).append(f);
    var inp = "";
    $.each(data.inputs, function(i, item){
        inp = "";
        if (data.inputs[i].type == "text") {
            inp += "<tr>";
            inp += "<td class='descr'>" + data.inputs[i].descr + ":</td>";
        }
        if (mode == 2 || mode == 1) {
            inp += "<td class='" + data.inputs[i].type + "'><div class='rem'>" + data.inputs[i].value + "</div></td>";
        }
        if (mode == 0) {
            inp += "<td class='" + data.inputs[i].type + "'><input class='rem' type='" + data.inputs[i].type + "' disable name='" + data.inputs[i].name + "' value='" + data.inputs[i].value + "' /></td>";
        }
        inp += "</tr>";
        $(profileContent).append(inp);
    });
    if (mode == 0) 
        $(profileContent).append("<tr><td colspan='2'>" + LZ.User.MSG_SAVE_PROFILE + "</td></tr>");
    if (mode != 0) 
        $(profileContent).append("<tr><td colspan='2'>" + LZ.User.MSG_EDIT_PROFILE + "</td></tr>");
    if (mode == 1) 
        LZ.User.Profile.alert(LZ.User.MSG_SUCCESS);
};
LZ.User.Profile.alert = function(p){
    if (!p) {
        return false;
    }
    if (p.style && p.text && !p.action) {
        if (!p.close) 
            var CL = "";
        else 
            if (p.close.link && p.close.text) 
                var CL = "<div class='close' onclick='" + p.close.link + "'>" + p.close.text + "</div>";
        var tc = '<div class="tc"><div><div></div></div></div>';
        var bc = '<div class="bc"><div><div></div></div></div>';
        var content = '<div class="MSG ' + p.style + '">' + tc + '<div class="inner">' + CL + p.text + '</div>' + bc + '</div>';
        if (!p.prepend) 
            p.prepend = LZ.User.Profile.div;
        if (p.allHide == "true") 
            $(p.prepend).children().hide();
        $(".MSG").remove();
        $(p.prepend).prepend(content);
        $(".MSG").show();
    }
    else 
        if (p.action) {
            if (p.action == "close") {
                $(".MSG").remove()
            }
        }
};
LZ.User.Profile.post = function(){
    $("#submit").toggleClass("show");
    LZ.User.Profile.alert({
        style: "load",
        text: "Отправка..."
    });
    var inputs = $(".rem");
    var postData = {};
    $.each(inputs, function(i, item){
        postData[inputs[i].name] = inputs[i].value;
    });
    $.ajax({
        type: "POST",
        url: $("#action").attr("act"),
        data: postData,
        dataType: "json",
        success: function(data){
            LZ.User.Profile.requestData = data;
            if (LZ.User.Profile.requestData.error) 
                LZ.User.Profile.alert({
                    style: "error",
                    text: LZ.User.Profile.requestData.error.msg
                });
            else 
                LZ.User.Profile.display(1);
        }
    });
};
LZ.Tree = function(id){
    if (id) 
        LZ.User.Profile.div = this.div = $(id);
};
LZ.Tree.prototype.Get = function(action, index){
    var obj = this;
    var id = "#infoBlock" + index;
    LZ.User.ScrollingTo("#up");
    LZ.User.Profile.alert(LZ.User.MSG_LOAD);
    $.get(action, function(data){
        obj._Clear(id);
        $(LZ.User.Profile.div).append("<ul id='" + id.slice(1) + "' class='structTree'></ul>");
        $(id).html(data);
        obj._Events(id, index);
        obj._Change(id);
    });
};
LZ.Tree.prototype.List = function(action, index, rid, paging){
    var obj = this;
    if (!rid) 
        rid = index;
    var id = "#infoList" + index + rid;
    var tid = "#infoBlock" + index;
    if ($(id)[0] && paging == 2) {
        obj._Change(id);
        return;
    }
    LZ.User.Profile.alert(LZ.User.MSG_LOAD);
    $.get(action, function(data){
        var back = "";
        obj._Clear(id);
        $(LZ.User.Profile.div).append("<div id='" + id.slice(1) + "' class='structList'></div>");
        if ($(tid)[0]) 
            back = '<a href="#" class="back">Назад</a>';
        $(id).html(back + data);
        $(".pageListE3").click(function(){
            if (action.indexOf("&page") != -1) 
                action = action.substr(0, action.indexOf("&page"));
            action = action + "&page=" + $(this).attr("page");
            LZ.User.Profile.alert(LZ.User.MSG_LOAD);
            obj.List(action, index, rid, 3);
            return false;
        });
        $(id + " .back").click(function(){
            obj._Change(tid);
        });
        obj._Change(id);
    });
};
LZ.Tree.prototype._Clear = function(id){
    if ($("#action")[0]) 
        $(LZ.User.Profile.div).empty();
    if ($(id)[0]) 
        $(id).remove();
};
LZ.Tree.prototype._Change = function(i){
    $(LZ.User.Profile.div).children().hide();
    $(i).show();
};
LZ.Tree.prototype._Unbind = function(id){
    $(id + " .fold").unbind("click");
    $(id + " .link").unbind("click");
};
LZ.Tree.prototype._Events = function(id, index){
    var obj = this;
    obj._Unbind(id);
    $(id + " .fold").attr("src", "/images/transparent.gif");
    $(id + " .fold").click(function(){
        var li = $(this).parent();
        var ul = $(this).next().next().next();
        var img = $(this);
        $(img).toggleClass("open");
        $(ul).slideToggle("fast");
        if ($(ul).attr("active") == "0") 
            return;
        url = $(ul).attr("action");
        $(ul).load(url, function(){
            $(ul).attr("active", 0);
            obj._Events(id, index);
        });
    });
    $(id + " .link").click(function(){
        var url = $(this).attr("action");
        var rid = $(this).parent().attr("id");
        LZ.User.ScrollingTo("#up");
        obj.List(url, index, rid);
    });
    $(id + " .link").mouseover(function(){
        $(this).css("color", "#d20000");
    });
    $(id + " .link").mouseout(function(){
        $(this).removeAttr("style");
    });
};

