$(document).ready(function() {

	$('a.imggaleria').lightBox(
	{
		imageLoading: baseURL+'/images/lightbox-ico-loading.gif',
		imageBtnClose: baseURL+'/images/lightbox-btn-close.gif',
		imageBtnPrev: baseURL+'/images/lightbox-btn-prev.gif',
		imageBtnNext: baseURL+'/images/lightbox-btn-next.gif'
	});
	
	$('#navegacion span').hover(function(){
		$(this).addClass("highlight");
		}, function() {
			$(this).removeClass("highlight");
		});
		
	$('.boton').hover(function(){
		$(this).addClass("highlight");
		}, function() {
			$(this).removeClass("highlight");
		});

	
	function funcionesvercesta()
	{
		$('#contenidocarrito').tablesorter({sortList:[[0,0]],widgets: ['zebra']});
		$('#datospedido').validate();		
		$('#realizarpedido').click(function(){
			var url=baseURL+'/catalogo/confirmarpedido';
			$.ajax({
				url: url,
				cache: "false",
				timeout: 19000,
				success: function(response){
					$('#contenidoCatalogo').html(response);
					funcionesvercesta();
				},
				error: function(){
					$('#contenidoCatalogo').html("<p>Se ha producido un error.</p>");
				}
			});
		});
		
		$('#vaciarcarrito').click(function(){
			jConfirm('Confirmar vaciar cesta','Vaciar Cesta',function(r){
				var url=baseURL+'/catalogo/vaciarcesta';
				$.ajax({
					url: url,
					cache: "false",
					timeout: 19000,
					success: function(response){
						$('#contenidoCatalogo').html(response);
						funcionesvercesta();
					},
					error: function(){
						$('#contenidoCatalogo').html("<p>Se ha producido un error.</p>");
					}
				});
			});			
		});
		
		$('.eliminararticulo').click(function(){
			var url=baseURL+'/catalogo/eliminarproducto';
			idcarrito=$(this).attr("name");
			$.ajax({
				url: url,
				cache: "false",
				timeout: 19000,
				data: {
					idcarrito: idcarrito
				},
				success: function(response){
					$('#contenidoCatalogo').html(response);
					funcionesvercesta();
				},
				error: function(){
					$('#contenidoCatalogo').html("<p>Se ha producido un error.</p>");
				}
			});
		
		});
		
		$('#confirmarpedido').click(function(){
			if ($('#datospedido').valid()) {
				var url = baseURL + '/catalogo/realizarpedido';
				var datos = $('#datospedido').serialize();
				$.ajax({
					url: url,
					cache: "false",
					timeout: 19000,
					data: datos,
					success: function(response){
						$('#contenidoCatalogo').html(response);
						funcionesvercesta();
					},
					error: function(){
						$('#contenidoCatalogo').html("<p>Se ha producido un error.</p>");
					}
				});
			}
			else
			{
				return false;
			}
		});
		
	}
	
	funcionesvercesta();		
	
	
	$("#tipoorden").change(function () {
		var url=$("#tipoorden").attr("value");
		location.href=url;
	});
	
});

function contacto(){
	
	$("#ajaxloading").hide();
	
	$("#btnenviar").click(function(){
	 
	 var respuesta=false;
	 
	 $("#formulariopedido").validate({
		  rules: {
		 nombre   	: "required",
		 apellidos	: "required",
		 telefono 	: "required number",
		 email	    : "required email",
		 mensaje	: "required"
		 
	 	    		}
	 	});
	 
	
		if($("#formulariopedido").valid())
			{
			 $("#ajaxloading").show();
			 
			 $.ajax({
				 
				 async	: true,
				 url	: baseURL + "/web/index/enviarmailcontacto",
				 type    : "POST",
				 data	  : {
							nombre: 		$("#nombre").attr("value"),
							apellidos: 		$("#apellidos").attr("value"),
					        telefono: 		$("#telefono").attr("value"),
					        email: 			$("#email").attr("value"),
					        mensaje: 		$('textarea#mensaje').val()
					       
				             
			                },	
				 timeout   : 30000,
				  success   : function(response){
			                	$("#ajaxloading").hide();
			                	$("#nombre").val(" ");
			                	$("#apellidos").val(" ");
			                	$("#telefono").val(" ");
			                	$("#email").val(" ");
			                	$("#mensaje").val(" ");
								if (response=="ok") {
									jAlert("Mensaje Enviado Corectamente", "Enviado");
								}
								else{
									jAlert("Su mensaje no ha podido ser enviado","Error");
								}
						 	}
				
			 });
			}
			
});	
}



function realizapedido(){
	
	$("#ajaxloadingpedido").hide();

	$("#btnenviar").click(function(){
		 
		 var respuesta=false;
		 
		 $("#formformulariopedido").validate({
			  rules: {
				 nombreapellidos    : "required",
				 telefono 	: "required number",
				 movil 		: "required number",
				 email	    : "required email",
				 mensaje	: "required"
			 
		 	    		}
		 	});
		 

		 if($("#formformulariopedido").valid())
			{
			 	
			 $("#ajaxloadingpedido").show();
			 $.ajax({
					 
					 async	: true,
					 url	: baseURL + "/web/index/realizarpedido",
					 type    : "POST",
					 data	  : {
								nombreapellidos: $("#nombreapellidos").attr("value"),
								movil: 			$("#movil").attr("value"),
						        telefono: 		$("#telefono").attr("value"),
						        email: 			$("#email").attr("value"),
						        mensaje: 		$('textarea#mensaje').val()
						       
					             
				                },	
					 timeout   : 30000,
					 success   : function(response){
				                	$("#ajaxloadingpedido").hide();
				                	$("#nombreapellidos").val(" ");
				                	$("#movil").val(" ");
				                	$("#telefono").val(" ");
				                	$("#email").val(" ");
				                	$("#mensaje").val(" ");

				                	jAlert("Mensaje Enviado");
							 	}
					
				 });
				}
				
	});	


	
	
}
function ampliarproducto(){
	
	
	$('img').click(function(){
		var id=$(this).attr("num");
		if (id>0)
		{
		$.ajax({
			 
			 async	: true,
			 url	: baseURL + "/web/catalogo/ampliarproducto",
			 type    : "POST",
			 data	  : {
						idproducto:id
				       		             
		               },	
			 timeout   : 30000,
			  success   : function(response){
		            	   $("#introproductodesglosado").html(response);
		               }
			
		});
		}
	});

}
function ampliarambiente(){
	
	
	$('img').click(function(){
		var id=$(this).attr("num");
		$.ajax({
			 
			 async	: true,
			 url	: baseURL + "/web/ambientes/ampliarproducto",
			 type    : "POST",
			 data	  : {
						idambiente:id
				       		             
		               },	
			 timeout   : 30000,
			  success   : function(response){
		            	   $("#introproductodesglosado").html(response);
		               }
			
		});
	});

}