$(document).ready(function() {

	$("#otherImagesBackground").css("filter", "filter: alpha(opacity = 50);");

	$("#featurePaneImageDiv").hover( function() {

			$("#otherImagesContainer").stop();
			$("#otherImagesContainer").animate( { top: "447px" }, "normal");
		},

		function() {

			$("#otherImagesContainer").stop();
			$("#otherImagesContainer").animate( { top: "554px" }, "normal");
		}
	);

	$("#otherImagesScrollerDiv div.thumb a.thumbImageLink").click( function() {

			var newImageID = $(this).attr("id");
			newImageID = newImageID.substr(15);
			$("#featurePaneImageDiv > img").attr("src", "images/product_features/" + historyID + "_" + newImageID + ".jpg");

			return false;
		}
	);

	var scrollPage = 0;

	var numThumbs = ($("#otherImagesScrollerDiv > div.thumb").size());
	var numPages = Math.floor(numThumbs / 4);

	if ((numThumbs % 4) > 0) {

		numPages += 1;
	}

	checkScrollArrows(numPages, scrollPage);

	$("#otherImagesScrollLeftButton").click( function() {

			if (scrollPage == 0) {

				return false;
			}

			else {

				scrollPage--;
				newLeft = (scrollPage * -412) + "px";
				$("#otherImagesScrollerDiv").animate({left: newLeft}, "normal");
				checkScrollArrows(numPages, scrollPage);
			}

			return false;
		}
	);

	$("#otherImagesScrollRightButton").click( function() {

			if (scrollPage == numPages - 1) {

				return false;
			}

			else {

				scrollPage++;
				newLeft = (scrollPage * -412) + "px";
				$("#otherImagesScrollerDiv").animate({left: newLeft}, "normal");
				checkScrollArrows(numPages, scrollPage);
			}

			return false;
		}
	);

	$("#optionColorDropdown").change( function() {

			var newChildIndex = $(this).attr("selectedIndex") + 1;
			$("#detailsSwatchesPanel a").removeClass("selectedSwatch");
			$("#detailsSwatchesPanel a:nth-child(" + newChildIndex + ")").addClass("selectedSwatch");
		}
	);

	$("#viewSwatchesLink").click( function() {

			$("#optionsSwatchesDiv").animate( { top: 55 }, 500);
			return false;
		}
	);

	$("#hideSwatchesLink").click( function() {

			$("#optionsSwatchesDiv").animate( { top: 555 }, 500);
			return false;
		}
	);

	$("#completeLinkHardware").click( function() {

			this.blur();
			openCompleteSection("Hardware");
			return false;
		}
	);

	$("#completeLinkTopTreatments").click( function() {

			this.blur();
			openCompleteSection("TopTreatments");
			return false;
		}
	);

	$("#restoreCurtainsLink").click( function() {

			this.blur();
			closeCompleteSection();
			return false;
		}
	);

	$("#optionsSwatchesDiv form.optionsSwatchesForm div.swatchesContainer div.swatchDiv a").click( function() {

			swatchID = $(this).attr("id");
			swatchID = swatchID.substr(12);
			swapSwatchScrim(swatchID);
			showScrim("SwatchDetails");
			return false;
		}
	);
});

var completeSectionOpen = "none";

function openCompleteSection(sectionName) {

	if (completeSectionOpen == sectionName) {

		closeCompleteSection();
	}

	else {

		$("#completeDiv a.completeLink").removeClass("completeLinkActive");
		$("#completeLink" + sectionName).addClass("completeLinkActive");
		$("#completeDiv").addClass("completeDivActive");
		$("#curtainsTopMask").animate( { top: 0 }, 500);
		$("#completeDiv").animate( { height: 471, top: -398 }, 500);
		$("#completeSection" + completeSectionOpen).animate( { height: 0 }, 500);
		$("#completeSection" + sectionName).animate( { height: 360 }, 500);
		completeSectionOpen = sectionName;
	}

	return false;
}

function closeCompleteSection() {

	$("#completeDiv a.completeLink").removeClass("completeLinkActive");
	$("#curtainsTopMask").animate({ top: -54 }, 500);
	$("#completeDiv").animate( { height: 120, top: 0 }, 500);
	$("#completeSection" + completeSectionOpen).animate( { height: 0 }, 500);
	setTimeout('$("#completeDiv").removeClass("completeDivActive");', 500);
	completeSectionOpen = "none";

	return false;
}

function checkScrollArrows(numPages, scrollPage) {

	if (scrollPage == 0) {

		$("#otherImagesScrollLeftButton > img").attr("src", "images/large_arrow_l_gray.png");
	}

	else {

		$("#otherImagesScrollLeftButton > img").attr("src", "images/large_arrow_l_pink.png");
	}

	if (scrollPage == numPages - 1) {

		$("#otherImagesScrollRightButton > img").attr("src", "images/large_arrow_r_gray.png");
	}

	else {

		$("#otherImagesScrollRightButton > img").attr("src", "images/large_arrow_r_pink.png");
	}
}

function selectSwatch(swatchColor) {

	if ($("#" + swatchColor + "_swatch").size() > 0) {

		if ($("a.selectedSwatch").size() > 0) {

			$("a.selectedSwatch").removeClass("selectedSwatch");
		}

		$("#selectedSwatchInput").attr("value", swatchColor);

		$("#" + swatchColor + "_swatch").addClass("selectedSwatch");
		$("#" + swatchColor + "_swatch").blur();

		$("#optionColorDropdown option").attr("selected", "false");
		$("#optionColorDropdown option[value=" + swatchColor + "]").attr("selected", "true");
	}

	return false;
}

function selectProductDetailsTab(tabName) {

	$("#detailsTextContainer div.innerContent div").hide();
	$("#details" + tabName).show();
	$("#detailsTabsContainer div a").removeClass("active").addClass("inactive");
	$("#detailsTabsContainer #details" + tabName + "Tab a").addClass("active").removeClass("inactive");
	return false;
}

