
if(get_cookie ('checkin'))
var checkin = get_cookie ('checkin');
if(get_cookie ('checkout'))
var checkout = get_cookie ('checkout');
function searchcity(){
set_cookie ( 'checkin',$("#checkin").val() ,"/",1);
set_cookie ( 'checkout',$("#checkout").val() ,"/",1);	
	
if($( "#auto_search" ).attr("url")!=""){

window.location.href ="/"+$( "#lang" ).val()+$( "#auto_search" ).attr("url");//+"?check_in="+$("#checkout").val()+"&?check_out="+$("#checkout").val();
}else
alert($("#er_msg").text());
}

$.widget( "custom.catcomplete", $.ui.autocomplete, {
		_renderMenu: function( ul, items ) {
			var self = this,
				currentCategory = "";
			$.each( items, function( index, item ) {
				if ( item.category != currentCategory ) {
					
					
					if(item.category =='Hotels'){
					ul.append( "<li class='ui-autocomplete-category'>" + transArr.Hotels + "</li>" );
					}else if(item.category =='City'){
					ul.append( "<li class='ui-autocomplete-category'>" + transArr.City + "</li>" );
					}else if(item.category =='Landmark'){
					ul.append( "<li class='ui-autocomplete-category'>" + transArr.Landmark + "</li>" );
					}
					
					currentCategory = item.category;
				}
				self._renderItem( ul, item );
			});
		}
	});
		
		
$(function() {


var cache = {},
			lastXhr;
var path= $( "#lang" ).val();

$( "#auto_search" ).catcomplete({
			autoFocus: true,
			minLength: 2, 
			delay: 800 ,
			source: function( request, response ) {
				var o_str_a;
				var term = request.term;
				if ( term in cache ) {
					response( cache[ term ] );
					return;
				}

				lastXhr = $.getJSON( "/"+path+"/autoSearch/city/", request, function( data, status, xhr ) {
					cache[ term ] = data;
					if ( xhr === lastXhr ) {
						response( data );
					}
				});
			},
			
			create: function(event, ui) { 
			
                  o_str_a = $(this).val();
			
			},
			
			select: function( event, ui ) {
			$( "#auto_search" ).attr("url", ui.item.url);
			$( "#auto_search" ).removeClass("ui-autocomplete-loading");
			$( "#auto2" ).show();	
			set_cookie ( 'city_sel',ui.item.id ,"/",1);
			}
			
		}).bind({
			
            focus : function() {
			if($(this).val()==o_str_a)
                $(this).val('');
            },
            blur : function() {
                if ($(this).val() == '') {
                    $(this).val(o_str_a);
            }
            }
        });	
		
$( "#auto_search2" ).catcomplete({
			minLength: 2, 
			delay: 800 ,
			
			source: function( request, response ) {
				 var defval;
			
				var term2 = request.term+"_2" ;
				if ( term2 in cache ) {
					response( cache[ term2 ] );
					return;
				}

				lastXhr = $.getJSON( "/"+path+"/autoSearch/hotel/?city="+get_cookie ('city_sel'), request, function( data, status, xhr ) {
					cache[ term2 ] = data;
					if ( xhr === lastXhr ) {
						response( data );
					}
				});
			},
			
			create: function(event, ui) { 
			
                    defval = $(this).val();
			
			},
			
			select: function( event, ui ) {
			$( "#auto_search" ).attr("url", ui.item.url);
			$( "#auto_search2" ).removeClass("ui-autocomplete-loading");
			}

			
		}).bind({
			focus : function() {
			if($(this).val()==defval)
                $(this).val('');
				$(this).css({  'opacity':'1'    });
            },
            blur : function() {
                if ($(this).val() == '') {
                    $(this).val(defval);
            }
            }
        });		

$( "#dest_search" ).catcomplete({
			autoFocus: true,
			minLength: 2, 
			delay: 800 ,
			
			source: function( request, response ) {
				if($("#ccid").val())
				var arg = $("#ccid").val();
				else
				arg = "";
				var o_str ;
				var term3 = request.term+"_3";
				if ( term3 in cache ) {
					response( cache[ term3 ] );
					return;
				}
				lastXhr = $.getJSON( "/"+path+"/autoSearch/dest/"+arg, request, function( data, status, xhr ) {
					cache[ term3 ] = data;
					if ( xhr === lastXhr ) {
						response( data );
					}
				});
			},
			
			create: function(event, ui) { 
			       o_str =  $(this).val();
			},
			
			select: function( event, ui ) {
			$( "#auto_search" ).attr("url", ui.item.url);
			$( "#dest_search" ).removeClass("ui-autocomplete-loading");
			set_cookie ( 'city_sel',ui.item.id ,"/",1);
			}
			
		}).bind({
			
            focus : function() {
			if($(this).val()==o_str)
                $(this).val('');
            },
            blur : function() {
                if ($(this).val() == '') {
                    $(this).val(o_str);
            }
            }
        });	
		
$(".btn_search").click(searchcity);
$("#btn_search").click(searchcity);

 if(checkin&&checkout)
{
$("#checkin").val(checkin);
$("#checkout").val(checkout);
}

});		
