var d1 = new Date();
var d2 = d1.getTime() + (24*60*60*1000);
d1.setTime(d2);

jQuery.noConflict();
jQuery(document).ready(function(){
	jQuery("#street").keyup(function(){
		ajax_input("/actions/find_street.php","#street");
	});
	jQuery("#city").keyup(function(){
		ajax_input("/actions/find_city.php","#city");
	});
	jQuery("#region").keyup(function(){
		ajax_input("/actions/find_region.php","#region");
	});
	jQuery("#hint a").live('click', function(){
		//alert(jQuery(this).html());
		jQuery(this).parent().parent().children("input").val(jQuery(this).html());
		jQuery(this).parent().hide();
		jQuery(this).parent().parent().children("input").focus();
	});
	

  function ajax_input (url,id) {
      jQuery.ajax({
        url:url,
        dataType:"post",
        data:{text:jQuery(id).val()},
        success:function(text){
          if(text=='')
          {
            jQuery(id).parent().children("div").hide();
          }
          else
          {
            jQuery(id).parent().children("div").show();
          }
          jQuery(id).parent().children("div").html(text);

        }
        //error:function(a){alert(a.status)}
      });
    }
	jQuery("#street").blur(function(){
		jQuery(document).not("#hint").click(function(){
			jQuery("#hint").addClass("invisible");
		});
	});
	
	jQuery("#sorter").change(function(){
		location.href='?' + jQuery("#sorter").val();
	});
	
	jQuery("#set_page_size").change(function(){
		var loc = location.search;
		
		var rexp = new RegExp('[?&]pagesize=[0-9]?[0-9]?[0-9]');
		var newloc = loc.replace(rexp, '');
		var rexp1 = new RegExp('[?&]page=[0-9]?[0-9]?[0-9]');
		newloc = newloc.replace(rexp1, '');
		var rexp2 = new RegExp('[?&]wrap=[0-9]?[0-9]?[0-9]');
		newloc = newloc.replace(rexp2, '');
		if(!newloc)
		{
			newloc = '?';
			link_text = 'pagesize=';
		}
		else
		{
			link_text = '&pagesize=';
		}
		
		location.href = newloc + link_text + jQuery(this).val();
	});
	
	jQuery("#clear_filter").click(function(){
		jQuery.ajax({
			url:"/actions/unset_filter.php",
			success:function(text){
				//alert(text);
				window.location.reload();
			}
			//error:function(a){alert(a.status)}
		});
	});
	
	jQuery(":checkbox").click(function(){
		var cObj = jQuery(this);
		
		if( cObj.attr("checked")) {
			act = 'add'; 
		} else {
 			act = 'remove';
        }
		jQuery.ajax({
			url:"/actions/add_to_notebook.php?lot_id="+cObj.val()+"&action="+act,
			success:function(text){
				var chosen = document.getElementById('show_chosen');
				if(text>0)
				{
					chosen.className='show_chosen';
					chosen.innerHTML = '<a href="/announcements_chosen.php">Показать выбранное ('+text+')</a>';
				}
				else
				{
					chosen.className='unshow_chosen';
				}
			}
		});
	});
	
	jQuery("#clear_notebook").click(function(){
		if(confirm('Очистить блокнот?')){
			jQuery.ajax({
				url:"/actions/clear_notebook.php?clear=1",
				success:function(){
					location.href='/announcements_chosen.php';
				}
			});
		}
	});


  //var city=jQuery("#select_city option:selected").val();
 // alert(city);
  //setCookie("city", city, "Mon, 01-Jan-2020 00:00:00 GMT", "/");


 jQuery("select.select_city").change(function() {
    var city=jQuery("#select_city option:selected").val();
    setCookie("city", city, d1, "/");
    //ajax_select("/actions/get_region.php",city,"#tr_region",0);
    //jQuery("#tr_street").hide();
    location.href="";
  })
 
    
  jQuery("select.#region_id").live('change',function() {
    var region=jQuery("#region_id option:selected").val();
    //alert(region);
    ajax_select("/actions/get_street.php",region,"#tr_street",0);
  });

});

function admin_city_check() {
   var id=jQuery("#admin_city_select option:selected").val();
    location.href='/useradmin/announcements.php?city_id='+id;
}
function ajax_select (url,text,id,param) {
      jQuery.ajax({
        url:url,
        dataType:"post",
        data:{text:text,
        param: param},
        success:function(text){
          //alert(text);
          if(text=='')
          {
            jQuery(id).hide();
          }
          else
          {
            jQuery(id).show();
          }
          jQuery(id).children(".set_text").html(text);
        }
        //error:function(a){alert(a.status)}
      });
    }

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
