// =======
// FUNKCJE
// =======

// ------------------------------------------------------------------
// FUNKCJA DO SPRAWDZENIA WYBRANYCH BONUSÓW PODCZAS AKTUALIZACJI CENY
// ------------------------------------------------------------------

var checkSelectedBonuses=function()
{
	checkPromotionalProducts();	
}





// ----------------------------------------------------
// FUNCKJA DO SPRAWDZANIA POPRAWNOŚCI KODU PROMOCYJNEGO
// ----------------------------------------------------

var validateCouponCode=function()
{
	if ($("#field_promotional_codes").length)
	{
		var coupon_code=$("#coupon_code").attr("value");
		var info_colors=new Array("#ff0000","#1ea100");
		var currency=$("#currency").attr("value");
		var product=$("#product").attr("value");

		if (coupon_code!="")
		{
			$("#validate_code").fadeOut(200,function()
			{
				$("#loader_coupon_code").fadeIn(200);

				$.ajax(
				{
					type:"POST",
					url:"include/ajax.actions.bonuses.php?action=validate_coupon_code",
					data:$("#ajax_form").serialize(),
					success:function(data)
					{
						data=data.split("|");

						$("#coupon_value").attr("value",data[2]);
						$("#value_type").attr("value",data[3]);

						if (data[0]==1)
						{
							countPrice();
						}

						$("#loader_coupon_code").fadeOut(200,function()
						{
							$("#validate_info").css("color",info_colors[data[0]]).html(data[1]).fadeIn(200,function()
							{
								if (data[0]==1)
								{
									$("#checked_code").attr("value",coupon_code);

									// -------
									// PROCENT
									// -------

									if (data[3]==1)
									{
										$("#rabat_floating").html("- "+data[2]+"%").fadeIn(300);
										$("#inp_rabat").attr("value",data[2]);
										$("#coupon_value_info").html("- "+data[2]+"%").show().css({"opacity":0,"right":"-100px"}).animate({"right":0,"opacity":1},200);
									}

									// -----
									// KWOTA
									// -----

									if (data[3]==2)
									{
										//$("#rabat_floating").html("- "+data[2]+" "+currency).fadeIn(300);
										$("#rabat_floating").fadeOut(300);
										$("#inp_rabat").attr("value",data[2]);
										$("#coupon_value_info").html("- "+data[2]+" "+currency).show().css({"opacity":0,"right":"-100px"}).animate({"right":0,"opacity":1},200);
									}

									if ($("#current_product_amount").attr("value")=="")
									{
										$("#current_product_amount").attr("value",data[4])
									}

									$("#is_promotional_code").attr("value",1);
								}
								else
								{
									$("#is_promotional_code").attr("value",0);
								}


								// ------------------------------------
								// SPRAWDZANIE INNYCH RABATÓW I BONUSÓW
								// ------------------------------------

								checkDiscounts();

								var is_discount=$("#is_discount").attr("value");

								if (is_discount==0 && data[0]==0)
								{
									$("#rabat_floating").fadeOut(300,function(){$(this).html("");});
									$("#coupon_value_info").fadeOut(300,function(){$(this).html("");});
								}
							});
						});
					}
				});
			});
		}
	}
}





// ---------------------------------------
// FUNKCJA DO SPRAWDZANIA PROGÓW I RABATÓW
// ---------------------------------------

var checkDiscounts=function()
{
	if ($("#field_discounts").length)
	{
		var ilosc=$("#ilosc").attr("value");
		var is_promotional_code=$("#is_promotional_code").attr("value");

		$.ajax(
		{
			type:"POST",
			url:"include/ajax.actions.bonuses.php?action=check_discounts",
			data:$("#ajax_form").serialize(),
			success:function(data)
			{		
				data=data.split("|");

				if (data[0]!=0)
				{
					$("#is_discount").attr("value",1);
				}
				else
				{
					$("#is_discount").attr("value",0);
				}

				if (data[0]!=0 && is_promotional_code==0)
				{
					$("#discount_value_info").html("- "+data[0]+"%").show().css({"opacity":0,"right":"-100px"}).animate({"right":0,"opacity":1},200);
					$("#discount_available_info").css({"color":"#333","font-weight":"bold"}).html("[ "+data[1]+" ]");

					$("#rabat_floating").html("- "+data[0]+"%").fadeIn(300);
					$("#inp_rabat").attr("value",data[0]);
				}
				else
				{
					$("#discount_value_info").html("").fadeOut(300);
					$("#discount_available_info").css({"color":"#aaa","font-weight":"normal"}).html("[ "+data[1]+" ]");
				}

				var is_discount=$("#is_discount").attr("value");

				if (data[0]==0 && is_promotional_code==0 && is_discount==0)
				{
					$("#rabat_floating").fadeOut(300,function(){$("#rabat_floating").html("");});
					$("#inp_rabat").attr("value","0");
				}

				if (data[2]=="no_discounts")
				{
					$("#line_discounts,#field_discounts").slideUp(300);
				}
				else
				{
					$("#line_discounts,#field_discounts").slideDown(300);
				}

				countPrice();
			}
		});
	}
}





