/**
 * @author cashbit
 * 
 * This is the search engine based on dinamic grouping of objects in more than one sets.
 * Example: 
 * 		you can have from 1 to N main groups
 * 		for each group you can have 1 to X categories
 * 		for each category you can have 1 to Y contents
 * 		Each content can be present in 1 or more categories at a time
 * 
 * 		Real world example based on ancient objects:
 * 			Groups and categories: 
 * 				- MATERIAL
 * 					- Metal
 * 					- Wood
 * 					- Glass
 * 					- Pottery
 * 				- ROOM
 * 					- Bedroom
 * 					- Bathroom
 * 					- Kitchen
 * 					- Salon
 * 				- AGE
 * 					- 700
 * 					- 800
 * 					- 900
 * 
 * A LAVATORY can be placed in
 * 		Material: Pottery, 
 * 		Room : Bathroom,
 * 		Age : 800
 * 
 * An OIL LAMP can be placed in
 * 		Material: Metal, Glass
 * 		Room: Bedroom, Kitche, Salon
 * 		AGE: 700,800,900
 * 
 * Algorithm:
 * 
 * 	1) Get Groups and categories from server, the initial request can be something like this:
  		request: {
  			Action: 'getgroups'
  		}
  		response: {
  			Groups:[
  				{groupname: 'MATERIAL'},
  				{groupname: 'ROOM'},
  				{groupname: 'AGE'}
  			]
  		}
 * 	2) Compose a graphic interface tool to let the user compose his query selecting 
 * 	   with a flag each category of items included in the result, for each click
 * 	   on the flag near the category the client JS asks to PHP the resulting combination of other groups categories 
 * 	   for the objects composing the resultset, the resultset is displayed in a resulting table with result pagination
 * 	   if result are more than N
 
		request:{
  			Groups:[
  				{
  					groupname: 'MATERIAL',
  					categories:[
  						{categoryname:'Metal'}
  					] 
  				},
  				{
  					groupname: 'ROOM',
  					categories:[
  						{categoryname:'Bedroom'},
  						{categoryname:'Salon'}
  					] 
  				},
  				 				{
  					groupname: 'AGE',
  					categories:[
  						{categoryname:'700'}
  					] 
  				}
  			]
		} 
		
		response:{
			contents:[
				{
					RecId: '09832083290',
					cTitle: 'OIL LAMP',
					cIntroText: 'The real 700 Oil lamp of Mr. Ciccio',
					....
				} 
			]
		}
 * 
 */

var SuggestionsToFind = 'Your search result is empty, can I suggest you to switch the check box of "Match all conditions" ?'
var SuggestionsToSearch = 'Your search seems to produce too much results, you can specify both free text and multiple categories, try again!'
var SearchRequestList = new Ext.util.MixedCollection() ; 

function MxKartDoSearch(){

	var MatchAllCondition = $('MxSearchAndOrSelection').getValue() ;
	
	var recidcatlist = '' ;
	SearchRequestList.eachKey(function(key,cat){
		if (recidcatlist != ''){
			recidcatlist += ',' 
		}
		recidcatlist +=  '"' + key + '"' ;
	})
	
	freetext = $('mxKartSearchField').getValue() ;
	
	if ((recidcatlist == '')&&(freetext == '' || freetext == '...')){
		OnMxGetMenuVoice(SuggestionsToSearch);
		return ;
	}

	var request = {
		action : 'mxkartsearchenginedosearch',
		freetext : freetext, 
		categories :  recidcatlist,
		matchall : MatchAllCondition,
		offset: 0,
		maxresults: 1000
	} ;
	var lm = new Ext.LoadMask(Ext.getBody()) ;
	lm.show() ;
	Ext.Ajax.request({
		   url: mxkartws,
		   success: function(r){
			  	lm.hide();
			  	var resp = Ext.util.JSON.decode(r.responseText);
				var cl = new Ext.util.MixedCollection();
				cl.addAll(resp.relatedcat) ;
				var mxkartsearchgroupandcategories = Ext.get('mxkartsearchgroupandcategories') ;
				var grouplist = mxkartsearchgroupandcategories.query('.mxkartsearchgroup') ;
				grouplist.each(function(item,i,l){
					var group = Ext.get(item.id) ;
					var catlist = group.query('.mxkartsearchcategory') ;
					catlist.each(function(item,i,l){
						var theid = item.id ;
						theid = theid.replace('searchcategory_','') ;
						if (cl.contains(theid)){
							Ext.get(item.id).setOpacity(1,true) ;
						} else {
							Ext.get(item.id).setOpacity(0.6,false) ;
						}
					})
				});
			  	if (resp.htmlresult == ''){
					OnMxGetMenuVoice(SuggestionsToFind);
				} else {
					OnMxGetMenuVoice(resp.htmlresult);
				}
		   },
		   failure: function(r){
		   	alert(r.statusText) ;
		   },			
		   params : js2php(request)
	});
	/*
	new Ajax.Request(mxkartws, {				
	  method: 'POST',
	  parameters : js2php(request),
	  onSuccess: function(transport) {
	  	lm.hide();
	  	var resp = Ext.util.JSON.decode(transport.responseText);
	  	if (resp.htmlresult == ''){
			OnMxGetMenuVoice(SuggestionsToFind);
		} else {
			OnMxGetMenuVoice(resp.htmlresult);
		}
	  } 
	});	
	*/
}

