
function buy_prod(prod_id)
{
	var p = $("#ul-" + prod_id).parent().parent();
	var href = $( p ).children("a.buy-prod").attr("href");
	var fields_num =  $("ul#ul-" + prod_id + "  select").length;
	var values = [];
	var errors = 0;
	var index = -1;
	$("ul#ul-" + prod_id + "  select").each(
		function() {
			if( $(this).val() == "" ) {
				errors++;
			} else {
				values[++index] = $(this).val();
			}
		}
	);
	add2cart(errors, values, prod_id);
}

function close_fields_div(prod_id)
{
	$("#cf-prod-" + prod_id).hide("normal");
}

function close_all(prod_id)
{
	$(".custom-fields-el-home").each(
		function() {
			$(this).hide("fast");
		}
	);
}

var buy_img = '';
var custom_fields_el = '<div id="cf-prod-FIELDS_ID" class="custom-fields-el-home">';
custom_fields_el += '<div class="custom-fields-el-home-prod-name"></div></div>';
$(document).ready(
	function() {
		$(".img-buy").click(
			function() {
				if(buying_process_active == true) return;
				var _tmp_ = this;
				var split_id = this.id.split("__SEP__");
				var _id = split_id[1];
				if(split_id[2] == 0 && split_id[4] < 1) {
					alert("Il prodotto non \xE8 disponibile");
					return false;
				}
				if(split_id[3] == 0) {
					buy_prod(_id);
					return true;
				}

				if($("#cf-prod-" + _id).css("display") == "block") {
					buy_prod(_id);
				} else if($("#cf-prod-" + _id).css("display") == "none") {
					close_all(_id);
					$("#cf-prod-" + _id).show("normal");
				} else {
					$.ajax({
						type: "POST",
						url: SITE_PATH + "ajax/jajax.php",
						data: "m=get_custom_fields&w=home&pid=" + _id,
						success: function(result) {
							if(result.match(/^Error:/)) {
								alert( result.replace("Error:","") );
								return;
							}
							if(result) {
								close_all(_id);
								$(_tmp_).parent().parent().append( custom_fields_el.replace("FIELDS_ID",_id) );
								$("#cf-prod-" + _id + " > .custom-fields-el-home-prod-name")
									.html( $( $(_tmp_).parent().parent() ).children(".prod-name").children(".product").html() );
								$("#cf-prod-" + _id).append( result ).show("normal");
							}
						}
					});
				}
				return false;
			}
		);
		$(".prod-details").click(
			function() {
				return !buying_process_active;
			}
		);
	}
);