// -----------------------------------------------
// FUNCKJA DO SPRAWDZANIA INFO O RABATACH PRODUKTU
// -----------------------------------------------

var checkDiscountsInfo=function()
{
	if ($("#field_discounts").length)
	{
		$.ajax(
		{
			type:"POST",
			url:"include/ajax.actions.bonuses.php?action=check_discounts_info",
			data:$("#ajax_form").serialize(),
			success:function(data)
			{		
				data=data.split("|");

				if (data[0]!="0")
				{
					$("#info_ilosc").attr("title",data[0]).fadeIn(300);
				}
				else
				{
					$("#info_ilosc").attr("title","").fadeOut(300);
				}


			}
		});
	}
}





// ------------------------------------------
// FUNCKJA DO SPRAWDZANIA PROMOCYJNEGO ZAKUPU
// ------------------------------------------

var checkPromotionalProducts=function()
{
	if ($("#field_promotional_products").length)
	{
		var is_promotional_product=$("#is_promotional_product").attr("value");

		$.ajax(
		{
			type:"POST",
			url:"include/ajax.actions.bonuses.php?action=check_promotional_products&is_promotional_product="+is_promotional_product,
			data:$("#ajax_form").serialize(),
			success:function(data)
			{		
				data=data.split("|");

				if (data[0]==0)
				{
					$(".promotional_product").attr({"checked":false,"disabled":"disabled"});
					$("#field_promotional_products").slideUp(300);
					$("#line_promotional_products").slideUp(300);
					$("#is_promotional_product").attr("value",0);
				}
				else
				{
					$("#products_container").html(data[0]);
					$("#field_promotional_products").slideDown(300);
					$("#line_promotional_products").slideDown(300);
					$("#all_promo_products").attr("value",data[1]);
					$(".no_active").animate({"opacity":0.4},200);

					//alert(data[2]);
					if (data[2]=="reset_is")
					{
						$("#is_promotional_product").attr("value",0);
					}

					// --------------------------------------------------
					// WZNOWIENIE TOOLTIP DLA OBIEKTÓW WSTAWIONYCH AJAXEM
					// --------------------------------------------------

					$("#s-m-t-tooltip").remove();

					$("#products_container .tooltip").style_my_tooltips(
					{
						tip_follows_cursor:"on",
						tip_delay_time:200
					});

					var price=$("#inp_price").attr("value");
					var all_products=$("#all_promo_products").attr("value");

					all_products=all_products.split(",");

					//alert(all_products);
					
					for (var z=0; z<all_products.length; z++)
					{
						if (eval($("#product_ceiling_"+all_products[z]).attr("value"))<eval(price))
						{
							$("#promo_product_"+all_products[z]).animate({"opacity":1},300);
							$("#promotional_product_"+all_products[z]).attr("disabled",false);
						}
						else
						{
							$("#promo_product_"+all_products[z]).animate({"opacity":0.4},300);
							$("#promotional_product_"+all_products[z]).attr({"disabled":"disabled","checked":false});
						}
					}
				}

				checkBonusesArea();
			}
		});
	}
}





// -------------------------------------------------------------
// FUNCKJA DO SPRAWDZANIA CZY OKNO Z BONUSAMI MA BYĆ WYŚWIETLANE
// -------------------------------------------------------------

var checkBonusesArea=function()
{
	if ($("#bonus_area_products").length)
	{
		//var is_promotional_code=$("#is_promotional_code").css("value");
		//var is_discount=$("#is_discount").attr("value");
		//var is_promotional_product=$("#is_promotional_product").attr("value");

		var is_promotional_code=$("#field_promotional_codes").css("display");
		var is_discount=$("#field_discounts").css("display");
		var is_promotional_product=$("#field_promotional_products").css("display");
		
		//alert(is_promotional_code+" - "+is_discount+" - "+is_promotional_product);

		if (is_promotional_code!="block" && is_discount!="block" && is_promotional_product!="block")
		{
			//$("#bonus_container").fadeOut(300);
		}
		else
		{
			//$("#bonus_container").fadeIn(300);
		}
	}
}








	
// =======
// OBIEKTY
// =======

