$(document).ready(function (){
//botones
$("a[class='sub_form']").click(function (event){
	event.preventDefault();
	$("#"+$(this).attr('val')).submit();
});

//botones carrito
$("a[class='act_cesta']").click(function (event){ //actualizar cesta
	event.preventDefault();
	$("#form_carro").submit();
});

$("a[class='imp_cesta']").click(function (event){ //imprimir cesta
	event.preventDefault();
	var ruta = '';
	if ($(this).attr('dni') == 'undefined')
		ruta = './foros/modules/Tienda/imp_carrito.php';
	else
		ruta = './foros/modules/Tienda/imp_carrito.php?dni='+$(this).attr('dni');
	window.open(ruta,'Presupuesto','toolbar=no,location=no,directories=no,status=no,menubar=no');
});

//fotos busqueda
$(".link_art").hover (function (e) {
	var exts = ["gif", "GIF", "jpg", "JPG", "png", "PNG"];
	var referencia =  $(this).attr('val_ref');

	$("body").append("<img id=\"foto_art\" src=\"\" />");
	$("#foto_art").css ("position", "absolute");
	$("#foto_art").css ("max-width", "125");
	$("#foto_art").css ("max-height", "100");
	$("#foto_art").hide();
	$("#foto_art").css ("left", (e.pageX+2) + "px");
	$("#foto_art").css ("top", (e.pageY+2) + "px");
	$.ajaxSetup({ cache: false });
	$.each(exts, function(index, value) {
		$.get("http://www.modpc.com/foros/modules/Tienda/images/" + referencia + "." + value, function(data) {
			$("#foto_art").attr("src", "http://www.modpc.com/foros/modules/Tienda/images/" + referencia + "." + value);
		});
		return (!$("#foto_art").attr("src"));
	});
	if (!$("#foto_art").attr("src"))
	{
		$("#foto_art").attr("src", "http://www.modpc.com/foros/modules/Tienda/icons/nopic.jpg");
	}
	$("#foto_art").show ();
}, function () {
	$("#foto_art").hide ();
	$("#foto_art").attr("src", "");
	$("#foto_art").remove();
});

//fotos home
var tope;
$(".fotos_home").each (
	function () {
		p_parent = $(this).position ();
		$(this).find(".art_foto_lista").hide ();
		$(this).find(".art_foto_lista").each (
			function () {
				$(this).show ();
				p = $(this).position ();
				if (p.top > p_parent.top)
					$(this).hide ();
			}
		);
	}
);

//pop ups
var w = $(this).width();
var h = $(this).height();

$("div[class='cerrar_popup']").click(function (event){
	event.preventDefault();
	$("#"+$(this).attr("nom_pop")).hide('slow');
});

$("a[class='popup']").click(function(event){
	event.preventDefault();
	open_pop_up ("#"+$(this).attr("nom_pop"));
});

//confirmar pedido
$(".boton_envio").click(function (event){
	$("#"+$(this).attr('recom')).attr('checked', 'checked');
	id_tabla = recorrer_botones ([".boton_envio", ".boton_trans"]);
	$('.tabla_genv').hide();
	$('#'+id_tabla).show();
});

$(".boton_trans").click(function (event){
	id_tabla = recorrer_botones ([".boton_envio", ".boton_trans"]);
	$('.tabla_genv').hide();
	$('#'+id_tabla).show();
});

$('input#valid_carro').click(function (event){
	event.preventDefault ();
	var dataString = "";
	var respuesta = "";
	jQuery.each (["apellidos", "nombre", "dni", "direccion", "poblacion", "provincia", "telefono", "email"], function () {
			dataString = dataString + this + "=" + valor_campo(this, 1) + "&";
		});
	jQuery.each (["tipo_ped", "tipo_trans"], function () {
			dataString = dataString + this + "=" + valor_campo(this, 2) + "&";
		});
	jQuery.each (["provincia"], function () {
			dataString = dataString + this + "=" + valor_campo(this, 3) + "&";
		});
	jQuery.each (["confirm"], function () {
			dataString = dataString + this + "=" + valor_campo(this, 4) + "&";
		});
	$.ajax({
		url: 'foros/modules/Tienda/valid_carro.php',
		method: 'POST',
		data: dataString,
		dataType: 'text',
		cache: false,
		success: function (data) {
			if (data) {
				$('div#pop_vericarrito font.content').empty ();
				$('div#pop_vericarrito font.content').append ('<font class="warning"><b>'+data+'</b></font>');
				open_pop_up ('#pop_vericarrito');
			}
			else {
				$('form#valid_carro').submit ();
			}
		}
	});
});

$('#toggle').toggle(function() {
	$('#cuadro').show('slow');
	$('#toggle').text('Cerrar');
}, function() {
	$('#cuadro').hide('slow');
	$('#toggle').text('Abrir');
});

$('.abrir_grupo_conf').toggle(function(event) {
	event.preventDefault();
	abrir_conf_grupos_arts ($(this).parent ());
	$(this).html('<img border="0" src="http://www.modpc.com/foros/modules/Tienda/icons/collapse_alt.gif"/> Cerrar');
}, function(event) {
	event.preventDefault();
	cerrar_conf_grupos_arts ($(this).parent ());
	$(this).html('<img border="0" src="http://www.modpc.com/foros/modules/Tienda/icons/collapse_alt_collapsed.gif"/> Desplegar otras opciones');
});

//personalizar configuracion
$('.grupo_arts input').click(function (event) {
	act_lista_confs_arts ();
	diff_precios_confs_arts ();
	if ($(this).attr ('ref').substr (0, 1) == 'C' || $(this).attr ('ref').substr (0, 1) == 'Q') mostrar_foto_confs ($(this).attr ('ref'));
});

$('.pedir_conf_pers').click(function (event) {
	event.preventDefault();
	$('#form_conf_equipo').submit ();
});

});

