function getIcon(color,path) {
  var icon = new GIcon(color);
  if(color == 'purple')
  	icon.image = path + 'images/map-pin-mov-big.png';
  icon.iconSize = new GSize(25, 27);
  icon.iconAnchor = new GPoint(20, 27);
  return icon;
}
function recalculate(opt){
	if(opt.div.length == 0 || opt.input.length == 0)
		return;
	opt.div.css('top',opt.input[0].offsetTop+opt.input[0].offsetHeight);
	opt.div.css('left',opt.input[0].offsetLeft);	
}
$.fn.autocomplete = function(opt) {
	this.after('<div class="autocomplete"><ol class="autocomplete"></ol></div>');
	var $div = this.next(); var $ol = $('ol.autocomplete',$div);
	$div.css('top',this[0].offsetTop+this[0].offsetHeight);
	$div.css('left',this[0].offsetLeft);
	$ol.css('width',this[0].offsetWidth);
	var has_focus = false;var point;var has_map = false;
	if((addnewtrip) && (typeof(google)!== 'undefined') && !!PdMarker && !!opt.map)
		has_map = true;
	if(has_map)
	{
		if(!(!opt.$loc_lat || !opt.$loc_lng))
			point = new GLatLng(opt.$loc_lat.val(),opt.$loc_lng.val());
		else
			point = new GLatLng(0,0);
		var country_code = '';
		var marker= new PdMarker(point , {icon: getIcon('purple',opt.icon_path), draggable: true});
		opt.map.addOverlay(marker);
		marker.setTooltip('<b>'+this.val()+'</b>');
		//if($('input#location_name').val() == '')
			//marker.setTooltip('');
		GEvent.addListener(marker, 'dragend', function(latlng){
			var geocoder = new GClientGeocoder();
			geocoder.getLocations(latlng, function(response){
				if(!response || response.Status.code != 200 || !response.Placemark[0] || !response.Placemark[0].AddressDetails.Country) 
					{marker.setPoint(point);return;	}
				country_code = (response.Placemark[0]).AddressDetails.Country.CountryNameCode;
				//alert(response);	
				var t = response.name.split(',');
				point = new GLatLng(t[0],t[1]);
				opt.$loc_lat.val(t[0]); opt.$loc_lng.val(t[1]); opt.$loc_code.val(country_code);
				marker.setPoint(point);
			});
		});
	}
		function selectCity(this_input){

		//has_focus = false;
		li = $('li.on',$ol);

		$div.hide();
		if(li.length ==0)
			return;

		//country_code = li.attr('code');
		$(this_input).val(li.html());
		if(!!opt.$loc_lat)
			opt.$loc_lat.val(li.attr('lat'));
		if(!!opt.$loc_lng)
			opt.$loc_lng.val(li.attr('lng'));
		if(!!opt.$loc_code)	
			opt.$loc_code.val(li.attr('code'));			
		if(has_map)
		{
			point = new GLatLng(li.attr('lat'),li.attr('lng'));
			marker.setPoint(point);
			marker.setTooltip('<b>'+li.html()+'</b>');
			if(li.html() == '')
				marker.setTooltip('');
			opt.map.setCenter(point);
		}
	}

	var timer;
	
	this.blur(function(){
		selectCity(this);
	});	
	this.closest('form').submit(function(){
		if($div.css('display')!='block')
		{}
		else
		{
			selectCity($('input.autocomplete'));
			$div.hide();			
			return false;
		}	

	});
		
	this.keyup(function(e){
		text = $(this).val();
		if(has_map)
		{
			marker.setTooltip('<b>'+text+'</b>');
			if(text == '')
				marker.setTooltip('');
		}
		if(text.length < 2) return;

		if(e.keyCode=="40")
		{			
			if($('li.on',$ol).html()!=$('li:last',$ol).html())
			{
				temp_li = $('li.on',$ol);
				$('li.on',$ol).removeClass('on');
				temp_li.next().addClass('on');
	
			}	
			return;
		}	
		if(e.keyCode=="38")
		{
			$div.focus();
			if($('li.on',$ol).html()!=$('li:first',$ol).html())
				$('li.on',$ol).removeClass('on').prev().addClass('on');
			return;
		}
		if(e.keyCode=="13")
			return;
		
		clearTimeout(timer);
		timer = setTimeout(function(){
				if(!!opt.$spinner)
					opt.$spinner.show();
				$.post(opt.url, { string: text },
					function(response){
						if(!!opt.$spinner)
							opt.$spinner.hide();
						$ol.html("");
						if(response == "") {$div.hide();return;}
						$(response).each(function(key,city){
							$ol.append('<li lat="'+city.lat+'" lng="'+city.lng+'" code="'+city.code+'">'+city.city_name+', '+city.country_name+'</li>');
						});
						$('li:first',$ol).addClass('on');
						$('li',$ol).hover(function(){
								$('li.on',$ol).removeClass('on');
								$(this).addClass('on');
						});
						
						$div.show();
					},'json'
				)},300);
	});
	this.focus(function(){
		if($('li',$ol).length > 0)
			$div.show();
		$div.css('top',this.offsetTop+this.offsetHeight);
		$div.css('left',this.offsetLeft);
		$ol.css('width',this.offsetWidth);
	});
};

