(function($) {

    $.fn.NewsSlider = function() {

        var obj = "";
        var panelWidth = 0;
        var sliderMaskWidth = 0;
        var totalPanels = 0;
        var currentPanel = 1;

        return this.each(function() {
            obj = '#' + this.id;

            //look for all panels in our slider div (classes that have panel)
            //Changed class*=panel --> class*=newsSlider
            var $panels = $(obj + ' .slider [class*=newsSlider] li');

            //width of the first panel (make sure all panels are the SAME width);
            panelWidth = 158;

            totalPanels = $panels.length;

            // calculate a new width for the slider (so it holds all panels) and set it
            $(obj + ' .slider').css('width', panelWidth * totalPanels);

            sliderMaskWidth = $(obj + ' .sliderMask').css('width');
            sliderMaskWidth = rtrim(sliderMaskWidth, "px");

            CheckArrowBtns();

            // bind the buttons
            $(obj + ' .leftSliderActiveBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href
                Slide(this, 'up');
            });

            $(obj + ' .rightSliderActiveBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href
                Slide(this, 'down');
            });

            // bind the homePageButtons
            $(obj + ' .leftSliderActiveHomeBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href
                //IE has 2 extra pixels, this accounts for that deviation
                var IE = (navigator.userAgent.indexOf("MSIE") >= 0) ? true : false;
                if (IE) {
                    panelWidth = 161;
                }
                Slide(this, 'up');
            });

            $(obj + ' .rightSliderActiveHomeBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href

                //IE has 2 extra pixels, this accounts for that deviation
                var IE = (navigator.userAgent.indexOf("MSIE") >= 0) ? true : false;
                if (IE) {
                    panelWidth = 161;
                }
                Slide(this, 'down');
            });

        });

        // Slide now
        function Slide(e, direction) {

            // hide btns until done slidding (so they can't jam on the btns)
            $(obj + ' .rightSliderActiveBtn').hide();
            $(obj + ' .leftSliderActiveBtn').hide();
            $(obj + ' .rightSliderActiveHomeBtn').hide();
            $(obj + ' .leftSliderActiveHomeBtn').hide();

            if (direction == "up") {
                $(obj + ' .slider').animate({ 'top': '-=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
                currentPanel++;
            }
            else {
                $(obj + ' .slider').animate({ 'top': '+=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
                currentPanel--;
            }
        }

        function CheckArrowBtns() {

            var sliderLeftPosition = $(obj + ' .slider').css('top');
            //trim the px so we are left with an int
            sliderLeftPosition = rtrim(sliderLeftPosition, "px");

            //make sliderLeftPosition positive
            var sliderLeftPositionPositive = sliderLeftPosition - (sliderLeftPosition * 2)

            var mod = sliderMaskWidth % panelWidth;

            //alert(currentPanel + " of " + totalPanels + " - " + sliderMaskWidth + " - " + panelWidth + " - " + mod + " - " + sliderLeftPosition);

            //some browsers use auto
            //if (sliderLeftPosition != 0 && sliderLeftPosition != "auto") {
            if (currentPanel != 1) {
                $(obj + ' .rightSliderActiveBtn').show();
                $(obj + ' .rightSliderActiveHomeBtn').show();
            }
            else {
                $(obj + ' .rightSliderActiveBtn').hide();
                $(obj + ' .rightSliderActiveHomeBtn').hide();
            }

            //if (sliderLeftPositionPositive <= sliderMaskWidth) {
            if (currentPanel < totalPanels) {
                $(obj + ' .leftSliderActiveBtn').show();
                $(obj + ' .leftSliderActiveHomeBtn').show();
            }

        }

        // trim function
        function rtrim(str, chars) {
            chars = chars || '\\s';
            return str.replace(new RegExp('[' + chars + ']+$', 'g'), '');
        }

    };

})(jQuery);