
$(document).ready(function(e) {
	
	var closeMore = function(productContainer) {
		$(productContainer).hide();
		return false;
	};
	
	var openMore = function(productContainer) {
		$(productContainer).show();
		return false;
	}
	
	var removeZIndex = function() {
		$('.highestIndex').removeClass('highestIndex');
		return false;
	}
	
	var addZIndex = function(productContainer) {
		removeZIndex();
		var detailsContainer = $(productContainer).closest('.productListEntryContainer');
		$(detailsContainer).addClass('highestIndex');
		return false;
	}
	
	var close = function(productContainer) {
		if ($(productContainer).hasClass('expanded')) {
			$(productContainer).removeClass('expanded');
			$('#'+ $(' .more a',productContainer).attr('rel')).hide();
			$('.more a span',productContainer).html('Mehr anzeigen');
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				DD_belatedPNG.fix('.png');
			}
			return true;
		};
		return false;
	};
	
	var open = function(productContainer) {
		if (!$(productContainer).hasClass('expanded')) {
			$(productContainer).addClass('expanded');
			$('#'+ $(' .more a',productContainer).attr('rel')).show();
			$('.more a span',productContainer).html('Weniger anzeigen');
			$('#'+ $(' .more a',productContainer).attr('rel')).slider();
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				DD_belatedPNG.fix('.png');
			}
			return true;
		}
		return false;		
	}
	
	var isOpen = function(productContainer) {
		return $(productContainer).hasClass('expanded');
	};
	
	var isOpenDetails = function(productContainer) {
		return $(productContainer).is(':visible');
	};
	
	var clickHandler = function(e) {
		if ($(this).attr('rel').length <= 0) {
			return true;
		}	
		
		// Close all open more-details in all rows before opening the current
		var detailsContainerSmall = $('.contentSmall');
		var detailsContainerFull = $('.contentFull');
		removeZIndex();
		openMore(detailsContainerSmall);
		closeMore(detailsContainerFull);

		var listEntry = $(this).closest('.productListEntryContainer');
		var productContainer = $(this).closest('.productContainer');
		if (!isOpen(productContainer)) {
			$('.productContainer',listEntry).each(function(i,e) {
				// Close all open ones in this row before opening the current
				close(e);
			});
			open(productContainer);
		} else {
			close(productContainer);
		}
		return false;
	};
	
	var clone;
	var removeClone = function (e) {
		$(clone).remove();
		$('.contentSmall').each(function(i,e) {
			// Close all open ones in this row before opening the current
			openMore(e);
		});
		return false;
	}
	var clickHandlerMore = function(e) {
		if ($(this).attr('rel').length <= 0) {
			return true;
		}	
		var listEntryFull = $(this).closest('.productListEntryContainer .contentFull');
		var listEntrySmall = $(this).closest('.productListEntryContainer .contentSmall');
		var detailsContainerSmall = $(this).closest('.descriptionContainer').children('.description').children('.contentSmall');//.children('.description .contentFull');
		var detailsContainerFull = $(this).closest('.descriptionContainer').children('.description').children('.contentFull');//.children('.description .contentFull');
		
		if (!isOpenDetails(detailsContainerSmall)) {
			$('.productContainer',listEntryFull).each(function(i,e) {
				// Close all open ones in this row before opening the current
				closeMore(e);
				removeZIndex();
			});
			$('.productContainer',listEntrySmall).each(function(i,e) {
				// Close all open ones in this row before opening the current
				openMore(e);
			});
			removeZIndex();
			openMore(detailsContainerSmall);
			closeMore(detailsContainerFull);
		} else {
			$('.productListEntryContainer .contentFull').each(function(i,e) {
				// Close all open ones in this row before opening the current
				closeMore(e);
				$(clone).remove();
				removeZIndex();
			});
			$('.productListEntryContainer .contentSmall').each(function(i,e) {
				// Close all open ones in this row before opening the current
				openMore(e);
			});

			addZIndex(detailsContainerSmall);
			closeMore(detailsContainerSmall);
			openMore(detailsContainerFull);
			
			/*
			 * clonen des vorhandenen Layers um ihn an das ende vom body zu heften da sonnst teile des Layers abgeshcnitten werden.
			 */
			var offset = $(detailsContainerFull).offset();
			closeMore(detailsContainerFull);
			clone = $(detailsContainerFull).clone().appendTo("body");
			openMore(clone);
			$(clone).addClass("clone");
			$(clone).css( "left" , offset.left+"px");
			$(clone).css( "top" , offset.top-12+"px");
			$(clone).css( "position" , "absolute");
		}
		return false;
	};

	// sets the same height for all products with long Text in one row
	setLongTextHeight = function() {
		var intMaxHeight = 0;
		var rowProducts = $('.products');

		// for each row
		$.each(rowProducts, function(key, value) {
			var rowProductsEntrys = $(this).find('.longText');
			intMaxHeight = 0;
			// for all products of this row
			$.each(rowProductsEntrys, function(key, value) {
				// select the maximum height
				if(intMaxHeight < $(this).height()) {
					intMaxHeight = $(this).height();
				};
			});
			// set the maximum height for all products
			$(this).find('.longText').css({ height : intMaxHeight + 'px'});
			var objDetails = $(this).closest('.productListEntryContainer').next();
		});
	};
	
	
	$('.productContainer .more a').click(clickHandler);
	$('.productContainer .product .image a').click(clickHandler);
	$('.descriptionContainer .description .detailsContent a').click(clickHandlerMore);
	$('.clone a').live('click', removeClone);
	setLongTextHeight();
	
});

