var FilterForm = function() {
	this.basePath = "";
	
	this.baseDropdownOptions = {
		emptyText: "--- Vyberte ---",
	    icon: {},
	    width: 175,
	    maxDropHeight: 300
	};
	
	this.AREA_FULL = 0;
	this.AREA_OFFICE = 1;
	this.AREA_AGENT = 2;
	
	this.advancedFilterInitialized = false;
	this.area = this.AREA_FULL;
	this.agent = 0;
	this.office = "";
	
	
};


/**
 * Initialize filter form
 * @param basePath
 */
FilterForm.prototype.init = function(basePath) {
	this.basePath = basePath;
	
	this.initDropdownOptions();
	this.initDropdowns();
	
	var selected = $('input[name="area"]:checked');
	if(selected.attr("id") == "area_full") {
		filterForm.area = filterForm.AREA_FULL;
	} else if(selected.attr("id") == "area_office") {
		filterForm.area = filterForm.AREA_OFFICE;
		filterForm.office = selected.val();
	} else if(selected.attr("id") == "area_agent") {
		filterForm.area = filterForm.AREA_AGENT;
		filterForm.agent = selected.val();
	}
	
	
	
	// office/agent search form - search range(area) selection
    $('input[name="area"]').change(function() {
    	var selected = $('input[name="area"]:checked');
    	if(selected.attr("id") == "area_full") {
    		$(".search_form form").attr("action", "sk/nehnutelnosti/hladat");
    		filterForm.area = filterForm.AREA_FULL;
    		filterForm.updateLists();
    	} else if(selected.attr("id") == "area_office") {
    		$(".search_form form").attr("action", selected.val() + "/ponukame");
    		filterForm.area = filterForm.AREA_OFFICE;
    		filterForm.office = selected.val();
    		filterForm.updateLists();
    	} else if(selected.attr("id") == "area_agent") {
    		$(".search_form form").attr("action", selected.val() + "/ponukam");
    		filterForm.area = filterForm.AREA_AGENT;
    		filterForm.agent = selected.val();
    		filterForm.updateLists();
    	}
    });
    
};



FilterForm.prototype.initDropdowns = function() {
	$("#district").dropdownchecklist(this.districtsDropdownOptions);
    $("#county").dropdownchecklist(this.countiesDropdownOptions);
                    
    $("#type, #city, #transaction-query").dropdownchecklist(this.baseDropdownOptions);
};

FilterForm.prototype.initAdvanced = function() {
	if(this.advancedFilterInitialized == false) {
		$("#office").dropdownchecklist(this.officesDropdownOptions);
		$("#transaction, #project, #user").dropdownchecklist(this.baseDropdownOptions);
		this.advancedFilterInitialized = true;
	}
};


/**
 * Initialize select box settings
 */
FilterForm.prototype.initDropdownOptions = function() {
	this.citiesDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
		icon: this.baseDropdownOptions.icon,
		width: this.baseDropdownOptions.width,
		maxDropHeight: this.baseDropdownOptions.maxDropHeight
	};

	this.countiesDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
	    icon: this.baseDropdownOptions.icon,
	    width: this.baseDropdownOptions.width,
	    maxDropHeight: this.baseDropdownOptions.maxDropHeight,
	    onComplete: this.countiesDropdownCompleteHandler
	          
	};

	this.districtsDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
		icon: this.baseDropdownOptions.icon,
		width: this.baseDropdownOptions.width,
		maxDropHeight: this.baseDropdownOptions.maxDropHeight,
		onComplete: this.districtsDropdownCompleteHandler	
	};
	
	this.typesDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
		icon: this.baseDropdownOptions.icon,
		width: this.baseDropdownOptions.width,
		maxDropHeight: this.baseDropdownOptions.maxDropHeight
	};
	
	this.officesDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
		icon: this.baseDropdownOptions.icon,
		width: this.baseDropdownOptions.width,
		maxDropHeight: this.baseDropdownOptions.maxDropHeight,
		onComplete: this.officesDropdownCompleteHandler
	};
	
	this.usersDropdownOptions = {
		emptyText: this.baseDropdownOptions.emptyText,
		icon: this.baseDropdownOptions.icon,
		width: this.baseDropdownOptions.width,
		maxDropHeight: this.baseDropdownOptions.maxDropHeight
	};
};


