(function($){
    $.fn.extend({ 
        //plugin name - animatemenu
        paginate: function(options) {
 
            //Settings list and the default values
            var defaults = {
                perPanel:10,
				html:'<div class="panel"><ul class="normal"></ul></div>',
				links:'<p class="inline-links"><a class="p-link"></a></p>'
            };
             
            var options = $.extend(defaults, options);
         
            return this.each(function() {
                var o =options;
              
                //Assign current element to variable
                var self = $(this);
				var origin = $("#original",self); 
                var html = ""; 
				var links = ""; 
                //Get all LI in the obj
                var items = $("li", self);
				var isRemainder = (items.length%o.perPanel != 0);
                var divisions = parseInt(items.length/o.perPanel);
				isRemainder ? divisions = divisions + 1 : divisions = divisions;
               	for(i=0; i < divisions; i++){
				  html += o.html;
				  links += o.links;
				} 
				links += '<div class="clear"></div>';
				self.prepend(html);
				self.prepend(links);
				var panels = $(".panel", self);
				var links = $(".p-link", self);
				var total = (links.length) - 1;
				var start = 0;
				panels.each(function(index){
					//console.log(index);
					var ul = $("ul",this);
					for(i=1; i <= o.perPanel; i++){
					  if (items[start]){
					  ul.append(items[start]);
					  //console.log("moved " + items[start]);
					  start++;
					  }
					}
					var ref = "p-" + index;
					$(this).prepend('<a name="p-' + index + '">').attr('ref',ref);
					if(index!=0){
						$(this).hide();	
					}
				});
				origin.remove();
				links.each(function(index){
					var href = "#p-" + index;
					var through = "";
					index == total ? through = items.length : through = ((index + 1)*o.perPanel);
					$(this).text((((index + 1)*o.perPanel)-(o.perPanel - 1)) + " through " + through);					
					$(this).attr('href',href);
				});
				//console.log(items[1]);
                links.click(function(evt){
					evt.preventDefault();
					var panel = $(this).attr('href');
					panel = panel.substring(1);
					panels.each(function(index){
						if(($(this).attr('ref')) == panel){
							$(this).show();
						} else {
							$(this).hide();	
						}
					});
					//console.log(panel);
				});
            });
        }
    });
})(jQuery);



