$(function()
{
	$('#Content ul li').wrapInner('<span></span>').addClass('wrapped');

	if ($('#Map').length)
	{  
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=InitializeMap";
		document.body.appendChild(script);
	}

	if ($('#BookAge').length)
	{
		$('#BookAge').change(function()
		{
			var Age = $('#BookAge').val();
			if (window.location.href.indexOf('/en/') >= 0)
			{
				window.location.href = '/index.php/en/gramatas/age/' + Age;
			}
			else
			{
				window.location.href = '/index.php/lv/gramatas/age/' + Age;
			}
		});
	}

	if ($('#Books').length)
	{
		$('td.Buy button.Add').click(function()
		{
			var AmountInput = $(this).parent().find('input');
			var Amount = parseInt(AmountInput.val());

			if (Amount > 0)
			{
				var BookID = AmountInput.attr('id').replace('Amount', '');
				$.post(window.location.href, {'Operation': 'AddToCart', 'BookID': BookID, 'Amount': Amount}, function (data)
				{
					if (data.OK)
					{
						window.location.reload(true);
					}
				}, 'json');
			}
		});
	}
	if ($('.AddBookToCart').length)
	{
		$('p.AddBookToCart button.Add').click(function()
		{
			var AmountInput = $(this).parent().find('input');
			var Amount = parseInt(AmountInput.val());

			if (Amount > 0)
			{
				var BookID = AmountInput.attr('id').replace('Amount', '');
				$.post(window.location.href, {'Operation': 'AddToCart', 'BookID': BookID, 'Amount': Amount}, function (data)
				{
					if (data.OK)
					{
						window.location.reload(true);
					}
				}, 'json');
			}
		});

	}

	$('button.SeeCart').click(function()
	{
		window.location.href = $('#CartLink a').attr('href');
	});

	if ($('#Cart').length)
	{
		$('button.CartPlus').click(function()
		{
			console.log('post');
			$.post(window.location.href, {'Operation': 'AddToCart', 'BookID': $(this).attr('id').replace('CartPlus', ''), 'Amount': 1}, function (data)
			{
				console.log(data);
				if (data.OK)
				{
					window.location.reload(true);
				}
			}, 'json');
		});

		$('button.CartMinus').click(function()
		{
			$.post(window.location.href, {'Operation': 'AddToCart', 'BookID': $(this).attr('id').replace('CartMinus', ''), 'Amount': -1}, function (data)
			{
				if (data.OK)
				{
					window.location.reload(true);
				}
			}, 'json');
		});

		CalculateTotal();

		var Shipping = {
			3: {
				50: 0.93,
				100: 1.12,
				250: 1.33,
				500: 1.87,
				1000: 2.27,
				2000: 3.77,
				3000: 4.49,
				4000: 5.23
			},
			4: {
				50: 1.55,
				100: 2.33,
				250: 4.02,
				500: 5.96,
				1000: 10.32,
				2000: 13.34,
				3000: 17.57,
				4000: 21.81
			},
			5: {
				50: 1.55,
				100: 2.6,
				250: 4.6,
				500: 7.8,
				1000: 14.28,
				2000: 20.69,
				3000: 27.2,
				4000: 33.71
			}
		}

		$('#ShippingType').change(function()
		{
			var ShippingType = $(this).val();
			var Price = 0;

			if (ShippingType > 2)
			{
				var TotalWeight = 0;
				$('tr.CartBook').each(function()
				{
					TotalWeight += parseInt($(this).find('input.Weight').val());
				});

				for (var i in Shipping[ShippingType])
				{
					if (TotalWeight >= i)
					{
						Price = Shipping[ShippingType][i];
					}
					else
					{
						break;
					}
				}
			}
			else if (ShippingType == 2)
			{
				Price = 3;
			}

			$('#ShippingPrice').text(Price.toFixed(2));
			CalculateTotal();
		});

		$('#Continue').click(function()
		{
			window.location.href = window.location.href + 'shipping/' + $('#ShippingType').val() + '/';
		});
	}

	if ($('#PayerType1').length)
	{
		$('#PayerType1, #PayerType2').change(function()
		{
			if ($('#PayerType1').is(':checked'))
			{
				$('#Payer div.Inputs input:not(#PayerName)').hide();
				$('#PayerName').show();
			}
			else
			{
				$('#Payer div.Inputs input:not(#PayerName)').show();
				$('#PayerName').hide();
			}

			$('#Payer div.Inputs input').each(function()
			{
				if ($(this).is(':visible'))
				{
					$('#Label_' + $(this).attr('id')).show();
				}
				else
				{
					$('#Label_' + $(this).attr('id')).hide();
				}
			});

			SetViewportHeight();
			SetupTwitter();
		});
		$('#PayerType1').change();
	}

	if ($('#Order').length)
	{
		var EN = (window.location.href.indexOf('/en/') >= 0);
	
		$('#Order').submit(function()
		{
			if (!$('#ContactFirstName').val() || !$('#ContactLastName').val() || !$('#ContactMail').val() || !$('#ContactPhone').val())
			{
				if (EN)
				{
					alert('Please provide your contact information!');
				}
				else
				{
					alert('Lūdzu, norādiet savu kontaktinformāciju!');
				}
				return false;
			}

			if ($('#ShippingInfo input').length)
			{
				if (!$('#DeliveryName').val() || !$('#DeliveryAddress').val() || !$('#DeliveryPostal').val())
				{
					if (EN)
					{
						alert('Please provide your delivery information!');
					}
					else
					{
						alert('Lūdzu, norādiet piegādes informāciju!');
					}
					return false;
				}
			}

			if (!$('#PaymentType input:checked').length)
			{
				if (EN)
				{
					alert('Please specify your payment option!');
				}
				else
				{
					alert('Lūdzu, norādiet apmaksas veidu!');
				}
				return false;
			}

			var data = $('#Payer input[type="text"]:visible').map(function()
			{
				return $(this).val() ? 1 : 0;
			}).get();

			if (data.indexOf(0) >= 0)
			{
				if (EN)
				{
					alert('Please provide the buyer information!');
				}
				else
				{
					alert('Lūdzu, norādiet maksātāja informāciju!');
				}
				return false;
			}

			return true;
		});
	}
});

function CalculateTotal()
{
	TotalPrice = Math.round(100 * (parseFloat($('#TotalWithoutShipping').text()) + parseFloat($('#ShippingPrice').text())))/100;
	$('#SumTotal').text(TotalPrice.toFixed(2));
}

function InitializeMap()
{
	var myLatlng = new google.maps.LatLng(56.95640, 24.126792);
	var myOptions = {
		zoom: 16,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	
	var map = new google.maps.Map(document.getElementById("Map"), myOptions);

	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map, 
		title: "Liels un mazs"
	});
}

