
var list_prods_int = '';
list_prods_int += '<li class="custom-list li-list-products-int">';
list_prods_int += '	<div class="li-content-container">';
list_prods_int += '		<div class="list-prod-img" style="padding-top:5px;">Img</div>';
list_prods_int += '		<div class="list-prod-cat">Categoria</div>';
list_prods_int += '		<div class="list-prod-name">Prodotto</div>';
list_prods_int += '		<div class="list-prod-price"><a id="order-link" class="common" href="">Prezzo</a></div>';
list_prods_int += '	</div>';
list_prods_int += '</li>';

var filter_prods = '';
filter_prods += '<div class="list-price-filter">';
filter_prods += '	Filtra i prodotti per prezzo:';
filter_prods += '	da&nbsp;<input type="text" size="5" name="lpf_price_from" value="" /> &euro;';
filter_prods += '	&nbsp;a&nbsp;<input type="text" size="5" name="lpf_price_to" value="" /> &euro;';
filter_prods += '	<a id="price-filter-submit" class="rb-cart-link" style="font-size: 11px;" href="#">';
filter_prods += '		<strong>Filtra</strong>';
filter_prods += '	</a>';
filter_prods += '</div>';

function list_by_price_routines()
{
	if( document.location.toString().match( /ricerca\/ricerca\.html/ ) == null ) {
		if( $( ".list-pagination" ).length > 0 ) {
			$( ".list-pagination" ).eq( 0 ).before( filter_prods );

			var url = document.location.toString();
			var has_price_from = url.match( /price_from=([0-9]+((\.|,)[0-9]{2})?)?/ );
			var has_price_to = url.match( /price_to=([0-9]+((\.|,)[0-9]{2})?)?/ );
			if( has_price_from != null ) {
				$( "input[name='lpf_price_from']" ).val( has_price_from[1] );
			}
			if( has_price_to != null ) {
				$( "input[name='lpf_price_to']" ).val( has_price_to[1] );
			}

			$( "#price-filter-submit" ).click(
				function( e ) {
					filter_by_price( e )
				}
			);
			$( "input[name='lpf_price_from'],input[name='lpf_price_to']" ).bind(
				"keypress",
				function (event) {
					var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
					if (keyCode == 13) {
						filter_by_price( event )
						return false;
					}
				}
			);
		}
	}
}

function misc_list_routines()
{
	$( "li.li-list-product" ).eq( 0 ).before( list_prods_int );

	var order_link = $( "#order-link" );
	if( order_link.length > 0 ) {
		var url = document.location.toString();
		var has_query_string = url.match( /\?(.*)$/ );
		var has_order = url.match( /order_by=([a-z0-9_]+)/ );
		var has_direction = url.match( /direction=(ASC|DESC)/i );
		var href = $( "#order-link" ).attr( "href" );
		var direction = ( has_direction == null ) ? 'ASC' : ( has_direction[1] == 'ASC' ? 'DESC' : 'ASC' );
		var title = "Ordina i prodotti per prezzo ";
		title += ( direction == 'ASC' ) ? "crescente" : "decrescente";
		if( has_order == null ) {
			var _href = '?';
			if( has_query_string != null ) {
				_href += has_query_string[1] + '&'
			}
			_href += 'order_by=price&direction=' + direction;
		} else {
			var _href = url.replace( has_direction[1], direction );
		}

		order_link.attr( "href", _href ).attr( "title", title );

		$( ".list-pagination a.common" ).each(
			function() {
				if( has_order != null ) {
					var href = $( this ).attr( "href" );
					var new_href = href + '&order_by=price&direction=';
					new_href += ( has_direction == null ) ? 'ASC' : has_direction[1];
					$( this ).attr( "href", new_href );
				}
			}
		)
	}
	list_by_price_routines();
}

function filter_by_price( e )
{
	e.preventDefault();
	var price_from = $( "input[name='lpf_price_from']" );
	var price_to = $( "input[name='lpf_price_to']" );
	var pf_is_correct = price_from.val().match( /^[0-9]+((\.|,)[0-9]{2})?$/ );
	var pt_is_correct = price_to.val().match( /^[0-9]+((\.|,)[0-9]{2})?$/ );
	if( pf_is_correct || pt_is_correct ) {
		var url = document.location.toString();
		var has_query_string = url.match( /\?(.*)$/ );
		var new_url = url + ( has_query_string ? '&' : '?' );
		window.location.href = new_url + 'price_from=' + price_from.val() + '&price_to=' + price_to.val();
	}
}