/**
 * Get selected values from the select box
 * @param selector
 * @returns {String}
 */
FilterForm.prototype.getSelectedValues = function(selector) {
	
	if(selector == null || selector.options == null) {
		return "";
	}
	
	var selectedValues = "";
	for( var i = 0; i < selector.options.length; i++ ) {
		if(selector.options[i].selected) {
			if(selectedValues != "") {
				selectedValues += "-"; 
			}
			selectedValues += selector.options[i].value;
		}
	}
	
	return selectedValues;
};


/**
 * Clear select box
 * @param id
 * @param options
 */
FilterForm.prototype.clearSelectBox = function(id, options) {
	$("#" + id).dropdownchecklist("destroy");
	$("#" + id).html("<option></option>");
	$("#" + id).dropdownchecklist(options);
};


/**
 * Replace select box with new select box defined by html
 * @param id
 * @param html
 * @param options
 */
FilterForm.prototype.replaceSelectBox = function(id, html, options) {
	$("#" + id).dropdownchecklist("destroy");
	$("#" + id).replaceWith(html);
	$("#" + id).dropdownchecklist(options);
};


/**
 * Call ajax 
 * @param path
 * @param postData
 * @param successCallback
 * @param errorCallback
 */
FilterForm.prototype.ajaxCall = function(path, postData, successCallback, errorCallback) {
	
	
	$.ajax({
		type: 'POST',
		data: postData,
		url: "sk/" + path,
		success: function(html)
		{ 
			html = jQuery.trim(html);
			if(html.substr(0, 5) == "ERROR") {
				errorCallback();
			} else {
				successCallback(html);
			}
		},
		error: errorCallback
	});
};



FilterForm.prototype.updateLists = function() {
	filterForm.updateEstateTypeList();
	filterForm.updateDistrictList();
};


/**
 * Update cities select box
 * @param selectedCounties
 * @param selectedCities
 */
FilterForm.prototype.updateCityList = function(selectedCounties, selectedCities) {
	var postData = "country=1";
	if(selectedCounties.length > 0) {
		postData += "&county=" + selectedCounties; 
	}
	if(selectedCities.length > 0) {
		postData += "&city=" + selectedCities;
	}
		
	if(filterForm.area == filterForm.AREA_AGENT) {
		postData += "&agent=" + filterForm.agent;
	} else if(filterForm.area == filterForm.AREA_OFFICE) {
		postData += "&office=" + filterForm.office;
	}
		
	var successFunction = function(html) {
		filterForm.replaceSelectBox("city", html, filterForm.citiesDropdownOptions);
	};
	
	var errorFunction = function() {
		filterForm.clearSelectBox("city", filterForm.citiesDropdownOptions);
	};
	
	filterForm.ajaxCall("nehnutelnosti/mesta", postData, successFunction, errorFunction);
	        	
};



/**
 * On counties select box selection change
 * @param selector
 */
FilterForm.prototype.countiesDropdownCompleteHandler = function(selector) {
			
	var selectedCounties = filterForm.getSelectedValues(selector);
	var selectedCities = filterForm.getSelectedValues($("#city").get(0));
	
	filterForm.updateCityList(selectedCounties, selectedCities);
};


/**
 * Update county select box
 * @param selectedDistricts
 * @param selectedCounties
 * @param selectedCities
 */
FilterForm.prototype.updateCountyList = function(selectedDistricts, selectedCounties, selectedCities) {
	var postData = "country=1";
	if(selectedDistricts.length > 0) {
		postData += "&district=" + selectedDistricts;
	}
	if(selectedCounties.length > 0) {
		postData += "&county=" + selectedCounties;
	}
	
	if(filterForm.area == filterForm.AREA_AGENT) {
		postData += "&agent=" + filterForm.agent;
	} else if(filterForm.area == filterForm.AREA_OFFICE) {
		postData += "&office=" + filterForm.office;
	}
	
	
			
	var successFunction = function(html) {
		filterForm.replaceSelectBox("county", html, filterForm.countiesDropdownOptions);
		filterForm.clearSelectBox("city", filterForm.citiesDropdownOptions);
	
		selectedCounties = filterForm.getSelectedValues($("#county").get(0));    			
		if(selectedCounties.length > 0) {
			filterForm.updateCityList(selectedCounties, selectedCities);
		}
	};
	
	var errorFunction = function() {
		filterForm.clearSelectBox("county", filterForm.countiesDropdownOptions);
		filterForm.clearSelectBox("city", filterForm.citiesDropdownOptions);
	};
	
	filterForm.ajaxCall("nehnutelnosti/okresy", postData, successFunction, errorFunction);
		
};


