
	// check for prototype and TechDivision libraries
	if(typeof SmartList == 'undefined' || typeof TechDivision.Autocompleter == 'undefined') {
		throw("td.library.smartlist.js requires including prototypes' smartlists.js library");
	}

	/**
	 * This class extends the original SmartList provided by
	 * prototype adding a method to render a certain page. 
	 *
	 * The original SmartList always renders page 1 and paging
	 * is only done by binding events not manually.
	 *
	 * @author Tim Wagner <t.wagnert@techdivision.com>
	 * @see SmartList
	 */
	TechDivision.SmartList = Class.create(SmartList, {

        /**
         * This method loads the page with the passed page
         * number and is a copy of the changePage() event
         * from the parent class without event handling.
         *
         * @param int page The page number to render
         * @return void
         * @see SmartList::changePage(event, page)
         */
         loadPage: function(page) {

             var options     = this.options;
             var currentPage = this.currentPage;

             if (page == currentPage)
               return;

             if (page == "next")
               page = currentPage + 1;
             else if (page == "previous")
               page = currentPage - 1;

             // find out which items are to be displayed.
             var selectedFlagItems = this._getItemsByFlag();

             // update the pagination(s)
             this._updatePaginationGroup("pagination", selectedFlagItems, page);

             if (this.hasSecondPagination)
               this._updatePaginationGroup("pagination2", selectedFlagItems, page);

             if (this.hasNumResults)
               $(options.baseName + "-num-results").innerHTML = selectedFlagItems.length;

             var firstItemToHide = (currentPage - 1) * options.numItemsPerPage;
             var maxLastItem     = firstItemToHide + options.numItemsPerPage;
             var lastItemToHide  = (maxLastItem > selectedFlagItems.length) ? selectedFlagItems.length : maxLastItem;

             var firstItemToShow = (page - 1) * options.numItemsPerPage;
             var maxLastItem     = firstItemToShow + options.numItemsPerPage;
             var lastItemToShow  = (maxLastItem > selectedFlagItems.length) ? selectedFlagItems.length : maxLastItem;

             for (var i=0; i<selectedFlagItems.length; i++)
             {
               if (i >= firstItemToHide && i < lastItemToHide)
               {
                 switch (options.pageChangeEffect)
                 {
                   case "Blind":
                     Effect.BlindUp(selectedFlagItems[i], { duration: options.pageChangeDuration });
                     break;
                   case "FadeAppear":
                     Effect.Fade(selectedFlagItems[i], { duration: options.pageChangeDuration });
                     break;
                   case "ShrinkGrow":
                     Effect.Shrink(selectedFlagItems[i], { duration: options.pageChangeDuration });
                     break;
                   default:
                     $(selectedFlagItems[i]).hide();
                     break;
                 }
               }
               if (i >= firstItemToShow && i < lastItemToShow)
               {
                 switch (options.pageChangeEffect)
                 {
                   case "Blind":
                     Effect.BlindDown(selectedFlagItems[i], { delay: options.pageChangeDuration, duration: options.pageChangeDuration });
                     break;
                   case "FadeAppear":
                     Effect.Appear(selectedFlagItems[i], { delay: options.pageChangeDuration, duration: options.pageChangeDuration });
                     break;
                   case "ShrinkGrow":
                     Effect.Grow(selectedFlagItems[i], { delay: options.pageChangeDuration, duration: options.pageChangeDuration });
                     break;
                   default:
                     $(selectedFlagItems[i]).show();
                     break;
                 }
               }
             }

         	this.currentPage = page;
         }
	});
	
