
var SmartyAjax = {
  category: function(update_id, url, method, params, callback) {

	// href // http://www.google.com:80/search?q=javascript#test
	// search // ?q=javascript
	pathname=location.pathname; //	/search
	host=location.host; // www.google.com:80
	hash=location.hash; // #test

	params=php_json(params);
	
	params["pathname"]=pathname;
	params["host"]=host;
	params["hash"]=hash;

	pn_CID = pathname.indexOf("categoryID"); // заменил на params.categoryID из за woman_clothes и т.д.
	ha_CID = hash.indexOf("categoryID");
	
	catID = params.categoryID; // берет categoryID с передаваемых параметров

	if( pathname!='/c' ){ // перегружаем страницу под ajax (params['changeURL'] == 1 && $('#cat_val').length==0) ||
	  if(catID<=0) catID=1;
	  loc = HTTP_HOST_S+'c#categoryID/'+catID+'';
	  showModal('После перехода на "ajax навигацию по категориям", критерии поиска исчезнут.', 180, 110, 5000, loc);
	}else{ // ajax уже работает
	  if(catID<=0) catID = searchAfter( hash ,"categoryID/");
	  if(catID<=0) catID=1;
	  
	  window.location.hash='categoryID/'+catID;  //change only hash
		
	  if( params['changeURL'] == 1 || $('#cat_val').length==0 ){ // NO loaded category.tpl.html // было раньше - $('#cat_val').length==0
		params3 = '"f":"show_category_tpl_html"';
		params3 = jQuery.parseJSON('{'+params3+'}');
		params3['categoryID']=catID;
		jQuery.ajax({
		  async:true, // false == waiting
		  type: method,
		  url: url,
		  data: params3,
		  success: function(data, textStatus, XMLHttpRequest){
			$('#main_content_template').html( data );
			
			if(jQuery('#cat_val').length>0 && params.categoryID==0) { // loaded category.tpl.html && приводим сортировку к надлежащему виду для отправки по гет
			  gsp = get_search_params( params ); /* after - jQuery.each(gsp,function(key,val) { params[key]=val; }); */
			}
			params['categoryID']=catID;
			params['ajaxn']=1;
			SmartyAjax.category_brief("category_brief", url, method, params , callback);
		  }
		});
	  }else{
		if(jQuery('#cat_val').length>0 && params.categoryID==0) { // loaded category.tpl.html && приводим сортировку к надлежащему виду для отправки по гет
		  gsp = get_search_params( params ); /* after - jQuery.each(gsp,function(key,val) { params[key]=val; }); */
		}
		params['categoryID']=catID;
		params['ajaxn']=1;
		SmartyAjax.category_brief("category_brief", url, method, params , callback);
	  }
	}
  },
  category_brief: function(update_id, url, method, params, callback) {
    params=php_json(params);
	
	update_id = "category_brief";
	jQuery.ajax({
	  async:true, // false == waiting
	  type: method,
	  url: url,
	  data: params,
	  beforeSend: function(a){
		jQuery("#main_content_template").fadeTo(0, 0.4);
	  },
	  success: function(data, textStatus, XMLHttpRequest){ 
		jQuery('#'+update_id).html( data );
		SmartyAjax.category_tree("category_tree", url, method, 'categoryID:'+catID+',f:category_tree' , callback); // подгружаем меню
	  },
	  complete: function(a,b){
		asic_slider(); // включает slider цены
		jQuery("#main_content_template").fadeTo(0, 1);
	  }
	});
  },
  category_tree: function(update_id, url, method, params, callback) {

    params=php_json(params);

	jQuery.ajax({
	  async:true, // false == waiting
	  type: method,
	  //dataType: 'json',
	  url: url,
	  data: params,
	  beforeSend: function(a){
		$('#'+update_id).html( "<div><h3>updating...</h3></div>" );
	  },
	  success: function(data, textStatus, XMLHttpRequest){
		$('#category_tree').html( data );
	  }
	});
  },
  
  update: function(update_id, url, method, params, callback) {

	params=php_json(params);
	
	if(params.getval==1) params.val = $('#'+update_id).html();
	params.sess_id = sess_id;
  
	  jQuery.ajax({
		async:true, // false == waiting
		type: method,
		//dataType: 'json',
		url: url,
		data: params,
		beforeSend: function(a){
		  $('#'+update_id).css("color", "red");
		  //$('#'+update_id).html("<div><h3>updating...</h3></div>"); 
		},
		success: function(data, textStatus, XMLHttpRequest){
		  if(params.value==1) $('#'+update_id).attr( 'value',data );
		  if(params.html==1) $('#'+update_id).html( data );
		  else $('#'+update_id).val( data );
		},
		complete: function(a,b){
		  $('#'+update_id).css("color", "black");
		}
	  });
  },

  upd_val: function(update_id, url, method, params, what, callback) {

	params = php_json(params);

	if( typeof(params['val'])=="undefined" ){
	  if(params['insert']=='html')         params['val'] = $('#'+update_id).html();
	  else if(params['insert']=='select')  params['val'] = $('#'+update_id+' option:selected').attr('value');
	  else                                 params['val'] = $('#'+update_id).val(); 
	}//добавляем к передаваемым параметрам val(значение)
	if( typeof(params['what'])=="undefined" ) params['what'] = what;

	jQuery.ajax({
	  async:true, // false == waiting
	  type: method,
	  //dataType: 'json',
	  url: url,
	  data: params,
	  beforeSend: function(a){
		if(params['insert']=='select'){
		  $('#'+update_id+'').css("color","red");
		} else $('#'+update_id).css("color", "red");
	  },
	  success: function(data, textStatus, XMLHttpRequest){
		if(params['insert']=='html') $('#'+update_id).html(data); //если передается параметр 'html'
		else if(params['insert']=='select'){
		  $('#'+update_id+' [value="'+data+'"]').attr("selected", "selected");
		} else $('#'+update_id).val(data);
	  },
	  complete: function(a,b){
		if(params['insert']=='select'){
		  $('#'+update_id+'').css("color","black");
		  $('#'+update_id+'').css("font-size","14px");
		  $('#'+update_id+'').css("font-weight","300");
		  $('#'+update_id+' option:selected').css("font-size","16px");
		  $('#'+update_id+' option:selected').css("font-weight","900");
		  $('#'+update_id+' option:selected').css("color","red");
		} else $('#'+update_id).css("color", "black");
	  }
	});

  },
  
  call: function(url, method, params, callback, params_func) {
	
    params=php_json(params);
	
	if(params.getVal==1){ // вобще бля суппер системма(предисловие), получать массив значений которые params.updID+'_'+i равны upd
	  //param2={};
	  jQuery.each(params, function(i,k){
		if(k=='upd') params[i] = $('#'+i).val();
		//alert( k+$('#'+i).val()+i );
	  });
	  //params['val']=param2;
	}
	if(params.getVal==2){
	  params['val']=ajax_php_insert( 'get', 0, '', params.updID ); // change 3 param on ''
	}
	
	jQuery.ajax({
	  async:false, // false == waiting
	  type: method,
	  url: url,
	  data: params,
	  complete: callback
	});
  }

}
/*
function(data){
$('#'+update_id).attr('value', data );
$('#cont_alt_links').append('<p>Test</p>');
}
*/