$(document).ready(function()
{
	// ==============
	// KOD PROMOCYJNY
	// ==============

	// ---------------------------
	// SPRAWDZENIE KODU NA STARCIE
	// ---------------------------

	var coupon_code=$("#coupon_code").attr("value");
	
	if (coupon_code!="")
	{
		validateCouponCode();
	}



	// --------------------------------
	// SPRAWDZANIE WCIŚNIĘTYCH KLAWISZY
	// --------------------------------

	$("#coupon_code").keyup(function(event)
	{
		var coupon_code=$("#coupon_code").attr("value");
		var checked_code=$("#checked_code").attr("value");
		var entered_code=$("#entered_code").attr("value");
		var product=$("#product").attr("value");

		coupon_code=coupon_code.toUpperCase();

		$("#coupon_code").attr("value",coupon_code);
		$("#validate_info").html("").fadeOut(200);

		if (coupon_code!=checked_code)
		{
			$("#coupon_value").attr("value",0);
			$("#value_type").attr("value",0);
			$("#checked_code").attr("value","");

			$("#coupon_value_info").fadeOut(300,function(){$(this).html("");});
		}

		if (coupon_code=="")
		{
			$("#validate_code").fadeOut(200);
		}
		else
		{
			$("#validate_code").fadeIn(200);

			if (event.which==13)
			{
				validateCouponCode();

				$("#coupon_code").blur();
				$("#entered_code").attr("value",coupon_code);
			}
			else
			{
				$("#entered_code").attr("value","");
				
				if (entered_code!="" && checked_code!="")
				{
					// -------------------------------------
					// SPRAWDZANIE INNYHCH BONUSÓW I RABATÓW
					// -------------------------------------

					$("#is_promotional_code").attr("value",0);

					checkDiscounts();
				}
			}
		}
	});



	// -----------
	// PRZYCISK OK
	// -----------

	$("#validate_code").click(function()
	{
		var coupon_code=$("#coupon_code").attr("value");

		validateCouponCode();

		$("#entered_code").attr("value",coupon_code);
	});



	// ----------------------------------------------------------
	// SPRAWDZANIE POLA PRODUCT (CZY ZMIENIŁ SIĘ BIEŻĄCY PRODUKT) 
	// ----------------------------------------------------------

	$("#product").click(function()
	{
		validateCouponCode();
	});



	// ---------------------------------------------
	// ZMIANA ILOŚCI PRODUKTÓW W BIEŻĄCYM ZAMÓWIENIU
	// ---------------------------------------------

	$(".bt_up,.bt_down").live("click",function()
	{
		var coupon_code=$("#coupon_code").attr("value");

		if (coupon_code!="")
		{
			$("#validate_info").fadeOut(200,function()
			{
				var z=0;

				check_arrows=setInterval(function()
				{
					if (z==1)
					{
						validateCouponCode();
						clearInterval(check_arrows);
					}

					z++;
				},200);
			});
		}
	});










	// ===============
	// RABATY ZA ILOŚĆ
	// ===============

	$("#ilosc").live("change",function()
	{
		checkDiscounts();
	});


	$("#product,#rodzaj_albumu,#kolekcja").live("change",function()
	{
		checkDiscounts();
		checkDiscountsInfo();
	});


	$(".bt_up,.bt_down").live("click",function()
	{
		checkDiscounts();
	});


	if ($("#is_discount").attr("value")!=0)
	{
		checkDiscounts();
	}


	checkDiscountsInfo();










	// ================
	// PROMOCYJNY ZAKUP
	// ================

	$(".no_active").animate({"opacity":0.4},200);

	$(".promotional_product").live("click",function()
	{
		var checked=$(this).attr("checked");
		var all_products=$("#all_promo_products").attr("value");
		var id=$(this).attr("id");

		id=id.split("_");

		all_products=all_products.split(",");

		for (var z=0; z<all_products.length; z++)
		{
			if (all_products[z]!=id[2])
			{
				$("#promotional_product_"+all_products[z]).attr("checked",false);
			}
		}

		if (checked)
		{
			$("#is_promotional_product").attr("value",id[2]);
		}
		else
		{
			$("#is_promotional_product").attr("value",0);
		}
	});
});
