(function (e) {
    function info(i, j, m) {
        return String(i + "-" + j + " из " + m + " новостей")
    }

    jQuery.fn.fancyNews = function (i) {
        function getFeed() {
                var d = [];
                e.jGFeed(a.feed, function (f) {
                    if (!f) {
                        b.find("#fn-preloader").remove();
                        b.append("<span id='fc-error'>Не удалось загрузить RSS-подписку!</span>");
                        return false
                    }
                    for (var g = 0; g < f.entries.length; g++) {
                        var c = f.entries[g];
                        q[g] = c.title;
                        r[g] = String(c.publishedDate).substr(0, 16);
                        v[g] = c.contentSnippet;
                        z[g] = c.content;
                        B[g] = c.link;
                        d[g] = "<div class='fn-newsPreview' style='width:" + a.width + "px; height:" + s + "px; background:" + a.backgroundColor + ";'><span class='fn-newsPreviewText' ><h3>" + q[g] + "</h3><h4>" + r[g] + "</h4>" + v[g] + "</span></div>"
                    }
                    makeContainer(d)
                }, a.numOfEntries)
        }
        function makeContainer(d) {
            b.find("#fn-preloader").remove();
            for (var f = 0; f < d.length; ++f) b.append(d[f]).find(".fn-newsPreview:last").css({
                top: f * (s + a.previewOffset)
            });

                k = e.makeArray(b.find(".fn-newsPreview"));
                b.append("<div id='fn-newsView' style='width:" + a.width + "px;height:" + b.height() + "px;'><h3 id='fn-newsViewTitle'></h3><div id='fn-newsViewHtml' style='width:" + (a.width - 30) + "px;height:" + (b.height() - 50) + "px;'></div></div>");

                d = b.find("#fn-newsViewHtml");


                b.after("<div id='fn-newsFooterBar' style='width:" + a.width + "px;'><span id='fn-previousButton'></span><span id='fn-nextButton'></span><div id='fn-newsViewClose'></div></div>");
                b.next("#fn-newsFooterBar").hide();
                b.next("#fn-newsFooterBar").find("#fn-newsViewClose").hide().click(function () {
                    hideShowControls();
                    b.next("#fn-newsFooterBar").find("#newsFooterText").text(info(h + 1, h + a.previewsPerPage, k.length));
                    b.find("#fn-newsView").stop().fadeTo(500, 0, function () {
                        e(this).css("visibility", "hidden")
                    })
                });

                    for (f = 0; f < a.previewsPerPage; ++f) e(k[f]).hide().fadeIn(800);
                    setTimeout(function () {
                        b.next("#fn-newsFooterBar").fadeIn(800, makeEvents)
                    }, 200 + a.previewsPerPage * 200)


        }
        function makeEvents() {
            l = false;
            b.find(".fn-newsPreview").hover(function () {
                if (!l) {
                    l = true;
                    e(this).stop().animate({
                        backgroundColor: a.backgroundOverColor
                    }, 800)
                }
            }, function () {
                if (!t) {
                    l = false;
                    e(this).stop().animate({
                        backgroundColor: a.backgroundColor
                    }, 500)
                }
            }).click(function () {

            });
            b.next("#fn-newsFooterBar").find("#fn-previousButton").click(function () {
                h != 0 && !l && vniz()
            });
            b.next("#fn-newsFooterBar").find("#fn-nextButton").click(function () {
                h != k.length - a.previewsPerPage && !l && vverh()
            });
            if (a.slideTime) A = setInterval(F, a.slideTime)
        }
        function vverh() {
            moveItem("-");
            ++h;
            b.next("#fn-newsFooterBar").find("#newsFooterText").text(info(h + 1, h + a.previewsPerPage, k.length));
            e(k[h + a.previewsPerPage - 1]).css("opacity", 0).animate({
                opacity: 1
            }, 300, function () {
                l = t = false
            })
        }
        function vniz() {
            --h;
            moveItem("+");
            b.next("#fn-newsFooterBar").find("#newsFooterText").text(info(h + 1, h + a.previewsPerPage, k.length));
            e(k[h]).css("opacity", 0).animate({
                opacity: 1
            }, function () {
                l = t = false
            })
        }
        function moveItem(d) {
            t = l = true;
            var f = s + 2;
            for (var g = h; g < k.length; ++g) e(k[g]).animate({
                top: d + "=" + f + "px"
            }, 300)
        }
        function hideShowControls() {
            if (b.next().find("#fn-previousButton").is(":hidden")) {
                b.next().find("#fn-newsViewClose").hide();
                b.next().find("#fn-previousButton, #fn-nextButton").show();
                if (a.slideTime) A = setInterval(F, a.slideTime)
            } else {
                b.next().find("#fn-newsViewClose").show();
                b.next().find("#fn-previousButton, #fn-nextButton").hide();
                a.slideTime && clearInterval(A)
            }
        }
        var a = e.extend({}, e.fn.fancyNews.defaults, i),
            b, s, w, A, q = [],
            r = [],
            v = [],
            z = [],
            B = [],
            k = [],
            h = 0,
            l = true,
            t = false,
            x = "next";
        return this.each(function () {

                b = e(this);
                var d = e.makeArray(b.find("div").hide());
                
                b.css({
                    overflow: "hidden",
                    position: "relative",
                    width: a.width,
                    height: a.height + a.previewOffset * a.previewsPerPage
                });
                b.append("<div id='fn-preloader'></div>");
                b.find("#fn-preloader").css({
                    top: a.height * 0.5 - b.find("#fn-preloader").height() * 0.5,
                    left: a.width * 0.5 - b.find("#fn-preloader").width() * 0.5
                });
                s = a.height / a.previewsPerPage;
                
                
                    for (var f = [], g = [], c = 0; c < d.length; ++c) {
                        r[c] = e(d[c]).attr("title");
                        var J = v,
                            K = c,
                            o;
                        var p = e(d[c]).find("span").text();
                        var hr = e(d[c]).find("a").attr("href");
                        var tit = e(d[c]).find("span").attr("title");
                        o = a.maxWords;
                        var H = p.split(" "),
                            I = "";
                        if (H.length > o) {
                            for (p = 0; p < o; ++p) I += H[p] + " ";
                            o = I;
                            J[K] = o + "[....]";
                        } else {o = p;
                            J[K] = o;
                        }
                        
                        z[c] = e(d[c]).find("span").html();

                        f[c] = "<div class='fn-newsPreview' style='width:" + a.width + "px; height:" + s + "px; background:" + a.backgroundColor + ";'><span class='fn-newsPreviewText' ><span class='date'>" + r[c] + "</span><a href='"+hr+"' title='"+tit+"'>" + v[c] + "</a><img class='more' title='Подробнее' src='http://86.57.251.49/radiotpl/img/more.png'></span></div>"
                    }
                     makeContainer(f)
                

        })
    };
    e.fn.fancyNews.defaults = {
        width: 590,
        height: 240,
        previewOffset: 2,
        maxWords: 10,
        previewsPerPage: 10,
        numOfEntries: 10,
        slideTime: 0,
        feed: "",
        backgroundColor: "#ffffff",
        backgroundOverColor: "#eaebec",
        useLinks: false
    }
})(jQuery);
