	$(document).ready(function(){

		var maybe = false;
		var input = $('#ecity');

		var clist = $('#clist');
		var flist = $('#flist');

		flist.click( function(){ input.focus(); });

		var ofs = input.position();
		flist.css({left: ofs.left + 'px', top: ofs.top+'px'});
		clist.css({left: ofs.left + 'px', top: ofs.top+input.height()+2+'px'});
		clist.css({maxHeight: '200px', overflow: 'auto'});
		if ( $.browser.mozilla ) {
			flist.css('borderLeftWidth', '2px');
		}
		if ( window.chrome ) {
			flist.css({ borderTopWidth: '0px', borderLeftWidth: '2px' });
		}

		var typeList = function(){

			var val = input.val();
			var temp = {};

			if ( val.match(/[A-z]/) ) {
				val = convert(val);
				input.val( val );
			}

			if ( val ) {

				var tr = false, first = '', firstK = '';

				for ( var k in listOfCities ) {

					if ( listOfCities[k][1].substring(0, val.length ).toLowerCase() == val.toLowerCase() ) {

						temp[k] = listOfCities[k];

						if ( !tr ) {
							tr = true;
							first = listOfCities[k][1];
							firstK = listOfCities[k][0];
						}
					}
				};

				if ( firstK ) {
					var s = '';
					for ( var k in temp ) {

						s += '<div class="city-item' + ((temp[k][0]==firstK)? ' city-cursor':'' ) + '" rel="'+temp[k][0]+'"><a href="http://'+temp[k][0]+window.global.zone+'">'+temp[k][1]+'</a></div>';
					};
					clist.show().html( s );
					if(clist.height()>200)
					clist.height(200);
					$('#guests').hide();
					flist.show().attr('rel', firstK).html( first );

						clist.find('div').each( function(){
							$(this).click( function(){
var dsn = $(this).attr('rel');
								var link = $(this).find('a').eq(0);

										clist.hide();
										$('#guests').show();
										flist.html( link.html() );
										flist.attr('rel', dsn);
										input.val( link.html() );
 					return false;					
								});
						});
					return;
				}
			}
				clist.hide();
				$('#guests').show();
				flist.hide().attr('rel', '');


		};

		var setValue = function () {
			var val = flist.html();
			if ( val ) {
				input.val( val );
				typeList();
			}
		};

		var gotoPage = function(){

			
			var dsn = flist.attr('rel');

			if ( dsn ) {

				clist.hide();
				$('#guests').show();
				input.val( flist.html() );
                var d1 = $('#daten1').val();
				var d2 = $('#daten2').val();
				var gu = $('#guests').val();
				var str = '';
				if(d1.length&&d2.length)
				str +='?date1='+d1+'&date2='+d2;
				if(gu.length){
				if(str.length)
				str +='&guest='+parseInt(gu);
				else
				str +='?guest='+parseInt(gu);
				}
				str = '/'+str;
				window.location.href =  'http://'+dsn + window.global.zone+str;
			} else {
				input.select().focus();
			}

		};

		var esc = function(t){
			clist.hide();
			$('#guests').show();
			flist.hide().html('').attr('rel', '');
			if (!t) input.focus();
		};

		var moveRq =  function (r){
				// reset all
				var t;
				if ( clist.find('div[class*="city-cursor"]').length ) {

					if (r) {
						// Down
						t=clist.find('div[class*="city-cursor"]').removeClass('city-cursor').next();
					} else {
						// Up
						t=clist.find('div[class*="city-cursor"]').removeClass('city-cursor').prev();
					}

					if ( !$(t).length ) {
						if (r) {
							// first
							t=clist.find('div:first');
						} else {
							// last
							t=clist.find('div:last');
						}
					}
				} else {
						if (r) {
							// first
							t=clist.find('div:first');
						} else {
							// last
							t=clist.find('div:last');
						}
				}
				t.addClass('city-cursor');
				correctVis();
				
				flist.html( t.find('a').eq(0).html() ).attr('rel', t.attr('rel') );

			};
		var correctVis = function(){

				var scr = clist.scrollTop();

				var k=0, t=false;
				var height = clist.find('div.city-item').height();

				clist.find('div').each(function(){

					if ( !t ) {
						k++;
						if ( $(this).hasClass('city-cursor') ) t = true;
					}
				});
				var heightTo = height * (k-1);
				
				clist.scrollTop( heightTo );


		};

		var move = function(e, type){
			if (!e) e = window.event;

			switch ( e.keyCode ) {
				case 40:
						// Down
						if (!type) moveRq(true);
					break;

				case 38:
						// Up
						if (!type) moveRq(false);
					break;
				case 39:
						// Right
						setValue();
					break;

				case 13:
					// Enter
					if ( !type ) gotoPage();

						return false;
					break;

				case 27:
						// Esc
						esc();
					break;
				default:
					if ( !!type ) typeList(e);

			}


		};
		var moveD = function(e){
			if (!e) e = window.event;
			move(e, true);
		};

var moveCaretToEnd = function () {
	var inputObject = document.getElementById('ecity');
	if (inputObject.createTextRange) {
		var r = inputObject.createTextRange();
		r.collapse(false);
		r.select();
	} else if (inputObject.selectionStart) {
		var end = inputObject.value.length;
		inputObject.setSelectionRange(end,end);
		inputObject.focus();
	}
}

		input.keyup(moveD).keydown(move).focus().select();//click();

		if ( $('#cButton').length ) $('#cButton').click( gotoPage );

		//moveCaretToEnd();
		$(document).click(function(){
			//if ( !maybe ) {
			//	esc( true );
			//}
		});

		var chars = ({ 'Q':'\u0419', 'W':'\u0426', 'E':'\u0423', 'R':'\u041a', 'T':'\u0415', 'Y':'\u041d', 'U':'\u0413', 'I':'\u0428', 'O':'\u0429', 'P':'\u0417', '[':'\u0425', ']':'\u042a', 'A':'\u0424', 'S':'\u042b', 'D':'\u0412', 'F':'\u0410', 'G':'\u041f', 'H':'\u0420', 'J':'\u041e', 'K':'\u041b', 'L':'\u0414', ';':'\u0416', '\'':'\u042d', 'Z':'\u042f', 'X':'\u0427', 'C':'\u0421', 'V':'\u041c', 'B':'\u0418', 'N':'\u0422', 'M':'\u042c', ',':'\u0411', '.':'\u042e', '/':'.', 'q':'\u0439', 'w':'\u0446', 'e':'\u0443', 'r':'\u043a', 't':'\u0435', 'y':'\u043d', 'u':'\u0433', 'i':'\u0448', 'o':'\u0449', 'p':'\u0437', '[':'\u0445', ']':'\u044a', 'a':'\u0444', 's':'\u044b', 'd':'\u0432', 'f':'\u0430', 'g':'\u043f', 'h':'\u0440', 'j':'\u043e', 'k':'\u043b', 'l':'\u0434', ';':'\u0436', '\'':'\u044d', 'z':'\u044f', 'x':'\u0447', 'c':'\u0441', 'v':'\u043c', 'b':'\u0438', 'n':'\u0442', 'm':'\u044c', ',':'\u0431', '.':'\u044e', '/':'.' });
		var convert = function(val){
			var s = '';
			for( var k=-1; k++<val.length; ) {

				if ( !!chars[ val.charAt(k) ]  ) {
					s += chars[ val.charAt(k) ];
				} else {
					s += val.charAt(k);
				}
			}
			return s;
		}


				$('head').append('<link rel="stylesheet" type="text/css" href="/css/ui.css" />');

		$('body').append('<div id="kalendarik2"></div>');

		$('#kalendarik2').load('/js/ui.pack.js',function(){

								var nowYear = new Date().getFullYear();
								var nowMonth = new Date().getMonth() +1;
								var nowDate = new Date().getDate();

								var now = new Date();


									$('#daten1').datepicker({
										yearRange: ( nowYear.toString())+':'+((nowYear+1).toString()),
										changeMonth: true,
										changeYear: true,
										defaultDate: $('#daten1').val(),
										minDate: new Date(),
										showOn: 'button',
										 dateFormat: 'dd.mm.yy',
										buttonImage: '/css/newdesign/calendar.gif',
										buttonImageOnly: true,
										onSelect: function(a, b){

											a = a.split('.'); // delimetr for format date

											var dt = new Date( parseInt(a[2]||nowYear), parseInt(a[1]||nowMonth)-1, parseInt(a[0]||nowDate));

											$('#daten2').datepicker('option', 'minDate',  dt );
										}
									});

									var d1 = $('#daten1').val().split('.'); // delimetr for format date
									d1 = new Date( parseInt(d1[2]||nowYear), parseInt(d1[1]||nowMonth)-1, parseInt(d1[0]||nowDate));

									$('#daten2').datepicker({
										yearRange: (new Date().getFullYear().toString())+':'+((new Date().getFullYear()+1).toString()),
										changeMonth: true,
										changeYear: true,
										defaultDate: $('#daten2').val(),
										minDate: d1, 
										showOn: 'button',
										 dateFormat: 'dd.mm.yy',
										buttonImage: '/css/newdesign/calendar.gif',
										buttonImageOnly: true
									});

		});

		if($('#chst').length){
		$('#chst').click(function(event){
		var $anchor = $('#spisok');
			  $('html, body').stop().animate({
				 scrollTop: $anchor.offset().top
			  }, 1000, function(){});
			 return false;
		});
		}
	});
