function roundNumber(rnum) { // Arguments: number to round, number of decimal places
  return Math.round(rnum*Math.pow(10,2))/Math.pow(10,2);
}

function number_format(number, decimals, dec_point, thousands_sep) {
  var n = !isFinite(+number) ? 0 : +number, 
  prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
  sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
  s = '',
  toFixedFix = function (n, prec) {
    var k = Math.pow(10, prec);
    return '' + Math.round(n * k) / k;        
  };
		
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }    
  return s.join(dec);
}

function my_number_format(number){
  if(parseInt(number)==number)return number_format(number,0,',','.');
  else return number_format(number,2,',','.');
}


function triiim(str) {
  var patern = /\(\d+\.?\d*,?\d*€\)/;
  var patern2 = /[\.\(\)€]/g;
  var numero = str.match(patern);
  if(numero==null)
    return parseInt('0');
  else
    return parseInt(numero[0].replace(patern2,''));
}

function sumaprecios(formulario){
	var precio=0;
	$(formulario).find("select option:selected").each(function(){
		precio += triiim($(this).text());
	});
	/*precio=String(precio);
	var patern3=/\d{4}$/;
	var pointer=patern3.exec(precio);
	if(pointer!=null){
		precio = precio.substr(0,parseInt(pointer['index'])+1) + '.' + precio.substr(parseInt(pointer['index'])+1);
	}*/
	return precio;
}

$(document).ready(function(){
	
	//add2cart effect noel 10/05/10
	if($("#ficha-producto form.ajax-cart-form").length){ //ficha producto
		$("#ficha-producto form.ajax-cart-form input:submit").bind("click",function(){
			$.add2cart($(this).parents("#product-form-price"),$("#block-uc_ajax_cart-0"));
		});
		
		//calculo de precios al vuelo 24/05/10
		$("#ficha-producto form select").each(function(){
			$(this).bind("change",function(){
				var precio=sumaprecios($(this).parents("form"));
                                $("#ficha-producto-precio").text(my_number_format(precio));

                                //dtos
                                if(dto!=1){
                                    $("#dto-box .precio-dto").text(my_number_format(precio * dto));
                                    $("#dto-box .ahorro").text(my_number_format(precio - precio * dto));
                                }
			});
		});

		$("#ficha-producto form select:first").trigger("change");
	}
	else if($(".view-custom-taxonomy-term form.ajax-cart-form").length){ //listado productos
		$(".view-custom-taxonomy-term form.ajax-cart-form input:submit").each(function(){
			$(this).bind("click",function(){
				$.add2cart($(this).parents("td").find(".product-image img"),$("#block-uc_ajax_cart-0"));
			});
		});
		
		//calculo de precios al vuelo 24/05/10
		$(".view-custom-taxonomy-term td form select").each(function(){
			$(this).bind("change",function(){
                                var preciofinal=sumaprecios($(this).parents("form"));

                                if($(this).parents("td").find(".dto").length>0){
                                    var view_products_list_dto = 1 - (parseInt($(this).parents("td").find(".dto span").text()) / 100);
                                    preciofinal=my_number_format(preciofinal * view_products_list_dto);
                                }


				$(this).parents("td").find("span.uc-price-product.uc-price-display.uc-price").text(preciofinal+'€');
			});
		});

		$(".view-custom-taxonomy-term td form").each(function(){
			$(this).find("select:first").trigger("change");
		});
	}

	if($("form#uc-cart-view-form input:checkbox").length){
		//añadimos los botones de eliminar producto
		$("form#uc-cart-view-form input:checkbox").hide().after('<img title="Eliminar producto" class="img-delete" src="/sites/all/themes/zen/zen/images/borrar.jpg" alt="eliminar" />');
		//añadimos el más y el menos
		$("form#uc-cart-view-form td input:text").attr("size","2").css({"vertical-align":"top" , "height":"26px" , "margin-right":"3px"}).after('<div class="mas-menos" style="width:18px;display:inline-block;"><img src="/sites/all/themes/zen/zen/images/mas.png" alt="+" style="cursor:pointer;" class="img-mas"/><img style="cursor:pointer;" src="/sites/all/themes/zen/zen/images/menos.png" alt="-" class="img-menos"/></div>');
		//añadimos el botón vaciar carro
		$("form#uc-cart-view-form td.subtotal").before('<td><img title="Vaciar carro" class="img-delete-all" src="/sites/all/themes/zen/zen/images/vaciar-carro.jpg" alt="vaciar" /></td>');
		//ocultamos el update submit
		$("form#uc-cart-view-form input#edit-update.form-submit").hide();
		
		$("form#uc-cart-view-form img.img-delete").each(function(){
			$(this).bind("click",function(e){
				$(this).prev().attr("checked","checked");
				//$(this).parents("form").trigger("submit");
				$(this).parents("form").find("input#edit-update.form-submit").trigger("click");
			});
		});
		
		$("form#uc-cart-view-form img.img-delete-all").bind("click",function(){
			$(this).parents("table").find("input:checkbox").attr("checked","checked");
			//$(this).parents("form").trigger("submit");
			$(this).parents("form").find("input#edit-update.form-submit").trigger("click");
		});
		
		$("form#uc-cart-view-form .mas-menos img.img-mas").each(function(){
			$(this).click(function(){
				var imput=$(this).parents("td").find("input:text");
				$(imput).val(parseInt($(imput).val()) + 1);
				$(this).parents("form").find("input#edit-update.form-submit").trigger("click");
			});
		});
		$("form#uc-cart-view-form .mas-menos img.img-menos").each(function(){
			$(this).click(function(){
				var imput=$(this).parents("td").find("input:text");
				$(imput).val(parseInt($(imput).val()) - 1);
				$(this).parents("form").find("input#edit-update.form-submit").trigger("click");
			});
		});
		$("form#uc-cart-view-form td input:text").each(function(){
			$(this).change(function(){
				$(this).parents("form").find("input#edit-update.form-submit").trigger("click");
			});
		});
	}
});

$(window).load(function(){ 
	if($("#content table.views-view-grid tr").length){
		//setTimeout(function(){
			$("#content table.views-view-grid tr").each(function(){
				var alto=0;
				$(this).find("td > div.node").each(function(){
					if($(this).height()>alto)alto=$(this).height();
				}).height(alto+40);
			});
		//},1000);
	}
});
