 // JavaScript Document

  

$(function()
{ 
		 $continuar = true; 
		function setCaja(obj, CajaConf)
		{
			try 
			{   //vaciar la caja
				$("#ul_caja li").detach();
				$("#ul_resumen li").detach();
				$("#aviso").detach();
				$continuar = false;
				var jcc_pos = 0;
				for(var i in obj)
				{
				
				  var img = (CajaConf) ? '<a onclick="DelElementoCaja(' + jcc_pos + ')"    class="cruz"><span></span></a>' : '';
					
				  $list.append('<li  id="' + i  + '"  ><img src="' + obj[i]["small_image"] + '" alt="" />' + img + JCC_CLASS_BOX[JCC_CLASE_BONTON]["ClassRelleno"] + '</li>'); 

				  $listResumen.append('<li  id="resumen_' + i + '" >' + obj[i]["name"] +img + '<span class="rounda"></span></li>'); 

				   $("#li_" +  jcc_pos  ).data("id", obj[i]["product_id"]);
				   $("#li_" +  jcc_pos  ).data("option_id", obj[i]["option_id"]   ); 
				   $("#li_" +  jcc_pos  ).data("selection_id", obj[i]["selection_id"]);      
				   $("#li_" +  jcc_pos  ).data("precio", obj[i]["precio"]); 
				   
				   if(JCC_PRECIO["bunde_" + $("#id_bundle").val()   ]["tipo_precio"] == 0)
					  SetPrecio($("#li_" +  jcc_pos  ), true)
				   
				  jcc_pos++;
				}    
			}
			catch(e)
			{
			 alert(e);
			} 
		}



	  $(".btn-aniadir, .btn-aniadir-sin, .btn-aniadirEspecial").click(function()
	   { 
			AjaxLoad($(this));
			var strUrl = "/checkout/cart/add";
			$.ajax({
					 type: "GET",
					 url: strUrl,
					 data: "product=" + $(this).attr("id") + "&qty=" + $("#cnt_" + $(this).attr("id")).val() , 
					 dataType: 'json',
					 complete: function(objeto, exito)
					 {
						  
						 try
						 { 
							eval("var res = " + objeto.responseText);
							$("#items").html(res.items + " art&iacute;culos<br /><span>en su carrito</span>");
							AjaxLoadHIde( "Se ha a&ntilde;ido el art&iacute;culo" );  
						}
						catch(e)
						{
							AjaxLoadHIde(""); 
						}
					 },                       
					 error: function(objeto, quepaso, otroobj)
					 {
						alert("Problema: "+ quepaso + objeto + otroobj);AjaxLoadHIde("Ha ocurrido un problema ")  
						AjaxLoadHIde("Problema: "+ quepaso + objeto + otroobj);AjaxLoadHIde("Ha ocurrido un problema ");                           
					 }   
			 }); 
	   });
		  
		   

		  // cajas configuradas

		 

		 $(".panel-linea > a").click(function()
		 {
			 
			AjaxLoad($(this));
			$("#id_bundle").val( $(this).attr("id").replace('jcc_cc_', ''));
			
			$("#jcc_caja").removeClass();  
			JCC_CLASE_BONTON =  $(this).attr("title").replace("-", "");  
			$("#jcc_caja").addClass( JCC_CLASS_BOX[JCC_CLASE_BONTON]["clase"] ); 
			 
			$.ajax({
					 type: "GET",
					 url: "/quierochocolate/ajax/cajaconf/id/" + document.getElementById( "id_bundle").value + "/",
					 data: "count=" + JCC_CLASS_BOX[JCC_CLASE_BONTON]["maxa"], 
					 dataType: 'json',
					 complete: function(objeto, exito)
					 {
						try
						{ 
							 eval("var res = " + objeto.responseText); 
							 
							 $(".block-boton").find("a").each(function(i,e)
							 {
								$(this).removeClass("active");  
							 }); 
							 $("." + JCC_CLASS_BOX[JCC_CLASE_BONTON]["ClassBoton"]).addClass("active");
							 
							 
							  if(JCC_PRECIO["bunde_" + $("#id_bundle").val()   ]["tipo_precio"] == 1)
							  {   
									PRECIO_CONF = JCC_PRECIO["bunde_" + $("#id_bundle").val()  ]["precio"];
									 
							  }
							  else
							  { 
								PRECIO_CONF = 0; 
							  }
							
							$("#jcc_precio").html( PRECIO_CONF + "  &euro;");
							  setCaja(res, false)
							 
						}

						catch(e){ } 
						AjaxLoadHIde("")    

					 },
					 error: function(objeto, quepaso, otroobj)
					 {
						alert("Problema: "+ quepaso + objeto + otroobj);
						AjaxLoadHIde(""); 
					 }   
			 });
		 });

		 
		 

		  $("#CajaAleatoria").click(function()
		  {
			 AjaxLoad($(this));
			 $.ajax({
					 type: "GET",
					 url: "/quierochocolate/ajax/rand/id/" + document.getElementById( "id_bundle").value + "/",
					 data: "count=" + JCC_CLASS_BOX[JCC_CLASE_BONTON]["maxa"], 
					 dataType: 'json',
					 complete: function(objeto, exito)
					 {
						try
						{
							eval("var res = " + objeto.responseText);
							setCaja(res, true)
						}
						catch(e){AjaxLoadHIde(""); }
						AjaxLoadHIde("")  
					 },
					 error: function(objeto, quepaso, otroobj)
					 {
						alert("Problema: "+ quepaso + objeto + otroobj);
						AjaxLoadHIde(""); 
					 }   
			 });        
		});

		  
		function IncluirDefault()
		{
			try
			{ 
			 
				 if(!JCC_DEFAULT["b_" + document.getElementById( "id_bundle").value ])
				 return;
				  var StrUrl = "bundle_option[" + JCC_DEFAULT["b_" + document.getElementById( "id_bundle").value ]["opcion"] + "][0]=" + JCC_DEFAULT["b_" + document.getElementById( "id_bundle").value ]["seleccion"];
				 return StrUrl;
			}
			catch(e)
			{
				return "";
			}
		   
		 }
		  

		$("#jcc_confirmarDos, #jcc_confirmar").click(function()
		{ 
		   Confirmar($(this), '');

		});
		
		
		
		$("#bundle_confirmar ,#bundle_confirmar_dos").click(function()
		{   
			Confirmar($(this), '');
			
		});
		
		
		
		
		function Confirmar($obj, url)
		{
			 if( (JCC_CLASS_BOX[JCC_CLASE_BONTON]["maxa"]) > $listResumen.children("li").length)
			 {
				  if(JCC_CLASS_BOX[JCC_CLASE_BONTON]["IncludingBase"] == 0)
				  {
					alert("Debe rellenar todas las opciones");
					return;
				  }
			 }
		 
			AjaxLoad($obj);
			var strUrl =  "";  
			if(JCC_CLASE_BONTON)
			{
				strUrl += (JCC_CLASS_BOX[JCC_CLASE_BONTON]["IncludingBase"] == 1) ? IncluirDefault() : "";
			}
			$("#ul_caja").children("li").each(function()
			{  
			
			   if($(this).data("selection_id"))
			   {
					strUrl +=  (strUrl.length > 0 ) ? "&bundle_option[" + $(this).data("option_id") + "][0]=" + $(this).data("selection_id")

												: "bundle_option[" + $(this).data("option_id") + "][0]=" + $(this).data("selection_id");
												
											   
			   }
			});
			 
			
			strUrl =   strUrl + "&qty=" +  document.getElementById( "cnt_cajas").value ;   
			$.ajax({
					 type: "GET",
					 url: "/checkout/cart/add/product/" + document.getElementById( "id_bundle").value + "/",
					 data: strUrl,
					 
					 dataType: 'json',
					 complete: function(objeto, exito)
					 {
						try
						{    
							 eval("var res = " + objeto.responseText);
							 $("#items").html(res.items + ( (!JCC_BUNDLE_ARTICULO) ? " items " : " art&iacute;culos<br /><span>en su carrito</span>" ) );
							 $("#total").html(res.total + " &euro;");
							 $("#msg_prote").html(res.msg);
							 AjaxLoadHIde("Se ha a&ntilde;ido el art&iacute;culo")  
							  
							 $continuar = true;
							 if(!JCC_BUNDLE_ARTICULO)
							 {
								DelCaja();
							 }
							 else
							 {
								 window.close(); 
							 }
							 if(url != '')
							 {
								 window.location.href = url;
							 }        

						}
						catch(e)
						{
						   alert(e)
						   AjaxLoadHIde(""); 
						}                       
					 },
					   
					 error: function(objeto, quepaso, otroobj)
					 {
						alert("Problema: "+ quepaso + objeto + otroobj);
						AjaxLoadHIde(""); 

					 }   

			 });
		
		}
		
		function DelCaja()
		{
			 
			$("#ul_caja li").detach();
			$("#ul_resumen li").detach();
			if(!document.getElementById( "aviso"))
			{ 
				 $list.append(' <li id="aviso"><img src="images/ahora_aqui.png" alt="" /></li>'); 
			} 
			 $("#id_bundle").val( $(".active").attr("id")); 
		}

		$("#continuar_carrito, .vs_continuar, .leer_mas").click(function()
		{ 
			if(!$continuar && $list.children("li").length > 1)
			{
			 
				if(!confirm("Hay configuraciones en curso\n  Quiere confirmarla ? "))
				{
					 $continuar = true;
					 window.location.href = $(this).attr("rel")
				}
				else
				{
					Confirmar($(this), $(this).attr("rel")); 
				}  
			}
			else
			{ 
			   window.location.href = $(this).attr("rel")
			}
		
		}) 
		
		
		$(".peque").click(function()
		{
			$("#img_grande").hide();
			document.getElementById("img_grande").src = $(this).attr("rel");
			$("#img_grande").fadeIn(1500);
			 
		});

})

 function AjaxLoad($item)
{
			  
			  $("#AjaxLoad").show("fast");
			  $("#AjaxLoad").css(
			  {
				  left:  ($(document).width() / 2) + "px",
				  top:   ($(document).height() / 2) + "px",
				  zIndex: 9999
			  }); 
			  
			  jQuery.overlay();   
			  
}

		 

