var ItemList = Class.create(
    {
      fillItems: function()
      {
		this.addToList();
		if(parseInt($('content').getStyle('height')) < (this.winHeight + 100)){
			this.timeoutID = window.setTimeout('list.fillItems()',500);
		}
	  },

	  startAjax: function()
	  {
		Ajax.Responders.register({
		  // when an Ajax request is started, show the indicator
		  onCreate: function() {
			if (Ajax.activeRequestCount > 0)
			  Element.show($("indicator"));
		  },
		  // when an Ajax request is finished, hide the indicator
		  onComplete: function() {
			if (Ajax.activeRequestCount == 0)
			  Element.hide($("indicator"));
		  }
		});
	  },
	
	  // add item to cart
	  addToList: function()
	  {
		if(Ajax.activeRequestCount>0){
			var el = $(document.documentElement);
			tmp = el.scrollTop;
			el.scrollTop = tmp-25; // We always need some space
			return; 
		}
		  
		LandCode		= $('LandCode').value;
		ObjectType		= $('ObjectType').value;
		StartDate		= $('StartDate').value;
		PeriodLenght	= $('PeriodLenght').value;
		CampingSoort	= $('CampingSoort').value;
		Departement 	= $('Departement').value;
		MaxPersons		= $('MaxPersons').value;
		
		if (StartDate=='' || PeriodLenght=='')
			return false;
	
		this.startAjax();
		new Ajax.Request(document.SearchAndBook.action, {
			method: 'post',
			parameters: "StartDate=" + StartDate + "&PeriodLenght=" + PeriodLenght + "&LandCode=" + LandCode + "&ObjectType=" + ObjectType + "&CampingSoort=" + CampingSoort + "&Departement=" + Departement + "&MaxPersons=" + MaxPersons,
			onSuccess: function(resp) {
				html = resp.responseText;
				if(html!=''){
					$('resultTable').insert( html, {position:'bottom'});
				} else {
					Element.hide($("more"));
					window.clearTimeout(list.timeoutID);
				}
			}
		});

		this.items[this.item+1] = {"html":""};
		return false;
	  },

      onScroll: function()
      {
        var previousScroll = this.scroll['top'];
        this.scroll = document.viewport.getScrollOffsets();
        if(this.scroll['top'] > previousScroll)
        {
//        if((this.scroll['top'] / this.viewport['height']) > 0.60)
		  var el = $(document.documentElement); // element
		  var h = el.scrollHeight; // height of scroll
		  var y = el.scrollTop; // vertical scroll offset from top (of scrollHeight)
		  var c = el.clientHeight; // scroll bar height
		  var scrollBottom = h - (y + c); // offset of scroll from bottom
	
		  if(scrollBottom<10)
          {
            if(this.item < (this.items.length))
            {
              this.addToList();
              this.reset();
            }
            else
            {
              this.stopObserving();
            }
          }
        }
      },
 
      observe: function()
      {
        Event.observe(window, 'scroll', this.onScroll.bind(this));
        Event.observe(window, 'resize', this.reset.bind(this));
      },
 
      reset: function()
      {
        this.scroll = document.viewport.getScrollOffsets();
        this.viewport = $('content').getDimensions();
      },
 
      stopObserving: function()
      {
        Event.stopObserving(window, 'scroll', this.onScroll.bind(this));
        Event.stopObserving(window, 'resize', this.reset.bind(this));
		Element.hide($('more')); 
		window.clearTimeout(this.timeoutID);
      },
 
      initialize: function(items, display)
      {
        this.reset();
        this.item = 0;
		this.winHeight = (document.all ? document.documentElement.clientHeight : window.innerHeight);
	    this.increment = 3;
        this.items = items;
        this.display = display;
		this.fillItems();
        this.observe();
		this.scrollbind = this.onScroll.bind(this); //this.onScroll.bindAsEventListener(this, true, true);
		this.resetbind = this.reset.bind(this); // this.reset.bindAsEventListener(this, true, true);
      }
    }
  );
