//load zoekopdracht 

function VacancyMain(pageSort, lang, vacancyJsonUrl, filterJsonUrl) {
	this.pageSort = pageSort;
	this.language = lang;
    this.presearchModel;
    this.vacanciesSearchModel;
	this.vacancyJsonUrl = vacancyJsonUrl;
	this.filterJsonUrl = filterJsonUrl;
    this.searchView;
	this.vacancyView;
	this.filtersView;
	this.currentPage;
 	var mainObject = this;
mainObject.utils = new Utils();
		
	//Load the json objects
    $.getJSON(mainObject.vacancyJsonUrl + '?lang=' + mainObject.language, function(JSONVacature){
		$.getJSON(mainObject.filterJsonUrl, function(JSONFilters){
			//Setup the models
			mainObject.presearchModel = new SearchModel();
			
			

 			mainObject.vacanciesSearchModel = new VacanciesSearchModel(JSONVacature,JSONFilters,mainObject.utils,mainObject.language,mainObject.presearchModel);

			//Setup the Views
			if(mainObject.pageSort == "result" || mainObject.pageSort == "resultlanding"){
				
				mainObject.searchView = new SearchView(mainObject.vacanciesSearchModel);
				mainObject.vacancyView = new VacanciesView('#searchresults',mainObject.vacanciesSearchModel);
				mainObject.filtersView = new FiltersView('filter',mainObject.vacanciesSearchModel);
				mainObject.initResultEvents();
				mainObject.initTabSelector("#tabs");
				mainObject.initToggleExtraInfo("#extrainfo");
				

			}
			else if(mainObject.pageSort == "homepage"){

				mainObject.searchView = new SearchView(mainObject.vacanciesSearchModel);
				//mainObject.expertiseView = new JobsListView('expertise', mainObject.vacanciesSearchModel);
				//mainObject.expertiseView = new JobsListView('location', mainObject.vacanciesSearchModel);
				mainObject.initHomepageEvents();
				
				mainObject.initTabSelector("#tabs");
				mainObject.initListTabSelector("#listtab");
				mainObject.utils.eraseCookie("searchFilters");
				mainObject.utils.eraseCookie("hideSeo");
			}
			if(mainObject.utils.readCookie("searchFilters") ){
				mainObject.setFilterValueFromCookie();
			}
			else{
				if(mainObject.pageSort == "resultlanding"){
					$(mainObject).trigger("initialised");
				}
				
			}
			
		}); 
 	}); 

}
VacancyMain.prototype.initHomepageEvents = function(){
	
	var mainObject = this;
	//this.initTabSelector();
 	$(".filterselect").change(function(){ 
 		var params = $(this).val().split("_");
		mainObject.addSelectFilter(params[0],params[1])
		return false;
	}); 
	
	$("#searchbutton").click(function(e){
		var resultPageLink =  mainObject.vacanciesSearchModel.linkSettings[0].searchResultLink;
		mainObject.createFilterCookie();
		window.location.href = resultPageLink;
	});

	$(".groupUrl").click(function(e){
		
		var resultPageLink =  mainObject.vacanciesSearchModel.linkSettings[2].searchResultSeoLink;
		var params = e.target.id.split("_");	
		mainObject.addSelectFilter(params[0],params[1])
		
		
		if(params[0] == "expertise"){
			
			$("select", $("#expertise"))[0].selectedIndex = mainObject.vacanciesSearchModel.searchFilterIndex(params[0],params[1]);
		}
		if(params[0] == "location"){
			$("select", $("#location"))[0].selectedIndex = mainObject.vacanciesSearchModel.searchFilterIndex(params[0],params[1]);
		}
		
		mainObject.createFilterCookie();
		window.location.href = resultPageLink;
	});
 
}
VacancyMain.prototype.initResultEvents = function(){
	
	var mainObject = this;

 	$(".filterselect").change(function(){
 		var params = $(this).val().split("_");
		mainObject.addSelectFilter(params[0],params[1])
		return false;	    
	});
	
	$("#searchbutton").click(function(e){
		if(mainObject.pageSort == "resultlanding"){
			var resultPageLink =  mainObject.vacanciesSearchModel.linkSettings[0].searchResultLink;
			mainObject.createFilterCookie();
			window.location.href = resultPageLink;
		}
		else{
		    mainObject.utils.createCookie("hideSeo","1" );
            $("#htmlmodule").css("display","none");
			mainObject.vacanciesSearchModel.search();	
			mainObject.renderAllResults();
	 		mainObject.createFilterCookie();
		}

		return false;	    
	});
	
 	$("#searchinput").keyup(function(){
		//console.log("input value ="+this.value)
		//mainObject.utils.eraseCookie("searchFilters")
		return true;	    
	});

 	$(".filteroption").live("click", function(){  	
 		var params = $(this).attr('name').split("_");
		mainObject.addFilter(params[0],params[1])	
		window.top.location ="#";
		if(mainObject.pageSort == "resultlanding"){
			$(mainObject).trigger("filterChange");
		}
		mainObject.createFilterCookie();
	});
	//bind the getCurrentPage function when no cookie exists otherwise binding is in setFilterValueFromCookie function
	$("#searchresults table").bind("getCurrentPage",function(e,currentPage) { 
		mainObject.currentPage = currentPage;
		mainObject.createFilterCookie();

	});
	
}
VacancyMain.prototype.setFilterValueFromCookie = function(){
	var mainObject = this;
	var cookieValues = mainObject.utils.readCookie("searchFilters");
	
	//console.log("cookieValues = "+cookieValues)
	cookieValues = cookieValues.split("&");
	var cookieValue;
	for (var i = cookieValues.length - 1; i >= 0; i--){
		if(cookieValues[i]){
			cookieValue = cookieValues[i].split("=");
			//set the filters for the presearch (select values)
			if(cookieValue[0] =="expertise" || cookieValue[0] =="location"){
				mainObject.addSelectFilter(cookieValue[0],cookieValue[1])
			}
			//Set select values
			else if(cookieValue[0] == "selectedIndex1"){
				mainObject.setSelectedIndex("expertise",cookieValue[1])
				//$("select", $("#expertise"))[0].options[cookieValue[1]].selected = true;
				
			}
			else if(cookieValue[0] =="selectedIndex2"){
				mainObject.setSelectedIndex("location",cookieValue[1])
				//$("select", $("#location"))[0].options[cookieValue[1]].selected = true;
			}
		}
		
	};

	
	if(mainObject.pageSort == "result" || mainObject.pageSort == "resultlanding"){
		//Loop through the selected filter values
		for (var i = cookieValues.length - 1; i >= 0; i--){
			if(cookieValues[i]){
				cookieValue = cookieValues[i].split("=")
				if(cookieValue[0] == "career" || cookieValue[0] == "education" || cookieValue[0] == "business"){
					mainObject.addFilter(cookieValue[0],cookieValue[1])		
				}
				else if(cookieValue[0] == "currentPage" ){
					var currentPage = cookieValue[1];
					if(cookieValue[1] == 'undefined' ) currentPage = 0;
				}
			}
		}
		mainObject.renderAllResults();

		$("#searchresults table").trigger("setCurrentPage",currentPage);
		$("#searchresults table").bind("getCurrentPage",function(e,currentPage) { 
			mainObject.currentPage = currentPage;
			$(mainObject).trigger("filterChange");
			mainObject.createFilterCookie();
		});


	}
		
}
VacancyMain.prototype.setSelectedIndex = function(obj,value){
	var container = "#"+obj;
	var select = $("select", $(container))[0];
	if(select != null){
		select.options[value].selected = true;
	}
	
}
VacancyMain.prototype.addSelectFilter = function(obj,value){
	var mainObject = this;
	//console.log("obj = "+ obj +" value = "+ value)
	mainObject.vacanciesSearchModel.presearchmodel.addFilter(obj,value,false);
	//set the presearch in the model & view
	mainObject.vacanciesSearchModel.presearch();
	mainObject.searchView.renderResults();
	mainObject.vacanciesSearchModel.search();
	

}
VacancyMain.prototype.renderAllResults = function(){
	var mainObject = this;
	
	//mainObject.searchView.renderResults();
	mainObject.filtersView.renderResults();
	mainObject.vacancyView.renderResults();
	

	
}
VacancyMain.prototype.addFilter = function(obj,value){
	var mainObject = this;
	
	mainObject.vacanciesSearchModel.searchmodel.addFilter(obj,value,true);	
	//set the search in the model & view
	mainObject.vacanciesSearchModel.filter();
	mainObject.renderAllResults();
}
VacancyMain.prototype.createFilterCookie = function(){

	var mainObject = this;
	var cookieValue ="";

	var resultsFilters = mainObject.vacanciesSearchModel.getFilters();
	var searchModel = mainObject.vacanciesSearchModel.searchmodel;
	var presearchmodel = mainObject.vacanciesSearchModel.presearchmodel;
	
	mainObject.utils.eraseCookie("searchFilters")
	
	//Get active filters add values to cookieValuestring
	$.each(resultsFilters.filters, function(index, filter){
		$.each(filter.options, function(index, filteroptie){
			//console.log(filteroptie.name)
			if (presearchmodel.filterOptionExists(filter.filterid,filteroptie.id,false)){
				//console.log("presearchmodel filter = "+filter.filterid+"="+filteroptie.id)
				cookieValue +=filter.filterid+"="+filteroptie.id+"&"
			}
			if (searchModel.filterOptionExists(filter.filterid,filteroptie.id,true)){
				//console.log("searchModel filter = "+filter.filterid+"="+filteroptie.id)
				cookieValue +=filter.filterid+"="+filteroptie.id+"&"
			}
		});
	});
	//Get the selected index from the selects & add them to cookieValue string
	cookieValue += "selectedIndex1="+ $("select", $("#expertise"))[0].selectedIndex +"&";
	cookieValue += "selectedIndex2="+ $("select", $("#location"))[0].selectedIndex +"&";
	cookieValue += "currentPage="+mainObject.currentPage;
	//create the Cookie searchFilters
	mainObject.utils.createCookie("searchFilters",cookieValue )
	

	
}
VacancyMain.prototype.initTabSelector = function(container){
	var container = $(container)[0] 
	$(".tab1",container).addClass('active');
	$(".tab2",container).removeClass('active');
	$("#tabs-2",container).css("display","none")

	$(".tab1",container).click(function(e){
		e.preventDefault();
		$(".tab1",container).addClass('active');
		$(".tab2",container).removeClass('active');
		$("#tabs-1",container).fadeIn("fast");
		$("#tabs-2",container).fadeOut("fast");

	});
	$(".tab2",container).click(function(e){
		e.preventDefault();
		$(".tab1",container).removeClass('active');
		$(".tab2",container).addClass('active');
		$("#tabs-1",container).fadeOut("fast");
		$("#tabs-2",container).fadeIn("fast");
	});
}
VacancyMain.prototype.initListTabSelector = function(container){
	var container = $(container)[0];
	$(".listtab > ul > li > a:first",container).addClass('active');
	$(".jobsindex",container).css("display","none")
	$(".jobsindex:first",container).css("display","block")
	$(".listtab > ul > li > a",container).click(function(e,index){
		e.preventDefault();
		$(".listtab > ul > li > a",container).removeClass('active');
		$(this).addClass('active');
		$(".jobsindex",container).fadeOut("fast");
		$("#"+this.href.split("#")[1],container).fadeIn("fast");
		$(".listcontainer").css("height",($("#"+this.href.split("#")[1],container).height()+30)+"px")
	});
	/*
	$(".tab2",container).click(function(e){
		e.preventDefault();
		$(".tab1",container).removeClass('active');
		$(".tab2",container).addClass('active');
		$("#tabs-1",container).fadeOut("fast");
		$("#tabs-2",container).fadeIn("fast");
	});
	*/
}
VacancyMain.prototype.initToggleExtraInfo = function(container){
	var container = $(container)[0];
	var mainObject = this;

	//$(".toggle",container).addClass('active');
	var closed = false;
	$(".toggleextrainfo").click(function(e,index){
		e.preventDefault();
		toggle();
	});
	
	var closeLabel = "Sluiten";
	if(mainObject.language == "en"){
		closeLabel = "Close";
	}
	
	var closeLink = document.createElement("a");
	closeLink.appendChild( document.createTextNode( closeLabel ) );
	closeLink.setAttribute('href', '#');
	//closeLink.setAttribute('class','close');
	closeLink.className = 'close';

		
	var extra = document.getElementById("extrainfo");
        if(extra != null){
        	extra.appendChild(closeLink);
              //  alert("child appended: " + closeLink.className);
        }
	
	$(".close",container).click(function(e){
		e.preventDefault();
		toggle();
	});
	

	$(mainObject).bind("filterChange",function(e) {
		closeExtraInfo();
	});
	var closeExtraInfo = function(){
		$(".right",container).css("display","none");
		if(container){
			$(container).slideUp("fast");
		}
		closed = true;
		
	}
	function toggle(){
		if(closed){
			$(".toggleextrainfo").addClass("toggleextrainfoactive")
			//$(".right",container).fadeIn("slow");
			$(".right",container).css("display","block");
			$(container).slideDown("fast");
			closed = false;
		}
		else{
			$(".toggleextrainfo").removeClass("toggleextrainfoactive")
			//$(".right",container).fadeOut("fast");
			$(".right",container).css("display","none");
			$(container).slideUp("fast");
			closed = true;
		}
		
	}
}

