$(document).ready(function() {	
	//
	// Popunjavanje liste drzava, saveznih drzava i gradova
	//
	//
	
	//$('.pretraga .avio-karte .iz, .pretraga .avio-karte .za').attr('autocomplete', 'off');
	
	
	var code, name, result, acomplete;
	result = '';
	for(i=0; i<hoteli_c_values.length; i++) {
		
		code = hoteli_c_values[i].substring(0,2);
		name = hoteli_c_values[i].substring(3);
		
		// proverimo da li postoji niz sa aerodromima ili saveznim drzavama
		if($.isArray(hoteli_a_values[code +':All']) || $.isArray(hoteli_s_values[code])) {
			result += '<option value="'+code+'">'+name+'</option>';
		}
		
	}
	$('.hotel_counties').html(result);
	
	//
	// Punjenje gradova, tj aerodroma
	//
	function fill_cities(key) {
		result = '';
		for(i=0; i<hoteli_a_values[key].length; i++) {
			code = hoteli_a_values[key][i].substring(0,5);
			name = hoteli_a_values[key][i].substring(6);
			result += '<option value="'+ code +'">'+ name +' ('+ code +')</option>';
		}
		$('.hotel_select_city').html(result);
	}
	
	//
	// Punjenje saveznih država
	//
	function fill_states(key) {
		result = '';
		for(i=0; i<hoteli_s_values[key].length; i++) {
			result += '<option value="'+ key +':'+ hoteli_s_values[key][i] +'">'+ hoteli_s_values[key][i] +'</option>';
		}
		$('.hotel_state').html(result);
	}
	//
	// Kada promenim country napuni gradove (i savezne drzave)
	//
	function onchange_country(key) {
		// Ukoliko postoji niz sa gradovima, sakrij savezne države i prikaži samo gradove
		if($.isArray(hoteli_a_values[key +':All'])) {
			fill_cities(key +':All');  
			$('#hotelstate').hide();
			return true;
		}
		
		// ... postoji savezna država
		var country = key.substring(0,2);
		var state = key.substring(3);

		if(state.length > 1) {
			fill_cities(country +':'+state);
		} else {
			fill_states(country);
			fill_cities(country +':'+ hoteli_s_values[country][0]);
		}
		$('#hotelstate').show();
	}
	$(".hotel_counties option[value='RS']").attr('selected', 'selected');
	onchange_country('RS');
	// 
	// Otvaranje popupa za izbor aerodroma
	//
	
	var za_input;
	var otvori_opet = true;
	function open_select() {
		// sakrij ga prvo ako je otvoren
		//if($('#avio_cities').is(':visible')) {$('#avio_cities').hide();}
		
		// Zbog vraćanja focusa na select input
		if(!otvori_opet) {
			otvori_opet = true;
			return false;
		}
		// otkrij izbor države
		$("#hotel_cities").children(':eq(0)').show();
		// Ukoliko je selektovano nešto u inputu, prođi kroz niz
		if($(".hotel_countries").val() != 'RS') {
			$(".hotel_counties option[value='RS']").attr('selected', 'selected');
			onchange_country('RS');
		}
		var position = za_input.offset();
		$("#hotel_cities").css("top", position.top+32);
		$("#hotel_cities").css("left", position.left+5);
		$("#hotel_cities").slideDown('fast', function() {
			$(document).click(function(e) {
				if($('#hotelavio_cities').is(':visible')) {
					var $clicked=$(e.target);
					if(!($clicked.is('#hotel_cities') || $clicked.parents().is('#hotel_cities'))){
						$('#hotel_cities').hide();
					}
				}
			});									
		});
	}
	
	$('.hoteli_pretraga .destination').click(function() {
		
		za_input = $(this);

		open_select();
		return false;
	});
	
	// Kada izaberem državu, popuni input i sakrij avio karte
	$('.hotel_select_city').click(function() {
		//otvori_opet = false;
		za_input.val($(this).val()).focus();
		$('#hotel_cities').hide();
		return false;
	});
	
	// Ako krenem da kucam otvori autosuggest
	$('.hoteli_pretraga .destination').keydown(function(e) {
		
		// ako je esc zatvori popup
		if(e.keyCode ==27) {
			$('#hotel_cities').hide();
			otvori_opet = false;
			return false;
			
		}
		
		
		// ako je tab, uzmi value iz select_city i nastavi...
		if(e.keyCode == 9) {
			
			if($('.hotel_select_city').val() != null){
				za_input.val($('.hotel_select_city').val());
			}
			$('#hotel_cities').hide();
			/*
			if($(this).hasClass('.hoteli_pretraga .avio-karte .iz')) {
				za_input = $('.hoteli_pretraga .avio-karte .za');
				open_select();
			} */
			return true;
		}
		
		// ako je enter uzmi value iz select_city i prekini
		if(e.keyCode == 13) {
			if($('.hotel_select_city').val() != null){
				za_input.val($('.hotel_select_city').val());
			}
			$('#hotel_cities').hide();
			otvori_opet = false;
			return false;
		}							   
											   
	});
	
	
	$('.hoteli_pretraga .destination').keyup(function(e) {
		if($('#hotel_cities').is(':hidden')) {
			za_input = $(this);
			open_select();
		}
		// enter
		if(e.keyCode == 13 || e.keyCode == 9 || e.keyCode ==27) { return false; }
											 
		// Ako je strelica na gore/dole biraj grad
		if(e.keyCode == 40 || e.keyCode==38){
			var sel = $('.hotel_select_city option:selected');
			var pos;
			if(sel.length >0) {
				if(e.keyCode == 40) {
					if(sel.next().length >0) {
						sel.removeAttr('selected');
						sel.next().attr('selected', 'selected');
						pos = sel.next();
					}
				}
				else {
					if(sel.prev().length >0) {
						sel.removeAttr('selected');
						sel.prev().attr('selected', 'selected');
						pos = sel.prev();
					}
				}
			}
			// selektuj prvi
			else { 
				$('.hotel_select_city option:first').attr('selected', 'selected');
				pos = $('.hotel_select_city option:first');
			}
			//za_input.val(pos.val());
			return false;
		}
		
		
		// za ostale karaktere uradi autocomplete
		var val = $(this).val().toLowerCase();
		
		if(val.length > 0) {
			// sakrij izbor drzave i state
			$("#hotel_cities").children(':eq(0), :eq(1)').hide();
			$('.hotel_select_city').empty();
			// prolazimo kroz niz i nalazimo ono što je ukucano
			var i, j, a, pos;
			var sel = false;
			var result = '';
			var k=0;
			for (i in hoteli_a_values ) {
				for(j=0; j<hoteli_a_values[i].length; j++) {
					a = hoteli_a_values[i][j];
					pos = a.toLowerCase().indexOf(val)
					if(pos > -1) {
						if(pos == 0 && !sel) {
							sel=true;
							$('.hotel_select_city').prepend('<option value="'+ a.substring(0,3) +'" selected="selected">'+ a.substring(4) +' ('+ a.substring(0,3) +')</option>');
						} else {
							$('.hotel_select_city').append('<option value="'+ a.substring(0,3) +'">'+ a.substring(4) +' ('+ a.substring(0,3) +')</option>');
						}
						k++;
					}
					// Prikazujemo samo prvih 10 rezultata, ubrzava stvar.
					if(k > 10) {
						k=0;
						sel=false;
						return true;
					}
				}
				
			}
			if(k == 1) {
					$('.hotel_select_city option').attr('selected', 'selected');
				}
		} else {
			$("#hotel_cities").children(':eq(0)').show();
			onchange_country($('.hotel_counties').val());
		}
	});
	
	// kada se promeni drzava, promeni gradove
	$(".hotel_counties").change(function() {
		onchange_country($(this).val()); 
		return false;
	});
	$(".hotel_state").change(function() {
		fill_cities($(this).val()); 
		return false;
	});
	
	$(".zatvori").click(function() {
		$('#hotel_cities').hide();
		otvori_opet = false;	
		return false;
	});
});
