(function($) {

    $.fn.PerfectSlider = function() {

        var obj = "";
        var panelWidth = 0;
        var sliderMaskWidth = 0;
        var sliderWidth = 0;

        return this.each(function() {
            obj = '#' + this.id;

            //hide nav arrows by default, CheckArrowBtns() will determine if they should display
            $(obj + ' .leftSliderActiveBtn').hide();
            $(obj + ' .rightSliderActiveBtn').hide();

            //look for all panels in our slider div (classes that have panel)
            var $panels = $(obj + ' .slider div[class*=panel]');

            //width of the first panel (make sure all panels are the SAME width);
            panelWidth = $panels[0].offsetWidth;
            sliderWidth = panelWidth * $panels.length;

            // calculate a new width for the slider (so it holds all panels) and set it
            $(obj + ' .slider').css('width', panelWidth * $panels.length);

            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, 'left');
            });

            $(obj + ' .rightSliderActiveBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href
                Slide(this, 'right');
            });

        });

        // 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();

            if (direction == "left") {
                $(obj + ' .slider').animate({ 'left': '+=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
            }
            else {
                $(obj + ' .slider').animate({ 'left': '-=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
            }
        }

        function CheckArrowBtns() {

            var sliderLeftPosition = $(obj + ' .slider').css('left');
            // trim the px so we are left with an int
            sliderLeftPosition = rtrim(sliderLeftPosition, "px");
            // make sliderLeftPosition positive
            if (sliderLeftPosition == "auto")
                sliderLeftPosition = 0;
            var sliderLeftPositionPositive = sliderLeftPosition - (sliderLeftPosition * 2)
            //some browsers use auto
            if (sliderLeftPosition != 0 && sliderLeftPosition != "auto") {
                $(obj + ' .leftSliderActiveBtn').show();
            }
            else {
                $(obj + ' .leftSliderActiveBtn').hide();
            }

            var multiPanel = Math.round(sliderMaskWidth / panelWidth);
            var testVal = (sliderLeftPositionPositive * 1) + (panelWidth * multiPanel);


            if (testVal < sliderWidth) {
                $(obj + ' .rightSliderActiveBtn').show();
            }

            if (sliderWidth == panelWidth) {
                $(obj + ' .rightSliderActiveBtn').hide();
            }

        }

        // trim function
        function rtrim(str, chars) {
            chars = chars || '\\s';
            return str.replace(new RegExp('[' + chars + ']+$', 'g'), '');
        }

    };

})(jQuery);

//Testimonial Slider
(function($) {

    $.fn.TestimonialSlider = function() {

        var obj = "";
        var panelWidth = 0;
        var sliderMaskWidth = 0;
        var sliderWidth = 0;

        return this.each(function() {
            obj = '#' + this.id;

            //look for all panels in our slider div (classes that have panel)
            var $panels = $(obj + ' .slider div[class*=testimonialPanel]');

            //width of the first panel (make sure all panels are the SAME width);
            panelWidth = $panels[0].offsetWidth;
            sliderWidth = panelWidth * $panels.length;

            // calculate a new width for the slider (so it holds all panels) and set it
            $(obj + ' .slider').css('width', panelWidth * $panels.length);

            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, 'left');
            });

            $(obj + ' .rightSliderActiveBtn').bind('click', function(event) {
                event.preventDefault(); //stop the link from going to href
                Slide(this, 'right');
            });

        });

        // 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();

            if (direction == "left") {
                $(obj + ' .slider').animate({ 'left': '+=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
            }
            else {
                $(obj + ' .slider').animate({ 'left': '-=' + panelWidth + 'px' }, 'normal', CheckArrowBtns);
            }
        }

        function CheckArrowBtns() {

            var sliderLeftPosition = $(obj + ' .slider').css('left');
            // trim the px so we are left with an int
            sliderLeftPosition = rtrim(sliderLeftPosition, "px");
            // make sliderLeftPosition positive
            var sliderLeftPositionPositive = sliderLeftPosition - (sliderLeftPosition * 2)
            //some browsers use auto
            if (sliderLeftPosition != 0 && sliderLeftPosition != "auto") {
                $(obj + ' .leftSliderActiveBtn').show();
            }
            else {
                $(obj + ' .leftSliderActiveBtn').hide();
            }

            var multiPanel = Math.round(sliderMaskWidth / panelWidth);
            var testVal = (sliderLeftPositionPositive * 1) + (panelWidth * multiPanel);

            if (testVal < sliderWidth) {
                $(obj + ' .rightSliderActiveBtn').show();
            }

            if (sliderWidth == panelWidth) {
                $(obj + ' .rightSliderActiveBtn').hide();
            }

        }

        // trim function
        function rtrim(str, chars) {
            chars = chars || '\\s';
            return str.replace(new RegExp('[' + chars + ']+$', 'g'), '');
        }

    };

})(jQuery);
