// Project: Natural Sensia
// Author: Moisés Martínez Muñoz
// Mail: moisesm83@gmail.com

//Carrousel for marcs bar in home page.

	
function equalHeight(group) 
{
	tallest = 0;
	
	group.each(function() 
	{
		thisHeight = jQuery(this).height();
		
		if(thisHeight > tallest) 
			tallest = thisHeight;
	});
	
	group.height(tallest);
}

function showRecaptcha(key, element) 
{ 
	Recaptcha.create(key, element, 
	{ 
    	theme: "green", 
        callback: Recaptcha.focus_response_field
	}); 
}

jQuery(function() 
{
	var path = '';
	
	jQuery('#promotionalCode').example('Promocode');
	
	
    jQuery(".rotarcentro").jCarouselLite({
        btnNext: ".rotarder",
        btnPrev: ".rotarizq",
		visible: 5
		
    });
	
	jQuery(".rotarcentrov").jCarouselLite({
        btnNext: ".rotarabjv",
        btnPrev: ".rotararrv",
	    autoPlay: true,
	    delay: 3000,
	  	startStopped: false,
    	animationTime: 300,
		visible: 4,
		vertical: true
    });
	
	jQuery(".rotarcentropro").jCarouselLite({
        btnNext: ".rotarproder",
        btnPrev: ".rotarproizq",
	    autoPlay: true,
	    delay: 3000,
	  	startStopped: false,
    	animationTime: 300,
		visible: 4
    });

	jQuery(document).ready(function() 
	{
		equalHeight(jQuery(".descripcion"));
		equalHeight(jQuery(".base"));
		equalHeight(jQuery(".cajaproducto2"));
	}); 
	
	jQuery(".otrosingredientes").jCarouselLite({
        btnNext: "#nextIng",
        btnPrev: "#preIng",
	    autoPlay: true,
	    delay: 3000,
	  	startStopped: false,
    	animationTime: 300,
		visible: 3
    });

	jQuery().ready(function()
	{
		
		jQuery('a.aviso').click(function () 
		{
			jQuery("#aviso").show();
			return false;
		});
		
		jQuery('a.close').live('click', function(event)
		{
			jQuery('#detalle').css({ display: 'none' });
			event.preventDefault();
		});
		
		jQuery('a.error-close').live('click', function(event)
		{
			jQuery('#error').css({ display: 'none' });
			event.preventDefault();
		});
		
		jQuery('a.suscribete').live('click', function(event)
		{
			jQuery('#suscripcion').submit();
		});
	
		jQuery("#ingrediente").click(function () 
		{
			jQuery("#detalle").show();
			return false;
		});
		
		jQuery('a.add-product').click(function() 
		{
			jQuery('#water_clock').hide();
			jQuery('#loader').show();
			
			var productID 		= jQuery(this).attr('rel');
			var productX     	= jQuery("#productImageID_" + productID).offset().left;
			var productY     	= jQuery("#productImageID_" + productID).offset().top;
			var basketX         = jQuery("#carro").offset().left;
			var basketY         = jQuery("#carro").offset().top;
			var gotoX 			= basketX - productX;
			var gotoY 			= basketY - productY;
			var newImageWidth   = jQuery("#productImageID_" + productID).width() / 3;
			var newImageHeight  = jQuery("#productImageID_" + productID).height() / 3;
			
			jQuery("#productImageID_" + productID + " img")
			.clone()
			.prependTo("#productImageID_" + productID)
			.attr('id', 'tempProduct_' + productID)
			.css({'position' : 'absolute', 'z-index' : '10000'})
			.animate({opacity: 0.4}, 100 )
			.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
				
				jQuery.ajax({
					type: "POST", 
	  				url: path + '/carrito/add_Product',
					data: 'data[id] =' + productID,
					success: function(data) 
					{
					    jQuery("#carritoState").html(data);
						jQuery("#tempProduct_" + productID).remove();
						jQuery('#loader').hide();
						jQuery('#water_clock').show();
  					}
				});			
				
			});
	
			return false;
		});
		
		jQuery('img.add-product').click(function() 
		{
			var productID 		= jQuery(this).attr('rel');
			var productX     	= jQuery("#productImageID_" + productID).offset().left;
			var productY     	= jQuery("#productImageID_" + productID).offset().top;
			var basketX         = jQuery("#carro").offset().left;
			var basketY         = jQuery("#carro").offset().top;
			var gotoX 			= basketX - productX;
			var gotoY 			= basketY - productY;
			var newImageWidth   = jQuery("#productImageID_" + productID).width() / 3;
			var newImageHeight  = jQuery("#productImageID_" + productID).height() / 3;

			
			jQuery("#productImageID_" + productID + " img")
			.clone()
			.prependTo("#productImageID_" + productID)
			.attr('id', 'tempProduct_' + productID)
			.css({'position' : 'absolute'})
			.animate({opacity: 0.4}, 100 )
			.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
				
				jQuery.ajax({
			        type: 'POST',
	  				url: path + '/carrito/add_Product',
					data: 'data[id] =' + productID,
					success: function(data) {
					    jQuery("#carritoState").html(data);
						jQuery("#tempProduct_" + productID).remove();
  					}
				});
				
			//document.location.href = path + '/carrito/addProduct/' + productID;
				
			});
	
			return false;
		});
		
		jQuery('a.verperfil').click(function () 
		{
			var id 	= jQuery(this).attr('rel');
			
			jQuery.ajax({
	  			type: 'GET',
  				url: path + '/quienes_somos/perfil/' + id,
				success: function(data) {
				    jQuery("#detalle").html(data);
					jQuery("#detalle").show();
  				}
			});
			
			return false;
		});
		
		jQuery('a.verPromocion').click(function () 
		{
			var id 	= jQuery(this).attr('rel');
			var url = 'carrito/promocion/' + id;
			
			jQuery.ajax({
	  			type: 'GET',
  				url: path + '/carrito/promocion/' + id,
				success: function(data) {
				    jQuery("#detalle").html(data);
					jQuery("#detalle").show();
  				}
			});

			return false;
		});
		
		jQuery('img.delete-product').live('click' ,function() 
		{
			var product = jQuery(this).attr('rel');
			
			location.href = path + '/carrito/delete_product/' + product;
			
			/*jQuery.ajax({
	  			type: 'GET',
  				url: path + '/carrito/delete_product/' + product,
				success: function(data) {
				    jQuery("#carrito").html(data);
					jQuery("#carritoState").load('carrito/update_products', function() {});
  				}
			});*/
	
			return false;
		});
		
		jQuery('a.modify-product').live('click' ,function() 
		{
			var product = jQuery(this).attr('rel');
			var url 	= path + '/carrito/viewChangeUnits/' + product;
			
			jQuery.ajax({
	  			url: url,
				success: function(data) {
				    jQuery("#detalle").html(data);
					jQuery("#detalle").show();
  				}
			});

			return false;
		});
		
		jQuery('a.update-product').live('click' ,function() 
		{
			var form = jQuery('#' + jQuery(this).attr('rel'));
			
			form.submit();
			
			/*jQuery.ajax({
	           	type: 'POST',
	            url: form.attr('action'),
    	        data: form.serialize(),
				success: function(data) {
					jQuery("#carrito").html(data);
					jQuery("#detalle").hide();
					jQuery("#carritoState").load('carrito/update_products', function() {});
  				}
			});*/
			
			return false;
		});
		
		
		/*Sell Process ajax*/
		
		jQuery(".select-promotion").live('click', function()
		{
			jQuery('#loader').show();
			
			var id 	= jQuery(this).attr('value');
			var data = jQuery(this).attr('rel').split('#');
			var url = path + '/compra/';
			
			if (jQuery(this).attr('checked'))
				url += 'activate_promotion/';
			else
				url += 'deactivate_promotion/';		
				
			location.href = url + '/' + id + '/' + data[0] + '/' + data[1] + '/' + data[2];
			
			/*jQuery.ajax({
				type: 'POST',
				url: url,
				data: 'data[Promotion][id] =' + id + '&data[Promotion][acumulate] =' + acu,
				success: function(data) {
					jQuery("#resumen").html(data);
				}
			});	*/
		});
		
		jQuery("img.delete-promotion").live('click', function()
		{
			var id 	= jQuery(this).attr('rel');
			var url = path + '/compra/deactivate_promotion/' + id;	
			
			location.href = url;
			
			/*jQuery.ajax({
				type: 'POST',
				url: url,
				data: 'data[Promotion][id] =' + id,
				success: function(data) {
					jQuery("#productos").html(data);
				}
			});*/
		});
		
		jQuery('img.delete-sell-product').live('click' ,function() 
		{
			var product = jQuery(this).attr('rel');
			
			location.href = path + '/compra/delete_product/' + product;
	
			return false;
		});
		
		jQuery('a.modify-sell-product').live('click' ,function() 
		{
			var product = jQuery(this).attr('rel');
			var url 	= path + '/compra/viewChangeUnits/' + product;
			
			jQuery.ajax({
	  			url: url,
				success: function(data) {
				    jQuery("#detalle").html(data);
					jQuery("#detalle").show();
  				}
			});

			return false;
		});				
		
		jQuery('a.update-sell-product').live('click' ,function() 
		{
			var form = jQuery('#' + jQuery(this).attr('rel'));
			
			form.submit();
			
			/*jQuery.ajax({
	           	type: 'POST',
	            url: form.attr('action'),
    	        data: form.serialize(),
				success: function(data) {
					jQuery("#carrito").html(data);
					jQuery("#detalle").hide();
					jQuery("#carritoState").load('carrito/update_products', function() {});
  				}
			});*/
			
			return false;
		});		
		
		
		/*Addresses ajax*/
		
		jQuery("#add-address").live('click', function()
		{
			jQuery.ajax({
  				type: 'POST',
				url: path + '/addresses/add',
				success: function(data) {
				    jQuery("#data").html(data);
					jQuery("#detalle").show();
				}
			});
		
			return false;
		});
	
		jQuery("#edit-address").live('click', function()
		{	
			var id 	= jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'POST',
				url: path + '/addresses/edit',
				data: 'data[Address][id] =' + id,
				success: function(data) {
						jQuery("#data").html(data);
						jQuery("#detalle").show();
				}
			});
		
			return false;
		});
		
		jQuery("#save-address").live('click', function()
		{	
			var form = jQuery('#' + jQuery(this).attr('rel'));
		
			jQuery.ajax({
            	type: 'POST',
	            url: form.attr('action'),
    	        data: form.serialize(),
        	    // Mostramos un mensaje con la respuesta de PHP
            	success: function(data) 
				{
					jQuery("#addresses1").html(data);
					jQuery("#addresses2").html(data);					
					jQuery("#detalle").hide();
    	        }
	        })
					
			return false;
		});		
		
		
	
		jQuery("#edit-delete").live('click', function()
		{	
			var id 	= jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'POST',
				url: path + '/addresses/delete',
				data: 'data[Address][id] =' + id,
				success: function(data) {
					jQuery("#address").html(data);
				}
			});
		
			return false;
		});
		
		jQuery("#select-address").live('click', function()
		{
			var id 	= jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'POST',
				url: path + '/addresses/list',
				data: 'data[client][id] =' + id,
				success: function(data) {
					jQuery("#address").html(data);
				}
			});
		
			return false;
		});
		
		jQuery("#country_id").live('change', function()
		{
			var id 	= jQuery(this).val();
				
			jQuery.ajax({
				type: 'POST',
				url: path + '/countries/change_country/' + id,
				data: 'data[Address][country_id][value] =' + id,
				success: function(data) {
					jQuery("#province_id").html(data);
				}
			});
			
		});		

		jQuery("a.save-order").live('click', function()
		{
			var id 		= jQuery(this).attr('rel');
			var method 	= jQuery('input:radio[name="data[Compra][method]"]:checked').val();
			var url 	= path + '/compra/guardar';	
			
			jQuery("#detalle").show();
			
			jQuery.ajax({
				type: 'POST',
				url: url,
				data: 'data[Compra][method] =' + method,
				success: function(data) 
				{
					var dataInfo = data.split('#');
					
					if ((dataInfo[0] < 0) || (dataInfo.length == 0))
					{						
						alert('Error ' + dataInfo[0]);
					}
					else if (dataInfo[0] == 1)
					{
						var form = document.createElement("form");
	
						form.setAttribute("method", "post");

						form.setAttribute("action", dataInfo[1]);
							
						var counter = 2;
							
						while (counter < dataInfo.length)
						{
							var hiddenField = document.createElement("input");
							     
							hiddenField.setAttribute("type", "hidden");
							hiddenField.setAttribute("name", dataInfo[counter]);
							hiddenField.setAttribute("value", dataInfo[counter + 1]);
							    
							form.appendChild(hiddenField);
								
							counter = counter + 2;
						}
							
						document.body.appendChild(form);
						form.submit();
					}
					else
					{		
						jQuery('#compra4').submit();
					}
				}
			});
		});

		jQuery("a.ver-ingrediente").live('click', function()
		{
			var id = jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'GET',
				url: path + '/producto/ver_ingrediente/' + id,
				success: function(data) 
				{
					jQuery("#ingrediente-principal").html(data);
					jQuery("#detalle").show();					
				}
			});
			
			return false;
		});		
		
		jQuery("a.cancel-order").live('click', function()
		{
			var id  = jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'GET',
				url: path + '/mi_cuenta/cancelar/' + id,
				success: function(data) 
				{
					jQuery("#stateOrder").html(data);
				}
			});
			
			return false;
		});
		
		jQuery("a.repeat-order").live('click', function()
		{
			var id  = jQuery(this).attr('rel');
			
			jQuery.ajax({
				type: 'GET',
				url: path + '/mi_cuenta/repetir/' + id,
				success: function(data) 
				{
					jQuery("#carritoState").html(data);
				}
			});
			
			return false;
		});		
		
		jQuery("a.address-error").live('click', function()
		{			
		
			if (jQuery('#type1').is(':checked'))
			{
				var radio = jQuery('input:radio[name="data[Compra][address_id]"]');
				if (radio.is(':checked'))
					jQuery('#compra2').submit();
				else
					jQuery("#error").show();			
			}
			else if (jQuery('#type2').is(':checked'))
			{
				jQuery('#compra2').submit();
			}
			else
				jQuery('#compra2').submit();
				
			return false;				
		});	
		
		jQuery("a.promotions-error").live('click', function()
		{	
			if (jQuery('#numPromotions').val() == 0)
				jQuery("#error").show();	
			else
				jQuery('#compra3').submit();		
			return false;				
		});		
		
		jQuery("a.page-comment").live('click', function()
		{
			var url = jQuery(this).attr('href');
			
			jQuery.ajax({
				type: 'GET',
				url: url,
				success: function(data) 
				{
					jQuery("#Pagecomments").html(data);
				}
			});				
				
			return false;				
		});					
		
		jQuery("#nuevoComentario").live('click', function()
		{
			var form = jQuery('#' + jQuery(this).attr('rel'));
			
			jQuery.ajax({
				type: 'POST',
	            url: form.attr('action'),
    	        data: form.serialize(),
				success: function(data) 
				{
					jQuery("#Comments").html(data);
					showRecaptcha('6LcYD8gSAAAAALab0XVtbtEQQrAq45Iq3-DPC5yt', 'captcha');
				}
			});				
				
			return false;				
		});		
		
		jQuery("a.apuntarse").live('click', function()
		{
			jQuery('#loader').show();	
			
			var form = jQuery('#NewsForm');
			
			jQuery.ajax({
				type: 'POST',
	            url: path + '/user_newsletters/add',
    	        data: form.serialize(),
				success: function(data) 
				{
					jQuery('#loader').hide();	
					jQuery("#message").html(data);
				}
			});
		});	
		
		jQuery("a.darsedebaja").live('click', function()
		{
			jQuery('#loader').show();	
			
			var form = jQuery('#NewsForm');
			
			jQuery.ajax({
				type: 'POST',
	            url: path + '/user_newsletters/delete',
    	        data: form.serialize(),
				success: function(data) 
				{
					jQuery('#loader').hide();	
					jQuery("#message").html(data);
				}
			});
		});		
		
		jQuery("a.add-promocode").live('click', function()
		{
			jQuery('#loader').show();
			
			var code = jQuery('#promotionalCode').attr('value');
			
			jQuery.ajax({
				type: 'GET',
				url: path + '/compra/activate_promocode/' + code,
				success: function(data) 
				{
					if (data == '')
						location.href = path + '/compra/paso3';
					else
					{
						jQuery("#productos").html(data);
					}
				}
			});
				
			return false;			
		});
		
		jQuery("img.delete-promocode").live('click', function()
		{
			var id 	= jQuery(this).attr('rel');
			var url = path + '/compra/delete_promocode/' + id;	
			
			location.href = url;
			
			/*jQuery.ajax({
				type: 'POST',
				url: url,
				data: 'data[Promotion][id] =' + id,
				success: function(data) {
					jQuery("#productos").html(data);
				}
			});*/
		});		
		
		jQuery("a.shop-close").live('click', function()
		{	
			jQuery("#error").show();		
			return false;				
		});					
				
				
		jQuery("a.recordar").live('click', function()
		{
			jQuery('#loader').show();
		});
		
		jQuery("a.loader").live('click', function()
		{
			jQuery('#loader').show();	
		});	
		
		jQuery("a.reservarTratamiento").live('click', function()
		{
			var values	= jQuery(this).attr('rel');
			var url 	= path + '/treatments/solicitar/';	
			
			jQuery('#loader').show();
			
			jQuery.ajax({
				type: 'POST',
				url: url,
				data: 'data[data]=' + values,
				success: function(data) 
				{					
					jQuery("#tratamiento").html(data);
					jQuery('#loader').hide();
				}
			});
		});	
	}); 
	
	jQuery("a.execute-action").live('click', function()
	{				
		var values  = jQuery(this).attr('rel').split('/'); /* Controller - action - id - values */		
		var url 	= path + '/' + values[0] + '/' + values[1] + '/';
		var params	= '';
				
		for (i=3; i<values.length; i++)
		{
			params += 'data[data][value' + (i-2) + ']=' + values[i] + '&';
		}			
				
		jQuery('#loader').show();

		jQuery.ajax({
            type: 'POST',
            url: url,
            data: params,			
            success: function(data) 
			{
				jQuery('#' + values[2]).html(data);
				jQuery('#loader').hide();
            }
        })
		
		return false;
	});
	
});

