/**
 * Eventhandler für Shop Funktionen
 */
function shopFunctions()
{
	// In den Warenkorb legen (ohne in den Warenkorb zu springen)
	/*
	$$('form .basketadd').each(
		function(oElement)
		{
			$(oElement).observe('click',
				function(oEvent)
				{
					oEvent.preventDefault();
					oEvent.element(oEvent).ancestors().each(
						function(oElement)
						{
							if(oElement.match('form'))
							{
								oElement.action = window.location.href;
								oElement.submit();
								return false;
							} // if
						} // function
					);
					return false;
				} // function
			);
		} // function
	);

	// In den Warenkorb legen (und in den Bestellprozess springen)
	$$('form .basketadd-checkout').each(
		function(oElement)
		{
			$(oElement).observe('click',
				function(oEvent)
				{
					oEvent.preventDefault();
					oEvent.element(oEvent).ancestors().each(
						function(oElement)
						{
							if(oElement.match('form'))
							{
								new Ajax.Request(oElement.action, {
									method:'post',
									parameters: $(oElement).serialize(true),
									onSuccess: function(response) {
										location.href = 'https://www.arbeitsrecht.org/shop/bestellung/';
								 	}
								});

								return false;
							} // if
						} // function
					);
					return false;
				} // function
			);
		} // function
	);*/
	$$('.basketdeletelink a').each(
		function(oElement)
		{
			oElement.href = oElement.href + '&tx_commerce_pi1[basket]=show';
		} // function
	);

	// Bestelleingaben speichern und zurück zum Warenkorb
	$$('form .backtobasket').each(
		function(oElement)
		{
			$(oElement).observe('click',
				function(oEvent)
				{
					oEvent.preventDefault();
					oEvent.element(oEvent).ancestors().each(
						function(oElement)
						{
							if(oElement.match('form'))
							{
								new Ajax.Request(oElement.action, {
									method:'post',
									parameters: $(oElement).serialize(true),
									onSuccess: function(response) {
										location.href = 'https://www.arbeitsrecht.org/shop/warenkorb/?tx_commerce_pi1[basket]=show';
								 	}
								});

								return false;
							} // if
						} // function
					);
					return false;
				} // function
			);
		} // function
	);

	if($('payart') != undefined)
	{
		$('payart').observe('change',
			function(oEvent)
			{
				$$('.paymentsettings').each(
					function(oElement)
					{
						$(oElement).hide();
					} // function
				);
				oPayElement = $(oEvent.element(oEvent));
				oActivePay = $('paymentsettings-' + $(oPayElement).value);

				if(oActivePay)
					oActivePay.show();
			} // function
		);
	} // if

	var iTopProductSleep = 1;
	$$('.topproducts').each(
		function(oElement)
		{
			new topProductBox(oElement, iTopProductSleep++);
		} // function
	);

	function topProductBox(oProductBox, iTopProductSleep)
	{
		// Navigation Top Produkte
		$(oProductBox).select('.nav-pager li a').each(
			function(oElement)
			{
				$(oElement).observe('click', function (oEvent) {
					clickTopProductNav(oEvent)
				});
			} // function
		);

		var oPECreate = new PeriodicalExecuter(function(){createPE();}, iTopProductSleep);
		var oPE = null;

		function createPE()
		{
			oPECreate.stop();
			oPE = new PeriodicalExecuter(function(){nextTP();}, 5);
		} // function
		var iTPCurrent = 1;

		function nextTP()
		{
			iTPCurrent++;

			if($(oProductBox).select('.topproduct-link-' + iTPCurrent).length == 0)
				iTPCurrent = 1;

			if($(oProductBox).select('.topproduct-link-' + iTPCurrent).length > 0)
				changeTopProduct($(oProductBox).select('.topproduct-link-' + iTPCurrent)[0].firstDescendant());
		} // function

		function clickTopProductNav(oEvent)
		{
			$(oEvent).preventDefault();
			oPE.stop();

			oLinkElement = $(oEvent).element(oEvent);
			changeTopProduct($(oLinkElement));
		} // function

		function changeTopProduct(oLinkElement)
		{
			var iNum = 0;
			var iNumPrev = 0;

			// Aktiven Navigationspunkt zu nicht aktiven Link umwandeln
			oLinkElement.ancestors().each(
				function(oElement)
				{
					if(oElement.match('ul.nav-pager'))
					{
						var oLiElement = $(oElement).getElementsByClassName('active')[0];
						$(oLiElement).removeClassName('active');
						var oLiPrevElement = $(oLiElement).previous('li');
						if(oLiPrevElement)
							$(oLiPrevElement).removeClassName('last');

						iNumPrev = parseInt($(oLiElement).className.replace('topproduct-link-', '').replace('last', ''));

						$(oLiElement).innerHTML = '<a href="#">' + iNumPrev + '</a>';
						$(oLiElement).firstDescendant().observe('click', function (oEvent) {
							clickTopProductNav(oEvent)
						});

						return false;
					} // if
				} // function
			);

			// Geklickten Navigationspunkt aktiv setzen und Produkt aus/einblenden
			oLinkElement.ancestors().each(
				function(oElement)
				{
					if(oElement.match('li'))
					{
						iNum = parseInt($(oElement).className.replace('topproduct-link-', '').replace('last', ''));
						$(oElement).innerHTML = '<strong>' + iNum  + '</strong>';
						$(oElement).addClassName('active');
						var oPrevElement = $(oElement).previous('li');
						if(oPrevElement)
							$(oPrevElement).addClassName('last');


						// Angeklicktes Produkt anzeigen (Vorheriges verstecken)
						$(oElement).ancestors().each(
							function(oElement)
							{
								if(oElement.match('div.topproducts'))
								{
									var oActiveProduct = $(oElement).getElementsByClassName('topproduct-' + iNumPrev)[0];
									var oProductToActivate = $(oElement).getElementsByClassName('topproduct-' + iNum)[0];

									$(oActiveProduct).removeClassName("active");
									$(oProductToActivate).addClassName("active");
									return false;
								} // if
							} // function
						);

						return false;
					} // if
				} // function
			);
			return false;
		} // function
	} // function
} // function


/**
 * Instanziierung
 */
document.observe('dom:loaded', function()
{
	new shopFunctions();
});