

//search.do?search.do?objectType=1803401776&attribute=1803401793&condition=2781&dictionaryValue=&textValue=%D0%B4

var listProcessors = {
    alphabet: Array.fromString("АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ"),
    location: window.location.href.toString(),
    buildAlpha: function(ot, attr, dm) {
        listProcessors.alphabet.foreach(function(a) {
            if(a!="Ж" && a!="О")
                appendAlpha(a, "search.do?objectType=" + ot + "&attribute=" + attr + "&condition=2781&dictionaryValue=&textValue=" + encodeURI(a) + (dm ? "displayMode=" + dm : ""));
        });
    },
    buildArticleAlpha: function(dm) {
        listProcessors.buildAlpha("1803401776", "1803401793", dm)
    },
    _processImage: function(img) {
        var maxWidth = 130
        var maxHeight = 90
        function setImgSize() {
            if(img.width>maxWidth || img.height>maxHeight) {
                if(img.width/img.height > maxWidth/maxHeight)
                    $dom.add(img, $dom.style("width", maxWidth.toString() + "px"), $dom.style("height", "auto"))
                else
                    $dom.add(img, $dom.style("height", maxHeight.toString() + "px"), $dom.style("width", "auto"))
            }
            img.onload = ""
            img = null
        }
        if(img.complete)
            $afterAll(setImgSize)
        else
            img.onload = function() { $afterAll(setImgSize); }    
        return img
    },
    PERSONS: function(parent, nodes) {
        var gr_num = 0;
        var gr_table = new Object();
        var alpha_table = {}
        nodes.filter($jsxt.pName("a")).group(function(el) {
            var t = $dom.textValue(el).substring(0, 1)
            if(gr_table[t]==undefined) {
                gr_table[t] = gr_num
                alpha_table[gr_num] = t
                gr_num++;
            }
            return gr_table[t]
        }).foreach(function(i, el) {
            var anchor  = ($dom.is.ie6 || $dom.is.ie7) ? document.createElement('<a name="person_' + i +'"></a>') : $dom.element("a", $dom.attr("name", "person_" + i))
            $dom.add(anchor, $dom.style("display", "block"), $dom.style("width", "0"), $dom.style("height", "0"), $dom.style("overflow", "hidden"))
            $dom.add(parent,
                [anchor,
                 $dom.element("table",
                    $dom.attr("class", "person_list"),
                    $dom.attr("cellspacing", "5"),
                    $dom.attr("cellpadding", "0"),
                    $dom.element("tbody",
                        el.group(function(i, e) { return Math.floor(i/3); }).map(function(e) {
                            return $dom.element("tr", e.map(function(a) {
                                return $dom.element("td",
                                        $dom.style("width", "33%"),
                                        $dom.event("click", function() { window.location.href = a.href; }),
                                        $dom.attr("class", "person_list_item"),
                                        $dom.event("mouseover", function() { $dom.add(this, $dom.attr("class", "person_list_item_hover")); }),
                                        $dom.event("mouseout", function() { $dom.removeClass(this,"person_list_item_hover"); }), $dom.element("a", $dom.attr("href", a.href), $dom.text($dom.textValue(a))));
                            }))
                        }))), $dom.element("div", $dom.attr("class", "person_divider"))])
        }, 1);
        listProcessors.buildAlpha("1803401770", "1803401859", null);
    },
    DOCGROUPS: function(parent, nodes) {
        $dom.add(parent, $dom.element("table",
            $dom.attr("class", "street_list"),
            $dom.element("tbody",
                nodes.filter($jsxt.pName("a")).group(function(i, v){ return Math.floor(i/3); }).map(function(sl) {
                    while(sl.length<3)
                        sl.push(null)
                    return $dom.element("tr", sl.map(function(e) {
                        return $dom.element("td",
                            $dom.attr("class", "street_list_item"),
                            $dom.event("click", function() { window.location.href=e.href; }),
                            $dom.event("mouseover", function() { $dom.add(this, $dom.attr("class", "street_list_item_hover")); }),
                            $dom.event("mouseout", function() { $dom.removeClass(this,"street_list_item_hover"); }), $dom.element("a", $dom.attr("href", e.href), $dom.text($dom.textValue(e))))
                    }))
                }))))
        listProcessors.buildAlpha("1803806958", "1803807124", null);
    },
    ARTICLES: function(parent, nodes) {
        function articleNode(el) {
            var ch = $dom.children(el)
            var imgages = ch.filter($jsxt.pName("img"))
            var link =ch.filter($jsxt.pName("a")).pop()
            var lines = ch.filter($jsxt.pName("div"))
            function getNode(img) {
                var image = (img ? $dom.element("td",
                    $dom.attr("class", "img_container"),
                        listProcessors._processImage($dom.element("img",
                            $dom.attr("alt", img.alt),
                            $dom.attr("src", img.src)))) : [])
                var ln_id = $uid();
                var ln = $dom.element("a",
                            $dom.attr("onclick", "return false;"),
                            $dom.attr("id", ln_id),
                            $dom.attr("rel", "rr"),
                            /displayMode=IMAGES/.test(listProcessors.location) ? $dom.attr("title", img.alt) : [],
                            $dom.text($dom.textValue(link)))
                ln.href = /displayMode=IMAGES/.test(listProcessors.location) ? img.src + "&original=1" : (link ? link.href : "#")
                return $dom.element("div",
                    $dom.event("mouseover", function() { $dom.add(this, $dom.attr("class", "article_list_item_hover")); }),
                    $dom.event("mouseout", function() { $dom.removeClass(this,"article_list_item_hover"); }),
                    /displayMode=IMAGES/.test(listProcessors.location)
                        ? [$dom.attr("onclick", "return false;"), $dom.event("click", function() { jsiBoxOpen(ln); })]
                        : $dom.event("click", function() { window.location.href = ln.href; }),
                    $dom.attr("class", "article_list_item"),
                    $dom.element("table",
                        $dom.element("tbody", $dom.element("tr", image,
                            $dom.element("td",
                                $dom.attr("class", "desc"), ln,
                                    /displayMode=IMAGES/.test(listProcessors.location)
                                        ? [$dom.element("div", $dom.text(img.alt)),
                                           $dom.element("a", $dom.attr("id", ln_id + "_article"), $dom.style("display", "none"), $dom.attr("href", link.href), $dom.text($dom.textValue(link)))]
                                        : [],
                                    lines.map(function(l) {
                                        return $dom.element("div", $dom.text($dom.textValue(l)))
                                    }))))))
            }
            return (imgages.length  || /displayMode=IMAGES/.test(listProcessors.location)) ? imgages.map(getNode) : getNode(false)
        }
        var b = $dom.element("tbody")
        var artBuff = []
        function appendElement(el) {
            if(el instanceof Array)
                el.foreach(appendElement)
            else {
                artBuff.push(el)
                if(artBuff.length==2) {
                    $dom.add(b, $dom.element("tr", artBuff.map(function(a) { return $dom.element("td", a); })))
                    artBuff = []
                }
            }
        }
        $dom.add(parent, $dom.element("table", $dom.style("vertical-align", "top"), $dom.attr("cellspacing", 0), $dom.attr("cellpadding", 0), b))
        nodes.filter($jsxt.pName("div")).sort(function(a, b) {
            var an = parseInt(a.className)
            var bn = parseInt(b.className)
            $log("cmp " + an + " " + bn)
            return an>bn ? 1 : -1;
        }).timerMap(function(el) {
            appendElement(articleNode(el));
        }, 30).onfinish.add(function() { if(artBuff.length) appendElement($dom.element("div")); })
        listProcessors.buildArticleAlpha(false);
    },
    IMAGES: function(parent, nodes) {
        function imageNode(el) {
            var text = el.alt;
            el.alt = "";
            return $dom.element("div",
                $dom.event("mouseover", function() { $dom.add(this, $dom.attr("class", "article_list_item_hover")); }),
                $dom.event("mouseout", function() { $dom.removeClass(this,"article_list_item_hover"); }),
                $dom.event("click", function() {  }),
                $dom.attr("class", "article_list_item"),
                $dom.element("table",
                    $dom.element("tbody", $dom.element("tr",
                        $dom.element("td",
                            $dom.attr("class", "img_container"),
                            $dom.element("div",
                                $dom.element("img",
                                    $dom.event("load", function() { listProcessors._processImage(this); }),
                                    $dom.attr("src", el.src)))),
                        $dom.element("td", $dom.attr("class", "desc"), $dom.element("div", $dom.attr("class", "img_desc"), $dom.text(text)))))))
        }
        var b = $dom.element("tbody")
        $dom.add(parent, $dom.element("table", $dom.attr("cellspacing", 0), $dom.attr("cellpadding", 0), b))
        nodes.filter($jsxt.pName("img")).group(function(i, v) {
            return Math.floor(i/2);
        }).foreach(function(el) {
            $dom.add(b, $dom.element("tr",
                el.map(function(e) { return $dom.element("td", $dom.style("width", "50%"),  imageNode(e)); }),
                el.length==2 ? [] : $dom.element("td", $dom.style("width", "50%"))))
        }, 30)
        listProcessors.buildArticleAlpha("IMAGES");
    }
}

