var distance = 625;


function initGrid() {
	for (rowIndex = 0; rowIndex < 5; rowIndex++) {
		gridRow = "gridRow" + rowIndex;
		
		// Make sure row is displayed / exists
		if ($chk($(gridRow)) == false) {
			continue;
		}
		
		$(gridRow).set("tween",{
			duration: 500,
			//duration: 1000,
			//transition: Fx.Transitions.Quart.easeOut,
			link: "ignore"
		});
		
		/*$("gridRow" + rowIndex).addEvent("mouseover", function(event) {
			rowIndex = this.id.substr(-1, 1);
			
			for (i = 0; i < 5; i++) {
				$visibility = i == rowIndex ? "visible" : "hidden";
				$("gridButtonImgLeft" + i).setStyle("visibility", $visibility);
				$("gridButtonImgRight" + i).setStyle("visibility", $visibility);
			}
		});*/
		
	}
	
	document.addEvent("mousemove", function(event) {
		updateNavigation(event);
	});
}


function updateNavigation(event) {
	
	// Cursor Position
	xCursor = event.client.x;
	yCursor = event.client.y;

	for (i = 0; i < 5; i++) {
	
		// Make sure row is displayed / exists
		if ($chk($("gridButtonLeft" + i)) == false) {
			continue;
		}

		// Left button
		btn = $("gridButtonLeft" + i);
		pos = btn.getPosition();
		btn.xMin = pos.x;
		btn.xMax = pos.x + 360;
		btn.yMin = pos.y;
		btn.yMax = pos.y + 110;
		visibility = (xCursor >= btn.xMin && xCursor <= btn.xMax && yCursor >= btn.yMin && yCursor <= btn.yMax) ? "visible" : "hidden";
		btn.setStyle("visibility", visibility);
		
		// Right button
		btn = $("gridButtonRight" + i);
		pos = btn.getPosition();
		btn.xMin = pos.x - 325;
		btn.xMax = pos.x + 35;
		btn.yMin = pos.y;
		btn.yMax = pos.y + 110;
		visibility = (xCursor >= btn.xMin && xCursor <= btn.xMax && yCursor >= btn.yMin && yCursor <= btn.yMax) ? "visible" : "hidden";
		btn.setStyle("visibility", visibility);
	}
}


function move(rowIndex, direction) {
	var gridRow = "gridRow" + rowIndex;
	var x1 = $(gridRow).getStyle("left").toInt();
	var x2 = x1 + direction * distance;
	var xMin = -1 * (maxAnimationStep[rowIndex] - 1) * distance;
	var xMax = 0;

	if (x2 > xMax) {
		x2 = xMin;
	}
	else if (x2 < xMin) {
		x2 = xMax
	}
	
	$(gridRow).tween("left", x2);
	
	saveRowPosition(rowIndex, x2);
}


function saveRowPosition(rowIndex, x) {
	var storedPositions = new Hash.Cookie("DMO_GRID_ROW_POS", {autoSave: false, path: "/"});

	storedPositions.set(rowIndex, x);

	storedPositions.save();
}


function focusProduct(dmoId) {
	if ($chk($("price" + dmoId)) == false) {
		return;
	}
	$("price" + dmoId).setStyle("visibility", "visible");
	$("header-title-img").src = "img/" + colorName + "/titel/produkt-" + dmoId + ".gif";
}


function blurProduct(dmoId) {
	if ($chk($("price" + dmoId)) == false) {
		return;
	}
	$("price" + dmoId).setStyle("visibility", "hidden");
	$("header-title-img").src = "img/" + colorName + "/titel/" + titelId + ".gif";
}


function selectVariation(dmoId, variationIndex, imgSize) {
	if ($chk($("product-image")) == false) {
		return;
	}
	
	// Update product image
	$("product-image").src = "img/produkte/" + dmoId + "_" + variationIndex + "_" + imgSize + ".jpg";
	
	// Update variation checkboxes
	checkboxIndex= 1;
	while ($chk($("variation" + checkboxIndex)) == true) {
		$("variation" + checkboxIndex).src = "img/" + colorName + "/checkbox-" + (checkboxIndex == variationIndex ? "active" : "normal") + ".gif";
		checkboxIndex++;
	}
	
	if (imgSize == "m") {
		// Update link on image with variation
		if ($chk($("zoom-link")) == true) {
			$("zoom-link").href = "produkte/zoom/produkt/" + dmoId + "/variation/" + variationIndex;
		}
		
		// Update basket link with variation
		if ($chk($("basket-link")) == true) {
			$("basket-link").href = "warenkorb/hinzufuegen/produkt/" + dmoId + "/variation/" + variationIndex;
		}
	}
	
	else if (imgSize == "xl") {
		// Update back link with variation
		if ($chk($("back-link")) == true) {
			$("back-link").href = "produkte/details/produkt/" + dmoId + "/variation/" + variationIndex;
		}
	}
}
