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

function requestMarketOffersAjax(id, action_type) {
	hideMarketButtons(id, action_type);
	var amount = 0;
	var price = 0;
	if (action_type != 'none' && action_type != 'remove') {
		amount = $j("input#amount_"+id).val();
		price = $j("input#price_"+id).val();
		price = price.replace(".", ":");
	}
	var company_id = $j("input#company_id").val();
	var url_page = "/"+$j("input#sf_culture").val()+"/market_offer_ajax/";
	url_page += $j("#_token").val()+"/"+company_id+"/";
	url_page += id+"/"+amount+"/"+price+"/"+action_type;
		$j.ajax({
			type: 'GET',
			url: url_page,
			dataType: 'html',
			success: function(html) {
				if (html == 'redirect') {
					window.location.reload();
				} else {
					if (html.substr(0, 7) == "stock--") {
						var position = html.indexOf("--stock");
						var stock = html.substr(7, position-7);
						html = html.substr(position+7, html.length);
						$j("span#number_of_products").html(stock);
					}
					$j('div#market_offers').empty();
					$j('div#market_offers').html(html);
				}
			}
		});
}
function hideMarketButtons(id, action_type) {
	if (action_type == 'new') {
		$j("span.new_market_button_"+id).css("visibility", "hidden");
	}
	if (action_type == 'update' || action_type == 'remove') {
		$j("span.market_buttons_"+id).css("visibility", "hidden");
	}

}