/* collapsible paragraph stuff ********************************************************************/
function toggleCollapsibleParagraph(id) {
	var paragraph = document.getElementById(id);
	if (paragraph) {
		var currentClass = paragraph.getAttribute("class");
		if (!currentClass) {
			currentClass = paragraph.getAttribute("className");
		}
		if (currentClass == "closed") {
			paragraph.setAttribute("class", "open"); 
			paragraph.setAttribute("className", "open");
		} else {
			paragraph.setAttribute("class", "closed"); 
			paragraph.setAttribute("className", "closed");
		}
	}
}

function openCollapsibleParagraph(id) {
	var paragraph = document.getElementById(id);
	if (paragraph) {
		var currentClass = paragraph.getAttribute("class");
		if (!currentClass) {
			currentClass = paragraph.getAttribute("className");
		}
		if (currentClass == "closed") {
			paragraph.setAttribute("class", "open"); 
			paragraph.setAttribute("className", "open");
		}
		window.location.hash=id;
	}
}

function resizeImage(image, width, height, isMax, center) {
	if (image) {
		var origWidth = image.width;
		var origHeight = image.height;
		var scaleFactor = 1;
		if (width && width > 0 && height && height > 0) {
			// Bild-Seitenverhältnis
			var ratioImage = origWidth / origHeight;
			// Box-Seitenverhältnis
			var ratioBox = width / height;			
			if (isMax) {
				if (ratioBox > ratioImage) {
					// Wenn das Box-Seiten-Verhältnis > Bild-Seitenverhältnis muss die Höhe übereinstimmen
					scaleFactor = height / origHeight;
				} else {
					// Wenn das Box-Seiten-Verhältnis < Bild-Seitenverhältnis muss die Breite übereinstimmen
					scaleFactor = width / origWidth;
				}
			} else {
				if (ratioBox < ratioImage) {
					// Wenn das Box-Seiten-Verhältnis < Bild-Seitenverhältnis muss die Höhe übereinstimmen
					scaleFactor = height / origHeight;
				} else {
					// Wenn das Box-Seiten-Verhältnis > Bild-Seitenverhältnis muss die Breite übereinstimmen
					scaleFactor = width / origWidth;
				}
			}
			var newWidth = Math.ceil(origWidth * scaleFactor);
			var newHeight = Math.ceil(origHeight * scaleFactor);
			$(image).css("width", "" + newWidth + "px");
			$(image).css("height", "" + newHeight + "px");
			$(image).css("position", "relative");
			if (isMax && center) {
				if (newWidth < width) {
					$(image).css("left", Math.round((width - newWidth) / 2) + "px");
				}
				if (newHeight < height) {
					$(image).css("top", Math.round((height - newHeight) / 2) + "px");
				}
			}
			if (!isMax && center) {
				if (newWidth > width) {
					$(image).css("left", "-" + Math.round((newWidth - width) / 2) + "px");
				}
				if (newHeight > height) {
					$(image).css("top", "-" + Math.round((newHeight - height) / 2) + "px");
				}
			}
		}
/*		} else if (width && width > 0) {
			ratio = width / origWidth;
		} else if (height && height > 0) {
			ratio = height / origHeight;
		}*/
//		percentage = ratio*100;
	}
}


function openGallery(galleryLinkId) {
	alert("gallery link id: "+galleryLinkId);
	alert("galley link: "+$('#' + galleryLinkId).attr('id'));
	$('#' + galleryLinkId).click();
}

function showZoomButtons() {
//	alert($("div.productlist div.produkt").size() + " bikes auf dieser Seite");
	$("div.productlist div.produkt").each(function (i) {
		if ($("div.produktbeschreibung", this).height() > $(this).height()) {
			$(this).addClass("visible_more-button");
			$(this).click(function() {
//				alert("+ button clicked");
				showFullDescription($(this).attr("id"));
				return false;
			});
		}
//		alert("beschreibungshöhe: "+ $("div.produktbeschreibung", this).height());
//		alert("produkthöhe: "+ $(this).height());
	});
}

function showFullDescription(productid) {
	// hide all other overflowing descriptions
	hideAllDescriptions();
	// show the selected description
	$("#" + productid).addClass("fully_visible_description");
	$("#" + productid).removeClass("visible_more-button");
	$("#" + productid).css("overflow", "visible");
	$("#" + productid).css("z-index", "50");
	// setup an overlay with the "close" action
	if ($("#overlay").size() < 1) {
		$("body").append("<div id=\"overlay\"><!-- --></div>");
	}
	$("#overlay").css("background-color", "transparent");
	$("#overlay").css("height", $(window).height() + "px");
	$("#overlay").css("z-index", "0");
	$("#overlay").click(function() {
		hideAllDescriptions();
		$("#overlay").remove();
	});
}

function hideAllDescriptions() {
//	alert("hideing all descriptions");
	$("div.productlist div.produkt").each(function(i) {
		if ($(this).hasClass("fully_visible_description")) {
			$(this).css("overflow", "hidden");
			$(this).removeClass("fully_visible_description");
			$(this).addClass("visible_more-button");
			$(this).css("z-index", "10");
		}
	});
//	showZoomButtons();
}
/* noyb stuff *************************************************************************************/
function dcmadr(nnnn) {
	var a = "";
	for(i=0,m=nnnn.length;i < m;i++){
		if(i%3==0){
			a += String.fromCharCode(nnnn.substr(i, 3)-37);
		}
	}
	return a;
}

function dcmt(nnnn) {
	var a = dcmadr(nnnn);
	location.href=('m'+'ail'+'t'+'o:'+a);
}
