function JobFilter(filter_url, options){

    var joinQueryParam = function(param, value){
	return [param, value].join("=");
    }

    this.filter_url = filter_url;
    
    if (!this.filter_url) { throw "Filter Url is not defined"; }


    this.province = options.province || null;
    this.country = options.country || null;
    this.contract = options.contract || null;
    this.telecommute = options.telecommute || null;
    this.parttime = options.parttime || null;

    this.tags = Array();
    if (options.tag) { this.tags.push(options.tag); }


    this.byCountry = function(code, elem){
	if (!$(elem).hasClass('active')){
	    this.province = null;
	    $("li.option_root li.country span").removeClass('active');
	    $("li.option_root li.province span").removeClass('active');
	    this.country = code;
	}
	else {
	    this.country = null;
	}
	$(elem).toggleClass('active');
	this.getFilteredResults();
    }
    
    this.byTag = function(tag, elem){
	var idx = this.tags.indexOf(tag);
	if (idx == -1){
	    this.tags.push(tag);
	}
	else {
	    this.tags.splice(idx, 1);
	}
	$(elem).toggleClass('active');
	this.getFilteredResults();
    }
    
    this.getFilteredResults = function(){
	
	filter_qs = new Array();
	if (this.contract !== null){
	    filter_qs.push(joinQueryParam("contract", this.contract));
	}
	if (this.telecommute !== null){
	    filter_qs.push(joinQueryParam("telecommute", this.telecommute));
	}
	if (this.province !== null){
	    filter_qs.push(joinQueryParam("province", this.province));
	}
	if (this.country !== null){
	    filter_qs.push(joinQueryParam("country", this.country));
	}
	if (this.parttime !== null){
	    filter_qs.push(joinQueryParam("parttime", this.parttime));
	}
	if (this.tags.length != 0){
	    filter_qs.push(joinQueryParam("tag", this.tags.join("+")));
	}

	var url = [this.filter_url, "?", filter_qs.join("&")].join("");

	$("#main").load(url);

	return;

    }


    
    
}


/* FIXME: Those should be functions on the JobFilter Object */

function filterByProvince(province, country, elem){
    if (!$(elem).hasClass('active')){
	TheFilter.country = country;
	$("li.option_root li.country span").removeClass('active');
	$("li.option_root li.province span").removeClass('active');
	TheFilter.province = province;
    }
    else {
	TheFilter.country = null
	TheFilter.province = null;
    }

    TheFilter.getFilteredResults();
    $(elem).toggleClass('active');
}

function filterPartTime(elem){
    TheFilter.parttime = elem.checked || null;
    TheFilter.getFilteredResults();
}


function filterTelecommute(elem){
    TheFilter.telecommute = elem.checked || null;
    TheFilter.getFilteredResults();
}


function filterContract(elem){
    TheFilter.contract = elem.checked || null;
    TheFilter.getFilteredResults();
}

