// This global flag is used for detect the active tab "List View" or "Detail View"
var tabFlag = true;
function getProductId(element) {
	if (!$(element).hasClass("productView")) {
		element = $(element).closest("div.productView")[0];
	}
	return $("input:first", element).attr("value");
}

function productPreparationsAndCommentsLink() {
	$('.extendedProperties', this.parentNode.parentNode.parentNode).toggle();
	$(this).toggleClass('expanded');
	var relatedRecipesContainer = $("div.relatedRecipesContainer", this.parentNode.parentNode);
	if ($("ul.relatedRecipes", relatedRecipesContainer).length == 0) {
		var productId = getProductId(this);
		relatedRecipesContainer.load(basePath + "ajax/products/relatedRecipes.aspx?id=" + productId);
	}
	return false;
}

function productShowComments() {
	var postId = getProductId(this);
	var commentsContainer = $('.commentsContainer', this.parentNode.parentNode.parentNode.parentNode);
	commentsContainer.show();
	$("iframe", commentsContainer[0]).attr("src", basePath + "ajax/posts/comments.aspx?id=" + postId);
	return false;
}

function setupProducts(parentElement) {
	parentElement = parentElement ? parentElement + " " : "";
	$(parentElement + "div.productView a.productPreparationsAndCommentsLink").click(productPreparationsAndCommentsLink);
	$(parentElement + "div.productView a.productsShowCommentsLink").click(productShowComments);
	setupRatingControl(parentElement);
}

function globalProductReset(parent){
	setupProducts(parent);
	setupTabs();
	//itemTitleReplace();
}

function adjustIFrameHeight(iframe) {
	var rv = null;
	if (iframe.contentDocument)
		rv = iframe.contentDocument;
	else
		rv = document.frames[iframe.id].document;
	if (rv) {
		var newHeight = rv.body.offsetHeight + 25;
		iframe.height = newHeight;
		if (iframe.id == "suggestion") {
			if (document.getElementById("modalPopUpContainer")) {
				document.getElementById("modalPopUpContainer").style.height = newHeight + "px";
				document.getElementById("modalPopUpContainer").style.top = "50%"
				document.getElementById("modalPopUpContainer").style.marginTop = (newHeight) / 2 * -1 + "px";
			}
		}
	}
}