﻿(function($) {
    $.fn.sfSlider = 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: '263',
            autoOpen: 'false',
            slidingWidth: '679',
            width: '700'
        };


        // 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;


        $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');
            if (options.prefix == "CompareBin") {
                $("." + options.prefix + "-pg-row").css({
                    'height': '125px',
                    'float': 'left'
                });

            } else {

                $("." + options.prefix + "-pg-row").css({
                    'height': options.itemsHeight + 'px',
                    'float': 'left'
                });
            }

            $("." + options.prefix + options.sliderWrap).css({
                'position': 'relative',
                'width': options.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, $this.pageCount);
                        e.preventDefault();
                    }
                }).addClass('prevBtn').appendTo(nextPrev);



                //                $("#" + 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, $this.pageCount);
                        e.preventDefault();
                    }
                }).addClass('nextBTN').appendTo(nextPrev);



                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, $this.pageCount);
                            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();
                    }
                }).css({
                    'float': 'right',
                    'width': '80px'
                }).insertBefore(pages);

                $("#" + options.prefix + options.pages).css({ 'padding-right': '1px' });

            }
        };

        $this.gotoPage = function(page, pageCount) {
            //if(options.prefix == options.prefix == "CompareBin"
            if (page == 0) {
                page = pageCount;
                if (page == 0) {
                    
                }
            }

            if (page == (pageCount + 1)) {
                page = 1;
            }

            $this.setCurrentPage(page);

            page -= 1;

            $this.animate({ 'left': '-' + (options.slidingWidth * 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': options.width,
                'overflow': 'hidden',
                'height': height + 'px'
            });

            $("." + options.prefix + "-pg-row").css({
                'padding-top': '3px',
                'padding-bottom': '3px'

            });

            $("#" + options.prefix + options.pages).css({ 'padding-right': '0px' });
        }

        $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': options.width,
                'overflow': 'hidden',
                'height': options.itemsHeight + 'px'
            });

            $("." + options.prefix + "-pg-row").css({
                'padding-top': '0px',
                'padding-bottom': '0px'

            });

            $("#" + options.prefix + options.pages).css({ 'padding-right': '1px' });

            $this.gotoPage($this.currentPage, $this.pageCount);

        }

        $this.init();

        return this;
    }
})(jQuery);
