;(function( $ ){
	//alert(location.protocol + "//" + location.host + "/hotel/Ajax/cityHelper/");
	$.fn.cityHelper = function(locationList){
		if(locationList==undefined) locationList='foundedCities';
		var target = $(this);

		$(target).keydown(function(key){
			switch (key.keyCode) {
				case 13:	//Enter
					if($('#'+locationList+' table tbody tr.active') && $('#'+locationList+'').is(':visible')){
						var currow = $('#'+locationList+' table tbody tr.active td');
						//$(target).val($(currow).eq(0).text() + ', ' + $(currow).eq(1).text());
						
						countryCode = '';
						if($(currow).eq(2).text()) 
							countryCode = ', ' + $(currow).eq(2).text();
					 	$(target).val($(currow).eq(0).text() + ', ' + $(currow).eq(1).text() + countryCode);
						
						$('#'+locationList+'').text('');
						$('#'+locationList+'').hide();
						$(target).removeClass('activeSearch');
					} 
					return false;
				break;
				default: break;
				}
		});
		
		$(target).keyup(function(key){

			switch (key.keyCode) {
				case 38:	//ArrowUp
							var visibleCities = $('#'+locationList+' table tbody tr:visible');
							var numberOfCities = visibleCities.length;
							var currentActive = numberOfCities;
							visibleCities.each(function(i){
								if($(this).hasClass('active')){
									currentActive = visibleCities.index(this);
									$(this).removeClass('active');
								}
							});
							var flag = true;
							while(flag){
								if(visibleCities.eq((numberOfCities+(currentActive-1))%numberOfCities).css('display') == 'none')
									--currentActive;
								else
									flag = false;
							}
							visibleCities.eq((numberOfCities+(currentActive-1))%numberOfCities).addClass('active');
							$('#'+locationList+'').scrollTo($('#'+locationList+' table tbody tr.active'),150);
						return false;
					break;
				case 40:	//ArrowDown
							var visibleCities = $('#'+locationList+' table tbody tr:visible');
							var numberOfCities = visibleCities.length;
							var currentActive = -1;
							visibleCities.each(function(i){
								if($(this).hasClass('active')){
									currentActive = visibleCities.index(this);
									$(this).removeClass('active');
								}
							});
							var flag = true;
							while(flag){
								if(visibleCities.eq((currentActive+1)%numberOfCities).css('display') == 'none')
									++currentActive;
								else
									flag = false;
							}
							visibleCities.eq((currentActive+1)%numberOfCities).addClass('active');
							$('#'+locationList+'').scrollTo($('#'+locationList+' table tbody tr.active'),150);
						return false;
					break;
				default:
				
						var url;
						if(location.host == 'sky4fly.net' || location.host == 'www.sky4fly.net')
							url = "http://www.sky4fly.net/hotelCityHelper.php?param=";
						else if(location.host == 'biletylotnicze.sky4fly.net' || location.host == 'www.biletylotnicze.sky4fly.net')
							url = "https://www.biletylotnicze.sky4fly.net/hotelCityHelper.php?param=";
						else
							url = location.protocol + "//" + location.host + "/hotel/Ajax/cityHelper/";

						if ($(this).val().length < 3) {
							$('#'+locationList+'').text('');
							$('#'+locationList+'').hide();
						}
						if ($(this).val().length == 3) {
							$(target).addClass('activeSearch');
							$.ajax({
								type: "GET",
								url: url + $(this).val(),
								//data: "name=John&location=Boston",
								async: true,
								success: function(cities){
									$('#'+locationList+'').html(cities);
									$('#'+locationList+'').show();
									//alert(cities);
								},
								error: function(){
									$('#'+locationList+'').hide();
								},
								complete: function(){
									if($(target).val().length != 3)
										$(target).keyup();
									$(target).removeClass('activeSearch');
									$('#'+locationList+' table tbody tr:first').addClass('active');
								}
							});
						}
						else if ($(this).val().length > 3) {
							var visibleRow = 0;
							if ($('#'+locationList+'').text()) {
								$('#'+locationList+' table tr').each(function(){
									var regex = new RegExp($(target).val(), "i");
									if (!regex.test($(this).text())) {
										$(this).hide();
									}
									else {
										$(this).show();
										++visibleRow;
									}
								});
								if (visibleRow == 0) 
									$('#'+locationList+'').hide();
								else 
									$('#'+locationList+'').show();
							}
						}
					break;
			}
		});
		
		$('#'+locationList+' table tbody tr').live("mouseover", function(){
			$('#'+locationList+' table tbody tr').removeClass('active');
			$(this).addClass('active');
		});
		
		$('#'+locationList+' table tbody tr').live("click", function(){
			countryCode = '';
			if($(this).find('td').eq(2).text()) 
				countryCode = ', ' + $(this).find('td').eq(2).text();
		 	$(target).val($(this).find('td').eq(0).text() + ', ' + $(this).find('td').eq(1).text() + countryCode);
			$('#'+locationList+'').text('');
			$('#'+locationList+'').hide();
			$(target).removeClass('activeSearch');
		});
		
		$(target).click(function(){
			$(this).select();
		});
	}
	
	$.fn.testerFunc = function(){
		$(this).click(function(){
			$(this).addClass($(this).val());
		});
	}
})( jQuery );
