
function arrowClass(id){
	
	this.arrowId = id;
	this.lockArrow = false;
	this.clickEvent = null;
	
	this.showArrow = function (px){
		if (this.lockArrow == false){
			document.getElementById(this.arrowId).style.backgroundPosition = -px+'px 0px';
		}
	}

	this.show = function (){
		this.lockArrow = true;
		this.clickEvent();
	}
}



var fromArrow = new arrowClass('arrowFromImg');
fromArrow.clickEvent = function (){
	obj = document.getElementById('formFormBlock');
	if (obj.style.display == 'none'){
		obj.style.display = '';
		closeSearch('quickSearchFrom', null);
		closeSearch('quickSearchTo', null);
	} else {
		closeSearch('formFormBlock', fromArrow);
	}
}

var toArrow = new arrowClass('arrowToImg');
toArrow.clickEvent = function (){
	obj = document.getElementById('toFormBlock');
	if (obj.style.display == 'none'){
		obj.style.display = '';
		closeSearch('quickSearchFrom', null);
		closeSearch('quickSearchTo', null);
	} else {
		closeSearch('toFormBlock', toArrow);
	}
}

function closeSearch(id, arrObj){
	obj = document.getElementById(id);
	obj.style.display = 'none';
	if (arrObj != undefined && arrObj != null){
		arrObj.lockArrow = false;
		arrObj.showArrow(0);
	}
}


var countryFrom;
var countryTo;
function setCountry(type, val){
	
	obj = document.getElementById(type+'Display');
	obj.value = val;
	if (type=='from'){
		countryFrom = val;
		document.getElementById('toDisplay').value = 'Wybierz';
	} else {
		
		countryTo = val;
	}
}

var airTo;
var airFrom;
function setAirport(type, val){
	obj = document.getElementById(type+'Display');
	if (type=='from'){
		obj.value = countryFrom +', '+ val;
		airFrom = val;
		document.getElementById('toDisplay').value = 'Wybierz';
		closeSearch('formFormBlock', fromArrow);
	} else {
		obj.value = countryTo +', '+ val;
		airTo = val;
		closeSearch('toFormBlock', toArrow);
	}
	
}


var keyCounterFrom = 0;
var keyCounterTo = 0;

function runSearch (type, time, lang){
	
	if ((type == 'from' && parseInt(time) == parseInt(lastFromCall)) || (type == 'to' && parseInt(time) == parseInt(lastToCall))){
	
		if (type  == 'from') {
			obj = lastFromObj;
		} else {
			obj = lastToObj;
		}
		
		if (obj.value != '' && obj.value.length > 2){
			new Ajax.Request('/'+lang+'/search.html', {
			    method:'post',
			    parameters: 'search='+obj.value+'&lang='+lang,
			    onSuccess: function(transport){
					var data = transport.responseText.evalJSON();
					
					if (type == 'from'){
						block = document.getElementById('quickSearchFrom');
					} else {
						block = document.getElementById('quickSearchTo');
					}
					
					for( var i = 1; block.childNodes[i]; i++ ){
						block.removeChild(block.childNodes[i]);
					}
					
					mDiv = document.createElement('div');
					mDiv.style.paddingTop = '15px';
					if (data.data.size() > 0){
						data.data.each(function(item) {
							div = document.createElement('div');
							div.className = 'item';
							div.style.className = 'item';
							div.ref = item.airportCode;
							div.id = item.countryCode;
							div.style.cursor = 'pointer';
							div.onmouseover = function(){
								this.style.className = 'item selected';
								this.className = 'item selected';
							}
							
							div.onmouseout = function(){
								this.style.className = 'item';
								this.className = 'item';
							}
							
							if (type == 'from'){
								div.onclick = function(){
									document.getElementById('countryFrom').value = this.id;
									fly.getAirports(this.id, 'airportFrom', 1, this.ref, '', '', lang);
									setCountry('from', document.getElementById('countryFrom').options[document.getElementById('countryFrom').selectedIndex].text);
									closeSearch('quickSearchFrom', null);
								}
								
							} else {
								div.onclick = function(){
									document.getElementById('countryTo').value = this.id;
									fly.getAirports(this.id, 'airportTo', 1, this.ref, '', '', lang);
									setCountry('to', document.getElementById('countryTo').options[document.getElementById('countryTo').selectedIndex].text);
									closeSearch('quickSearchTo', null);
								}
							}
							
							text = item.cityName + ', ' + item.airportName +' ('+item.airportCode+')';
							
							temp = document.createTextNode(text.substring(0, text.indexOf(obj.value)));
							div.appendChild(temp);
							
							b = document.createElement('b');
							temp = document.createTextNode(text.substring(text.indexOf(obj.value), text.indexOf(obj.value)+obj.value.length));
							
							b.appendChild(temp);
							div.appendChild(b);
							
							temp = document.createTextNode(text.substring(text.indexOf(obj.value)+obj.value.length));
							div.appendChild(temp);
							mDiv.appendChild(div);
						});
					} else {
						text = document.createTextNode('Prosimy podać misto, nazwę lotniska lub kod lotniska');
						mDiv.appendChild(text);
					}
					block.appendChild(mDiv);
					block.style.display = '';
					closeSearch('formFormBlock', fromArrow);
					closeSearch('toFormBlock', toArrow);
				}
			});
		}
	}
}

lastFromObj = '';
lastFromCall = '';
lastToObj = '';
lastToCall = '';
function searchFly(type, obj, lang){
	if(lang == 'pl'){
		if (type == 'from'){
			lastFromObj = obj;
			lastFromCall = new Date().getTime();
			if (lastFromObj.value.length > 2) {
				setTimeout("runSearch('from', "+lastFromCall+", 'pl')", 500);
			}
		} else {
			lastToObj = obj;
			lastToCall = new Date().getTime();
			if (lastToObj.value.length > 2) {
				setTimeout("runSearch('to', "+lastToCall+", 'pl')", 500);
			}
		}
	}
	if(lang == 'en'){
		if (type == 'from'){
			lastFromObj = obj;
			lastFromCall = new Date().getTime();
			if (lastFromObj.value.length > 2) {
				setTimeout("runSearch('from', "+lastFromCall+", 'en')", 500);
			}
		} else {
			lastToObj = obj;
			lastToCall = new Date().getTime();
			if (lastToObj.value.length > 2) {
				setTimeout("runSearch('to', "+lastToCall+", 'en')", 500);
			}
		}
	}
	
	if(lang == 'de'){
		if (type == 'from'){
			lastFromObj = obj;
			lastFromCall = new Date().getTime();
			if (lastFromObj.value.length > 2) {
				setTimeout("runSearch('from', "+lastFromCall+", 'de')", 500);
			}
		} else {
			lastToObj = obj;
			lastToCall = new Date().getTime();
			if (lastToObj.value.length > 2) {
				setTimeout("runSearch('to', "+lastToCall+", 'de')", 500);
			}
		}
	}
	
}


