$j(document).ready(function() {
	requestJobOffersAjax(0, 'none');
});

function addJobOffer() {
	if ($j("tr#job_table_header").css('display') == 'none') {
		$j("tr#job_table_header").css('display', '');
	}
	$j("td.new_job_offer").show();
	$j("span#add_job_offer").hide();
}

function requestJobOffersAjax(job_id, action_type) {
	hideButtons(job_id, action_type);
	var company_id = $j("input#company_id").val();
	if (action_type != 'none' && action_type != 'apply') {
		var nr 	       = $j("input#number_"+job_id).val();
		var skill      = $j("select#skill_"+job_id).val();
		var salary     = $j("input#salary_"+job_id).val().replace(".", ":");
	} else {
		nr = 0;
		skill = 0;
		salary = 0;
	}
	var url_page = "/"+$j("input#sf_culture").val()+"/job_offer_ajax/";
	url_page += $j("#_token").val()+"/"+action_type+"/";
	url_page += nr+"/"+skill+"/"+salary+"/"+company_id+"/"+job_id;
		$j.ajax({
			type: 'GET',
			url: url_page,
			dataType: 'html',
			success: function(html) {
				if (html == 'redirect') {
					window.location.reload();
				} else {
					$j('div#job_offers').empty();
					$j('div#job_offers').html(html);
				}
			}
		});
}

function hideButtons(job_id, action_type) {
	if (action_type == 'new' || action_type == 'none') {
		$j("span.new_job_button").css("visibility", "hidden");
	}
	if (action_type == 'update' || action_type == 'remove') {
		$j("span.job_buttons_"+job_id).css("visibility", "hidden");
	}
	if (action_type == 'apply') {
		$j("span.apply_to_job_"+job_id).css("visibility", "hidden");
	}

}
