(function($){

    $.fn.extend({ 
        //maybe a function called help to display the usage of the plugin
     

        variablePaginate: function(options) {
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                paginateElements: $(this).find(".paginate-element"),
                paginateContent: $(this).find(".paginate-content"),
                paginateHandler: ".paginate-element",
                paginateWrapper: null,
                limiter: ($(this).find(".paginate-element").length)/2, //lets say by default we make two pages
                nextButton : $(this).find(".paginate-next-btn"),
                prevButton : $(this).find(".paginate-prev-btn"),
                viewAllButton : $(this).find(".paginate-viewall-btn"),
                pager : $(this).find(".paginate-page-btn") //sample style/html for the numbers
            }
                
            var options =  $.extend(defaults, options);

            var current_page = 0;
            return this.each(function() { //$(this) is the container, the container contains both content and the buttons, we need a separate container for the content because we need to separate the buttons
                var o = options;
                var wrapper = "";
                var content = o.paginateContent;
                if(o.paginateWrapper == null){
                    wrapper = "<div class='paginate-wrapper'></div>"
                }
                else{
                    wrapper = o.paginateWrapper;
                }
                content.wrap(wrapper); //wrap something around it so that all pages are contained in this one element
                wrapper = content.parent();//the element we just inserted is the wrapper
                //console.log(wrapper);
                var mother_page = o.paginateContent.clone();
                var empty_mother = o.paginateContent.clone().empty()[0];
                //wrapper.empty();
                var all_pages = [];
                if(typeof o.paginateElements == "undefined"){
                    o.paginateElements = o.paginateContent.find(o.paginateHandler);
                }
                //console.log(o.paginateElements);
                var max_num_pages = Math.ceil(o.paginateElements.length/o.limiter);
                //console.log(max_num_pages);
                var mother_locator = 0;
                o.pager.attr("rel",0);
                var mother_btn = o.pager.clone();
                for(var i = 0; i< max_num_pages; i++){
                    all_pages[i] = $(empty_mother).clone();
                    //adding elements to pages, row by row
                    for(var j=0; j< o.limiter; j++){
                        //$(o.paginateElements[mother_locator]).appendTo(all_pages[i]);
                        if( typeof o.paginateElements[mother_locator] != "undefined"){
                            $(o.paginateElements[mother_locator]).appendTo(all_pages[i]);
                            mother_locator++;
                        }
                    }
                    all_pages[i].appendTo(wrapper);
                    if(i > 0){
                        all_pages[i].hide();
                        //adding numbers to pages
                        mother_btn.clone().text(i+1).attr("rel",i).insertBefore(o.viewAllButton);
                            //* rt#12532 inserting AFTER the pager button exhibits odd
                            //* behavior when its > 3 pages
                    }
                }
                o.pager.addClass("paginate-active"); //initializing first pager, may replace with triggerign click on the first button
                //wrapper.empty();
                $(wrapper.children()[0]).remove();
                //console.log(all_pages);
                //setting up events
                $(this).find(".paginate-page-btn").click(function(eventObj){
                    $(this).siblings(".paginate-page-btn, .paginate-viewall-btn").removeClass("paginate-active");
                    $(this).addClass("paginate-active");
                    var target = $(eventObj.currentTarget);

                    for(var i=0; i< all_pages.length; i++){
                        var element = all_pages[i];
                        if($(element).is(":visible")){
                            $(element).fadeOut(function(){
                                $(all_pages[parseInt(target.attr("rel"))]).fadeIn();
                            });
                        }
                    }

                    
                    return false;
                });
                o.nextButton.click(function(){
                    $(all_pages[current_page]).fadeOut(function(){
                        if(current_page+1 == max_num_pages){
                               return false;
                        }
                        current_page++;
                        all_pages[current_page].fadeIn();
                        return false;
                    });
                        return false;
                });
                o.prevButton.click(function(){
                    $(all_pages[current_page]).fadeOut(function(){
                        if(current_page == 0){
                               return false;
                        }
                        current_page--;
                        all_pages[current_page].fadeIn();
                        return false;
                    });
                        return false;
                });
                
                o.viewAllButton.click(function(){
                    $(this).siblings(".paginate-page-btn, .paginate-viewall-btn").removeClass("paginate-active");
                    $(this).addClass("paginate-active");
                    $(all_pages[current_page]).fadeOut(function(){
                        for(var i=0; i< all_pages.length; i++){
                            var element = all_pages[i];
                            $(element).fadeIn();
                        }
                    });
                    return false;
                });
                
            });
        }
    });
    
})(jQuery);

