function Form() {
	this.id = 'szukaj';
	
	this.submit = function() {
		var objForm = $(this.id);
		if (objForm) {
			objForm.submit();
		}
	}
}

function AutoCompleter() {
	
	var inputNazwa = $('nazwa');
	var selectPosition = -1;
	var lastInputValue = '';
	var lastRequestTime = -1;
	
	this.init = function() {
		
		inputNazwa.set('autocomplete', 'off');
		inputNazwa.addEvents({
			'keydown' : function(event) {
				onKeyDown(event);
			},
			'keyup' : function(event) {
				onKeyUp(event);
			},
			'blur' : function() {
				onBlur();
			}
		});
		
		$('szukaj').addEvent('submit', function(event){
			return goToSelectedTeam(event);
		});
		
		
		function onKeyDown(event) {
			if (event.key == 'enter') {
				return goToSelectedTeam(event);
			}
		}
		
		function goToSelectedTeam(event) {
			var oldUl = $('nameSearchUl');
			if (null == oldUl) {
				return true;
			}
			if (oldUl != null && selectPosition != -1) {
				event.preventDefault;
				var children = oldUl.getChildren();
				if (selectPosition < children.length) {
					var child = children[selectPosition];
					var a = child.getChildren();
					a = a[0];
					document.location.href = a.get('href');
					return false;
				}
			}	
			return true;
		}
		
		function onKeyUp(event) {
			
			var oldUl = $('nameSearchUl');
			if (null == oldUl) {
				return;
			}
			
			if (event.key == 'up' && oldUl != null) {
				upArrowAction();
			} else if (event.key == 'down' && oldUl != null) {
				downArrowAction();
			} else if (event.key != 'enter') {
				defaultKeyAction();
			}
			
			function defaultKeyAction() {
				selectPosition = -1;
				updateSelection();
			}
			
			function upArrowAction() {
				
				var children = oldUl.getChildren();
				if (selectPosition == -1 || children.length < selectPosition) {
					if (children.length > 0) {
						selectPosition = 0;
					}
				}
				if (selectPosition > 0) {
					selectPosition--;
				}
				updateSelection();
			}
			
			function downArrowAction() {
				
				var children = oldUl.getChildren();
				if (selectPosition < children.length-1) {
					selectPosition++;
				}
				updateSelection();
			}
		}
		
		function onBlur() {
			
			ul = $('nameSearchUl');
			if (ul != null) {
				setTimeout("ul.dispose()",200);
			}
		}
	}
	
	this.update = function() {
		
		if (inputNazwa.value == lastInputValue) {
			return;
		}
		
		lastInputValue = inputNazwa.value;
		selectPosition = -1;
		
		if ('' == lastInputValue) {
			var ul = $('nameSearchUl');
			if (null != ul) {
				ul.empty();
				ul.dispose();
			}
			return;
		}
		
		var request = new Request.JSON({
			url : 'default.php?c=request&a=searchTeams&request=json&pattern=' + lastInputValue,
			onComplete : function(result) {
				var oldUl = $('nameSearchUl');
				var ulElements = new Array;
				$each(result, function(element, index) {
					var a = new Element('a');
					a.set('href',element.url);
					a.set('html',element.name);
					var li = new Element('li');
					li.addEvent('mouseover', function() {
						onMouseOver(index);
					});
					a.inject(li);
					ulElements.push(li);
				});
				if (0 == ulElements.length) {
					if (oldUl != null) {
						oldUl.empty();
						oldUl.dispose();
					}
				} else {
					
					// Tworzenie nowej listy
					var ul = new Element('ul');
					ul.set('id', 'nameSearchUl');
					ulElements.each(function(element) {
						element.inject(ul);
					});
					par = $('szukaj');
					position = $('nazwa').getPosition(par);	
					ul.setStyles({
						left : position.x + 3,
						top : position.y + 24
					});
					if (oldUl != null ) {
						ul.replaces(oldUl);
					} else if (parent != null){
						ul.inject(par);
					}
				}
			}
		}).get();
		
		function onMouseOver(index) {
			selectPosition = index;
			updateSelection();
		}
	}
	
	function updateSelection() {
		
		var children = $('nameSearchUl').getChildren();
		children.each(function(elem,key){
			if (key == selectPosition) {
				elem.addClass('chosen');
				
			} else {
				elem.removeClass('chosen');
			}
		});
	}
}

function initDruzyny () {
	
	// Autosubmit
	var FormZapisy = new Form;
	var arrSubmitFields = new Array('wojewodztwo', 'powiat', 'gmina', 'typSzkoly', 'szkola');
	arrSubmitFields.each(function(element) {
		element = $(element);
		if (element) {
			element.addEvent('change', function() {
				FormZapisy.submit();
			});
		}
	});
	var showWithIgnoreRoi = $('showWithIgnoreRoi');
	if (showWithIgnoreRoi) {
		showWithIgnoreRoi.addEvent('click',function(){
			$('ignoreSio').set('value', 'yes');
			$('szukaj').submit();
		});
	}
	
	// Autocompleter
	SearchAutoCompleter = new AutoCompleter;
	SearchAutoCompleter.init();
	setInterval('SearchAutoCompleter.update()', 1000);
	
}