function MxKartSearchCategory(catid) {
	var parentgroup = Ext.get('searchcategory_'+catid).parent().parent(); 
	var catlist = parentgroup.query('.mxkartsearchcategory') ;
	catlist.each(function(item,i,l){
		var theid = item.id ;
		theid = theid.replace('searchcategory_','') ;
		if (theid != catid){	
			var thecheckbox = Ext.get("checkcategory_"+theid) 
			thecheckbox.dom.checked = false ;
		}
	})
		/*
	var mxkartsearchgroupandcategories = Ext.get('mxkartsearchgroupandcategories') ;
	var grouplist = mxkartsearchgroupandcategories.query('.mxkartsearchgroup') ;
	grouplist.each(function(item,i,l){
		var group = Ext.get(item.id) ;
		var catlist = group.query('.mxkartsearchcategory') ;
		catlist.each(function(item,i,l){
			var theid = item.id ;
			theid = theid.replace('searchcategory_','') ;
			if (theid != catid){	
				var thecheckbox = Ext.get("checkcategory_"+theid) 
				thecheckbox.dom.checked = false ;
			}
		})
	});
	*/
	SearchRequestList.clear();
	
	var el = $('checkcategory_' + catid) ;
	if (el.getValue() == 'on') {
		SearchRequestList.add(catid,true) ;
	} else {
		SearchRequestList.removeKey(catid) ;
	}
	MxKartDoSearch() ;


};

function MxKartCreateSearchEngine(recid){
	
	
	var divsearch=Ext.get('search');
	if (!divsearch)
		return;
	SearchRequestList.clear();
	
	var request = {
		action : 'mxkartcreatesearchengine'
	} ;
	new Ajax.Request(mxkartws, {				
	  method: 'POST',
	  parameters : js2php(request),
	  onSuccess: function(transport) {
	  		$('mxkartsearchgroupandcategories').update(transport.responseText) ;
	  		if (recid != undefined && recid)
	  		{
	  		//	setTimeout( function() {
	  				//checkcategory_MOAS200810060859230000000332
	  				var _recid=recid;
		  			//alert("r="+_recid)
	  				if (Ext.get("checkcategory_"+ _recid)!=null)
	  				{
			  		Ext.get("checkcategory_"+ _recid).dom.checked=true;
			  		//Ext.get("checkcategory_MOAS200810060859230000000332").dom.checked=true;
		  			
		  			MxKartSearchCategory(_recid);
	  				}
		  			//alert(_recid);
		  	//	}, 1000);
	  			
	  		}
	  } 
	});
}





function MxKartCreateSearchEngineCat(recid){
	
	var divsearch=Ext.get('search');
	if (!divsearch)
		return;
	SearchRequestList.clear();
	
	var request = {
		action : 'mxkartcreatesearchengine'
	} ;
	new Ajax.Request(mxkartws, {				
	  method: 'POST',
	  parameters : js2php(request),
	  onSuccess: function(transport) {
	  		$('mxkartsearchgroupandcategories').update(transport.responseText) ;
	  		setTimeout( function() {
		  		Ext.get("checkcategory_"+ recid).dom.checked=true;
	  			MxKartSearchCategory(recid);
	  			//alert(recid);
	  		}, 1000);
	  		
	  } 
	});
}


