function VacanciesSearchModel(JSONVacancies, JSONFilters,utils, language, presearchModel) {

	this.allVacancies = JSONVacancies;
	this.allFilters = JSONFilters;
	this.utils = utils;
	this.language = language;
	
	this.vacatureFilterMatrix = "";
	
	this.presearchmodel = presearchModel;
	this.searchmodel= jQuery.extend(true, {}, this.presearchmodel);
	
	
	this.presearchids 	= "";
	this.searchids 		= "";
	this.filterids 		= "";	

	this.init();
	this.presearch();
	this.search();
} 




VacanciesSearchModel.prototype.init = function(){
		//alert('start');
	
	//Maak voor elke zoekparameter een 
	var matrix = new Array();
	$.each(this.allFilters.filters , function(index, filter){
		
		matrix[filter.filterid] = new Array();
		
		$.each(filter.options , function(index, option){

			matrix[filter.filterid][option.id] = new Array();
			
		});
	});
	
	//Vul de matrix met de juiste waarden
	$.each(this.allVacancies.vacancies , function(indexVac, vacancie){

		//alert("vacancie.features = "+vacancie.features)
		
		$.each(vacancie.features, function(index, feature){
			//
			//matrix[index][kenmerk]
			//console.log("feature = "+feature)
			//check of location 1 of 2 is gevuld en zet hem in var location
			if(index == "location1" || index == "location2"){
				if (feature != "" && feature != 'undefined'){
					//console.log("matrix[location][feature]"+matrix["location"][feature])
if(matrix["location"][feature] != null){
					matrix["location"][feature].push(indexVac);
}
				}
			}
			else if(index == "bedrijfsonderdeellogo"){
			
			}
			else if(feature != "" ){
				matrix[index][feature].push(indexVac);
				
			}

		});	
		
	});
	this.vacatureFilterMatrix = matrix;
}

VacanciesSearchModel.prototype.searchFilterIndex = function(filtername, id){
 	var mainObject = this;
	var result = 0;
	var found = false;
	
	$.each(mainObject.allFilters.filters, function(index, filter){
		
		if(filter.filterid == filtername ){
			
			$.each(filter.options, function(index, option){
				
				if(!found){
					result = result + 1;
				}
				
				if(option.id == id){
					found = true;
				}
			});
			
		}
	});
	
	return result;
}


VacanciesSearchModel.prototype.presearch = function(){	
	var tempArray = new Array();
	$.each(this.allVacancies.vacancies, function(index,vacature){
		tempArray.push(index);
	});
	this.presearchids = this.searchVacancieIds(tempArray,this.presearchmodel);
	//alert("presearchids:"+this.presearchids);
}

VacanciesSearchModel.prototype.search = function(){
	
	this.searchmodel = jQuery.extend(true, {}, this.presearchmodel);
	this.searchids   = this.presearchids.slice(0);	
	this.filterids 	 = this.presearchids.slice(0);
	this.setFilterValues();
}

VacanciesSearchModel.prototype.filter = function(){
	this.filterids = this.searchids.slice(0);	
	this.filterids = this.searchVacancieIds(this.filterids,this.searchmodel);	
	this.setFilterValues();
}

VacanciesSearchModel.prototype.searchVacancieIds= function(initialarray,searchmodel){
	var vacatureMatrix = this.vacatureFilterMatrix;
	var filteredvacancies = initialarray;
	//alert('a');
	$.each(searchmodel.filters , function(index, selectedFilter){	
		var filterArray = vacatureMatrix[selectedFilter.FILTERID][selectedFilter.FILTEROPTIONID];
		filteredvacancies = filteredvacancies.containsBoth(filterArray);				
	});
	return filteredvacancies;	
}

VacanciesSearchModel.prototype.setFilterValues = function(){

	var searchids = this.searchids;
	var vacatureMatrix = this.vacatureFilterMatrix;
	
	var searchmodel = this.searchmodel;
	var filters = this.allFilters;
	var filteredids = this.filterids;
	var me = this;
	
	$.each(filters.filters , function(index, filter){		
	
		
		//Get filtered list
		var tempSearchObject = jQuery.extend(true, {}, searchmodel);
		//remove this filter from temp searchobject
		tempSearchObject.addFilter(filter.filterid,"");
		
		var tempList = me.searchVacancieIds(searchids,tempSearchObject);
			
		//alert(tempList);
		$.each(filter.options , function(indexopt, option){	
			var filterArray = vacatureMatrix[filter.filterid][option.id];
			option.aantal = filterArray.containsBoth(tempList).length;
		});
		
	});
}

VacanciesSearchModel.prototype.getVacancies = function(){
	var result = new Array();
	
	result["vacancies"] = new Array();
	var alvacs = this.allVacancies;
	
	$.each(this.filterids, function(index,vacid){
		result["vacancies"].push(alvacs.vacancies[vacid]);
	});
	return result;	
}

VacanciesSearchModel.prototype.getFilters= function(){
	return this.allFilters;
}

Array.prototype.containsBoth = function(array){

	var temp = new Array();
	var result = new Array();
	$.each(this,function(index,item){
		temp[item] = true;
	});
	$.each(array,function(index,item){
		if (temp[item]){
			result.push(item);	
		}
	});
	return result;	
	temp = null;
}
VacanciesSearchModel.prototype.linkSettings = [
	{
	"searchResultLink" :"vacancy-result.jsp?menopt=vre"
	},
	{
	"vacancyDetaillink" : "vacancy-detail.jsp?"
	},
	{
	"searchResultSeoLink" : "vacancy-result-seo.html?"
	}
	
];
VacanciesSearchModel.prototype.LanguageSettings = [
	{
	"selectDefaultOption" :
		{
			"nl":"Maak een selectie",
			"en":"Make a selection"
		}
	},
	{
	"resultsFound" : 
		{
			"en":" vacancies found",
			"nl":" vacatures gevonden"
		}
	},
	{
	"resultsFoundFor" : 
		{
			"en":" vacancies found for",
			"nl":" vacatures gevonden voor"
		}
	},
	{
	"tableHeaders" :
		{
			"en":"<thead><th title='Select to sort on Nr. Click for Descending or Ascending' class='nr'>Nr.</th><th title='Select to sort on Function. Click for Descending or Ascending' class='function'>Function</th><th title='Select to sort on Date. Click for Descending or Ascending' class='date'>Date</th><th title='Select to sort on Location. Click for Descending or Ascending' class='region'>Location</th><th title='Select to sort on Company. Click for Descending or Ascending' class='company'>Company</th></thead>",
			"nl":"<thead><th title='Selecteer Nr. op volgorde. Klik heen en weer voor oplopend of aflopend' class='nr'>Nr.</th><th title='Selecteer Functie op volgorde. Klik heen en weer voor oplopend of aflopend' class='function'>Functie</th><th title='Selecteer Datum op volgorde. Klik heen en weer voor oplopend of aflopend' class='date'>Datum</th><th title='Selecteer Locatie op volgorde. Klik heen en weer voor oplopend of aflopend' class='region'>Locatie</th><th title='Selecteer Bedrijf op volgorde. Klik heen en weer voor oplopend of aflopend' class='company'>Bedrijf</th></thead>"
		}
	},
	{
	"pagerText" : 
		{
			"en":"<span class='prev' >PREVIOUS</span><span class='next'>NEXT</span>",
			"nl":"<span class='prev' >VORIGE</span><span class='next'>VOLGENDE</span>"
		}
	}
	
];
