	
	
$(document).ready(function() {

	//find all form with class jqtransform and apply the plugin
	$(function() {
		$("form.jqtransform").jqTransform();
	});


	// input logic
	var fillInputWithTitle = function() {
		if ($(this).val() == '' && $(this).attr('title')) {
			$(this).val($(this).attr('title'));
			$(this).addClass('inputDefaultValue');
		}
	};
	
	var removeTitleFromInput = function() {
		if ($(this).hasClass('inputDefaultValue') && $(this).val() == $(this).attr('title')) {
			$(this).val('');
			$(this).removeClass('inputDefaultValue');
		}
	}

	$('input[type=text]').each(fillInputWithTitle);
	$('input[type=text]').blur(fillInputWithTitle);
	$('input[type=text]').bind('click focus', removeTitleFromInput);
	$('form').bind('submit', function() {
		$('input[type=text]').each(removeTitleFromInput);
	});


	// menu logic
	$('#menu li').click(function(evt) {
		return;	// disabled in TYPO3
		if ($(this).children('ul').length > 0) {
			$('li.open', $(this).siblings()).removeClass('open');
			$(this).siblings().removeClass('open');
			$(this).toggleClass('open');
		}
		evt.stopPropagation();
	});


	// startpage layer
	$('.startpageItemMoreButton').mouseover(function(evt) {
		var layer = $(this).next();
		if (layer.filter(':hidden').length) {
			layer.fadeIn();
		}
		return false;
	});

	$('.startpageItemMoreLayer').mouseleave(function(evt) {
		$(this).delay(500).fadeOut();
		return false;
	});


	$('.thumbnailItemTextWrapper').click(function(evt) {
		evt.stopPropagation();
	});


	// product pages and shop the look
	$('.thumbnailItem').click(function(evt) {
		var anchor = $(this).children('.thumbnailItemImage').children(':first');
		if (anchor.attr('href')) {
			window.location.href = anchor.attr('href');
			evt.preventDefault();
			return false;
		}
	});


	// sliding through the images
	$('.thumbnailRotatorWrapper').each(function() {
		var innerDiv = $('.thumbnailRotatorInnerWrap', this);
		if (innerDiv.children().length > 3) {
			Rotator.slidePictureIcons(innerDiv, 'start');
			$('.thumbnailRotatorLeft', this).click(function() {
				Rotator.slidePictureIcons(innerDiv, 'left');
				return false;
			});
			$('.thumbnailRotatorRight', this).click(function() {
				Rotator.slidePictureIcons(innerDiv, 'right');
				return false;
			});
		} else {
			var arrowLeftDiv = $(innerDiv).parent().prev();
			var arrowRightDiv = $(innerDiv).parent().next();
			$(arrowLeftDiv).addClass('thumbnailRotatorLeftOff');
			$(arrowLeftDiv).removeClass('thumbnailRotatorLeftOn');
			$(arrowRightDiv).addClass('thumbnailRotatorRightOff');
			$(arrowRightDiv).removeClass('thumbnailRotatorRightOn');
		}
	});

	$('.thumbnailRotatorImage').click(function(evt) {
		$(this).siblings().removeClass('imageActiveMarker');
		$(this).addClass('imageActiveMarker');
		Rotator.exchangeMainPicture($(this));
		evt.preventDefault();
	});
	
	// color selector
	$('.productColors li').click(function(evt) {
		$(this).siblings().removeClass('imageActiveMarker');
		$(this).addClass('imageActiveMarker');
		Rotator.showMainColorPicture(this);
		evt.preventDefault();
	});


	
	// additional product description
	$('.openMore, .moreLink, .closeMore').click(function(evt) {
		if ($(this).parents('.productDetailsDescription').length) {
			$(this).parents('.productDetailsDescription').children('.productDetailsLongDescription').toggle();
			evt.preventDefault();
		}
	});

	$('.buttonLinkWishlist').click(function(evt) {
		$(this).hide();
		$(this).next('.wishlistConfirmationBox').fadeIn();
		evt.preventDefault();
	});


	// on click the link should submit the form
	$('#newsletter-register-now').click(function() {
		$('#newsletter-register').submit();
	});

	// additional product description in cartview for product-options
	$('.openMore, .moreLink, .closeMore').click(function(evt) {
		if ($(this).parents('.cartItemSelectColOptions').length) {
			$(this).parents('.cartItemSelectColOptions').children('.cartItemSelectColOptionsLongDescription').toggle();
			evt.preventDefault();
		}
	});


	// labels for the giftwrap, need to hook into the jqtransform process (and detect the "a"
	$('input[type=radio]').each(function(evt) {
		if ($(this).prev('a.jqTransformRadio').length) {
			var grp = $(this).attr('name');
			$(this).prev('a.jqTransformRadio').click(function() {
				$('input[name=' + grp + ']').not($(this).next('input')).each(function() {
					$(this).parent().next('label').removeClass('active').addClass('inactive');
				});
				$(this).parent().next('label').removeClass('inactive').addClass('active');
			});
		}
	});

	// show shipping address
	$('input#shippingAddress').change(function() {
		if ($(this).attr('checked')) {
			$('#formShippingAddress').css('visibility', 'visible');
		} else {
			$('#formShippingAddress').css('visibility', 'hidden');
		}
	});


	// checkout logic (AP2)
	$('input#creditcard').prev('a.jqTransformRadio').click(function() {
		$('#paymentCreditcard').removeClass('paymentOptionDivHide');
		$('#paymentBe').addClass('paymentOptionDivHide');
		$('#paymentCreditcardText').show();
		$('#paymentBeText, #paymentNnText, #paymentVkText').hide();
		$('#paymentCreditcard .jqTransformSelectWrapper').show();
	});

	$('input#be').prev('a.jqTransformRadio').click(function() {
		$('#paymentBe').removeClass('paymentOptionDivHide');
		$('#paymentCreditcard').addClass('paymentOptionDivHide');
		$('#paymentBeText').show();
		$('#paymentCreditcardText, #paymentNnText, #paymentVkText').hide();
		$('#paymentCreditcard .jqTransformSelectWrapper').hide();
	});

	$('input#nn').prev('a.jqTransformRadio').click(function() {
		$('#paymentBe, #paymentCreditcard').addClass('paymentOptionDivHide');
		$('#paymentNnText').show();
		$('#paymentCreditcardText, #paymentBeText, #paymentVkText').hide();
		$('#paymentCreditcard .jqTransformSelectWrapper').hide();
	});

	$('input#vk').prev('a.jqTransformRadio').click(function() {
		$('#paymentBe, #paymentCreditcard').addClass('paymentOptionDivHide');
		$('#paymentVkText').show();
		$('#paymentCreditcardText, #paymentBeText, #paymentNnText').hide();
		$('#paymentCreditcard .jqTransformSelectWrapper').hide();
	});



	/*** store locator ***/
	$('a#joopmap_submitform').click(function(evt) {
		
		$('#formStoreLocator').submit();
		
		evt.preventDefault();
		return false;
	});
	
	$('#link-hidemap').click(function(evt) {
		$('#joopmap').hide();
		$(this).addClass('active');
		$('#link-showmap').removeClass('active');
		evt.preventDefault();
	});

	$('#link-showmap').click(function(evt) {
		$('#joopmap').show();
		$(this).addClass('active');
		$('#link-hidemap').removeClass('active');
		evt.preventDefault();
	});
	
	
	
	/*** Cookie / Shopping Cart ***/
	// Contains the session, the total price and the number of products in the cart
	// Value: 4033628,1,129.00 ( = <hSessionID/BestNr>,<Menge>,<Gesamtpreis>)
	var cookieCart = $.cookie('DePauli_hSessionID_Value');
	if (cookieCart) {
 		cookieCart = decodeURIComponent(cookieCart);
		var cartData = cookieCart.split(',');
		$('#cart-totalitems').html(cartData[1]);
		$('#cart-totalprice').html(cartData[2].replace(/\./, ","));
	}

	// contains all products
	// Value: 12345,23456,34566  (=<ProdNr1>,<ProdNr2>,<ProdNr3>,…)
 	var cookieProducts = $.cookie('DePauli_hSessionID_Produkte');
 	if (cookieProducts) {

 		// add the container for the items
 		$('.cart-button').append('<ul class="cartRollover"></ul>');

 		cookieProducts = decodeURIComponent(cookieProducts);
 		products = cookieProducts.split(',');
 		$(products).each(function(indexNo, productNo) {
/*  			var productUrl = productNo.substr(0, 2) + '/' + productNo; */
			var productUrl = parseInt(productNo/1000) + '/' + productNo;
 			$('.cartRollover').append('<li><div class="cartRolloverImage"><a href="/produkte/' + productUrl + '.html"><img src="/pimages/' + productUrl + '_140.jpg" width="70" height="100" /></a></div><div class="cartRolloverHighlight"><a href="/produkte/' + productUrl + '.html"></a></div></li>');
 		});
 	}

	// contains all products in the merkzettel
	// Value: ,12345,23456,34566,  (=<ProdNr1>,<ProdNr2>,<ProdNr3>,…)
 	var cookieFavorites = $.cookie('DePauli_Merkzettel');
 	if (cookieFavorites) {
 		cookieFavorites = decodeURIComponent(cookieFavorites);
 		var totalFavorites = 0;
 		favorites = cookieFavorites.split(',');
 		$(favorites).each(function(indexNo, productNo) {
 			if (productNo) {
 				totalFavorites++;
 			}
 		});
 		if (totalFavorites > 0) {
 			$('#menu .merkzettel').children().append('&nbsp;(' + totalFavorites + ' Artikel)');
 		}
 	}
 	
 	
 	
 	/*** looks and slideshow ***/
	$('.lookItemSlideshowView').each(function() {
		var maxHeight = 0;
		$('.lookSlideshowImage', this).each(function() {
			if ($(this).height() > maxHeight) {
				maxHeight = $(this).height();
			}
		});
		$(this).height(maxHeight);
		$(this).children().hide();
		var firstChild = $(this).children().first();
		firstChild.show();
		
		var fadeToNextItem = function(currentItem) {
			var nextItem = currentItem.next();
			if (!nextItem.length) {
				nextItem = currentItem.prevAll().last();
			}
			currentItem.fadeOut('slow');
			nextItem.fadeIn('slow', function() {
				window.setTimeout(function() {
					fadeToNextItem(nextItem);
				}, 4000);
			});
		}
		window.setTimeout(function() {
			fadeToNextItem(firstChild);
		}, 4000);
	});
});