function AjaxLoadHIde( msg)
{    
			  jQuery.disable();
			  if(msg == "")
			  {
				 $("#AjaxLoad").fadeOut(1000); 
				 return;
			  }
			  $("#AjaxMsg").show("fast");
			  $("#AjaxMsg").css(
			  {
				  left: ($("#AjaxLoad").offset().left + 30) + "px",
				  top:  ($("#AjaxLoad").offset().top + 30) + "px",
				  zIndex: 9999
			  });  

			  $("#AjaxLoad").fadeOut(1000); 
			  $("#AjaxMsg").show("fast"); 
			  $("#AjaxMsg").html('<p align="center">' + msg + "</p>") ;
			  $("#AjaxMsg").delay(1500).fadeOut(1500); 
 
			  
}






jQuery.extend({
  overlay: function(options)
  {
	opts = jQuery.extend({}, {'background-color': '#fff', 'opacity': '0.3'}, options);

	jQuery('body').append('<div id="overlay-div"></div>')
	
	 

	jQuery('#overlay-div').css('height', $(document).width() + 'px');
	jQuery('#overlay-div').css('width',  $(document).height()+ 'px');
	jQuery('#overlay-div').css('position', 'absolute');
	jQuery('#overlay-div').css('left', '0px');
	jQuery('#overlay-div').css('top', '0px');
	jQuery('#overlay-div').css('z-index', '99');

	jQuery('#overlay-div').css('background-color', opts['background-color']);
	jQuery('#overlay-div').css('opacity', opts['opacity']);

	if (opts['content'] != undefined)
	{
	  jQuery('#overlay-div').append('<div>'+opts['content']+'</div>');
	  jQuery('#overlay-div div').css('z-index', '100');

	  div_height = jQuery('#overlay-div div').height();
	  window_height = jQuery(window).height();
	  margin_top = (window_height - div_height) / 2;

	  jQuery('#overlay-div div').css('margin-top', margin_top+'px');
	}
  },
  
  disable: function()
  {
	   jQuery('#overlay-div').detach();
  }
});


 
