﻿(function($) {
    $.fn.paginate = function(listSelector, ipp) {
		return this.each(function() {
			if($(this).data("paginated") == null)
			{
				$(this).data("paginated", {
					totalResults: $(this).find(listSelector + ">li").length,
					masterWidth: $(this).find(listSelector + ">li:first").outerWidth(true),
					totalPages: Math.ceil($(this).find(listSelector + ">li").length / ipp),
					currentIdx: 0 });
				
				var _this = this;
				
				this.ulBuild = function () {
					var ulWidth = $(this).data("paginated").masterWidth + 'px';
					$(this).find(listSelector).append($(this).find(listSelector + ':not(:first)>li'));
					$(this).find(listSelector + ':gt(0)').remove();

					for (var u = 1; u < $(this).data("paginated").totalPages; u++) {						
						$(this).append('<ul class="' + listSelector.substr(listSelector.indexOf(".")+1) + '"></ul>');
					}
					$(this).find(listSelector).css({ width: ulWidth });

					if ($(this).data("paginated").totalPages > 1) {
						for (var l = 0; l < $(this).data("paginated").totalResults; l++) {
							var page = Math.floor(l / ipp) + 1;
							$($(this).find(listSelector)[page]).append($(this).find(listSelector + ':first>li')[ipp]);
						}
					}
				};
				
				this.changePage = function(newPageIdx, doScroll) {
					$(this).data("paginated").currentIdx = newPageIdx;
					// determine height of ul based on number of items shown
					var offset = -(($(this).data("paginated").currentIdx) * $(this).data("paginated").masterWidth);

					$(this).find(listSelector).addClass("hidden");
					for (var p = $(this).data("paginated").currentIdx; p < $(this).data("paginated").currentIdx + 1; p++) {
						if ($($(this).find(listSelector)[p]).length > 0) {
							$($(this).find(listSelector)[p]).removeClass("hidden");
						}
					}

					$(this).find(listSelector).not(".hidden").animate({ left: offset, opacity: 1 }, 500, function () {
						if ($.browser.msie)
							this.style.removeAttribute("filter");
					});
					$(this).find(listSelector).filter(".hidden").animate({ left: offset, opacity: 0 }, 500);
					
					if(doScroll)
						$('html, body').animate({ scrollTop: $(this).offset().top-20 }, 500);

					if ($(this).data("paginated").totalPages > 0)
						$(this).css({ width: ($(this).data("paginated").totalPages * $(this).data("paginated").masterWidth) });
					else
						$(this).css({ width: $(this).data("paginated").masterWidth });
			
					$(this).css({overflow:"hidden", height:$($(this).find(listSelector)[$(this).data("paginated").currentIdx]).outerHeight(true)});
					$(this).parent().css({width: $(this).data("paginated").masterWidth, overflow: "hidden"});
					this.updatePages();
				};

				// controls pagination
				this.updatePages = function () {
					if($(this).data("paginated").totalPages > 1)
					{
						$(this).next('div.pageButtons').remove();
						$(this).after('<div class="pageButtons"></div>');
						if($(this).data("paginated").currentIdx != 0)
							$(this).next('div.pageButtons').append('<input type="submit" class="btn" rel="' + ($(this).data("paginated").currentIdx - 1) + '" value="Previous" />');
							
						for (var u = 0; u < $(this).data("paginated").totalPages; u++) {
							if($(this).data("paginated").currentIdx == u)
							{
								$(this).next('div.pageButtons').append('<input type="submit" class="btn on" disabled="disabled" value="' + (u + 1) + '" />');
							} else {
								$(this).next('div.pageButtons').append('<input type="submit" class="btn" rel="' + u + '" value="' + (u + 1) + '" />');
							}						
						}
						if($(this).data("paginated").currentIdx + 1 != $(this).data("paginated").totalPages)
							$(this).next('div.pageButtons').append('<input type="submit" class="btn" rel="' + ($(this).data("paginated").currentIdx + 1) + '" value="Next" />');

						$(this).next('div.pageButtons').find("input:enabled").click(function(e) { _this.changePage(parseInt($(this).attr("rel")), true); });
					}
				};

				_this.ulBuild();
				$(this).find(listSelector).css({margin: "0px", position: "relative", float: "left"});
				$(this).css("position", "relative").parent().css("position", "relative");
				_this.changePage(0, false);
			}
		});
    };
})(jQuery);

$(document).ready(function() { $("div.gce-page-list").paginate("ul.gce-list", 5); });