function createTabs() {
    var a = $dom.children("tab_list").filter($jsxt.pName("a"))
    if(a.length==0)
        $dom.add("tab_list", $dom.style("display", "none"))
    else {
        $dom.add("tab_list",
            $dom.element("table",
                $dom.style("border-collapse", "collapse"),
                $dom.element("tbody",
                    $dom.element("tr",
                        a.map(function(i, v) {
                            function getNode() {
                                var t = $dom.textValue(v)
                                $dom.remove($dom.children(v))
                                return $dom.add(v, $dom.element("span", $dom.text(t)));
                            }
                            return $dom.element("td", $dom.style("padding", "0"), $dom.attr("class", "tab_" + i), getNode(v))
                        }),
                        (a.length!=4) ? $dom.element("td", $dom.attr("class", "empty"), $dom.attr("id", "empty_tab")) : []))))
    }
}

function buildAlphabet() {
    $dom.add("alphabet",
        listProcessors.alphabet.map(function(i, v) {
            return $dom.element("a",
                $dom.attr("title", v),
                $dom.attr("class", "alpha"),
                $dom.style("left", (20*i).toString() + "px"),
                $dom.style("background-position", "-" + (20*i).toString() + "px 0"),
                (!alphaRef[v]) ? [$dom.attr("href", "#"), $dom.style("cursor", "default")] : [
                    $dom.attr("href", alphaRef[v]),
                    $dom.event("mouseover", function() { $dom.add(this, $dom.attr("class", "hover")); }),
                    $dom.event("mouseout", function() { $dom.removeClass(this,"hover"); })]);
        }))
}

var alphaRef = new Object()

function appendAlpha(alpha, href) {
    alphaRef[alpha] = href
}

function appendDownloadLink(obj_id) {
    $log("download_link")
    $dom.add("empty_tab", $dom.element("a", $dom.attr("target", "_blank"), $dom.attr("href", "doc/" + obj_id + ".pdf"), $dom.attr("id", "download"), $dom.text("Экскурсия по улице")))
}

function activateLink() {
    var ref = window.location.toString().split("/").last()
    Array.fromObject(document.getElementsByTagName("a")).foreach(function(nd) {
        if(nd.href && nd.href.substring(nd.href.length-ref.length, nd.href.length)===ref) {
            $dom.add(nd, $dom.attr("class", "active_link"))
        }
    })
}
