//INICIO
var agregar_carrito;
$(function(){

	/* Agrega un producto con talle */
	$('.agregar_talle').click(function(){
		var $id = $(this).attr('rel');
		$.facebox({ajax:'ajax-talles.php?id='+$id});
		return false;
	});
	
	/* Agrega un producto sin talle */
	$('.agregar').click(function(){
		var $id = $(this).attr('rel');
		agregar_carrito($id);
		return false;
	});
	
	/* Agregar favoritos */
	$('.agregar_favorito').click(function(){
		var $id = $(this).attr('rel');
		$.facebox({ajax:'ajax-add-favorito.php?id='+$id});
		return false;
	});
	
	/* Agrega un producto al carrito */
	agregar_carrito = function(id, talle){
		/* Mensaje de espera "Agregando" */
		$.blockUI.defaults.growlCSS.opacity = '0.8';
		$.blockUI.defaults.growlCSS.background = '#000 url(images/cargando.gif) no-repeat 20px 15px';
		$.growlUI('Agregando producto', '&nbsp;', 5000);	

		var $id = id;
		var $t = new Date().getTime();
		var $talle = talle||'';

		$.get('add-carrito.php', {id: $id, talle: $talle, t: $t},
					function(datos){
						//alert(datos);
						if(datos != 'error')
							$.blockUI.defaults.growlCSS.opacity = '0.8';
							$.blockUI.defaults.growlCSS.background = '#000 url(images/check48.png) no-repeat 10px 10px';
							$.growlUI('Producto agregado', 'El producto: '+datos['producto']+', fué agregado al carrito<br /><br /><a href="carrito1">» Ir al carrito ('+datos['total']+' productos)</a>', 5000);	
							$('#total_carrito').html('('+datos['total']+')');
					}, 'json');
		return false;
	};
	
	/* Sistema de encuestas */
/*	$('#encuesta').load('ajax-encuesta.php', '', function(){
		$('#votar').click(function(){
			$valor = $("input[name='encuesta']:checked").attr('value');
			$encuesta = $("input[name='encuesta_id']").attr('value');
			if($valor > 0){
				$('#encuesta').load('ajax-encuesta.php', {opcion: $valor, id: $encuesta});
			}
			else{
				alert('Seleccione una opción');
			}
		});
	});*/
	
	/* Acordeón de categorías */
	var tcat = $('a.cat');
	if(tcat.length){
		tcat.click(function(){
			$(this).parent().children('ul').toggle();
		});
	}
	
	/* Orden de productos */
	var ordp = $('.orden_pro');
	if(ordp.length){
		ordp.change(function(){
			vinc = $(this).attr('value');
			location.href = vinc;
		});
	}
	
	/* Miniatura de imágenes */
	var mini = $('.mini');
	if(mini.length){
		mini.click(function(){
			nsrc = $(this).attr('href');
			nimg = '<img id="maxi" class="prod_img" src="'+nsrc+'" />';
			$('#foto_maxi').html(nimg);
			return false;
		});
	}
	
		/* Habilita el facebox */
	if($.facebox){
	  $('a[rel*=facebox]').facebox();
	}
	
});