// do check_form
  var daysValue = false;
  var priceValue = false;
  var hotelValue = false;

// JavaScript Document

    
    var timeoutId = null;
    
    var mouseX;
    var mouseY;
    
    var daysStart = 0;
    var daysMax  = 0;
    var daysInterval = 20;
    var daysNames = new Array("dowolna", "do 3", "do 7", "do 14", "do 21", "powyżej 21");
    var daysValues = new Array("", "3", "7", "14", "21", "100");
    
    var priceStart = 0;
    var priceMax  = 0;
    var priceInterval = 20;
    var priceNames = new Array("dowolna", "do 1000", "do 2000", "do 3000", "do 4000", "powyżej 4000");
    var priceValues = new Array("", "0,1000", "1000,2000", "2000,3000", "3000,4000", "4000,50000");
    
    var hotelStart = 0;
    var hotelMax  = 0;
    var hotelInterval = 20;
    var hotelNames = new Array("dowolna", "*", "**", "***", "****", "*****");
    var hotelValues = new Array("", "1", "2", "3", "4", "5");
    
    
    function calcStartPosition(arrVal, value) {
      
      arrLength = arrVal.length;
      for (i=0; i<arrLength; i++) {
        if (arrVal[i] == value) index = i;
      }
      
      return index;
    }
    
    function setStartPosition(type, value) {
      if (type == 'days') {
        index = calcStartPosition(daysValues, value);
        setDaysValue(index);
        moveToX = index * daysInterval;
        new Effect.Move('dragDays', { x: moveToX, y: 0, mode: 'absolute'  });
      } else if (type == 'price') {
        index = calcStartPosition(priceValues, value);
        setPriceValue(index);
        moveToX = index * priceInterval;
        new Effect.Move('dragPrice', { x: moveToX, y: 0, mode: 'absolute'  });
      } else if (type == 'hotel') {
        index = calcStartPosition(hotelValues, value);
        moveToX = index * priceInterval;
        setHotelValue(index);
        new Effect.Move('dragHotel', { x: moveToX, y: 0, mode: 'absolute'  });
      }
    }
    
    function onLoadStartPosition() {
      
      if (daysValue == false) setStartPosition('days', '');
      else setStartPosition('days', daysValue);
      if (priceValue == false) setStartPosition('price', '');
      else setStartPosition('price', priceValue);
      if (hotelValue == false) setStartPosition('hotel', '');
      else setStartPosition('hotel', hotelValue);
    }
    
    //absolutna pozycja myszki
    function getMouseCords(e){
				mouseX = Event.pointerX(e);
				mouseY = Event.pointerY(e);
		}
			
		
    function calcDays() {
      clearTimeoutResult();
      containerPos = $('dragDaysContainer').cumulativeOffset();
      mouseDayX = mouseX - containerPos[0];
      index = Math.round(mouseDayX / daysInterval);
      moveToX = index * daysInterval;
      setDaysValue(index);
      
      new Effect.Move('dragDays', { x: moveToX, y: 0, mode: 'absolute'  });
      timeoutId = setTimeout("checkResult()", 500); 
    }
    
    
    function calcPrice() {
      clearTimeoutResult();
      containerPos = $('dragPriceContainer').cumulativeOffset();
      mousePriceX = mouseX - containerPos[0];
      index = Math.round(mousePriceX / daysInterval);
      moveToX = index * priceInterval;
      setPriceValue(index);
      new Effect.Move('dragPrice', { x: moveToX, y: 0, mode: 'absolute'  });
      timeoutId = setTimeout("checkResult()", 500); 
    }
    
    
    function calcHotel() {
      clearTimeoutResult();
      containerPos = $('dragHotelContainer').cumulativeOffset();
      mouseHotelX = mouseX - containerPos[0];
      index = Math.round(mouseHotelX / daysInterval);
      moveToX = index * priceInterval;
      setHotelValue(index);
      new Effect.Move('dragHotel', { x: moveToX, y: 0, mode: 'absolute'  });
      timeoutId = setTimeout("checkResult()", 500); 
    }
    
    
    function checkResult() {
      
      var pars = $('main_finder').serialize();
      
      new Ajax.Request('index.rst', {
        method: 'post',
	      parameters: pars,
        onComplete: function(response) {
          $('finderResult').innerHTML = response.responseText;
        }
      });

    }

    function checkResultPart(part, find_sess) {
      
      new Ajax.Request('wyszukiwanie,'+part+','+find_sess+',show_html_find_trip.html', {
        method: 'get',
        onComplete: function(response) {
          $('finderResult').innerHTML = response.responseText;
        }
      });

    }
    
    
    function clearTimeoutResult() {
      clearTimeout ( timeoutId );
    }
    
    function checkForm()
    {
      timeoutId = setTimeout("checkResult()", 500); 
    }

    
    function checkDays() {
      
      x = $('dragDays').positionedOffset();
      x_container = $('dragDaysContainer').positionedOffset();
      position = x[0] - x_container[0];
      
      if (position > daysMax) {
        moveDaysToMax();
        setDaysValue(daysMax / daysInterval);
      } else if (position < daysStart) {
        moveDaysToMin();
        setDaysValue(0);
      } else {
        setDaysValue(position / daysInterval);
        
      }
      timeoutId = setTimeout("checkResult()", 500); 
      
    }
    
    function checkPrice() {
      
      x = $('dragPrice').positionedOffset();
      x_container = $('dragPriceContainer').positionedOffset();
      position = x[0] - x_container[0];
      
      if (position > priceMax) {
        movePriceToMax();
        setPriceValue(priceMax / priceInterval);
      } else if (position < priceStart) {
        movePriceToMin();
        setPriceValue(0);
      } else {
        setPriceValue(position / priceInterval);
      }
      timeoutId = setTimeout("checkResult()", 500); 
    }
    
    function checkHotel() {
    
      x = $('dragHotel').positionedOffset();
      x_container = $('dragHotelContainer').positionedOffset();
      position = x[0] - x_container[0];
      
      if (position > hotelMax) {
        moveHotelToMax();
        setHotelValue(hotelMax / hotelInterval);
      } else if (position < hotelStart) {
        moveHotelToMin();
        setHotelValue(0);
      } else {
        setHotelValue(position / hotelInterval);
      }
      timeoutId = setTimeout("checkResult()", 500); 
    }
    
    function setFirstPosition() {
      x = $('dragDaysContainer').positionedOffset();
      daysStart =  0;
      daysMax = daysStart + $('dragDaysContainer').getWidth() - 10; 
      
      x = $('dragPriceContainer').positionedOffset();
      priceStart =  0;
      priceMax = priceStart + $('dragPriceContainer').getWidth() - 10;
      
      x = $('dragHotelContainer').positionedOffset();
      hotelStart =  0;
      hotelMax = hotelStart + $('dragHotelContainer').getWidth() - 10; 
       
    }
    function moveDaysToMax() {
      new Effect.Move('dragDays', { x: daysMax, y: 0, mode: 'absolute'  });
    }
    function moveDaysToMin() {
      new Effect.Move('dragDays', { x: daysStart, y: 0, mode: 'absolute'  });
    }
    function movePriceToMax() {
      new Effect.Move('dragPrice', { x: priceMax, y: 0, mode: 'absolute'  });
    }
    function movePriceToMin() {
      new Effect.Move('dragPrice', { x: priceStart, y: 0, mode: 'absolute'  });
    }
    function moveHotelToMax() {
      new Effect.Move('dragHotel', { x: hotelMax, y: 0, mode: 'absolute'  });
    }
    function moveHotelToMin() {
      new Effect.Move('dragHotel', { x: hotelStart, y: 0, mode: 'absolute'  });
    }
    
    function setDaysValue(index) {
      $('daysView').value = daysNames[index];
      $('NoLength').value = daysValues[index];
    }
    function setPriceValue(index) {
      $('priceView').value = priceNames[index];
      $('price_select').value = priceValues[index];
    }
    function setHotelValue(index) {
      $('hotelView').value = hotelNames[index];
      $('NoHotelClass').value = hotelValues[index];
    }
    
    Event.observe(document, 'mousemove', getMouseCords); 
    
    window.onload = function () {
      new Draggable('dragDays', { constraint: 'horizontal', handle: 'dragDays', snap: [daysInterval, 20], onEnd: checkDays, onStart: clearTimeoutResult });
      new Draggable('dragPrice', { constraint: 'horizontal', handle: 'dragPrice', snap: [priceInterval, 20], onEnd: checkPrice, onStart: clearTimeoutResult });
      new Draggable('dragHotel', { constraint: 'horizontal', handle: 'dragHotel', snap: [hotelInterval, 20], onEnd: checkHotel, onStart: clearTimeoutResult });
      setFirstPosition();
      onLoadStartPosition();
    } 