function buy_prod(prod_id)
{
	var fields_num =  $("div#div-list-" + prod_id + "  select").length;
	var values = [];
	var errors = 0;
	var index = -1;
	$("div#div-list-" + 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() {
			$("#cf-prod-" + prod_id).parent().css("height","50px");
		}
	);
}

function close_all(prod_id)
{
	$(".custom-fields-el-list").each(
		function() {
			$(this).hide("fast",
				function() {
					$(this).parent().css("height","50px");
				}
			);
		}
	);
}

function field_self_close(a_el)
{
	close_fields_div(
		$( a_el ).parent().parent().parent().attr('id').replace('cf-prod-','')
	);
	return false;
}

var custom_fields_el = '<div id="cf-prod-FIELDS_ID" class="custom-fields-el-list">';
custom_fields_el += '<div class="custom-fields-el-list-prod-name">CHOOSE&nbsp;';
custom_fields_el += '&nbsp;&nbsp;<span class="close-cf-div">[<a href="#close-options" ';
custom_fields_el += 'onclick="return field_self_close(this)" title="Chiudi le opzioni">';
custom_fields_el += 'chiudi</a>]</span></div></div>';

var _img = new Image();
_img.src = SITE_PATH + 'img/bg-prod-offer.png';
var _img = new Image();
_img.src = SITE_PATH + 'img/icona video1.png';
$(document).ready(
	function() {
		$(".list-img-buy").click(
			function() {
				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;
				}

				var ord_minimo = parseInt(split_id[5]);
				var ord_multiplo = parseInt(split_id[6]);
				var quantity = parseInt(parseInt($("#prod-q-" + _id).val()));
				if(ord_minimo > 1) {
					var txt = "La quantit\xE0 minima acquistabile del prodotto \xE8 ";
					txt += ord_minimo + " pezzi e pu\xF2 essere acquistato solo a multipli di " + ord_multiplo;
					if(quantity < ord_minimo) {
						alert(txt);
						return false;
					}
					if(quantity > ord_minimo && (/*(quantity - ord_minimo)*/quantity % ord_multiplo) != 0) {
						alert(txt);
						return false;
					}
				}

				if(split_id[3] == 0) {
					buy_prod(_id);
					return true;
				}

				var prod_name = $(this).parent().children("div.list-prod-name").children("a.common").html();

				if($("#cf-prod-" + _id).css("display") == "block") {
					buy_prod(_id);
				} else if($("#cf-prod-" + _id).css("display") == "none") {
					close_all(_id);
					$("#cf-prod-" + _id).parent().css("height","95px");
					$("#cf-prod-" + _id).show("normal");
				} else {
					$.ajax({
						type: "POST",
						url: SITE_PATH + "ajax/jajax.php",
						data: "m=get_custom_fields&w=list&pid=" + _id,
						success: function(result) {
							if(result.match(/^Error:/)) {
								alert( result.replace("Error:","") );
								return;
							}
							if(result) {
								close_all(_id);
								$(_tmp_).parent().parent().css("height","95px")
								.append( custom_fields_el.replace("FIELDS_ID",_id)
								.replace("CHOOSE","Scegli le opzioni del prodotto " + prod_name) );

								$("#cf-prod-" + _id).append( result ).show("normal");
							}
						}
					});
				}
				return false;
			}
		);

		$( "li.li-list-product" ).each(
			function() {
				var prod_name_div = $( this ).find( "div.list-prod-name" );
				if( prod_name_div.html().match( /<!--HAS_VIDEO-->/ ) != null ) {
					prod_name_div.prepend( '<img style="float:left;padding-right:5px;" src="' + SITE_PATH + 'img/icona video1.png" alt="Il prodotto ha uno o pi&ugrave; video!" title="Il prodotto ha uno o pi&ugrave; video!" />' );
				}
				if( document.location.toString().match( /offerte\/lista-offerte/ ) == null ) {
					if( $( this ).find( "span.list-offer-price" ).length > 0 ) {
						//$( this ).css( "background", "url('" + SITE_PATH + "img/bg-prod-offer.png') repeat-x top left" );
						//$( this ).css( "background-color", "#fde8c2" );
						$( this ).append( '<div class="offer-text">Offerta!</div>' );
						$( this ).attr( "title", "Prodotto in Offerta!" );
					}
				}
			}
		);
		misc_list_routines();
	}
);
