﻿(function($) {
    $.fn.ssSlider = function(options) {

        // Set plugin default options
        var defaults = {
            prefix: '',
            speed: 500,
            pages: '-pg-page-links',
            nextPrev: '-product-list',
            prevBtn: '-prev_button',
            nextBtn: '-next_button',
            pageLink: '-page_link_',
            viewMoreBtn: '-btn_viewmore',
            sliderWrap: '-slider-wrap',
            itemsHeight: '165',
            autoOpen: 'false'
        };


        // Merge user options with defaults
        var options = $.extend(defaults, options);
        var $this = $(this);

        var pages = "#" + options.prefix + options.pages;
        var nextPrev = "#" + options.prefix + options.nextPrev;

        $this.currentPage = 1;
        $this.rowsHidden = true;
        $this.currentPos = 0;
        $this.items = $this.children();
        $this.pageCount = $this.items.length;
        var width = 672;


        $this.init = function() {

            // Cache items

            $this.wrap('<div class="' + options.prefix + options.sliderWrap + ' slider-wrap" />');

            $this.css({
                'position': 'absolute',
                'padding': 0
            });

            $this.items.addClass(options.prefix + '-pg-row');

            $("." + options.prefix + "-pg-row").css({
                'width': width,
                'height': options.itemsHeight + 'px',
                'float': 'left'
            });

            $("." + options.prefix + options.sliderWrap).css({
                'position': 'relative',
                'width': width,
                'overflow': 'hidden',
                'height': options.itemsHeight + 'px'
            });

            $this.buildNavigation();
            if (options.autoOpen == 'true') {
                $this.viewAll();
            }
        };

        $this.buildNavigation = function() {
            if ($this.pageCount > 1) {


                jQuery('<a/>', {
                    href: '#',
                    id: options.prefix + options.prevBtn,
                    text: "Previous",
                    click: function(e) {
                        $this.gotoPage($this.currentPage - 1);
                        e.preventDefault();
                    }
                }).appendTo(nextPrev);

                $("#" + options.prefix + options.prevBtn).css({
                    'background': 'url("/images/pag-btn-prev.png") no-repeat',
                    'height': '81px',
                    'width': '19px',
                    'position': 'absolute',
                    'margin-top': '-135px',
                    'margin-left': '-1px',
                    'text-indent': '-9000px'
                });

                //                $("#" + options.prefix + options.prevBtn + ":hover").css({
                //                    'background-position':'bottom'
                //                });

                jQuery('<a/>', {
                    href: '#',
                    id: options.prefix + options.nextBtn,
                    text: 'Next',
                    click: function(e) {
                        $this.gotoPage($this.currentPage + 1);
                        e.preventDefault();
                    }
                }).appendTo(nextPrev);

                $("#" + options.prefix + options.nextBtn).css({
                    'background': 'url("/images/pag-btn-next.png") no-repeat',
                    'height': '81px',
                    'width': '19px',
                    'position': 'absolute',
                    'margin-left': width + 7 + 'px',
                    'margin-top': '-135px',
                    'text-indent': '-9000px'
                });

                for (var pg = 1; pg <= $this.pageCount; pg++) {
                    jQuery('<a/>', {
                        rel: pg,
                        href: '#',
                        id: options.prefix + options.pageLink + pg,
                        title: 'Page ' + pg,
                        text: pg,
                        click: function(e) {
                            var thisPage = parseInt($(this).attr('rel'));
                            $this.gotoPage(thisPage);
                            e.preventDefault();
                            $this.setCurrentPage(thisPage);
                        }
                    }).appendTo(pages);
                }

                jQuery('<a/>', {
                    id: options.prefix + options.viewMoreBtn,
                    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(pages);


            }
        };

        $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) {
            $("#" + options.prefix + options.pageLink + $this.currentPage).removeClass("current");
            $("#" + options.prefix + options.pageLink + page).addClass("current");
            $this.currentPage = page;
        };





        $this.viewAll = function() {
            $("#" + options.prefix + options.viewMoreBtn).text("View Less");
            $("#" + options.prefix + options.prevBtn + ", #" + options.prefix + options.nextBtn + "," + pages).hide();

            $this.currentPos = $this.css('left');

            var height = ([options.itemsHeight] * $this.pageCount) + (15 * this.pageCount);

            $this.css({
                'left': '0'
            });

            $("." + options.prefix + options.sliderWrap).css({
                'position': 'relative',
                'width': width,
                'overflow': 'hidden',
                'height': height + 'px'
            });
        }

        $this.viewLess = function() {
            $("#" + options.prefix + options.viewMoreBtn).text("View All");
            $("#" + options.prefix + options.prevBtn + ", #" + options.prefix + options.nextBtn + "," + pages).show();

            $("." + options.prefix + options.sliderWrap).css({
                'position': 'relative',
                'width': width,
                'overflow': 'hidden',
                'height': options.itemsHeight + 'px'
            });

            $this.gotoPage($this.currentPage);

        }

        $this.init();

        return this;
    }
})(jQuery);