/**
 * On district select box change
 * @param selector
 */
FilterForm.prototype.districtsDropdownCompleteHandler = function(selector) {
	
	var selectedDistricts = filterForm.getSelectedValues(selector);
	var selectedCounties = filterForm.getSelectedValues($("#county").get(0));
	var selectedCities = filterForm.getSelectedValues($("#city").get(0));
	
	filterForm.updateCountyList(selectedDistricts, selectedCounties, selectedCities);
};



FilterForm.prototype.updateDistrictList = function() {
	var postData = "country=1";
		
	if(filterForm.area == filterForm.AREA_AGENT) {
		postData += "&agent=" + filterForm.agent;
	} else if(filterForm.area == filterForm.AREA_OFFICE) {
		postData += "&office=" + filterForm.office;
	}
	
	selectedDistricts = filterForm.getSelectedValues($("#district").get(0)); 
	var selectedCounties = filterForm.getSelectedValues($("#county").get(0));
	var selectedCities = filterForm.getSelectedValues($("#city").get(0));
	
	if(selectedDistricts.length > 0) {
		postData += "&district=" + selectedDistricts;
	}
	
	
	
	var successFunction = function(html) {
		filterForm.replaceSelectBox("district", html, filterForm.districtsDropdownOptions);
		filterForm.clearSelectBox("county", filterForm.countiesDropdownOptions);
		filterForm.clearSelectBox("city", filterForm.citiesDropdownOptions);
	
		selectedDistricts = filterForm.getSelectedValues($("#district").get(0));    			
		if(selectedDistricts.length > 0) {
			filterForm.updateCountyList(selectedDistricts, selectedCounties, selectedCities);
		}
	};
	
	var errorFunction = function() {
		filterForm.clearSelectBox("district", filterForm.districtsDropdownOptions);
		filterForm.clearSelectBox("county", filterForm.countiesDropdownOptions);
		filterForm.clearSelectBox("city", filterForm.citiesDropdownOptions);
	};
	
	filterForm.ajaxCall("nehnutelnosti/kraje", postData, successFunction, errorFunction);
	
};


FilterForm.prototype.updateEstateTypeList = function() {
	
	var selectedEstateTypes = filterForm.getSelectedValues($("#type").get(0));
	
	var postData = "country=1";
		
	if(selectedEstateTypes.length > 0) {
		postData += "&type=" + selectedEstateTypes;
	}
	
	if(filterForm.area == filterForm.AREA_AGENT) {
		postData += "&agent=" + filterForm.agent;
	} else if(filterForm.area == filterForm.AREA_OFFICE) {
		postData += "&office=" + filterForm.office;
	}
	
	
	
	var successFunction = function(html) {
		filterForm.replaceSelectBox("type", html, filterForm.typesDropdownOptions);
	};
	
	var errorFunction = function() {
		filterForm.clearSelectBox("type", filterForm.typesDropdownOptions);
	};
	
	filterForm.ajaxCall("nehnutelnosti/typy", postData, successFunction, errorFunction);
	
	
	
};


FilterForm.prototype.updateUsersList = function(selectedOffices) {
	var postData = "";
	if(selectedOffices.length > 0) {
		postData += "&office=" + selectedOffices;
	}
	
			
	var successFunction = function(html) {
		filterForm.replaceSelectBox("user", html, filterForm.usersDropdownOptions);
	};
	
	var errorFunction = function() {
		filterForm.clearSelectBox("user", filterForm.countiesDropdownOptions);
	};
	
	filterForm.ajaxCall("makleri/makleri", postData, successFunction, errorFunction);
};


FilterForm.prototype.officesDropdownCompleteHandler = function(selector) {
	
	var selectedOffices = filterForm.getSelectedValues(selector);
	
	filterForm.updateUsersList(selectedOffices);
};



var filterForm = new FilterForm();

/*
$(document).ready(function() {
	
	
	
    
});*/



