(function($) {
    $.fn.mfSlider = function(options) {

        // Set plugin default options
        var defaults = {
            speed: 500,
            pages: '#pages',
            nextPrev: '#product-list'
        };

        // Merge user options with defaults
        var options = $.extend(defaults, options);

        var $this = $(this);

        $this.currentPage = 1;
        $this.rowsHidden = true;
        $this.currentPos = 0;
        $this.items = $this.children();
        var height = $this.items.height();

        $this.init = function() {

            // Cache items

            $this.pageCount = $this.items.length;


            $this.wrap('<div class="slider-wrap" />');

            $this.css({
                'width': '99999px',
                'position': 'absolute',
                'padding': 0
            });

            $this.items.addClass('pg-row-float');

            $(".slider-wrap").css({
                'position': 'relative',
                'width': $this.items.width(),
                'overflow': 'hidden',
                'height': $this.items.height() + 10 + 'px'
            });
            $this.buildNavigation();
        };

        $this.buildNavigation = function() {
            if ($this.pageCount > 1) {
                jQuery('<a/>', {
                    href: '#',
                    id: 'prev_button',
                    text: "Previous",
                    click: function(e) {
                        $this.gotoPage($this.currentPage - 1);
                        e.preventDefault();
                    }
                }).appendTo(options.nextPrev);

                jQuery('<a/>', {
                    href: '#',
                    id: 'next_button',
                    text: 'Next',
                    click: function(e) {
                        $this.gotoPage($this.currentPage + 1);
                        e.preventDefault();
                    }
                }).appendTo(options.nextPrev);

                for (var pg = 1; pg <= $this.pageCount; pg++) {
                    jQuery('<a/>', {
                        rel: pg,
                        href: '#',
                        id: 'page_link_' + pg,
                        title: 'Page ' + pg,
                        text: pg,
                        click: function(e) {
                            var thisPage = parseInt($(this).attr('rel'));
                            $this.gotoPage(thisPage);
                            e.preventDefault();
                        }
                    }).appendTo(options.pages);
                }

                jQuery('<a/>', {
                    id: 'btn_viewmore',
                    text: 'View All',
                    href: '#',
                    click: function(e) {
                        if ($this.rowsHidden == true) {
                            $this.viewAll();
                            $this.rowsHidden = false;
                        } else if ($this.rowsHidden == false) {
                            $this.viewLess();
                            $this.rowsHidden = true;
                        }

                        e.preventDefault();
                    }
                }).insertAfter(options.pages);

                $this.setCurrentPage(1);
            }
        };

        $this.gotoPage = function(page) {
            if (page == 0) {
                page = $this.pageCount;
            }

            if (page == ($this.pageCount + 1)) {
                page = 1;
            }

            $this.setCurrentPage(page);

            page -= 1;

            $this.animate({ 'left': '-' + ($this.parent().width() * page) }, options.speed, 'easeOutExpo');
        };

        $this.setCurrentPage = function(page) {
            $("#page_link_" + $this.currentPage).removeClass("current");
            $("#page_link_" + page).addClass("current");
            $this.currentPage = page;
        };

        $this.viewAll = function() {
            $("#btn_viewmore").text("View Less");
            $("#prev_button, #next_button," + options.pages).hide();

            $this.currentPos = $this.css('left');

            var height = ($this.pageCount * ($this.items.height() + 10)) + 10;
            $this.css({
                'left': '0'
            });

            $(".slider-wrap").css({
                'position': 'relative',
                'width': $this.items.width(),
                'overflow': 'hidden',
                'height': height + 'px'
            });
            $this.items.removeClass('pg-row-float');
            //alert(height);

        }

        $this.viewLess = function() {
            $("#btn_viewmore").text("View More");
            $("#prev_button, #next_button," + options.pages).show();
            $this.css({ 'left': $this.currentPos });

            $(".slider-wrap").css({
                'position': 'relative',
                'width': '670px',
                'overflow': 'hidden',
                'height': $this.items.height() + 10 + 'px'
            });
            $this.items.addClass('pg-row-float');



        }

        $this.init();

        return this;
    }
})(jQuery);
