﻿jQuery.fn.sliderRubric = function (settings) {
    return this.each(function (i) {
        aRubricSlider.itemWidth = parseInt(jQuery(".rubric_slider").css("width"));
        aRubricSlider.init(settings, this);
    });
};
var aRubricSlider = {
    itemWidth: 0,
    init: function (s, p) {
        itemLength = jQuery(".item", p).length;
        itemContainerWidth = itemLength * aRubricSlider.itemWidth;
        jQuery(".container", p).css("width", itemContainerWidth + "px");
        jQuery(".next", p).css("display", "block");
        jQuery(".prev", p).css("display", "block");
        jQuery("td.bgnext div", p).addClass("news_element1b");
        jQuery("td.bgprev div", p).addClass("news_element1");
        var animating = true;
        jQuery(".next", p).click(function () {
            if (animating) {
                edgeWidth = -(aRubricSlider.itemWidth * (itemLength - 1));
                if (parseInt(jQuery(".container", p).css("left")) > edgeWidth) {
                    animating = false;
                    animateLeft = parseInt(jQuery(".container", p).css("left")) - aRubricSlider.itemWidth;
                    jQuery(".container", p).animate({ left: animateLeft }, "fast", function () { animating = true; });
                    jQuery("td.bgprev div", p).removeClass().addClass("news_element1b");
                    if (animateLeft > edgeWidth) {
                        jQuery("td.bgnext div", p).removeClass().addClass("news_element1b");
                    } else {
                        jQuery("td.bgnext div", p).removeClass().addClass("news_element1");
                    }
                }
            } else {
                animating = true;
            }
            return false;
        });
        jQuery(".prev", p).click(function () {
            if (animating) {
                if (parseInt(jQuery(".container", p).css("left")) < 0) {
                    animating = false;
                    animateLeft = parseInt(jQuery(".container", p).css("left")) + aRubricSlider.itemWidth;
                    jQuery(".container", p).animate({ left: animateLeft }, "fast", function () { animating = true; });
                    jQuery("td.bgnext div", p).removeClass().addClass("news_element1b");
                    if (animateLeft < 0) {
                        jQuery("td.bgprev div", p).removeClass().addClass("news_element1b");
                    } else {
                        jQuery("td.bgprev div", p).removeClass().addClass("news_element1");
                    }
                }
                //animating = false;
            } else {
                animating = true;
            }
            return false;
        });
    }
};

jQuery(function () {
    jQuery(".rubric_slider").sliderRubric();
});