var Rotator = {
	slidePictureIcons: function(innerDiv, direction) {
		var posLeft = parseInt($(innerDiv).css('marginLeft'));
		var itemWidth = parseInt($(innerDiv).children(':first').outerWidth(true));
		var totalWidth = $(innerDiv).children().length * itemWidth;
		//$(innerDiv).width(totalWidth);
		
		var arrowLeftDiv = $(innerDiv).parent().prev();
		var arrowRightDiv = $(innerDiv).parent().next();
		
		switch (direction) {
			case 'start':
				$(innerDiv).css('marginLeft', '0px');
				$(arrowLeftDiv).addClass('thumbnailRotatorLeftOff');
				$(arrowLeftDiv).removeClass('thumbnailRotatorLeftOn');
				$(arrowRightDiv).addClass('thumbnailRotatorRightOn');
				$(arrowRightDiv).removeClass('thumbnailRotatorRightOff');
				break;
			// allow slideLeft as long as the first item is not visible yet
			case 'left':
				if (posLeft < 0) {
					$(arrowLeftDiv).unbind('click');
					$(innerDiv).animate({
						'marginLeft': (posLeft + itemWidth) + 'px'
					}, 'fast', function() {
						$(arrowLeftDiv).click(function() {
							Rotator.slidePictureIcons(innerDiv, 'left');
							return false;
						});
					});
					$(arrowRightDiv).addClass('thumbnailRotatorRightOn');
					$(arrowRightDiv).removeClass('thumbnailRotatorRightOff');
					posLeft += itemWidth;
				}
				if (posLeft >= 0) {
					$(arrowLeftDiv).addClass('thumbnailRotatorLeftOff');
					$(arrowLeftDiv).removeClass('thumbnailRotatorLeftOn');
				}
				break;
			// allow slideRight as long as the last item is not visible yet
			case 'right':
				var totalWidth = ($(innerDiv).children().length - 3) * itemWidth;
				if (Math.abs(posLeft) <= (totalWidth - itemWidth)) {
					$(arrowRightDiv).unbind('click');
					$(innerDiv).animate({
						'marginLeft': (posLeft - itemWidth) + 'px'
					}, 'fast', function() {
						$(arrowRightDiv).click(function() {
							Rotator.slidePictureIcons(innerDiv, 'right');
							return false;
						});
					});
					$(arrowLeftDiv).addClass('thumbnailRotatorLeftOn');
					$(arrowLeftDiv).removeClass('thumbnailRotatorLeftOff');
					posLeft -= itemWidth;
				}
				if (Math.abs(posLeft) > (totalWidth - itemWidth)) {
					$(arrowRightDiv).addClass('thumbnailRotatorRightOff');
					$(arrowRightDiv).removeClass('thumbnailRotatorRightOn');
				}
				break;
		}
	},
	
	
	exchangeMainPicture: function(selectedThumb) {
		var thumbId = $(selectedThumb).attr('id');
		var mainId = thumbId.replace(/thumb/, 'image');
		
		$('#' + mainId).siblings().hide();
		$('#' + mainId).show();
	},

	showMainColorPicture: function(selectedColorThumb) {
		var colorThumbId = $('img:first', selectedColorThumb).attr('id');
		var mainId = colorThumbId.replace(/colorthumb/, 'color');

		var oldPicture = $('#' + mainId).siblings(':visible');
		if (oldPicture) {
			oldPicture.parent().data('lastPic', oldPicture.attr('id'));
		}

		$('#' + mainId).siblings().hide();
		$('#' + mainId).show();
	},

	hideMainColorPicture: function(selectedColorThumb) {
		var colorThumbId = $('img:first', selectedColorThumb).attr('id');
		var mainId = colorThumbId.replace(/colorthumb/, 'color');

		var oldPicture = $('#' + mainId).parent().data('lastPic');
		if (!oldPicture) {
			oldPicture = $('#' + mainId).siblings().first().attr('id');
		}

		$('#' + mainId).hide();
		$('#' + oldPicture).show();
	}
	
};
