// JavaScript Document

$(document).ready(function(){
     $('button.search_bttn').click(function(){
              var input = $('input[name="search_request"]');
	      var href='/baki.info/search/'+input.val();
              location.href=href;
	 });
	$('.select_country').click(function(){
		$('.weather_select').slideToggle();
		$('.w_bottom_shadow').fadeToggle(200);
	})
	$('#fake_advanced').click(function(){
              $(".advanced_search").trigger('click');
	});  
	$('#company').keydown(function(e){
		if(e.keyCode==13) $('#searchfilter').trigger('click');
	});
	
	$('#searchfilter').click(function(){
	    var phone = $('#phonenumber').val();
		var cat = $('#fcat').val();
		var subcat = $('#fsubcat').val();
		var company = $('#company').val();
		var keywords = '';
		//phone=phone.replace('-', ' ').replace('-', ' ').replace('+', "").replace('(', "").replace(')', "").replace(' ', "_");
		  phone= phone.replace(/[^0-9]+/g, '_').replace('_','');
		
		$('.keywords span').each(function(){
		    keywords  += $.trim($(this).text())+';';
		});
		if($('.keywords input').val()){keywords  += $.trim($('.keywords input').val())};
		if(!phone){phone='0';}
		if(!subcat){subcat='0';}
		if(!company){company='none';}
		if(!keywords){keywords='none';}
		var href='/baki.info/filter/'+cat+'/'+subcat+'/'+company+'/'+phone+'/'+keywords;
                //console.log(href);
		location.href=href;
		//alert(phone);
		 	
	});
	
	 $('.keywords').click(function(){
		$('input',this).focus();
		//$(this).addClass('focus');
	  });
	  $('.keywords input').blur(function(){
		  $(this).css('width','200px');
		  });
      $('.keywords input').keydown(function(e){
		 inputResize (this,75);
		  
	  });
	    $('.keywords input').live('keydown',function(e){
		  var a = $(this);
		  if(e.keyCode==8){
			if(a.val().length<1){
				a.prev('.add_el').remove();
			}
		  }
		});
	  $('.keywords input').live('keyup',function(e){
                if($('.keywords .add_el').length>4) return false;
                var a = $('.keywords input');

                if(e.keyCode==32 || e.keyCode==13){
                    if(!$.trim(a.val())) return false;
                      /*
                      $.get('/templates/add_el.html',function(data){
                              $(a).before(data.replace("text", a.val().replace(' ','')));
                              $(a).val('').css({'width':'60px'});
                      });*/
		  }
	  });
	$(document).click(function(e){
		var target = $(e.target);
		if ( target.is('.autocomplete *') || target.is('input[name="search_request"]') ) return false;
		
		autocomplete.hide();
	});
	
	$('.autocomplete ul li a').live('click',function(){
	  var href=$(this).attr('href');
	 location.href=href;
	});
	
	var autocomplete=$(".autocomplete");
	
   $("ul li",autocomplete).live("mouseover", function(){
	   $(".autocomplete ul li.act").removeClass("act");
		$(this).addClass("act");
	 });
  $('input[name="search_request"]').keyup(function(e){ // if keyup on fast search
	  if($(this).val() == "" || $(this).val() == " " || $(this).val().length<2){
			$(".autocomplete").hide();
			return false;
		}
		 checkSearchKey(e.keyCode);
		return false;
  });
  

  $(".search_result").click(function(){
		var id=$(this).attr('rel');
		location.href='/firm/'+id;
	});
   $(".search_result").live('mouseover ',function () {
            //var top = 200;
            //console.log(top);
            //$('.map_handler').css('top', top);
            var id=$(this).attr('rel');
            $(this).addClass("hover").next().addClass("border_t");

             var xy = markers[id].getPosition();
             if(xy.lat()!=0){
                $('.map_handler  ').show();
                var h = $('.map_handler').height();
                $('.map_handler').css('top', 275-h);
                $('.map_handler h3').html(markers[id]['name']);
                $('.map_handler p').html(markers[id]['address']);
                var newlatlng = new google.maps.LatLng(xy.lat()+zoom (map.zoom), xy.lng());
                map.panTo( newlatlng );
             }

        
    });
	
	
	$(".search_result").live('mouseout ', function () {
            $('.map_handler  ').hide();
            $(this).removeClass("hover").next().removeClass("border_t");;
    });
	
	
	
	$('.sendFeed').click(function(){
	    var contactName =  $('#contactName').val();
		var contactEmail = $('#contactEmail').val();
		var contactText =  $('#contactText').val();
		if(!contactName || !contactEmail || !contactText) return false;
		 $.post('/ajax/feedback.php',{contactName:contactName,contactEmail:contactEmail,contactText:contactText},function(data){
			
			alert(data);
			$('#contactName').val('');
		    $('#contactEmail').val('');
		    $('#contactText').val(''); 
		})	
	});
	
	/*
	$(".advanced_search").click(function () {
            var t=$(this);
            if(!t.hasClass('opened')){
                $(this).addClass('opened');
                $('.filter').animate({'margin-top':'+=160px'},{  duration:600, easing:"easeOutQuint"});
            }else {
                $(this).removeClass('opened');
                $('.filter').animate({'margin-top':'-=160px'},{  duration:600, easing:"easeOutQuint"});
            }
        });*/
	$('.n_list_block').each(function(){
		var t=$(this);
		height=0;
		$('.n_list_item',t).each(function(){
			var sheight=$(this).height();
			if(sheight>height) { 
				height=sheight;}
				console.log(sheight);
		});
		$('.n_list_item',t).each(function(){
			$(this).css('height',height);
		});
		t.css('height',height);
		
	});
        
	
	
});
